PassengerFlowCenter.java 3.48 KB
package com.bsth.data.passenger_flow;

import com.bsth.data.BasicData;
import com.bsth.data.alarm.AlarmCenter;
import com.bsth.data.gpsdata_v2.cache.GeoCacheData;
import com.bsth.data.gpsdata_v2.entity.StationRoute;
import com.bsth.message.entity.PassengerFlow;
import com.bsth.repository.realcontrol.PassengerFlowRepository;
import com.google.common.collect.BiMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Component
public class PassengerFlowCenter implements InitializingBean {

    /**
     * 车辆当前乘客数
     */
    private Map<String, Integer> vehicle2num = new ConcurrentHashMap<>();

    private static Logger log = LoggerFactory.getLogger(AlarmCenter.class);

    private volatile boolean initialized = false;

    private List<PassengerFlow> cache = new ArrayList<>();

    @Autowired
    private PassengerFlowRepository passengerFlowRepository;

    public void put(List<PassengerFlow> passengerFlows) {
        if (!initialized) {
            cache.addAll(passengerFlows);
        }
        if (cache.size() > 0) {
            calc(cache);
            cache.clear();
        }
        calc(passengerFlows);
        try {
            BiMap<String, String> shangHaiCode2LineCode = BasicData.lineCode2ShangHaiCodeMap.inverse();
            for (PassengerFlow passengerFlow : passengerFlows) {
                String line = shangHaiCode2LineCode.get(passengerFlow.getLine());
                if (line == null) {
                    log.warn("没有匹配的线路,line= {}", passengerFlow.getLine());
                    continue;
                }
                passengerFlow.setLine(line);
                StationRoute stationRoute = GeoCacheData.getStationRoute(passengerFlow.getLine(), passengerFlow.getDirection()).get(passengerFlow.getStationSeq() - 1);
                passengerFlow.setLineName(BasicData.lineCode2NameMap.get(passengerFlow.getLine()));
                passengerFlow.setStationCode(stationRoute.getCode());
                passengerFlow.setStationName(stationRoute.getName());
                passengerFlow.setStationRouteCode(stationRoute.getRouteSort());
            }
            passengerFlowRepository.saveAll(passengerFlows);
        } catch (Exception e) {
            log.error("保存客流信息失败,data={}", passengerFlows, e);
        }
    }

    private void calc(List<PassengerFlow> passengerFlows) {
        for (PassengerFlow passengerFlow : passengerFlows) {
            Integer num = vehicle2num.get(passengerFlow.getInsideCode());
            if (num == null) {
                num = 0;
            }
            num = num + passengerFlow.getUpNum() - passengerFlow.getDownNum();
            num = num < 0 ? 0 : num;
            vehicle2num.put(passengerFlow.getInsideCode(), num);
        }
    }

    public Integer getNumByVehicle(String vehicle) {
        Integer num = vehicle2num.get(vehicle);

        return num == null ? 0 : num;
    }

    public void clearByVehicle(String vehicle) {
        vehicle2num.remove(vehicle);
    }

    public void clear(){
        vehicle2num.clear();
        log.info("清除车辆乘客数据...");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        // 暂不做故障恢复
        initialized = true;
    }
}