CalcToolServiceImpl.java 13.8 KB
package com.bsth.service.calc.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.bsth.entity.calc.CalcInterval;
import com.bsth.entity.calc.CalcWaybill;
import com.bsth.entity.realcontrol.ChildTaskPlan;
import com.bsth.entity.realcontrol.ScheduleRealInfo;
import com.bsth.entity.sys.Interval;
import com.bsth.repository.calc.CalcIntervalRepository;
import com.bsth.repository.calc.CalcWaybillRepository;
import com.bsth.service.calc.CalcToolService;
import com.bsth.service.calc.CalcCulateMileageService;
import com.bsth.service.report.CulateMileageService;
import com.bsth.util.Arith;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

/**
 * Created by 17/11/16.
 */
@Service
public class CalcToolServiceImpl implements CalcToolService {
	
	@Autowired
	private CalcWaybillRepository clacRepository;

	@Autowired
	private CalcIntervalRepository culcIntervalRepository;
	
	@Autowired
	private CalcCulateMileageService culateMileageService;
    
	@Autowired
	JdbcTemplate jdbcTemplate;


	Logger logger = LoggerFactory.getLogger(this.getClass());
    
	
	@Override
    public List<CalcWaybill> findAll(String rq, String lineCode, List<ScheduleRealInfo> list, List<Map<String, Object>> xlList, List<Interval> djgList) {
    	List<CalcWaybill> listCal=new ArrayList<CalcWaybill>();
		
        List<Map<String, String>> listMap = new ArrayList<Map<String, String>>();
		Set<String> tempSet = new HashSet<String>();
		for(ScheduleRealInfo s : list){
			String jGh = s.getjGh()!=null?s.getjGh().trim():"";
			String sGh = s.getsGh()!=null?s.getsGh().trim():"";
			String clZbh = s.getClZbh()!=null?s.getClZbh().trim():"";
			String xlBm = s.getXlBm()!=null?s.getXlBm().trim():"";
			String lpName = s.getLpName()!=null?s.getLpName().trim():"";
			String key = jGh + sGh + clZbh + xlBm + lpName;
			if(tempSet.add(key)){
				Map<String, String> m = new HashMap<String, String>();
				m.put("jGh", jGh);
				m.put("sGh", sGh);
				m.put("clZbh", clZbh);
				m.put("xlBm", xlBm);
				m.put("lpName", lpName);
				listMap.add(m);
			}
		}
		
        for (int i = 0; i < listMap.size(); i++) {
			Map<String, String> m=listMap.get(i);
			String jGh=m.get("jGh");
			String sGh=m.get("sGh");
			String clZbh=m.get("clZbh");
			String xlBm=m.get("xlBm");
			String lpName=m.get("lpName");
			//所有数据排班数据
			List<ScheduleRealInfo> list_=new ArrayList<ScheduleRealInfo>();
			//执行了的班次的排班数据
			List<ScheduleRealInfo> lists_=new ArrayList<ScheduleRealInfo>();
			for (int j = 0; j < list.size(); j++) {
				ScheduleRealInfo s=list.get(j);
				s.setCreateDate(new Date());
				if(jGh.equals(s.getjGh())
						&& sGh.equals(s.getsGh()==null?"":s.getsGh())
						&& clZbh.equals(s.getClZbh())
						&& xlBm.equals(s.getXlBm())
						&& lpName.equals(s.getLpName())){
					
					list_.add(s);
					
					Set<ChildTaskPlan> cts = s.getcTasks();
		            if(cts != null && cts.size() > 0){
		            	lists_.add(s);
		            }else{
			            if(s.getZdsjActual()!=null 
			            		&& s.getFcsjActual()!=null){
			            	lists_.add(s);
			            }
		            }
				}
			}
			
			//计算方法
			if(list_.size()>0){
				listCal.addAll(this.cul(list_, lists_, xlList, djgList));
			}
		}

		//按线路计算大间隔
        if(list.size() > 0){
			ScheduleRealInfo s = list.get(0);
			Map<String, Object> culateDjg = culateMileageService.culateDjg(list, lineCode, xlList, djgList);
			CalcInterval interval = new CalcInterval();
			interval.setGsdm(s.getGsBm());
			interval.setGsname(s.getGsName());
			interval.setFgsdm(s.getFgsBm());
			interval.setFgsname(s.getFgsName());
			interval.setRq(s.getScheduleDate());
			interval.setRqStr(s.getScheduleDateStr());
			interval.setXl(s.getXlBm());
			interval.setXlName(s.getXlName());
			if(culateDjg.keySet().size() > 0){
				if(culateDjg.containsKey("djgcsq")){
					interval.setDjg(Integer.valueOf(culateDjg.get("djgcsq").toString()));
				} else {
					interval.setDjg(0);
				}
				if(culateDjg.containsKey("djgcsz")){
					interval.setDjgM(Integer.valueOf(culateDjg.get("djgcsz").toString()));
				} else {
					interval.setDjgM(0);
				}
				if(culateDjg.containsKey("djgcsw")){
					interval.setDjgA(Integer.valueOf(culateDjg.get("djgcsw").toString()));
				} else {
					interval.setDjgA(0);
				}
				if(culateDjg.containsKey("djgsj")){
					interval.setDjgTime(Integer.valueOf(culateDjg.get("djgsj").toString()));
				} else {
					interval.setDjgTime(0);
				}
			} else {
				interval.setDjg(0);
				interval.setDjgM(0);
				interval.setDjgA(0);
				interval.setDjgTime(0);
			}
			culcIntervalRepository.save(interval);
        }
		
        while(true){
			int i = 0, l = 0;
			boolean flag = false;
			for(; i < listCal.size(); i++){
				CalcWaybill c1 = listCal.get(i);
				String key = c1.getRqStr() +"-"+ c1.getXl() +"-"+ c1.getCl() +"-"+ c1.getjGh() +"-"+ c1.getsGh() +"-"+ c1.getLp();
				for(l = 0; l < listCal.size(); l++){
					if(l != i){
						CalcWaybill c2 = listCal.get(l);
						String key_ = c2.getRqStr() +"-"+ c2.getXl() +"-"+ c2.getCl() +"-"+ c2.getjGh() +"-"+ c2.getsGh() +"-"+ c2.getLp();
						if(key_.equals(key)){
							flag = true;
							break;
						}
					}
				}
				if(flag){
					break;
				}
			}
			if(i != l){
				CalcWaybill c1 = listCal.get(i);
				CalcWaybill c2 = listCal.get(l);
//
//	    		c1.setSjyylc(c1.getSjyylc() + c2.getSjyylc());
//	    		c1.setSjfyylc(c1.getSjfyylc() + c2.getSjfyylc());
//	    		c1.setLjyylc(c1.getLjyylc() + c2.getLjyylc());
//	    		c1.setLjfyylc(c1.getLjfyylc() + c2.getLjfyylc());
//	    		
	    		listCal.remove(l);
			} else {
				break;
			}
		}
        return listCal;
    }
	
