ScheduledTask.java 5.61 KB
package com.xkl.config;

import com.xkl.domain.*;
import com.xkl.model.CityModel;
import com.xkl.repository.*;
import lombok.extern.apachecommons.CommonsLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.*;


/**
 * Created by win7 on 2016/11/21.
 * 定时任务读取打分标准
 */
@Component
@CommonsLog
public class ScheduledTask implements Constants{
    @Autowired
    private XklAmpReportMetaScoreStandardRespository scoreStandardRespository;
    @Autowired
    private XklAmpReportMetaItemsRespository metaItemsRespository;
    @Autowired
    private XklAmpReportCategoryRespository reportCategoryRespository;
    @Autowired
    private XklAmpReportAveScoreRespository reportAveScoreRespository;
    @Autowired
    private XklCityRespository xklCityRespository;

    @Scheduled(initialDelay = 0,fixedRate = 24*60*60*1000)
    public void getTableMap(){
        log.info("Load ScoreStandard and reportCategory Table");
        Iterator<XklAmpReportMetaScoreStandardEntity> it=scoreStandardRespository.findAll().iterator();
        while(it.hasNext()){
            XklAmpReportMetaScoreStandardEntity scoreStandard=it.next();
            scoreMap.put(scoreStandard.getItemId(),scoreStandard);
        }
    }

    @Scheduled(initialDelay = 0,fixedRate = 24*60*60*1000)
    public void getWeightedScoreMap(){
        log.info("Load WeightedScore Table");
        Iterator<XklAmpReportCategoryEntity> iterator = reportCategoryRespository.findAll().iterator();
        while (iterator.hasNext()){
            XklAmpReportCategoryEntity reportCategory = iterator.next();
            weightedScoreMap.put(reportCategory.getId(),reportCategory);
        }
    }

    @Scheduled(initialDelay = 0,fixedRate = 24*60*60*1000)
    public void getItemMetaMap(){
        log.info("Load ItemMeta Table");
        Iterator<XklAmpReportMetaItemsEntity> it = metaItemsRespository.findAll().iterator();
        while(it.hasNext()){
            XklAmpReportMetaItemsEntity reportMetaItems = it.next();
            if(reportMetaItems.getType()!=1) {//测试项目除外
                itemMetaMap.put(reportMetaItems.getItemId(), reportMetaItems);
            }else{
                testItemSet.add(reportMetaItems.getItemId());
            }
        }
    }

    @Scheduled(initialDelay = 0,fixedRate = 24*60*60*1000)
    public void getAveScoreMap(){
        log.info("Load AveScore Table");
        Iterator<XklAmpReportAverageScoreEntity> iterator = reportAveScoreRespository.findAll().iterator();
        while (iterator.hasNext()){
            XklAmpReportAverageScoreEntity reportCategory = iterator.next();
            String ageAndType = reportCategory.getAgeId()+"-"+reportCategory.getScoreType();
            aveScoreMap.put(ageAndType,reportCategory.getAverageScore());
        }
    }

    @Scheduled(initialDelay = 0,fixedRate = 24*60*60*1000)
    public void getCityMap(){
        log.info("Load City Table");
        Map<Long,XklCityEntity> idCityMap = new HashMap<>();
        Iterator<XklCityEntity> iterator = xklCityRespository.findAll().iterator();
        while(iterator.hasNext()){
            XklCityEntity city = iterator.next();
            idCityMap.put(city.getId(),city);
        }
        for (Map.Entry<Long, XklCityEntity> entry : idCityMap.entrySet()) {
            long id = entry.getKey();
            XklCityEntity xklCity = entry.getValue();
            long parentId = xklCity.getParentId();
            List<Long> idList = new ArrayList<>();
            idList.add(id);
            idList.add(parentId);
            getIdList(parentId,idCityMap,idList);
            CityModel cityModel = new CityModel();
            if(idList.size()== Constants.ID_TYPE_COUNTRY){//国家级:中国000000
                cityModel.setType(Constants.ID_TYPE_COUNTRY);
                cityModel.setCountry(idList.get(0));
            }else if(idList.size() == Constants.ID_TYPE_PROVINCE){//省级:北京110000,河北130000
                cityModel.setType(Constants.ID_TYPE_PROVINCE);
                cityModel.setCountry(idList.get(1));
                cityModel.setProvince(idList.get(0));
            }else if(idList.size() == Constants.ID_TYPE_CITY){//市级:东城区110101,石家庄市130100
                cityModel.setType(Constants.ID_TYPE_CITY);
                cityModel.setCountry(idList.get(2));
                cityModel.setProvince(idList.get(1));
                cityModel.setCity(idList.get(0));
            }else if(idList.size() == Constants.ID_TYPE_COUNTY){//区级:石家庄市长安区130102
                cityModel.setType(Constants.ID_TYPE_COUNTY);
                cityModel.setCountry(idList.get(3));
                cityModel.setProvince(idList.get(2));
                cityModel.setCity(idList.get(1));
                cityModel.setCounty(idList.get(0));
            }else{
                cityModel.setType(Constants.ID_TYPE_COUNTRY);
                cityModel.setCountry(idList.get(0));
            }
            cityMap.put(xklCity.getIdCardCode(),cityModel);
        }
        log.info("load city table success");
    }

    /**
     * 递归找parent,直到中国
     * @param parentId
     * @param idCityMap
     * @param idList
     */
    public void getIdList(long parentId,Map<Long,XklCityEntity> idCityMap,List<Long> idList){
        if(parentId == 0) return;

        XklCityEntity city = idCityMap.get(parentId);
        if(city!=null){
            long grandParentId = city.getParentId();
            idList.add(grandParentId);
            getIdList(grandParentId,idCityMap,idList);
        }else{
            return;
        }
    }
}