ScheduleRealInfoServiceImpl.java 10.9 KB
package com.bsth.service.realcontrol.impl;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.transaction.Transactional;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bsth.common.ResponseCode;
import com.bsth.entity.Cars;
import com.bsth.entity.Line;
import com.bsth.entity.Personnel;
import com.bsth.entity.realcontrol.ScheduleRealInfo;
import com.bsth.entity.schedule.CarConfigInfo;
import com.bsth.entity.schedule.EmployeeConfigInfo;
import com.bsth.entity.sys.SysUser;
import com.bsth.repository.realcontrol.ScheduleRealInfoRepository;
import com.bsth.repository.schedule.CarConfigInfoRepository;
import com.bsth.repository.schedule.EmployeeConfigInfoRepository;
import com.bsth.security.util.SecurityUtils;
import com.bsth.service.impl.BaseServiceImpl;
import com.bsth.service.realcontrol.ScheduleRealInfoService;
import com.bsth.service.realcontrol.buffer.ScheduleBuffer;
import com.bsth.util.ReportUtils;
import com.bsth.vehicle.common.CommonMapped;
import com.google.common.base.Splitter;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;

@Service
public class ScheduleRealInfoServiceImpl extends BaseServiceImpl<ScheduleRealInfo, Long> implements ScheduleRealInfoService{

	@Autowired
	ScheduleRealInfoRepository scheduleRealInfoRepository;
	
	@Autowired
	EmployeeConfigInfoRepository employeeConfigInfoRepository;
	
	@Autowired
	CarConfigInfoRepository carConfigInfoRepository;
	
	Logger logger = LoggerFactory.getLogger(this.getClass());
	
	SimpleDateFormat sdfMonth = new SimpleDateFormat("yyyy-MM-dd")
			, sdfMinute = new SimpleDateFormat("yyyy-MM-dd HH:mm")
			, sdfShort = new SimpleDateFormat("HH:mm");
	
	@Override
	public Map<String, Collection<ScheduleRealInfo>> findByLines(String lines) {
		List<String> lineList = Lists.newArrayList(Splitter.on(',').trimResults().omitEmptyStrings().split(lines));
		
		Multimap<String, ScheduleRealInfo> mMap = ArrayListMultimap.create();
		
		for(String lineCode : lineList){
			mMap.putAll(lineCode, ScheduleBuffer.schedulListMap.get(lineCode));
		}
		return mMap.asMap();
	}

	@Override
	public Map<String, Object> outgoAdjust(Long id, String remarks, String dfsj) {
		Map<String, Object> map = new HashMap<>();
		try{
			
			ScheduleRealInfo schedule = ScheduleBuffer.pkSchedulMap.get(id);
			schedule.setDfsjT(sdfMinute.parse(sdfMonth.format(new Date()) + " " + dfsj).getTime());
			schedule.setDfsj(dfsj);
			schedule.addRemarks("[待发调整] " + remarks +";");
			//持久化到数据库
			ScheduleBuffer.persistentList.add(schedule);
			
			map.put("status", ResponseCode.SUCCESS);
			map.put("dfsj", dfsj);
			map.put("remarks", schedule.getRemarks());
		}catch(Exception e){
			logger.error("", e);
			map.put("status", ResponseCode.ERROR);
		}
		return map;
	}

