package com.yuchen.saas.manage.system.api.cache;

import com.yuchen.saas.manage.system.api.entity.Region;
import com.yuchen.saas.manage.system.api.feign.ISysClient;
import org.nest.springwrap.core.cache.utils.CacheUtil;
import org.nest.springwrap.core.tool.api.R;
import org.nest.springwrap.core.tool.utils.SpringUtil;

import static org.nest.springwrap.core.cache.constant.CacheConstant.SYS_CACHE;


public class RegionCache {
    public static final int PROVINCE_LEVEL = 1;
    public static final int CITY_LEVEL = 2;
    public static final int DISTRICT_LEVEL = 3;
    public static final int TOWN_LEVEL = 4;
    public static final int VILLAGE_LEVEL = 5;

    private static final String REGION_CODE = "region:code:";

    private static ISysClient sysClient;

    private static ISysClient getSysClient() {
        if (sysClient == null) {
            sysClient = SpringUtil.getBean(ISysClient.class);
        }
        return sysClient;
    }


    public static Region getByCode(String code) {
        return CacheUtil.get(SYS_CACHE, REGION_CODE, code, () -> {
            R<Region> result = getSysClient().getRegion(code);
            return result.getData();
        });
    }

}
