YlbServiceImpl.java 12.1 KB
package com.bsth.service.oil.impl;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.transaction.Transactional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;

import com.bsth.common.ResponseCode;
import com.bsth.entity.oil.Cyl;
import com.bsth.entity.oil.Ylb;
import com.bsth.entity.oil.Ylxxb;
import com.bsth.entity.search.CustomerSpecs;
import com.bsth.repository.oil.CylRepository;
import com.bsth.repository.oil.YlbRepository;
import com.bsth.repository.oil.YlxxbRepository;
import com.bsth.service.impl.BaseServiceImpl;
import com.bsth.service.oil.YlbService;
import com.bsth.service.realcontrol.ScheduleRealInfoService;
import com.github.abel533.echarts.code.Y;

@Service
public class YlbServiceImpl extends BaseServiceImpl<Ylb,Integer> implements YlbService{
	@Autowired
	YlbRepository repository;
	
	@Autowired
	YlxxbRepository ylxxbRepository;
	
	@Autowired
	CylRepository cylRepository;
	
	@Autowired
	ScheduleRealInfoService scheduleRealInfoService;
	
	Logger logger = LoggerFactory.getLogger(this.getClass());
	
	
	/**
	 * 获取进存油信息
	 * @Transactional 回滚事物
	 */
	@Transactional  
	@Override
	public Map<String, Object> obtain(String rq) {
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		//保留两位小数
		DecimalFormat df = new DecimalFormat("#.00");
		// TODO Auto-generated method stub
		Map<String, Object> newMap=new HashMap<String,Object>();
		//当天YLB信息
		List<Ylb> ylList=repository.obtainYl(rq);
		//当天YLXXB信息
		List<Ylxxb> ylxxList=ylxxbRepository.obtainYlxx(rq);
		//前一天所有车辆最后进场班次信息
		List<Ylb> ylListBe=repository.obtainYlbefore(rq);
		//从排班表中计算出行驶的总里程
		List<Map<String,Object>> listpb=scheduleRealInfoService.yesterdayDataList("1024",rq);
		
		for(int x=0;x<listpb.size();x++){
			
			Map<String, Object> map=listpb.get(x);
		
			//判断驾驶员驾驶的该车辆是否已经存入了(查出的结果集中日期是相同的,根据驾驶员、内部编号、线路编码判断)
			Ylb t=new Ylb();
			for(int k=0;k<ylList.size();k++){
				Ylb t1=ylList.get(k);
				if(t1.getNbbm().equals(map.get("clZbh").toString())
						&&t1.getJsy().equals(map.get("jGh").toString())
						&&t1.getXlbm().equals(map.get("xlBm").toString()))
				{
					t=t1;
				}
			}
			try {
				//当日的第一个班次,出场油量等于前一天的最后一个班次的进场油量
				if(map.get("seqNumber").toString().equals("1")){
					for (int y = 0; y < ylListBe.size(); y++) {
						Ylb ylb=ylListBe.get(y);
						if(map.get("clZbh").toString().equals(ylb.getNbbm())){
							t.setCzyl(ylb.getJzyl());
							break;
						}else{
							t.setCzyl(0.0);
						}
					}
				}
				
				Double jzl=0.0;
				//把当天的YLXXB的加注量设置为当天YLB的加注量(根据车号,驾驶员判断)
				for(int j=0;j<ylxxList.size();j++){
					Ylxxb ylxxb= ylxxList.get(j);
					if(map.get("clZbh").toString().equals(ylxxb.getNbbm()) &&map.get("jGh").toString().equals(ylxxb.getJsy())){
						jzl+=ylxxb.getJzl();
					}
				}
				t.setJzl(jzl);
				t.setNbbm(map.get("clZbh").toString());
				t.setJsy(map.get("jGh")==null?"":map.get("jGh").toString());
				t.setZlc(map.get("totalKilometers")==null?0.0:Double.parseDouble(df.format(Double.parseDouble(map.get("totalKilometers").toString()))));
				t.setXlbm(map.get("xlBm")==null?"":map.get("xlBm").toString());
				t.setJcsx(Integer.parseInt(map.get("seqNumber").toString()));
				t.setSsgsdm(map.get("company").toString());
				t.setRq(sdf.parse(rq));
				repository.save(t);
				newMap.put("status", ResponseCode.SUCCESS);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				newMap.put("status", ResponseCode.ERROR);
				e.printStackTrace();
			}
		}
		
		return newMap;
	}
	
	
	/**
	 * 进场等于出场
	 */
	@Transactional  
	@Override
	public Map<String, Object> outAndIn(Map<String, Object> map){
		// TODO Auto-generated method stub
		Map<String, Object> newMap=new HashMap<String,Object>();
		Map<String, Object> map2=new HashMap<String,Object>();
		String rq=map.get("rq").toString();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		
		try {
			map.put("rq_eq", sdf.parse(rq));
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		//获取车辆存油信息
		List<Cyl> cylList=cylRepository.findAll(new CustomerSpecs<Cyl>(newMap));
		//指定日期YLB信息
		Iterator<Ylb> iterator= repository.findAll(new CustomerSpecs<Ylb>(map)).iterator();
		while(iterator.hasNext()){
			Ylb ylb=iterator.next();
				//判断是否已经计算过
				if(newMap.get("nbbm"+ylb.getNbbm())==null){
					String nbbm_eq=ylb.getNbbm();
					Date rq_eq=ylb.getRq();
					//得到一天总的加油和里程(根据车,时间)
					List<Object[]> sumList=repository.sumLcYl(nbbm_eq, rq_eq);
					//保存总的加油量
					Double jzl=Double.valueOf(sumList.get(0)[0].toString());
					//保存总的里程
					Double zlc=Double.valueOf(sumList.get(0)[1].toString());
					//保留两位小数
					DecimalFormat df = new DecimalFormat("#.00");
					Double zyl=0.0;
					Double nextJzyl=0.0;
					
					//保存已经计算过的车辆,相同车辆编号的车不在计算
					newMap.put("nbbm"+ylb.getNbbm(), ylb.getNbbm());
					
					//查询指定车辆,设置进、存、耗油量
					map.remove("nbbm_eq");
					map.put("nbbm_eq", ylb.getNbbm());
					Iterator<Ylb> iterator2= repository.findAll(new CustomerSpecs<Ylb>(map),new Sort(Direction.ASC, "jcsx")).iterator();
					while(iterator2.hasNext()){
						try{
							Ylb t = iterator2.next();
							if(t.getJcsx()==1){
								//进场等于出场的操作 既 最后进场存油量等于第一次的出场存油量
								Double yl=t.getCzyl();
								Double jcyl=t.getCzyl();
								zyl=jcyl+jzl-yl;
								Double yh=Double.parseDouble(df.format(zyl*(t.getZlc()/zlc)));
								t.setYh(yh);
								nextJzyl=t.getJzl()+t.getCzyl()-yh;
								t.setJzyl(Double.parseDouble(df.format(nextJzyl)));
							}else{
								t.setCzyl(Double.parseDouble(df.format(nextJzyl)));
								Double yh=Double.parseDouble(df.format(zyl*(t.getZlc()/zlc)));
								t.setYh(yh);
								nextJzyl=t.getJzl()+nextJzyl-yh;
								t.setJzyl(Double.parseDouble(df.format(nextJzyl)));
							}
							
							repository.save(t);
							//设置存油量
							Cyl cyl=null;
							boolean fage=false;
							for(int z=0;z<cylList.size();z++){
								cyl=cylList.get(z);
								if(t.getNbbm().equals(cyl.getNbbm())){
									cyl.setCyl(t.getJzyl());
									cyl.setUpdatetime(t.getRq());
									fage=true;
									break;
								}
							}
							
							if(fage){
								cylRepository.save(cyl);
							}else{
								cyl=new Cyl();
								cyl.setNbbm(t.getNbbm());
								cyl.setCyl(t.getJzyl());
								cyl.setGsdm(t.getSsgsdm());
								cyl.setUpdatetime(t.getRq());
								cylRepository.save(cyl);
							}
							
							
							map2.put("status", ResponseCode.SUCCESS);
						}catch(Exception e){
							map2.put("status", ResponseCode.ERROR);
							logger.error("save erro.", e);
						}
					}
					
					
				}
		}
		
		return map2;
	}
	
	/**
	 * 拆分
	 */
	@Transactional  
	@Override
	public Map<String, Object> sort(Map<String, Object> map) {
		// TODO Auto-generated method stub
		Map<String, Object> newMap = new HashMap<String, Object>();
		//获取车辆存油信息
		List<Cyl> cylList=cylRepository.findAll(new CustomerSpecs<Cyl>(newMap));
		int id=Integer.parseInt(map.get("id").toString());
		//最后存油量
		Double yl=Double.parseDouble(map.get("jzyl").toString());
		Ylb ylb=repository.findOne(id);
		String nbbm_eq=ylb.getNbbm();
		Date rq_eq=ylb.getRq();
		//得到一天总的加油和里程(根据车,时间)
		List<Object[]> sumList=repository.sumLcYl(nbbm_eq, rq_eq);
		//保存总的加油量
		Double jzl=Double.valueOf(sumList.get(0)[0].toString());
		//保存总的里程
		Double zlc=Double.valueOf(sumList.get(0)[1].toString());
		map.put("nbbm_eq", nbbm_eq);
		map.put("rq_eq",rq_eq);
		Iterator<Ylb> iterator= repository.findAll(new CustomerSpecs<Ylb>(map),new Sort(Direction.ASC, "jcsx")).iterator();
		//根据jcyl排序1为该车当日第一个出场,出场油量为前一天的存油
		//保留两位小数
		DecimalFormat df = new DecimalFormat("#.00");
		Double zyl=0.0;
		Double nextJzyl=0.0;
		//车的,进,出油量及耗油
		while(iterator.hasNext()){
			try{
				Ylb t = iterator.next();
				if(t.getJcsx()==1){
					Double jcyl=t.getCzyl();
					zyl=jcyl+jzl-yl;
					Double yh=Double.parseDouble(df.format(zyl*(t.getZlc()/zlc)));
					t.setYh(yh);
					nextJzyl=t.getJzl()+t.getCzyl()-yh;
					t.setJzyl(Double.parseDouble(df.format(nextJzyl)));
				}else{
					if(t.getZlc()!=0){
						t.setCzyl(Double.parseDouble(df.format(nextJzyl)));
						Double yh=Double.parseDouble(df.format(zyl*(t.getZlc()/zlc)));
						t.setYh(yh);
						nextJzyl=t.getJzl()+nextJzyl-yh;
						t.setJzyl(Double.parseDouble(df.format(nextJzyl)));
					}
					
				}
				repository.save(t);
				
				//设置存油量
				Cyl cyl=null;
				boolean fage=false;
				for(int z=0;z<cylList.size();z++){
					cyl=cylList.get(z);
					if(t.getNbbm().equals(cyl.getNbbm())){
						cyl.setCyl(t.getJzyl());
						cyl.setUpdatetime(t.getRq());
						fage=true;
						break;
					}
				}
				if(fage){
					cylRepository.save(cyl);
				}else{
					cyl=new Cyl();
					cyl.setNbbm(t.getNbbm());
					cyl.setCyl(t.getJzyl());
					cyl.setGsdm(t.getSsgsdm());
					cyl.setUpdatetime(t.getRq());
					cylRepository.save(cyl);
				}
				
				newMap.put("status", ResponseCode.SUCCESS);
			}catch(Exception e){
				newMap.put("status", ResponseCode.ERROR);
				logger.error("save erro.", e);
			}
		}
		return newMap;
	}

	/**
	 * 核对,有加注没里程
	 * @param map
	 * @return
	 */
	@Transactional
	@Override
	public Map<String, Object> checkYl(Map<String, Object> map) {
		Map<String, Object> newMap=new HashMap<String,Object>();
		// TODO Auto-generated method stub
		try{
			//获取车辆存油信息
			List<Cyl> cylList=cylRepository.findAll(new CustomerSpecs<Cyl>(newMap));
			String rq=map.get("rq").toString();
			List<Ylb> ylbList=repository.obtainYl(rq);
			List<Ylxxb> ylxxbList=ylxxbRepository.obtainYlxx(rq);
			for (int i = 0; i < ylxxbList.size(); i++) {
				Boolean fage=true;
				Ylxxb y1=ylxxbList.get(i);
				for(int y=0;y<ylbList.size();y++){
					Ylb y2=ylbList.get(y);
					if(y1.getNbbm().equals(y2.getNbbm())){
						fage=false;
						break;
					}
				}
				
				if(fage){
					Ylb t=new Ylb();
					t.setNbbm(y1.getNbbm());
					t.setRq(y1.getYyrq());
					t.setJsy(y1.getJsy());
					t.setJzl(y1.getJzl());
					t.setSsgsdm(y1.getGsdm());
					t.setXlbm("1024");
					repository.save(t);
					
					//设置存油量
					Cyl cyl=null;
					boolean status=false;
					for(int z=0;z<cylList.size();z++){
						cyl=cylList.get(z);
						if(t.getNbbm().equals(cyl.getNbbm())){
							cyl.setCyl(cyl.getCyl()+t.getJzl());
							cyl.setUpdatetime(t.getRq());
							status=true;
							break;
						}
					}
					if(status){
						cylRepository.save(cyl);
					}else{
						cyl=new Cyl();
						cyl.setNbbm(t.getNbbm());
						cyl.setCyl(t.getJzl());
						cyl.setGsdm(t.getSsgsdm());
						cyl.setUpdatetime(t.getRq());
						cylRepository.save(cyl);
					}
					
					
				}
			}
			newMap.put("status", ResponseCode.SUCCESS);
		}catch(Exception e){
			newMap.put("status", ResponseCode.ERROR);
			logger.error("save erro.", e);
		}
		
		return newMap;
	}
	

}