Blame view

src/main/java/com/xkl/config/ScheduledTask.java 5.45 KB
1 2
package com.xkl.config;
fangyeqing authored
3 4 5
import com.xkl.domain.*;
import com.xkl.model.CityModel;
import com.xkl.repository.*;
6 7 8 9 10
import lombok.extern.apachecommons.CommonsLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
fangyeqing authored
11
import java.util.*;
12 13 14 15 16 17 18 19


/**
 * Created by win7 on 2016/11/21.
 * 定时任务读取打分标准
 */
@Component
@CommonsLog
20
public class ScheduledTask implements Constants{
21 22
    @Autowired
    private XklAmpReportMetaScoreStandardRespository scoreStandardRespository;
23 24
    @Autowired
    private XklAmpReportMetaItemsRespository metaItemsRespository;
fangyeqing authored
25 26 27 28
    @Autowired
    private XklAmpReportCategoryRespository reportCategoryRespository;
    @Autowired
    private XklAmpReportAveScoreRespository reportAveScoreRespository;
fangyeqing authored
29 30
    @Autowired
    private XklCityRespository xklCityRespository;
31 32 33

    @Scheduled(initialDelay = 0,fixedRate = 24*60*60*1000)
    public void getTableMap(){
fangyeqing authored
34
        log.info("Load ScoreStandard and reportCategory Table");
35 36 37
        Iterator<XklAmpReportMetaScoreStandardEntity> it=scoreStandardRespository.findAll().iterator();
        while(it.hasNext()){
            XklAmpReportMetaScoreStandardEntity scoreStandard=it.next();
38 39 40 41 42
            scoreMap.put(scoreStandard.getItemId(),scoreStandard);
        }
    }

    @Scheduled(initialDelay = 0,fixedRate = 24*60*60*1000)
fangyeqing authored
43 44 45 46 47 48 49 50 51 52
    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)
53 54 55 56 57 58
    public void getItemMetaMap(){
        log.info("Load ItemMeta Table");
        Iterator<XklAmpReportMetaItemsEntity> it = metaItemsRespository.findAll().iterator();
        while(it.hasNext()){
            XklAmpReportMetaItemsEntity reportMetaItems = it.next();
            itemMetaMap.put(reportMetaItems.getItemId(),reportMetaItems);
59 60
        }
    }
fangyeqing authored
61 62 63 64 65 66 67 68 69 70 71

    @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());
        }
    }
fangyeqing authored
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135

    @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;
        }
    }
136
}