Station2ParkBuffer.java 6.5 KB
package com.bsth.data;

import com.bsth.common.ResponseCode;
import com.bsth.entity.realcontrol.ChildTaskPlan;
import com.bsth.entity.realcontrol.ScheduleRealInfo;
import com.bsth.entity.realcontrol.StationToPark;
import com.bsth.repository.realcontrol.StationToParkRepository;
import com.bsth.util.Arith;
import com.google.common.collect.ArrayListMultimap;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * 站到场 历时、公里 数据缓存
 * Created by panzhao on 2017/7/10.
 */
@Component
public class Station2ParkBuffer implements CommandLineRunner {

    @Autowired
    StationToParkRepository stationToParkRepository;

    private static ArrayListMultimap listMultimap;

    private static Set<StationToPark> pstBuff = new HashSet<>();

    static Logger log = LoggerFactory.getLogger(Station2ParkBuffer.class);

    @Override
    public void run(String... strings) throws Exception {
        listMultimap = ArrayListMultimap.create();
        Iterator<StationToPark> iterator = stationToParkRepository.findAll().iterator();
        StationToPark stp;
        while (iterator.hasNext()) {
            stp = iterator.next();
            listMultimap.put(stp.getLineCode(), stp);
        }
    }

    public static List<StationToPark> get(String lineCode) {
        return listMultimap.get(lineCode);
    }

    public static StationToPark get(String lineCode, String sName, String eName) {
        List<StationToPark> list = get(lineCode);
        if(null == list)
            return null;
        StationToPark stp = null;
        for (StationToPark s : list) {
            if (s.getStationName().equals(sName) && s.getParkName().equals(eName)) {
                stp = s;
                break;
            }
        }
        return stp;
    }

    private static DateTimeFormatter fmtHHmm = DateTimeFormat.forPattern("HH:mm");

    public static void put(ChildTaskPlan ctask) {
        try{
            String type2 = ctask.getType2();
            String lineCode = ctask.getSchedule().getXlBm(), sName, eName;

            if (type2.equals("2")) {
                sName = ctask.getStartStationName();
                eName = ctask.getEndStationName();
            } else if (type2.equals("3")) {
                eName = ctask.getStartStationName();
                sName = ctask.getEndStationName();
            } else {
                return;
            }

            Float time = calcMinute(ctask);
            Float mileage = ctask.getMileage();

            StationToPark stp = get(lineCode, sName, eName);
            if (stp == null) {
                stp = new StationToPark();
                stp.setLineCode(lineCode);
                stp.setStationName(sName);
                stp.setParkName(eName);
                listMultimap.put(lineCode, stp);
            }

            if (type2.equals("2")) {
                stp.setTime1(time);
                stp.setMileage1(mileage);
            } else {
                stp.setTime2(time);
                stp.setMileage2(mileage);
            }

            pstBuff.add(stp);
        }catch (Exception e){
            log.error("", e);
        }
    }

    public static void put(ScheduleRealInfo sch){
        try{
            String type = sch.getBcType();
            String lineCode = sch.getXlBm(), sName, eName;

            if (type.equals("in")) {
                sName = sch.getQdzName();
                eName = sch.getZdzName();
            } else if (type.equals("out")) {
                eName = sch.getQdzName();
                sName = sch.getZdzName();
            } else {
                return;
            }

            long dt = sch.getZdsjT() - sch.getDfsjT();
            Float time = Float.parseFloat(String.valueOf(Arith.div(Arith.div(dt, 1000), 60)));
            Float mileage = Float.parseFloat(sch.getJhlc().toString());

            StationToPark stp = get(lineCode, sName, eName);
            if (stp == null) {
                stp = new StationToPark();
                stp.setLineCode(lineCode);
                stp.setStationName(sName);
                stp.setParkName(eName);
                listMultimap.put(lineCode, stp);
            }

            if (type.equals("in")) {
                stp.setTime1(time);
                stp.setMileage1(mileage);
            } else {
                stp.setTime2(time);
                stp.setMileage2(mileage);
            }

            pstBuff.add(stp);
        }catch (Exception e){
            log.error("", e);
        }
    }

    public static Float calcMinute(ChildTaskPlan ctask) {
        long t = 0;

        try {
            long st = fmtHHmm.parseMillis(ctask.getStartDate());
            long et = fmtHHmm.parseMillis(ctask.getEndDate());

            t = et - st;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Float.parseFloat(String.valueOf(Arith.div(Arith.div(t, 1000), 60)));
    }

    public void saveAll(){
        if (pstBuff.size()==0) {
            return;
        }

        Set<StationToPark> pstBuffCopy =  pstBuff;
        pstBuff = new HashSet<>();
        //持久化到数据库
        stationToParkRepository.saveAll(pstBuffCopy);
    }

    public Map<String, Object> delete(String lineCode, Integer id) {
        Map<String, Object> rs = new HashMap<>();
        try {
            List<StationToPark> list = listMultimap.get(lineCode);

            StationToPark stp = null;
            for(StationToPark temp : list){
                if(temp.getId().equals(id)){
                    stp=temp;
                    break;
                }
            }

            if(stp != null){
                listMultimap.remove(lineCode, stp);
                stationToParkRepository.deleteById(id);
                rs.put("status", ResponseCode.SUCCESS);
            }
            else{
                rs.put("status", ResponseCode.SUCCESS);
                rs.put("msg", "操作失败,可能数据已经被删除!");
            }

        }catch (Exception e){
            rs.put("status", ResponseCode.ERROR);
            rs.put("msg", e.getMessage());
            log.error("", e);
        }
        return rs;
    }
}