YlbServiceImpl.java 17.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
package com.bsth.service.oil.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
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.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
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;
	
	@Autowired
	JdbcTemplate jdbcTemplate;
	
	Logger logger = LoggerFactory.getLogger(this.getClass());
	
	
	
	/**
	 * 获取进存油信息
	 * @Transactional 回滚事物
	 */
	@Transactional  
	@Override
	public String obtainDsq() {
		String result = "failure";
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		Date dNow = new Date();   //当前时间
		Date dBefore = new Date();
		Calendar calendar = Calendar.getInstance(); //得到日历
		calendar.setTime(dNow);//把当前时间赋给日历
		calendar.add(Calendar.DAY_OF_MONTH, -1);  //设置为前一天
		dBefore = calendar.getTime();   //得到前一天的时间
		String rq=sdf.format(dBefore);
		//保留两位小数
		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("",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")==null?"":map.get("company").toString());
				t.setFgsdm(map.get("bCompany")==null?"":map.get("bCompany").toString());
				t.setRq(sdf.parse(rq));
				repository.save(t);
				result = "success";
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally{
				logger.info("setDDRB:"+result);
			}
		}
		
		return result;
	}
	
	/**
	 * 获取进存油信息
	 * @Transactional 回滚事物
	 */
	@Transactional  
	@Override
	public Map<String, Object> obtain(Map<String, Object> map2) {
		String rq=map2.get("rq").toString();
		String line="";
		if(map2.get("xlbm_eq")!=null){
			line=map2.get("xlbm_eq").toString();
		}
		
		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(line,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")==null?"":map.get("company").toString());
				t.setFgsdm(map.get("bCompany")==null?"":map.get("bCompany").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=0.0;
					if(zlc>0&&t.getZlc()>0){
						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;
	}


	@Override
	public List<Ylb> oilListMonth(String line, String date) {
		String date2=date.substring(0, 8)+"01";
		String lineStr="";
		if(line !=null && !line.equals("")){
			lineStr =" and xlbm= '"+ line +"'";
		}
		String sql="select a.nbbm,a.rq,a.jzyl from (" +
				" select x.nbbm,max(x.rq) as rq,max(x.jcsx) as jcsx from "
				+ "(select nbbm,rq,max(jcsx)  as jcsx from bsth_c_ylb  where rq between  '"
				+ date2 +"'  and '" + date+"' "+lineStr
				+ "group by   nbbm,rq)  x group by x.nbbm ) b" 
				+ "	left join (select nbbm,rq,jzyl,jcsx  FROM   bsth_c_ylb  "
				+ "	where rq between  '"
				+ date2 +"'  and '" + date+"' "+lineStr
				+ ") a "
				+ "	on a.nbbm =b.nbbm and a.jcsx=b.jcsx and a.rq=b.rq "
				+ " where a.nbbm is not null order by a.nbbm";
		
		// TODO Auto-generated method stub
		List<Ylb> list =jdbcTemplate.query(sql,  
			        new RowMapper<Ylb>(){  
			            @Override  
			            public Ylb  mapRow(ResultSet rs, int rowNum) throws SQLException {  
		            		Ylb s  = new Ylb(); 
			            	s.setNbbm(rs.getString("nbbm"));
			            	s.setRq(rs.getDate("rq"));
			            	s.setJzyl(rs.getDouble("jzyl"));
			                return s;  
			            }  
					});  
		return list;
	}
	

}