基于内存进行缓存


package com.izkml.energy.score.init;

import com.izkml.energy.score.enums.WorkPropertyEnum;
import com.izkml.energy.score.response.BatchResponse;
import com.izkml.energy.score.response.OrganResponse;
import com.izkml.energy.score.response.ScoreRulesResponse;
import com.izkml.energy.score.response.SysAreaResponse;
import com.izkml.energy.score.service.BatchService;
import com.izkml.energy.score.service.OrganService;
import com.izkml.energy.score.service.ScoreRulesService;
import com.izkml.energy.score.service.SysAreaService;
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: fyqing
 * @Date: 2020-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;
        }
    }
}


posted @ 2020-07-14 15:33  荭丶尘  阅读(122)  评论(0编辑  收藏  举报