//package com.ruoyi.cache;
//
//import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
//import com.github.benmanes.caffeine.cache.Cache;
//import com.github.benmanes.caffeine.cache.Caffeine;
//import com.ruoyi.business.util.StringKit;
//import com.ruoyi.database.domain.SystemAreacodeInfo;
//import com.ruoyi.database.service.SystemAreacodeInfoService;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.scheduling.annotation.Scheduled;
//import org.springframework.stereotype.Component;
//
//import java.util.List;
//import java.util.Map;
//import java.util.stream.Collectors;
//
///**
// * 区域缓存
// *
// * @author 戴鹏涛
// * @since 2023-09-16 11:04
// */
//@Component
//public class AreaCache {
//
//    /**
//     * key: areaName，value: subAreaCode; 例：北京市，110000000000
//     */
//    private static final Cache<String, String> cache = Caffeine.newBuilder().build();
//
//    private static SystemAreacodeInfoService systemAreacodeInfoService;
//
//    @Autowired
//    AreaCache(SystemAreacodeInfoService systemAreacodeInfoService) {
//        AreaCache.systemAreacodeInfoService = systemAreacodeInfoService;
//        //初始化
//        load();
//    }
//
//    public static String get(String key) {
//        return cache.get(key, s -> {
//            try {
//                return String.valueOf(systemAreacodeInfoService.getOne(new LambdaQueryWrapper<SystemAreacodeInfo>().eq(SystemAreacodeInfo::getSubAreaCode, key)).getAreaName());
//            } catch (Exception e) {
//                return "";
//            }
//        });
//    }
//
//    /**
//     * 缓存刷新
//     */
//    @Scheduled(cron = "0 0 3 * * ?")
//    public static void refresh() {
//        load();
//    }
//
//    private static void load() {
//        List<SystemAreacodeInfo> list = systemAreacodeInfoService.list();
//        Map<String, String> dictMap = list.stream().collect(Collectors.toMap(SystemAreacodeInfo::getAreaName, e -> String.valueOf(e.getSubAreaCode()), (v1, v2) -> v2));
//        Map<String, String> dictMapCode = list.stream().collect(Collectors.toMap(
//                e -> e.getSubAreaCode().toString(),
//                e -> String.valueOf(e.getAreaName()), (v1, v2) -> v2));
//        cache.putAll(dictMap);
//        cache.putAll(dictMapCode);
//    }
//}