	@Override
	public Map<String, Object> destroy(String idsStr, int spaceAdjust, String  remarks, String reason, int spaceNum) {
		
		Map<String, Object> map = new HashMap<>();
		List<ScheduleRealInfo> rsList = new ArrayList<>();
		map.put("list", rsList);
		try{
			List<String> idList = Lists.newArrayList(Splitter.on(',').trimResults().omitEmptyStrings().split(idsStr));
			
			ScheduleRealInfo schedule = null;
			for(String id : idList){
				schedule = ScheduleBuffer.pkSchedulMap.get(Long.parseLong(id));
				if(null != schedule){
					schedule.setStatus(-1);
					schedule.setRemarks("计划烂班["+reason+"] " + remarks);
					
					rsList.add(schedule);
				}
			}
			
			//调整间隔
			if(spaceAdjust == 1){
				
				ScheduleRealInfo first = ScheduleBuffer.pkSchedulMap.get(Long.parseLong(idList.get(0)));
				String lineCode = first.getXlBm();
				String upDown = first.getXlDir();
				
				List<ScheduleRealInfo> schList = ScheduleBuffer.schedulListMap.get(lineCode)
							,dirList = new ArrayList<>();
				//筛选走向
				for(ScheduleRealInfo s : schList){
					if(s.getXlDir().equals(upDown)){
						dirList.add(s);
					}
				}
				
				int size = dirList.size();
				Long st = null;
				int diff = spaceNum * 60 * 1000;
				for(int i = 0; i < size; i ++){
					schedule = dirList.get(i);
					
					if(schedule.getId() == first.getId()){
						if(i == 0)
							st = schedule.getDfsjT() - diff;
						else
							st = dirList.get(i - 1).getDfsjT();
						continue;
					}
					if(null == st || schedule.getStatus() == -1)
						continue;
					
					st = st + diff;
					schedule.setDfsjT(st);
					schedule.setDfsj(sdfShort.format(new Date(st)));
					
					ScheduleBuffer.persistentList.add(schedule);
					//将调整的班次返回给页面
					rsList.add(schedule);
				}
			}
			
			map.put("status", ResponseCode.SUCCESS);
		}catch(Exception e){
			logger.error("", e);
			map.put("status", ResponseCode.ERROR);
		}
		return map;
	}

	//线路id获取驾驶员
	@Override
	public List<Map<String, String>> findDriverByLine(String lineCode) {
		List<EmployeeConfigInfo> list = employeeConfigInfoRepository.findBylineCode(lineCode);
		
		List<Map<String, String>> rsList = new ArrayList<>();
		Map<String, String> map = null;
		Personnel driver = null;
		String code = null;
		
		for(EmployeeConfigInfo employee : list){
			driver = employee.getJsy();
			if(driver != null){
				map = new HashMap<>();
				code = driver.getJobCode();
				map.put("id", code + "/" + driver.getPersonnelName());
				map.put("text", code + "/" + driver.getPersonnelName());
				rsList.add(map);
			}
		}
		return rsList;
	}

	//线路id获取售票员
	@Override
	public List<Map<String, String>> findConductorByLine(String lineCode) {
		List<EmployeeConfigInfo> list = employeeConfigInfoRepository.findBylineCode(lineCode);
		
		List<Map<String, String>> rsList = new ArrayList<>();
		Map<String, String> map = null;
		Personnel conductor = null;
		String code = null;
		
		for(EmployeeConfigInfo employee : list){
			conductor = employee.getSpy();
			if(conductor != null){
				code = conductor.getJobCode();
				map = new HashMap<>();
				map.put("id", code + "/" + conductor.getPersonnelName());
				map.put("text", code + "/" + conductor.getPersonnelName());
				rsList.add(map);
			}
		}
		return rsList;
	}

	@Override
	public List<Map<String, String>> findCarByLine(String lineCode) {
		
		List<CarConfigInfo> list = carConfigInfoRepository.findBylineCode(lineCode);
		
		List<Map<String, String>> rsList = new ArrayList<>();
		Map<String, String> map = null;
		Cars car = null;
		String code = null;
		
		for(CarConfigInfo cci : list){
			car = cci.getCl();
			if(car != null){
				code = car.getInsideCode();
				map = new HashMap<>();
				map.put("id", code);
				map.put("text", code);
				rsList.add(map);
			}
		}
		return rsList;
	}

	/**
	 * 临加班次
	 */
	@Override
	public Map<String, Object> save(ScheduleRealInfo t) {
		SysUser user = SecurityUtils.getCurrentUser();
		
		t.setScheduleDate(new Date());
		t.setCreateBy(user);
		t.syncTime();
		
		Map<String, Object> map = super.save(t);
		
		//加入缓存
		ScheduleBuffer.put(t);
		return map;
	}

