基于内存进行缓存

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: fh
 * @Date: 2022-41-04 10:41:43
 * @Desription:
 **/

@Component
public class InitData implements CommandLineRunner {

    @Autowired(required = false)
    BatchService batchService;

    @Autowired(required = false)
    ScoreRulesService scoreRulesService;

    @Autowired(required = false)
    OrganService organService;

    @Autowired
    SysAreaService sysAreaService;

    private static Map<Integer, String> batchProperties;

    private static List<ScoreRulesResponse> focusScoreRules;

    private static List<ScoreRulesResponse> aloneScoreRules;

    public static List<OrganResponse> allOrganResponse;

    public static List<SysAreaResponse> allArea;

    public static List<ScoreRulesResponse> getByWorkProperty(String workProperty) {
        return workProperty.equals(WorkPropertyEnum.FOCUS.toString()) ? focusScoreRules : aloneScoreRules;
    }

    public static String getBatchById(Integer id) {
        return batchProperties.get(id);
    }

    private static Map<Integer, OrganResponse> organProperties;

    private static Map<String, String> areaProperties;

    public static String getAreaNameById(String id){
        return areaProperties.get(id);
    }

    public static OrganResponse getOrganById(Integer id) {
        return organProperties.get(id);
    }

    public static void resetBatches(Map<Integer, String> batchInitDataMap) {
        batchProperties = batchInitDataMap;
    }

    public static void resetOrgans(List<OrganResponse> organResponses) {
        allOrganResponse = organResponses;
    }

    @Override
    public void run(String... args) throws Exception {
        if (batchProperties == null) {
            batchProperties = new HashMap<>(16);
        }
        List<BatchResponse> batchResponses = batchService.findList();
        if (CollectionUtils.isNotEmpty(batchResponses)) {
            Map<Integer, String> batchInitDataMap = batchResponses.stream().
                    collect(Collectors.toMap(BatchResponse::getId, a -> a.getName(), (k1, k2) -> k1));
            batchProperties = batchInitDataMap;
        }
        //初始化评分规则列表
        List<ScoreRulesResponse> scoreRulesList = scoreRulesService.findAllList();
        focusScoreRules = scoreRulesList.stream().filter(s -> s.getWorkProperty().equals(WorkPropertyEnum.FOCUS.toString())).collect(Collectors.toList());
        aloneScoreRules = scoreRulesList.stream().filter(s -> s.getWorkProperty().equals(WorkPropertyEnum.ALONE.toString())).collect(Collectors.toList());
        //把单位数据加载到内存
        List<OrganResponse> organResponses = organService.getAllOrgans();
        allOrganResponse = organResponses;
        if (CollectionUtils.isNotEmpty(organResponses)) {
            Map<Integer, OrganResponse> organInitDataMap = organResponses.stream().
                    collect(Collectors.toMap(OrganResponse::getId, o -> o));
            organProperties = organInitDataMap;
        }

        List<SysAreaResponse> areaResponses = sysAreaService.getAll();
        allArea = areaResponses;
        if (CollectionUtils.isNotEmpty(areaResponses)) {
            Map<String, String> areaInitDataMap = areaResponses.stream().
                    collect(Collectors.toMap(SysAreaResponse::getId, SysAreaResponse::getAreaName));
            areaProperties = areaInitDataMap;
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值