package com.ghy.web.controller.tool;


import com.alibaba.fastjson.JSONObject;
import com.ghy.common.config.BaiduConfig;
import com.ghy.common.core.controller.BaseController;
import com.ghy.common.core.domain.AjaxResult;
import com.ghy.common.utils.ExceptionUtil;
import com.ghy.common.utils.http.HttpUtils;
import com.ghy.framework.web.domain.server.Sys;
import com.ghy.system.domain.SysArea;
import com.ghy.system.service.ISysAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 百度地图逆解析
 * @author clunt
 */
@Controller
@RequestMapping("/tool/baidu")
public class BaiduController extends BaseController {

    @Autowired
    private BaiduConfig baiduConfig;

    @Autowired
    private ISysAreaService iSysAreaService;

    @PostMapping("/getLocation")
    @ResponseBody
    public AjaxResult getLocationByLot(@RequestBody JSONObject jsonObject){
        try {
            JSONObject json = new JSONObject();

            String location = jsonObject.getString("location");
            String url = baiduConfig.getUrl().replace("#AK#", baiduConfig.getAk()) + location;
            String result = HttpUtils.sendGet(url);
            result = result.replaceAll("\n", "").replaceAll("\t", "");
            JSONObject resultJson = JSONObject.parseObject(result);
            if("0".equals(resultJson.getString("status"))){
                JSONObject addressJson = resultJson.getJSONObject("result").getJSONObject("addressComponent");
                String provinceName = addressJson.getString("province");
                logger.info("provinceName :" + provinceName);
                SysArea provinceArea = iSysAreaService.selectByName(provinceName, null);
                String cityName = addressJson.getString("city");
                logger.info("cityName :" + cityName);
                SysArea cityArea = iSysAreaService.selectByName(cityName, provinceArea.getAreaCode());
                String countryName = addressJson.getString("district");
                logger.info("countryName :" + countryName);
                SysArea countryArea = iSysAreaService.selectByName(countryName, cityArea.getAreaCode());
                json.put("provinceArea", provinceArea);
                json.put("cityArea", cityArea);
                json.put("countryArea", countryArea);
            }else {
                return AjaxResult.error("Api服务异常!");
            }
            return AjaxResult.success(json);
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return AjaxResult.error(ExceptionUtil.getExceptionMessage(e));
        }
    }


    public static void main(String[] args) {
        String result = " {\"status\":0,\"result\":{\"location\":{\"lng\":113.29950224957551,\"lat\":23.019282373814027},\"formatted_address\":\"广东省广州市番禺区安平路\",\"business\":\"大石\",\"addressComponent\":{\"country\":\"中国\",\"country_code\":0,\"country_code_iso\":\"CHN\",\"country_code_iso2\":\"CN\",\"province\":\"广东省\",\"city\":\"广州市\",\"city_level\":2,\"district\":\"番禺区\",\"town\":\"\",\"town_code\":\"\",\"distance\":\"\",\"direction\":\"\",\"adcode\":\"440113\",\"street\":\"安平路\",\"street_number\":\"\"},\"pois\":[],\"roads\":[],\"poiRegions\":[],\"sematic_description\":\"\",\"cityCode\":257}}\n";
    }
}
