package com.fjhx.utils;

import cn.hutool.core.convert.Convert;
import cn.hutool.extra.spring.SpringUtil;
import com.fjhx.feign.IRegionClient;
import org.springblade.core.tool.api.R;

import java.util.HashSet;
import java.util.List;
import java.util.Map;

public class RegionClientUtil {

    private static IRegionClient iRegionClient;

    /**
     * 懒汉式单例
     */
    private synchronized static IRegionClient getRegionClient() {
        if (iRegionClient == null) {
            iRegionClient = SpringUtil.getBean(IRegionClient.class);
        }
        return iRegionClient;
    }

    public static void setRegionName(List<Map<String, Object>> list) {
        IRegionClient regionClient = getRegionClient();

        HashSet<String> regionSet = new HashSet<>();

        for (Map<String, Object> item : list) {
            // 查询国省市
            regionSet.add(Convert.toStr(item.get("countryId")));
            regionSet.add(Convert.toStr(item.get("provinceId")));
            regionSet.add(Convert.toStr(item.get("cityId")));
        }
        regionSet.remove(null);

        R<Map<String, String>> r = regionClient.getChineseNameById(regionSet);
        Map<String, String> regionMap = Assert.result(r);

        for (Map<String, Object> item : list) {
            // 赋值国省市
            item.put("countryName", regionMap.get(Convert.toStr(item.get("countryId"))));
            item.put("provinceName", regionMap.get(Convert.toStr(item.get("provinceId"))));
            item.put("cityName", regionMap.get(Convert.toStr(item.get("cityId"))));
        }

    }

    public static void setEntityRegionName(List<? extends Region> list) {
        if (list.size() == 0) return;


        HashSet<String> regionSet = new HashSet<>();

        for (Region region : list) {
            // 查询国省市
            regionSet.add(region.getCountryId());
            regionSet.add(region.getProvinceId());
            regionSet.add(region.getCityId());
        }

        regionSet.remove(null);

        if (regionSet.size() == 0) return;

        R<Map<String, String>> r = getRegionClient().getChineseNameById(regionSet);
        Map<String, String> regionMap = Assert.result(r);

        for (Region region : list) {
            // 赋值国省市
            region.setCountryName(regionMap.get(region.getCountryId()));
            region.setProvinceName(regionMap.get(region.getProvinceId()));
            region.setCityName(regionMap.get(region.getCityId()));
        }

    }


    public static void setEntityRegionName(Region region) {

        HashSet<String> regionSet = new HashSet<>();
        regionSet.add(region.getCountryId());
        regionSet.add(region.getProvinceId());
        regionSet.add(region.getCityId());
        regionSet.remove(null);

        if (regionSet.size() == 0) return;

        R<Map<String, String>> r = getRegionClient().getChineseNameById(regionSet);
        Map<String, String> regionMap = Assert.result(r);

        // 赋值国省市
        region.setCountryName(regionMap.get(region.getCountryId()));
        region.setProvinceName(regionMap.get(region.getProvinceId()));
        region.setCityName(regionMap.get(region.getCityId()));

    }


    public interface Region {

        String getCountryId();

        String getProvinceId();

        String getCityId();

        void setCountryName(String countryName);

        void setProvinceName(String provinceName);

        void setCityName(String cityName);

    }


}