    //list_ 全部班次  lists_执行了的班次(实到实发不为空)
    private List<CalcWaybill> cul(List<ScheduleRealInfo> list_,List<ScheduleRealInfo> list_s, List<Map<String, Object>> xlList, List<Interval> djgList){
    	List<CalcWaybill> list=new ArrayList<CalcWaybill>();
    	List<CalcWaybill> list2=new ArrayList<CalcWaybill>();
    	if(list_.size()>0){
    		Date date=list_.get(0).getScheduleDate();
    		String dateStr=list_.get(0).getScheduleDateStr();
    		String gsdm=list_.get(0).getGsBm();
    		String gsname=list_.get(0).getGsName();
    		String fgsdm=list_.get(0).getFgsBm();
    		String fgsname=list_.get(0).getFgsName();
    		String xlbm=list_.get(0).getXlBm();
    		String xlname=list_.get(0).getXlName();
    		String lp=list_.get(0).getLpName();
    		String jgh=list_.get(0).getjGh();
    		String jname=list_.get(0).getjName();
    		String sgh=list_.get(0).getsGh();
    		String sname=list_.get(0).getsName();
    		String clzbh=list_.get(0).getClZbh();
    		int jhyybc=culateMileageService.culateJhyybc(list_, "");
    		int jhyybcZgf=culateMileageService.culateJhyybc(list_, "zgf");
    		int jhyybcWgf=culateMileageService.culateJhyybc(list_, "wgf");
    		double jhyylc=culateMileageService.culateJhyylc(list_);
    		double jhfyylc=culateMileageService.culateJhfyylc(list_);
    		int sjyybc=culateMileageService.culateSjyybc(list_s, "");
    		int sjyybcZgf=culateMileageService.culateSjyybc(list_s, "zgf");
    		int sjyybcWgf=culateMileageService.culateSjyybc(list_s, "wgf");
    		double sjyylc=culateMileageService.culateSjyylc(list_s);//驾售共有
    		double sjyylc_j=culateMileageService.culateSjyylc_j(list_s);//无售
    		double sjfyylc=culateMileageService.culateSjfyylc(list_s);//驾售
    		double sjfyylc_j=culateMileageService.culateSjfyylc_j(list_s);//无售
    		int ljbc=culateMileageService.culateLjbc(list_s, "");
    		int ljbcZgf=culateMileageService.culateLjbc(list_s, "zgf");
    		int ljbcWgf=culateMileageService.culateLjbc(list_s, "wgf");
    		double ljyylc=culateMileageService.culateLjyylc(list_s);//驾售
    		double ljyylc_j=culateMileageService.culateLjyylc_j(list_s);//无售
    		double ljfyylc=culateMileageService.culateLjfyylc(list_s);//驾售
    		double ljfyylc_j=culateMileageService.culateLjfyylc_j(list_s);//无售
    		int lbbc=culateMileageService.culateLbbc(list_);
    		double lblc=culateMileageService.culateLbgl(list_);
    		double ssgl_lz= culateMileageService.culateCJLC(list_, "路阻");
            double ssgl_dm= culateMileageService.culateCJLC(list_, "吊慢");
            double ssgl_gz= culateMileageService.culateCJLC(list_, "故障");
            double ssgl_jf= culateMileageService.culateCJLC(list_, "纠纷");
            double ssgl_zs= culateMileageService.culateCJLC(list_, "肇事");
            double ssgl_qr= culateMileageService.culateCJLC(list_, "缺人");
            double ssgl_qc= culateMileageService.culateCJLC(list_, "缺车");
            double ssgl_kx= culateMileageService.culateCJLC(list_, "客稀");
            double ssgl_qh= culateMileageService.culateCJLC(list_, "气候");
            double ssgl_yw= culateMileageService.culateCJLC(list_, "援外");
            
            double ssgl_pc=culateMileageService.culateCJLC(list_, "配车");
            double ssgl_by=culateMileageService.culateCJLC(list_, "保养");
            double ssgl_cj=culateMileageService.culateCJLC(list_, "抽减");
            double ssgl_qt=culateMileageService.culateCJLC(list_, "其他");
    		double ssgl_qtz=Arith.add(Arith.add(ssgl_pc, ssgl_by), Arith.add(ssgl_cj,ssgl_qt));
    		int fzbc=culateMileageService.culateDtfzbc(list_s, "major", "");//放站
    		int fzbcZgf=culateMileageService.culateDtfzbc(list_s, "major", "zgf");
    		int fzbcWgf=culateMileageService.culateDtfzbc(list_s, "major", "wgf");
    		int zfbc=culateMileageService.culateDtfzbc(list_s, "venting", "");//直放
    		int zfbcZgf=culateMileageService.culateDtfzbc(list_s, "venting", "zgf");
    		int zfbcWgf=culateMileageService.culateDtfzbc(list_s, "venting", "wgf");
	    	CalcWaybill t=new CalcWaybill();
	    	t.setRq(date);
	    	t.setRqStr(dateStr);
	    	t.setGsdm(gsdm);
	    	t.setGsname(gsname);
	    	t.setFgsdm(fgsdm);
	    	t.setFgsname(fgsname);
	    	t.setXl(xlbm);
	    	t.setXlName(xlname);
	    	t.setLp(lp);
	    	t.setCl(clzbh);
	    	t.setjGh(jgh);
	    	t.setjName(jname);
	    	t.setsGh(sgh);
	    	t.setsName(sname);
	    	t.setJhyybc(jhyybc);
	    	t.setJhyybczgf(jhyybcZgf);
	    	t.setJhyybcwgf(jhyybcWgf);
	    	t.setJhyylc(jhyylc);
	    	t.setJhfyylc(jhfyylc);
	    	t.setSjyybc(sjyybc);
	    	t.setSjyybczgf(sjyybcZgf);
	    	t.setSjyybcwgf(sjyybcWgf);
	    	t.setSjyylc(sjyylc);
	    	t.setSjfyylc(sjfyylc);
	    	t.setLjbc(ljbc);
	    	t.setLjbczgf(ljbcZgf);
	    	t.setLjbcwgf(ljbcWgf);
	    	t.setLjyylc(ljyylc);
	    	t.setLjfyylc(ljfyylc);
	    	t.setLbbc(lbbc);
	    	t.setLblc(lblc);
	    	t.setLblcLz(ssgl_lz);
	    	t.setLblcDm(ssgl_dm);
	    	t.setLblcGz(ssgl_gz);
	    	t.setLblcJf(ssgl_jf);
	    	t.setLblcZs(ssgl_zs);
	    	t.setLblcQr(ssgl_qr);
	    	t.setLblcQc(ssgl_qc);
	    	t.setLblcKx(ssgl_kx);
	    	t.setLblcQh(ssgl_qh);
	    	t.setLblcYw(ssgl_yw);
	    	t.setLblcQt(ssgl_qtz);
	    	t.setFzbc(fzbc+zfbc);
	    	t.setFzbczgf(fzbcZgf+zfbcZgf);
	    	t.setFzbcwgf(fzbcWgf+zfbcWgf);
	    	t.setDtbc(0);
	    	t.setDtbczgf(0);
	    	t.setDtbcwgf(0);
	    	list.add(t);
	    	//驾驶员单独行驶里程
	    	if(sjyylc_j>0||sjfyylc_j>0||ljyylc_j>0||ljfyylc_j>0){
	    		CalcWaybill t_=new CalcWaybill();
	    		t_.setRq(date);
		    	t_.setRqStr(dateStr);
	    		t_.setGsdm(gsdm);
	    		t_.setGsname(gsname);
	    		t_.setFgsdm(fgsdm);
	    		t_.setFgsname(fgsname);
	    		t_.setXl(xlbm);
	    		t_.setXlName(xlname);
	    		t_.setLp(lp);
	    		t_.setCl(clzbh);
	    		t_.setjGh(jgh);
	    		t_.setjName(jname);
	    		t_.setSjyylc(sjyylc_j);
	    		t_.setSjfyylc(sjfyylc_j);
	    		t_.setLjyylc(ljyylc_j);
	    		t_.setLjfyylc(ljfyylc_j);
	    		t_.setsGh("");
		    	t_.setsName("");
		    	t_.setJhyybc(0);
		    	t_.setJhyybczgf(0);
		    	t_.setJhyybcwgf(0);
		    	t_.setJhyylc(0.0);
		    	t_.setJhfyylc(0.0);
		    	t_.setSjyybc(0);
		    	t_.setSjyybczgf(0);
		    	t_.setSjyybcwgf(0);
		    	t_.setLjbc(0);
		    	t_.setLjbczgf(0);
		    	t_.setLjbcwgf(0);
		    	t_.setLbbc(0);
		    	t_.setLblc(0.0);
		    	t_.setLblcLz(0.0);
		    	t_.setLblcDm(0.0);
		    	t_.setLblcGz(0.0);
		    	t_.setLblcJf(0.0);
		    	t_.setLblcZs(0.0);
		    	t_.setLblcQr(0.0);
		    	t_.setLblcQc(0.0);
		    	t_.setLblcKx(0.0);
		    	t_.setLblcQh(0.0);
		    	t_.setLblcYw(0.0);
		    	t_.setLblcQt(0.0);
		    	t_.setFzbc(0);
		    	t_.setFzbczgf(0);
		    	t_.setFzbcwgf(0);
		    	t_.setDtbc(0);
		    	t_.setDtbczgf(0);
		    	t_.setDtbcwgf(0);
		    	list.add(t_);
		    	list2.add(t_);
	    	}
    	}
    	for(CalcWaybill c2 : list2){
    		boolean flag = true;
			String key2 = c2.getjGh() + c2.getsGh()==null?"":c2.getsGh().trim()
					+ c2.getCl() + c2.getXl() + c2.getLp();
    		for(CalcWaybill c : list){
    			String key = c.getjGh() + c.getsGh()==null?"":c.getsGh().trim()
    					+ c.getCl() + c.getXl() + c.getLp();
    			if(key2.equals(key)){
    				c.setSjyylc(Arith.add(c.getSjyylc(), c2.getSjyylc()));
    				c.setSjfyylc(Arith.add(c.getSjfyylc(), c2.getSjfyylc()));
    				c.setLjyylc(Arith.add(c.getLjyylc(), c2.getLjyylc()));
    				c.setLjfyylc(Arith.add(c.getLjfyylc(), c2.getLjfyylc()));
    				flag = false;
    				break;
    			}
    		}
    		if(flag)
    			list.add(c2);
    	}
    	return list;
    }
    
}