package com.hwrj.cloud.admin;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hwrj.cloud.admin.mapper.UmsCompanyInfoMapper;
import com.hwrj.cloud.admin.mapper.UmsMemberOriginMapper;
import com.hwrj.cloud.admin.model.*;
import com.hwrj.cloud.common.util.HttpUtils;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.util.StringUtils;

import java.util.*;

@Slf4j
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = AdminApplication.class)
public class FastCompanyInfo {



private int i = 1;

    @Value("${qiniu.path}")
    private String url;

    @Autowired
    UmsCompanyInfoMapper umsCompanyInfoMapper;
    @Autowired
    UmsMemberOriginMapper umsMemberOriginMapper;
    @Test
    public void readFile(){
       UmsCompanyInfoExample example = new UmsCompanyInfoExample();
        List<UmsCompanyInfo> companyInfos = umsCompanyInfoMapper.selectByExample(example);
        for (UmsCompanyInfo ums: companyInfos) {
            UmsCompanyInfo u = new UmsCompanyInfo();
            String cityId = ums.getCityId();
            String countyId = ums.getCountyId();
            String detailAddress = ums.getDetailAddress();
            LagLatEntity lagLat = getLagLat(cityId, countyId, detailAddress);
            if (lagLat == null || lagLat.getLag()==null){
                System.out.println("获取对应的坐标失败"+cityId+"|"+countyId+"|"+detailAddress);
            }else {
                u.setId(ums.getId());
                u.setLatitude(lagLat.getLat());
                u.setLongitude(lagLat.getLag());
                umsCompanyInfoMapper.updateByPrimaryKeySelective(u);
                System.out.println(JSONObject.toJSONString(u));
            }
        }
    }


    public LagLatEntity getLagLat(String cityName,String countyName,String detail){
        LagLatEntity la = new LagLatEntity();
        if (StringUtils.isEmpty(countyName)||StringUtils.isEmpty(countyName)){
            return null;
        }

        if (StringUtils.isEmpty(detail)){

            AmapEntity county = getAddressDetail(cityName, countyName);
            if (county != null){
                String[] split = county.getLocation().split(",");
                la.setLag(split[0]);
                la.setLat(split[1]);
            }else {
                AmapEntity city = getAddressDetail(cityName, cityName);
                if (city != null){
                    String[] split = city.getLocation().split(",");
                    la.setLag(split[0]);
                    la.setLat(split[1]);
                }else {
                    return null;
                }
            }

        }else {
            AmapEntity addressDetail = getAddressDetail(cityName, detail);
            if (addressDetail != null){
                String[] split = addressDetail.getLocation().split(",");
                la.setLag(split[0]);
                la.setLat(split[1]);
            }else {
                AmapEntity county = getAddressDetail(cityName, countyName);
                if (county != null){
                    String[] split = county.getLocation().split(",");
                    la.setLag(split[0]);
                    la.setLat(split[1]);
                }else {
                    AmapEntity city = getAddressDetail(cityName, cityName);
                    if (city != null){
                        String[] split = city.getLocation().split(",");
                        la.setLag(split[0]);
                        la.setLat(split[1]);
                    }else {
                        return null;
                    }
                }
            }
        }
        return la;
    }


    public AmapEntity getAddressDetail(String cityName, String key) {
        if (StringUtils.isEmpty(cityName)){
            return null;
        }
        if (StringUtils.isEmpty(key)){
            return null;
        }
        String s = sendAddress(cityName, key);
        log.info("返回地址信息"+s);
        JSONObject object = JSON.parseObject(s);

        String status = object.getString("status");
        if ("1".equals(status)){
            String count = object.getString("count");
            if (Integer.valueOf(count)<1){
                return null;
            }
            JSONArray weatherArray = JSON.parseArray(object.getString("geocodes"));
            for (Object json :weatherArray) {
                AmapEntity amapEntity = JSONObject.parseObject(JSONObject.toJSONString(json), AmapEntity.class);
                return amapEntity;
            }
        }



        return null;
    }

    public String sendAddress(String city, String key) {
        String lbsDevKey = "52277a894c1855d8917634177809169a";
        String addUrl ="https://restapi.amap.com/v3/geocode/geo";
        Map<String, String> queryParas = new HashMap<String, String>();
        queryParas.put("address", key);
        queryParas.put("key", lbsDevKey);
        queryParas.put("city", city);
        String weatherInfo = null;
        try {
            weatherInfo = HttpUtils.get(addUrl, queryParas);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return weatherInfo;
    }




}
