JdlServiceImpl.java 13.7 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
package com.bsth.service.oil.impl;

import java.io.File;
import java.io.FileInputStream;
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 org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bsth.common.ResponseCode;
import com.bsth.entity.oil.Jdl;
import com.bsth.entity.oil.JdlReception;
import com.bsth.entity.sys.SysUser;
import com.bsth.repository.oil.JdlReceptionRepository;
import com.bsth.repository.oil.JdlRepository;
import com.bsth.security.util.SecurityUtils;
import com.bsth.service.impl.BaseServiceImpl;
import com.bsth.service.oil.JdlService;
import com.bsth.util.ReportUtils;

@Service
public class JdlServiceImpl extends BaseServiceImpl<Jdl, Integer> implements JdlService {
	
	@Autowired
	JdlRepository repository;
	
	@Autowired
	JdlReceptionRepository jdlReceptionRepository;

	@Override
	public String importExcel(File file, String gsbm, String gsName, String fgsbm, String fgsName) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		DecimalFormat df = new DecimalFormat("######0.00");
		List<String> textList = new ArrayList<String>();
		try {
			POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(file));
			HSSFWorkbook wb = new HSSFWorkbook(fs);
			HSSFSheet sheet = wb.getSheetAt(0);
			// 取得总行数
			int rowNum = sheet.getLastRowNum() + 1;
			// 取得总列数
			int cellNum = sheet.getRow(0).getLastCellNum();
			HSSFRow row = null;
			HSSFCell cell = null;
			for(int i = 2; i < rowNum; i++){
				row = sheet.getRow(i);
				if (row == null){
					continue;
				}
				String text = "";
				for(int j = 0; j < cellNum; j++){
					cell = row.getCell(j);
					if(cell == null){
						text += ",";
						continue;
					}
						text += String.valueOf(cell) + ",";
				}
				String[] split = (text+";").split(",");
				String str = "";
				for(int j = 0; j < split.length && j < 5; j++){
					str += split[j];
				}
				if(str.trim().length() == 0)
					continue;
				textList.add(text + ";");
			}
			for(int i = 0; i < textList.size(); i++){
				String text = textList.get(i);
				String[] split = text.split(",");
				String rq = split[0].trim();
				String nbbm = split[1].trim();
//				String jsy= split[2].trim();
				double jdl = Double.valueOf(split[2].trim().length()!=0?split[2]:"0");
				String jdz = split[3].trim();
				String remarks = split[4].trim();
				if(rq.trim().length() == 0){
					rq = sdf.format(new Date());
				}
				
				List<Double> jdl_ = repository.queryBySame(gsbm, fgsbm, rq, nbbm);
				
				if(jdl_.size() == 0){
					repository.insertData(gsbm, gsName, fgsbm, fgsName, rq, nbbm,
							df.format(jdl), jdz, remarks, sd.format(new Date()));
				}else{
//					jdl += jdl_.get(0);
					repository.UpdateJdl(df.format(jdl), gsbm, fgsbm, rq, nbbm);
				}
			}
			wb.close();
			fs.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "文件导入失败";
		} finally {
			file.delete();
		}
		return "文件导入成功";
	}

	@Override
	public Map<String, Object> query(Map<String, Object> map) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		String gsbm = map.get("gsbm").toString();
		String fgsbm = map.get("fgsbm")!=null?map.get("fgsbm").toString().trim():"";
		String rq = map.get("rq").toString();
		String nbbm = map.get("nbbm").toString();
		Integer page = Integer.valueOf(map.containsKey("page")?map.get("page").toString():"0");
		List<Jdl> query = new ArrayList<Jdl>();
		if(fgsbm.length() > 0){
			query = repository.query(gsbm, fgsbm, rq, nbbm);
		} else {
			query = repository.query(gsbm, rq, nbbm);
		}
		
		if(!map.containsKey("type")){
			
			int end = (page+1)*10>query.size()?query.size():(page+1)*10;
			modelMap.put("dataList", query.subList(page*10, end));
			modelMap.put("totalPages", query.size()%10>0?query.size()/10+1:query.size()/10);
			
		} else if(map.get("type").toString().equals("export")){
			SimpleDateFormat sdfMonth = new SimpleDateFormat("yyyy-MM-dd"),
					sdfSimple = new SimpleDateFormat("yyyyMMdd");
			
			List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
			for(Jdl jdl : query){
				Map<String, Object> m = new HashMap<String, Object>();
				m.put("rq", sdfMonth.format(jdl.getRq()));
				m.put("nbbm", jdl.getNbbm());
				m.put("jdl", jdl.getJdl());
				m.put("jdz", jdl.getJdz());
				m.put("remarks", jdl.getRemarks());
				list.add(m);
			}
			
			List<Iterator<?>> listI = new ArrayList<Iterator<?>>();
			Map<String, Object> m = new HashMap<String, Object>();
			ReportUtils ee = new ReportUtils();
			try {
				listI.add(list.iterator());
				String path = this.getClass().getResource("/").getPath()+"static/pages/forms/";
				ee.excelReplace(listI, new Object[] { m }, path+"mould/export_Jdl.xls",
						path+"export/车辆充电量" + sdfSimple.format(sdfMonth.parse(rq)) + ".xls");
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}
		return modelMap;
	}

	@Override
	public String importExcel_2412(File file, String gsbm, String gsName, String fgsbm, String fgsName) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		SimpleDateFormat ddFormat = new SimpleDateFormat("dd");
		DecimalFormat df = new DecimalFormat("######0.00");
		SysUser user = SecurityUtils.getCurrentUser();
		Date currDate = new Date();
		List<String> textList = new ArrayList<String>();
		try {
			POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(file));
			HSSFWorkbook wb = new HSSFWorkbook(fs);
			HSSFSheet sheet = wb.getSheetAt(0);
			// 取得总行数
			int rowNum = sheet.getLastRowNum() + 1;
			// 取得总列数
			int cellNum = sheet.getRow(0).getLastCellNum();
			HSSFRow row = null;
			HSSFCell cell = null;
			for(int i = 3; i < rowNum; i++){
				row = sheet.getRow(i);
				if (row == null){
					continue;
				}
				String text = "";
				for(int j = 0; j < cellNum; j++){
					cell = row.getCell(j);
					if(cell == null){
						text += ",";
						continue;
					}
						text += String.valueOf(cell) + ",";
				}
				String[] split = (text+";").split(",");
				String str = "";
				for(int j = 0; j < split.length && j < 12; j++){
					str += split[j];
				}
				if(str.trim().length() == 0)
					continue;
				textList.add(text + ";");
			}
			List<JdlReception> list = new ArrayList<JdlReception>();
			for(int i = 0; i < textList.size(); i++){
				String text = textList.get(i);
				String[] split = text.split(",");
				if(split[1] != null && split[1].trim().length() > 0){
					Integer dd = Integer.valueOf(split[1].trim().split("\\.")[0]);	// 日期
					String zh = split[2].trim();	// 桩号
					String nbbm = split[3].trim();	// 车号
					String startTime = split[4].trim();	// 开始时间
					String endTime = split[5].trim();	// 结束时间
					String sumTime = split[6].trim();	// 总计(分钟)
					String startSoc = split[7].trim();	// 起始电量SOC%
					String endSoc = split[8].trim();	// 结束电量SOC%
					String jdl = split[9].trim();	// 充电度数
					String lc = split[10].trim();	// 里程度数
					String stopReason = split[11].trim();	// 一次不能正常充电记录
					JdlReception jdlRe = new JdlReception();
					Date date = sd.parse(startTime);
					if(!(dd == Integer.valueOf(ddFormat.format(date)))){
						date.setTime(date.getTime() - 1l*1000*60*60*24);
					}
					jdlRe.setDateStr(sdf.format(date));
					jdlRe.setConnectorId(zh);
					jdlRe.setCarCode(nbbm);
					jdlRe.setStartTime(startTime);
					jdlRe.setEndTime(endTime);
					jdlRe.setSumTime(Integer.valueOf(sumTime.split("\\.")[0]));
					jdlRe.setStartSoc(Double.valueOf(startSoc));
					jdlRe.setEndSoc(Double.valueOf(endSoc));
					jdlRe.setChargeCapacity(Double.valueOf(jdl));
					jdlRe.setStopReason(stopReason);
					jdlRe.setCreateBy(user.getUserName());
					jdlRe.setCreateDate(currDate);
					jdlRe.setOrigin(1);
					list.add(jdlRe);
				}
			}
			jdlReceptionRepository.saveAll(list);
			wb.close();
			fs.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "文件导入失败";
		} finally {
			file.delete();
		}
		return "文件导入成功";
	}

	@Override
	public Map<String, Object> query_2412(Map<String, Object> map) {
		SimpleDateFormat sdfMonth = new SimpleDateFormat("yyyy-MM-dd"),
				sdfSimple = new SimpleDateFormat("yyyyMMdd");
		Map<String, Object> modelMap = new HashMap<String, Object>();
		String rq = map.get("rq").toString();
		String nbbm = map.get("nbbm").toString();
		Integer page = Integer.valueOf(map.containsKey("page")?map.get("page").toString():"0");
		List<Jdl> query = new ArrayList<Jdl>();
		
		List<Object[]> querySum = jdlReceptionRepository.querySum(rq, nbbm);
		
		try {
			for(Object[] j : querySum){
				Jdl jdl = new Jdl();
				jdl.setRq(sdfMonth.parse(j[0].toString()));
				jdl.setNbbm(j[1].toString());
				jdl.setJdl(Double.valueOf(j[2].toString()));
				jdl.setJdz("");
				jdl.setRemarks("");
				query.add(jdl);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		if(!map.containsKey("type")){
			
			int end = (page+1)*10>query.size()?query.size():(page+1)*10;
			modelMap.put("dataList", query.subList(page*10, end));
			modelMap.put("totalPages", query.size()%10>0?query.size()/10+1:query.size()/10);
			
		} else if(map.get("type").toString().equals("export")){
			
			List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
			for(Jdl jdl : query){
				Map<String, Object> m = new HashMap<String, Object>();
				m.put("rq", sdfMonth.format(jdl.getRq()));
				m.put("nbbm", jdl.getNbbm());
				m.put("jdl", jdl.getJdl());
				m.put("jdz", jdl.getJdz());
				m.put("remarks", jdl.getRemarks());
				list.add(m);
			}
			
			List<Iterator<?>> listI = new ArrayList<Iterator<?>>();
			Map<String, Object> m = new HashMap<String, Object>();
			ReportUtils ee = new ReportUtils();
			try {
				listI.add(list.iterator());
				String path = this.getClass().getResource("/").getPath()+"static/pages/forms/";
				ee.excelReplace(listI, new Object[] { m }, path+"mould/export_Jdl.xls",
						path+"export/车辆充电量" + sdfSimple.format(sdfMonth.parse(rq)) + ".xls");
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}
		return modelMap;
	}

	@Override
	public List<JdlReception> queryJdlReception(Map<String, Object> map) {
		String rq = map.get("rq").toString();
		String nbbm = map.get("nbbm").toString();
		List<JdlReception> query = jdlReceptionRepository.query(rq, nbbm);
		return query;
	}

	@Override
	public Map<String, Object> queryJdlReceptionBatch(Map<String, Object> map) {
		SimpleDateFormat sdfMonth = new SimpleDateFormat("yyyy-MM-dd");
		Map<String, Object> modelMap = new HashMap<String, Object>();
		String rq = map.get("rq").toString();
		Integer page = Integer.valueOf(map.containsKey("page")?map.get("page").toString():"0");
		List<Map<String, Object>> query = new ArrayList<Map<String, Object>>();
		
		List<Object[]> queryBatch = jdlReceptionRepository.queryBatch(rq);
		
		try {
			for(Object[] j : queryBatch){
				Map<String, Object> m = new HashMap<String, Object>();
				m.put("createBy", j[0]);
				m.put("createDate", j[1]);
				query.add(m);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		int end = (page+1)*10>query.size()?query.size():(page+1)*10;
		modelMap.put("dataList", query.subList(page*10, end));
		modelMap.put("totalPages", query.size()%10>0?query.size()/10+1:query.size()/10);
			
		return modelMap;
	}

	@Override
	public List<JdlReception> queryJdlReceptionBatchData(Map<String, Object> map) {
		String createBy = map.get("createBy").toString();
		String createDate = map.get("createDate").toString();
		List<JdlReception> query = jdlReceptionRepository.queryBatchData(createBy, createDate);
		return query;
	}

	@Override
	public Map<String, Object> deleteJdlReceptionBatch(Map<String, Object> map) {
		Map<String, Object> resMap = new HashMap<>();
		try{
			String createBy = map.get("createBy").toString();
			String createDate = map.get("createDate").toString();
			jdlReceptionRepository.deleteBatch(createBy, createDate);
			resMap.put("status", ResponseCode.SUCCESS);
		}catch (Exception e){
			resMap.put("status", ResponseCode.ERROR);
			resMap.put("msg", e.getMessage());
		}
		return resMap;
	}

	@Override
	public List<Jdl> queryJdlByJdlReception(String rq, String nbbm) {
		List<Jdl> list = new ArrayList<Jdl>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		List<JdlReception> query = jdlReceptionRepository.query(rq, nbbm);
		for(JdlReception jr : query){
			try {
				Jdl jdl = new Jdl();
				jdl.setFgsBm("-1");
				jdl.setRq(sdf.parse(jr.getDateStr()));
				jdl.setJdl(jr.getChargeCapacity());
				jdl.setNbbm(jr.getCarCode());
				list.add(jdl);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return list;
	}
	
}