package com.sifang.modules.platform.service;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.config.SfCacheService;
import com.sifang.modules.platform.utils.AddressUtils;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AddressService {

    @Autowired
    private  SfCacheService cacheService;

    /**
     * 获取省份
     */
    public  List<Record> getProvinces(){
        String provincesList = cacheService.get("provincesList");
        List<Record> list ;
        if (StringUtil.isNotBlank(provincesList)) {
            list = new Gson().fromJson(provincesList, new TypeToken< List<Record>>() {
            }.getType());
        }else
        {
            List<Record> li = AddressUtils.getProvincesList();
            list = li;
            cacheService.set("provincesList",new Gson().toJson(list));
        }
        return list;
    }

    /**
     * 获取市
     */
    public  List<Record> getCityList(){
        String provincesList = cacheService.get("getCityList");
        List<Record> list ;
        if (StringUtil.isNotBlank(provincesList)) {
            list = new Gson().fromJson(provincesList, new TypeToken< List<Record>>() {
            }.getType());
        }else
        {
            List<Record> li = AddressUtils.getCityList();
            list = li;
            cacheService.set("getCityList",new Gson().toJson(list));
        }
        return list;
    }


    public List<Record> getCityByProviceId(String provinceId){
        String citysList = cacheService.hget("citysList",provinceId);
        List<Record> list ;
        if (StringUtil.isNotBlank(citysList)) {
            list = new Gson().fromJson(citysList, new TypeToken< List<Record>>() {
            }.getType());
        }else
        {
            List<Record> li = AddressUtils.getCitysByProId(provinceId);
            list = li;
            cacheService.hset("citysList",provinceId,new Gson().toJson(list));
        }
        return list;
    }
    public List<Record> getCityByProviceName(String provinceName){

        String citysList = cacheService.hget("citysList",provinceName);
        List<Record> list ;
        if (StringUtil.isNotBlank(citysList)) {
            list = new Gson().fromJson(citysList, new TypeToken< List<Record>>() {
            }.getType());
        }else
        {
            List<Record> li = AddressUtils.getCityByProName(provinceName);
            list = li;
            cacheService.hset("citysList",provinceName,new Gson().toJson(list));
        }
        return list;
    }

    public List<Record> getAreaByCityId(String cityId){
        String areasList = cacheService.hget("areasList",cityId);
        List<Record> list ;
        if (StringUtil.isNotBlank(areasList)) {
            list = new Gson().fromJson(areasList, new TypeToken< List<Record>>() {
            }.getType());
        }else
        {
            List<Record> li = AddressUtils.getAreaByCityId(cityId);
            list = li;
            cacheService.hset("areasList",cityId,new Gson().toJson(list));
        }
        return list;
    }
    public List<Record> getAreaByCityName(String cityName){

        String areasList = cacheService.hget("areasList",cityName);
        List<Record> list ;
        if (StringUtil.isNotBlank(areasList)) {
            list = new Gson().fromJson(areasList, new TypeToken< List<Record>>() {
            }.getType());
        }else
        {
            List<Record> li = AddressUtils.getAreaByCityName(cityName);
            list = li;
            cacheService.hset("areasList",cityName,new Gson().toJson(list));
        }
        return list;
    }

    public List<Record> getProviceCodeList(){

        String procodeList = cacheService.get("proviceCodeList");
        List<Record> list ;
        if (StringUtil.isNotBlank(procodeList)) {
            list = new Gson().fromJson(procodeList, new TypeToken< List<Record>>() {
            }.getType());
        }else
        {
            List<Record> li = AddressUtils.findProCodeList();
            list = li;
            if (list != null && list.size() > 0) {
                cacheService.set("proviceCodeList",new Gson().toJson(list));
            }
        }
        return list;
    }

}
