SchAttrCalculator.java 5.54 KB
package com.bsth.data.schedule;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.bsth.data.LineConfigData;
import com.bsth.entity.realcontrol.LineConfig;
import com.bsth.entity.realcontrol.ScheduleRealInfo;

/**
 * 
 * @ClassName: SchAttrCalculator
 * @Description: TODO(班次相关属性计算器)
 * @author PanZhao
 * @date 2016年8月15日 下午4:40:26
 *
 */
@Component
public class SchAttrCalculator {

	@Autowired
	LineConfigData lineConfigData;

	private final static long DAY_TIME = 1000 * 60 * 60 * 24L;

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

	/**
	 * @Title: calcRealDate
	 * @Description: TODO(计算班次的真实执行日期)
	 */
	public SchAttrCalculator calcRealDate(ScheduleRealInfo sch) {
		LineConfig conf = lineConfigData.get(sch.getXlBm());

		try {
			if (null == sch.getFcsjT())
				calcFcsjTime(sch);

			SimpleDateFormat sdfyyyyMMdd = new SimpleDateFormat("yyyy-MM-dd");
			/*
			 * 早于线路开始运营时间的,加一天
			 * 如该线路 2点开始运营,2016-08-23的班次,则 2016-08-23 0:25 的班次应该调整成 2016-08-24 0:25
			 
			
						,sdfyyyyMMdd = new SimpleDateFormat("yyyy-MM-dd");
			long st = sdfyyyyMMddHHmm.parse(sch.getScheduleDateStr() + conf.getStartOpt()).getTime();
			if (st > sch.getFcsjT()) 
				sch.setFcsjAll(sch.getFcsjT() + DAY_TIME);*/
			
			if(sch.getFcsj().compareTo(conf.getStartOpt()) < 0){
				sch.setFcsjAll(sch.getFcsjT() + DAY_TIME);
			}
			
			sch.setRealExecDate(sdfyyyyMMdd.format(new Date(sch.getFcsjT())));
		} catch (Exception e) {
			logger.error("", e);
		}
		return this;
	}

	/**
	 * 
	 * @Title: calcAllTimeByFcsj
	 * @Description: TODO(根据发车时间字符串计算 (计发时间,终点时间,待发时间))
	 */
	public SchAttrCalculator calcAllTimeByFcsj(ScheduleRealInfo sch) {
		try {
			// 生成时间戳
			calcTimestamp(sch);
			
			SimpleDateFormat sdfHHmm = new SimpleDateFormat("HH:mm");
			// 计划终点时间
			if (sch.getBcsj() != null) {
				Date zdDate = new Date(sch.getDfsjT() + (sch.getBcsj() * 60 * 1000));
				sch.setZdsjT(zdDate.getTime());
				sch.setZdsj(sdfHHmm.format(zdDate));
			}
		} catch (ParseException e) {
			logger.error("", e);
		}
		return this;
	}
	
	/**
	 * 
	 * @Title: calcQdzTimePlan 
	 * @Description: TODO(计算班次的起点应到时间,list 必须是同一辆车的班次) 
	 */
	public void calcQdzTimePlan(List<ScheduleRealInfo> list){
		Collections.sort(list, new ScheduleComparator.FCSJ());
		
		int len = list.size();
		if(len == 0)
			return;
		
		ScheduleRealInfo prve = list.get(0), curr;
		for(int i = 1; i < len; i ++){
			curr = list.get(i);
			if(prve.getZdzName().equals(curr.getQdzName()))
				curr.setQdzArrDateJH(prve.getZdsj());
			
			prve = curr;
		}
	}
	
	/**
	 * 
	 * @Title: updateQdzTimePlan 
	 * @Description: TODO(更新班次的起点应到时间,list 必须是同一辆车的班次) 并返回被更新的班次 
	 */
	public List<ScheduleRealInfo> updateQdzTimePlan(List<ScheduleRealInfo> list){
		Collections.sort(list, new ScheduleComparator.FCSJ());
		
		List<ScheduleRealInfo> updateList = new ArrayList<>();
		int len = list.size();
		if(len == 0)
			return updateList;
		
		ScheduleRealInfo prve = list.get(0), curr;
		for(int i = 1; i < len; i ++){
			curr = list.get(i);
			
			if(prve.getZdzName().equals(curr.getQdzName())){
				
				if(curr.getQdzArrDateJH() != null && prve.getZdsj().equals(curr.getQdzArrDateJH())){
					prve = curr;
					continue;
				}
					
				curr.setQdzArrDateJH(prve.getZdsj());
				updateList.add(curr);
			}
			else{
				curr.setQdzArrDateJH(null);
				updateList.add(curr);
			}
			prve = curr;
		}
		
		return updateList;
	}
	
	/**
	 * 
	 * @Title: connectOutSchedule 
	 * @Description: TODO(关联出场班次) 
	 */
	public void connectOutSchedule(List<ScheduleRealInfo> list){
		Collections.sort(list, new ScheduleComparator.FCSJ());
		
		int len = list.size();
		if(len == 0)
			return;
		
		ScheduleRealInfo prve = list.get(0), curr;
		for(int i = 1; i < len; i ++){
			curr = list.get(i);
			
			//出站即出场关联
			if(prve.getBcType().equals("out") && prve.getJhlc() == null)
				curr.setTwinsSch(prve);
			
			//进站即进场关联
			if(curr.getBcType().equals("in") && curr.getJhlc() == null)
				prve.setTwinsSch(curr);
			
			prve = curr;
		}
	}

	public SchAttrCalculator calcFcsjTime(ScheduleRealInfo sch) throws ParseException {
		SimpleDateFormat sdfyyyyMMddHHmm = new SimpleDateFormat("yyyy-MM-ddHH:mm");
		sch.setFcsjT(sdfyyyyMMddHHmm.parse(sch.getRealExecDate() + sch.getFcsj()).getTime());
		return this;
	}
	
	public void calcTimestamp(ScheduleRealInfo sch) throws ParseException{
		//计发时间
		if(sch.getFcsjT() == null)
			calcFcsjTime(sch);
		
		//待发时间
		if(sch.getDfsj() == null)
			sch.setDfsjAll(sch.getFcsjT());
		if(sch.getDfsjT() == null)
			sch.setDfsjAll(sch.getDfsj());
		
		//实发时间戳
		if(sch.getFcsjActualTime() == null && sch.getFcsjActual() != null)
			sch.setFcsjActualAll(sch.getFcsjActual());
		
		//实达时间戳
		if(sch.getZdsjActualTime() == null && sch.getZdsjActual() != null)
			sch.setZdsjActualAll(sch.getZdsjActual());
	}
}