package com.casic.missiles.modular.system.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.casic.missiles.core.base.response.ResponseData;
import com.casic.missiles.core.common.service.ICommonPermissionService;
import com.casic.missiles.modular.system.model.BusWellInfo;
import com.casic.missiles.modular.system.service.IBusWellInfoService;
import com.casic.missiles.modular.system.util.HttpUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.List;

@Controller
public class BusWellAddressController {

    private static final Logger logger = LoggerFactory.getLogger(BusWellAddressController.class);

    @Autowired
    private IBusWellInfoService busWellInfoService;

    @Autowired
    private ICommonPermissionService commonPermissionService;

    /**
     * 获取所有的井，按照经纬度反查地址，结果输出到文件
     */
    @RequestMapping("/addressConv")
    @ResponseBody
    public Object addressConv() throws IOException {
        // 获取所有的闸井
        List<BusWellInfo> list = busWellInfoService.getAllWellList();
        System.out.println(list.size() + "======");

        for (BusWellInfo well : list) {
            String url = "http://api.map.baidu.com/reverse_geocoding/v3/?ak=c9GSEIXv6xb88X3vXPDNrVpnrgfNoW2l&output=json";
            String location = "&location=";
            location += well.getLatBaidu() + "," + well.getLngBaidu();
            url += location;
            System.out.println(url);

            String response = HttpUtil.httpGet(url);
            System.out.println(response);

            JSONObject jsonObject = JSON.parseObject(response);
            String address = ((JSONObject) jsonObject.get("result")).getString("formatted_address");
            well.setNotes(address);
            busWellInfoService.updateById(well);
        }

        return ResponseData.success("转换成功");
    }

}
