BasicCacheData.java 2.93 KB
package com.bsth.data;

import com.bsth.entity.Line;
import com.github.stuxuhai.jpinyin.PinyinException;
import com.github.stuxuhai.jpinyin.PinyinFormat;
import com.github.stuxuhai.jpinyin.PinyinHelper;
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

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

/**
 * 基础数据缓存 by panzhao
 */
@Component
public class BasicCacheData {

    /**
     * K: lineCode
     */
    public static Map<String, Line> code2LineMap;

    public static BiMap<String, String> device2nbbmMap;

    public static Map<String, String> device2plateMap;

    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    JdbcTemplate jdbcTemplate;

    public void reLoad() {
        loadCode2LineMap();
        loadDevice2nbbmMap();
    }

    private void loadDevice2nbbmMap() {
        List<Map<String, Object>> list = jdbcTemplate.queryForList("select INSIDE_CODE,EQUIPMENT_CODE,CAR_PLATE from bsth_c_cars where equipment_code is not null");

        BiMap<String, String> biMap = HashBiMap.create();
        Map<String, String> pateMap = new HashMap<>();

        for (Map<String, Object> map : list) {
            try {
                biMap.put(map.get("EQUIPMENT_CODE").toString(), map.get("INSIDE_CODE").toString());
                pateMap.put(map.get("EQUIPMENT_CODE").toString(), map.get("CAR_PLATE").toString());
            } catch (Exception e) {
                logger.error("", e);
            }
        }

        device2nbbmMap = biMap;
        device2plateMap = pateMap;
    }

    private void loadCode2LineMap() {
        String sql = "select line_code,`name`,start_station_name,end_station_name,start_station_first_time,start_station_end_time,end_station_first_time,end_station_end_time from bsth_c_line WHERE destroy=0 and `remove`!=1";
        List<Line> list = jdbcTemplate.query(sql, BeanPropertyRowMapper.newInstance(Line.class));

        Map<String, Line> map = new HashMap<>();
        for (Line line : list) {
            try {
                line.setFullChars(PinyinHelper.convertToPinyinString(line.getName(), "", PinyinFormat.WITHOUT_TONE));
                line.setCamelChars(PinyinHelper.getShortPinyin(line.getName()));
            } catch (PinyinException e) {
                logger.error("", e);
            }
            map.put(line.getLineCode(), line);
        }

        if (map.size() > 0)
            code2LineMap = map;
    }

    @Component
    public class RefreshDataThead extends Thread {
        @Override
        public void run() {
            reLoad();
        }
    }
}