	@Override
	public List<Map<String, String>> sreachVehic(String nbbm) {
		//转大写
		nbbm = nbbm.toUpperCase();
		
		List<Map<String, String>> list = new ArrayList<>();
		Map<String, String> map;
		Set<String> allSet = CommonMapped.vehicCompanyMap.keySet();
		
		Line line;
		for(String k : allSet){
			if(k.indexOf(nbbm) != -1){
				//所属线路
				map = new HashMap<>();
				line = CommonMapped.vehicLineMap.get(k);
				map.put("id", k);
				map.put("text", k);
				if(null != line){
					map.put("lineName", line.getName());
					map.put("lineCode", line.getLineCode());
				}
				
				list.add(map);
			}
			
			if(list.size() > 20)
				break;
		}
		return list;
	}

	@Override
	public Map<String, Object> adjust(Long id, String nbbm, String jsy, String spy) {
		//班次
		ScheduleRealInfo schedule = ScheduleBuffer.pkSchedulMap.get(id);
		
		//换车
		if(!StringUtils.isBlank(nbbm)){
			adjustCar(schedule, nbbm);
		}
		
		List<String> tempArray;
		//换驾驶员
		if(!StringUtils.isBlank(jsy)){
			tempArray = Splitter.on("/").splitToList(jsy);
			adjustDriver(schedule, tempArray.get(0), tempArray.get(1));
		}
		
		//换售票员
		if(!StringUtils.isBlank(spy)){
			tempArray = Splitter.on("/").splitToList(spy);
			adjustConductor(schedule, tempArray.get(0), tempArray.get(1));
		}
		
		ScheduleBuffer.persistentList.add(schedule);
		
		Map<String, Object> map = new HashMap<>();
		map.put("status", 200);
		map.put("t", schedule);
		return map;
	}

	@Override
	public void adjustCar(ScheduleRealInfo schedule, String car) {
		schedule.setClZbh(car);
	}

	@Override
	public void adjustDriver(ScheduleRealInfo schedule, String driver, String driverName) {
		schedule.setjGh(driver);
		schedule.setjName(driverName);
	}

	@Override
	public void adjustConductor(ScheduleRealInfo schedule, String conductor, String conductorName) {
		schedule.setsGh(conductor);
		schedule.setsName(conductorName);
	}
	
	@Override
	public List<ScheduleRealInfo> queryUserInfo(String line, String date) {
		// TODO Auto-generated method stub
		return scheduleRealInfoRepository.queryUserInfo(line, date);
	}

	@Override
	public List<ScheduleRealInfo> exportWaybill(String jName, String clZbh, String lpName) {
		ReportUtils ee = new ReportUtils();
		List<Iterator<?>> list = new ArrayList<Iterator<?>>();
		List<ScheduleRealInfo> scheduleRealInfos = scheduleRealInfoRepository.exportWaybill(jName,clZbh,lpName);
		ScheduleRealInfo scheduleRealInfo = scheduleRealInfoRepository.findOne(scheduleRealInfos.get(0).getId());
		
		File source =new File("D:\\export\\source");
		File target =new File("D:\\export\\target");
		if  (!source .exists()  && !source .isDirectory()){
			source.mkdirs();
		}
		if  (!target .exists()  && !target .isDirectory()){
			target.mkdirs();
		}
		
		list.add(scheduleRealInfos.iterator());
		ee.excelReplace(list, new Object[]{scheduleRealInfo}, "D:\\export\\source\\waybill.xls",
				"D:\\export\\target\\"+jName+".xls");
		return scheduleRealInfos;
	}

	@Override
	public List<Map<String, Object>> dailyInfo(String line, String date) {
		// TODO Auto-generated method stub
		return scheduleRealInfoRepository.dailyInfo(line,date);
	}

	@Override
	public List<ScheduleRealInfo> historyMessage(String line, String date,
			String code) {
		// TODO Auto-generated method stub
		return scheduleRealInfoRepository.historyMessage(line, date,code+"%");
	}
}