package com.hfkj.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.hfkj.common.utils.HttpsUtils;
import com.hfkj.dao.BsCityMapper;
import com.hfkj.entity.BsCity;
import com.hfkj.entity.BsCityExample;
import com.hfkj.service.CommonService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service("commonService")
public class CommonServiceImpl implements CommonService {

    @Resource
    private BsCityMapper bsCityMapper;

    @Override
    public List<BsCity> getCityList(Long parentId) {
        BsCityExample example = new BsCityExample();
        BsCityExample.Criteria criteria = example.createCriteria();

        if (parentId != null) {
            criteria.andParentIdEqualTo(parentId);
        }else {
            criteria.andParentIdIsNull();
        }
        criteria.andStatusEqualTo(1);

        return bsCityMapper.selectByExample(example);
    }

    @Override
    public List<BsCity> getCityListAll() {
        BsCityExample example = new BsCityExample();
        BsCityExample.Criteria criteria = example.createCriteria();

        criteria.andStatusEqualTo(1);

        return bsCityMapper.selectByExample(example);
    }

    @Override
    public List<JSONObject> getCity(String code, String name, String child) {

        // 获取父类
        List<BsCity> regionListParent = getCityList(null);

        List<JSONObject> jsonProvince = new ArrayList<>();

        for (BsCity bsCity : regionListParent) {
            JSONObject provinceObject = new JSONObject();
            provinceObject.put(code , bsCity.getRegionId());
            provinceObject.put(name , bsCity.getRegionName());

            List<BsCity> cityList = getCityList(bsCity.getRegionId());

            if (cityList.size() > 0) {
                List<JSONObject> jsonCity = new ArrayList<>();
                for (BsCity city : cityList) {
                    JSONObject objectCity = new JSONObject();
                    objectCity.put(code , city.getRegionId());
                    objectCity.put(name , city.getRegionName());

                    List<BsCity> areaList = getCityList(city.getRegionId());

                    if  (areaList.size() > 0 ) {
                        List<JSONObject> jsonArea = new ArrayList<>();
                        for (BsCity area : areaList) {
                            JSONObject objectArea = new JSONObject();
                            objectArea.put(code , area.getRegionId());
                            objectArea.put(name , area.getRegionName());
                            objectArea.put("isLeaf" , true);
                            jsonArea.add(objectArea);
                        }
                        objectCity.put(child , jsonArea);
                    }
                    jsonCity.add(objectCity);
                }
                provinceObject.put(child , jsonCity);

            }
            jsonProvince.add(provinceObject);

        }

        return jsonProvince;
    }

    @Override
    public JSONObject locateByLngAndLat(String location) {

        Map<String , Object> map = new HashMap<>();
        map.put("key" , "0083a5b1cc5289d7e9691424ee62136e");
        map.put("location" , location);

        return HttpsUtils.doGet("https://restapi.amap.com/v3/geocode/regeo" , map);

    }

}
