package com.ruoyi.ss.city.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.ss.city.domain.City;
import com.ruoyi.ss.city.mapper.CityMapper;
import com.ruoyi.ss.city.service.ICityService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * 城市Service业务层处理
 *
 * @author ruoyi
 * @date 2024-09-18
 */
@Slf4j
@Service
public class CityServiceImpl implements ICityService
{
    public static final String GEO_WEB_KEY = SpringUtils.getRequiredProperty("geo.key");

    @Resource
    private CityMapper rlCityMapper;

    @Autowired
    private ICityService cityService;

    /**
     * 查询城市
     *
     * @param cityId 城市主键
     * @return 城市
     */
    @Override
    public City selectRlCityByCityId(Long cityId)
    {
        return rlCityMapper.selectRlCityByCityId(cityId);
    }

    /**
     * 根据地址编码查询城市
     *
     * @param adcode 地址编码
     * @return 城市
     */
    @Override
    public City selectRlCityByAdcode(String adcode)
    {
        return rlCityMapper.selectRlCityByAdcode(adcode);
    }


    /**
     * 查询城市列表
     *
     * @param rlCity 城市
     * @return 城市
     */
    @Override
    public List<City> selectRlCityList(City rlCity)
    {
        return rlCityMapper.selectRlCityList(rlCity);
    }

    /**
     * 查询所有地级市城市列表
     */
    @Override
    public List<City> selectAllCity(String cityName) {
        return rlCityMapper.selectAllCity(cityName);
    }

    /**
     * 新增城市
     *
     * @param rlCity 城市
     * @return 结果
     */
    @Override
    public int insertRlCity(City rlCity)
    {
        return rlCityMapper.insertRlCity(rlCity);
    }

    /**
     * 修改城市
     *
     * @param rlCity 城市
     * @return 结果
     */
    @Override
    public int updateRlCity(City rlCity)
    {
        return rlCityMapper.updateRlCity(rlCity);
    }

    /**
     * 批量删除城市
     *
     * @param cityIds 需要删除的城市主键
     * @return 结果
     */
    @Override
    public int deleteRlCityByCityIds(Long[] cityIds)
    {
        return rlCityMapper.deleteRlCityByCityIds(cityIds);
    }

    /**
     * 删除城市信息
     *
     * @param cityId 城市主键
     * @return 结果
     */
    @Override
    public int deleteRlCityByCityId(Long cityId)
    {
        return rlCityMapper.deleteRlCityByCityId(cityId);
    }

    /**
     * 根据定位获取哪个城市
     */
    @Override
    public City getCityByLocation(String lon, String lat) {
        try {
            return getCity(lon, lat);
        } catch (Exception e) {
            log.error("【根据定位获取地址】转换地址报错", e);
        }
        return null;
    }

    /**
     * 根据定位获取哪个城市
     */
    @Override
    public Long getCityIdByLocation(String lon, String lat) {
        try {
            City city = getCity(lon, lat);
            if(ObjectUtil.isNotNull(city)){
                return city.getCityId();
            }
            return null;
        } catch (Exception e) {
            log.error("【根据定位获取地址】转换地址报错", e);
        }
        return null;
    }

    private City getCity(String lon, String lat) {
        String location = lon + "," + lat;
        String url = StrUtil.format("https://restapi.amap.com/v3/geocode/regeo?key={}&location={}&&radius=1000&extensions=all", GEO_WEB_KEY, location);
        String result = HttpUtils.sendGet(url);
        //将json字符串转换为Object
        JSONObject jsonObject = JSONObject.parseObject(result,JSONObject.class);
        JSONObject regeocode1 = jsonObject.getJSONObject("regeocode");
        JSONObject addressComponent = regeocode1.getJSONObject("addressComponent");
        String adcode = addressComponent.getString("adcode");
        // 如果adcode不是以"00"结尾，则将后面两位替换成"00"
        if (!adcode.endsWith("00")) {
            adcode = adcode.substring(0, adcode.length() - 2) + "00";
        }
        return cityService.selectRlCityByAdcode(adcode);
    }

    /**
     * 根据定位获取哪个城市
     */
    @Override
    public String getAddressByLocation(String lon, String lat) {
        String address = null;
        try {
            String location = lon + "," + lat;
            String url = StrUtil.format("https://restapi.amap.com/v3/geocode/regeo?key={}&location={}&&radius=1000&extensions=all", GEO_WEB_KEY, location);
            String result = HttpUtils.sendGet(url);
//            log.info("【根据定位获取地址】请求结果result:{}",result);
            //将json字符串转换为Object
            JSONObject jsonObject = JSONObject.parseObject(result,JSONObject.class);
            JSONObject regeocode1 = jsonObject.getJSONObject("regeocode");
            address = regeocode1.getString("formatted_address");
//            log.info("【根据定位获取地址】address=:【{}】",result);
            return address;
        } catch (Exception e) {
            log.error("【根据定位获取地址】转换地址报错", e);
        }
        return address;
    }

}
