Commit 4d4dc43f3faea09ceaab52ce33cc1fb1e8b9f290

Authored by 娄高锋
1 parent 18ae8245

验证异常的数据导入并返回错误信息

src/main/java/com/bsth/service/impl/RefuelServiceImpl.java
... ... @@ -13,6 +13,7 @@ import java.io.File;
13 13 import java.io.FileInputStream;
14 14 import java.math.BigDecimal;
15 15 import java.text.DecimalFormat;
  16 +import java.text.ParseException;
16 17 import java.text.SimpleDateFormat;
17 18 import java.util.ArrayList;
18 19 import java.util.Date;
... ... @@ -189,7 +190,7 @@ public class RefuelServiceImpl extends BaseServiceImpl<Refuel, Long> implements
189 190 realMileage = add(add(sjgl, ksgl), add(jccgl, ljgl));
190 191 }
191 192  
192   - if(r.getOutOil().length() > 0 && r.getInOil().length() > 0){
  193 + if(r.getOutOil() != null && r.getInOil() != null && r.getOutOil().length() > 0 && r.getInOil().length() > 0){
193 194 oil = new BigDecimal(r.getOutOil()).subtract(new BigDecimal(r.getInOil())).toString();
194 195 }
195 196 if(r.getInStation0().length() > 0){
... ... @@ -239,7 +240,10 @@ public class RefuelServiceImpl extends BaseServiceImpl<Refuel, Long> implements
239 240 public String importExcel(File file) {
240 241 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
241 242 SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  243 + DecimalFormat df = new DecimalFormat("0.###");
  244 + List<Refuel> list = new ArrayList<Refuel>();
242 245 List<String> textList = new ArrayList<String>();
  246 + String msg = "";
243 247 try {
244 248 POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(file));
245 249 HSSFWorkbook wb = new HSSFWorkbook(fs);
... ... @@ -257,6 +261,7 @@ public class RefuelServiceImpl extends BaseServiceImpl&lt;Refuel, Long&gt; implements
257 261 userName = user.getUserName();
258 262 }
259 263  
  264 + //取值
260 265 for(int i = 1; i < rowNum; i++){
261 266 row = sheet.getRow(i);
262 267 if (row == null){
... ... @@ -280,6 +285,11 @@ public class RefuelServiceImpl extends BaseServiceImpl&lt;Refuel, Long&gt; implements
280 285 continue;
281 286 textList.add(text + ";");
282 287 }
  288 +
  289 + wb.close();
  290 + fs.close();
  291 +
  292 + //验证
283 293 for(int i = 0; i < textList.size(); i++){
284 294 String text = textList.get(i);
285 295 String[] split = text.split(",");
... ... @@ -301,25 +311,107 @@ public class RefuelServiceImpl extends BaseServiceImpl&lt;Refuel, Long&gt; implements
301 311 String outStation0 = split[12].trim();
302 312 String outStation10 = split[13].trim();
303 313  
304   - List<Refuel> list = repository.selectByDateAndCar(rq, line, car, driver);
305   - if(list.size() > 0){
306   - for(Refuel r : list){
307   - if(r.getId() != null){
308   - repository.update(r.getId(), rq, rq, line, car, driver, outOil, inOil, inStation0, inStation5,
309   - oilCard0, oilCard10, eastStation0, eastStation10, outStation0, outStation10, userName, sd.format(new Date()));
  314 + driver = driver.replace(".0", "");
  315 +
  316 + String l = "", obj = "";
  317 + try {
  318 + rq = sdf.format(sdf.parse(rq));
  319 +
  320 + if(outOil.length() != 0){
  321 + l = "E";obj = outOil;
  322 + outOil = df.format(new BigDecimal(outOil));
  323 + }
  324 + if(inOil.length() != 0){
  325 + l = "F";obj = inOil;
  326 + inOil = df.format(new BigDecimal(inOil));
  327 + }
  328 + if(inStation0.length() != 0){
  329 + l = "G";obj = inStation0;
  330 + inStation0 = df.format(new BigDecimal(inStation0));
  331 + }
  332 + if(inStation5.length() != 0){
  333 + l = "H";obj = inStation5;
  334 + inStation5 = df.format(new BigDecimal(inStation5));
  335 + }
  336 + if(oilCard0.length() != 0){
  337 + l = "I";obj = oilCard0;
  338 + oilCard0 = df.format(new BigDecimal(oilCard0));
  339 + }
  340 + if(oilCard10.length() != 0){
  341 + l = "J";obj = oilCard10;
  342 + oilCard10 = df.format(new BigDecimal(oilCard10));
  343 + }
  344 + if(eastStation0.length() != 0){
  345 + l = "K";obj = eastStation0;
  346 + eastStation0 = df.format(new BigDecimal(eastStation0));
  347 + }
  348 + if(eastStation10.length() != 0){
  349 + l = "L";obj = eastStation10;
  350 + eastStation10 = df.format(new BigDecimal(eastStation10));
  351 + }
  352 + if(outStation0.length() != 0){
  353 + l = "M";obj = outStation0;
  354 + outStation0 = df.format(new BigDecimal(outStation0));
  355 + }
  356 + if(outStation10.length() != 0){
  357 + l = "N";obj = outStation10;
  358 + outStation10 = df.format(new BigDecimal(outStation10));
  359 + }
  360 + } catch (ParseException e) {
  361 + // TODO: handle exception
  362 + msg += "\\n"+(i+2)+"行日期格式错误";
  363 + continue;
  364 + } catch (NumberFormatException e) {
  365 + // TODO: handle exception
  366 + msg += "\\n"+(i+2)+"行"+l+"列内容:“"+obj+"”数据异常或有误";
  367 + continue;
  368 + }
  369 +
  370 + Refuel r = new Refuel();
  371 + r.setDateStr(rq);
  372 + r.setLineName(line);
  373 + r.setCar(car);
  374 + r.setDriver(driver);
  375 + r.setOutOil(outOil);
  376 + r.setInOil(inOil);
  377 + r.setInStation0(inStation0);
  378 + r.setInStation5(inStation5);
  379 + r.setOilCard0(oilCard0);
  380 + r.setOilCard10(oilCard10);
  381 + r.setEastStation0(eastStation0);
  382 + r.setEastStation10(eastStation10);
  383 + r.setOutStation0(outStation0);
  384 + r.setOutStation10(outStation10);
  385 + list.add(r);
  386 + }
  387 +
  388 + //导入(msg是验证中产生的格式报错信息)
  389 + if(msg.length() == 0){
  390 + for(Refuel r : list){
  391 + if(msg.length() == 0){
  392 + List<Refuel> rs = repository.selectByDateAndCar(r.getDateStr(), r.getLineName(), r.getCar(), r.getDriver());
  393 + if(rs.size() > 0){
  394 + for(Refuel re : rs){
  395 + if(re.getId() != null){
  396 + repository.update(re.getId(), r.getDateStr(), r.getDateStr(), r.getLineName(), r.getCar(), r.getDriver(),
  397 + r.getOutOil(), r.getInOil(), r.getInStation0(), r.getInStation5(), r.getOilCard0(), r.getOilCard10(),
  398 + r.getEastStation0(), r.getEastStation10(), r.getOutStation0(), r.getOutStation10(), userName, sd.format(new Date()));
  399 + }
  400 + }
  401 + } else {
  402 + repository.insertData(r.getDateStr(), r.getDateStr(), r.getLineName(), r.getCar(), r.getDriver(),
  403 + r.getOutOil(), r.getInOil(), r.getInStation0(), r.getInStation5(), r.getOilCard0(), r.getOilCard10(),
  404 + r.getEastStation0(), r.getEastStation10(), r.getOutStation0(), r.getOutStation10(), userName, sd.format(new Date()));
310 405 }
311 406 }
312   - } else {
313   - repository.insertData(rq, rq, line, car, driver, outOil, inOil, inStation0, inStation5, oilCard0, oilCard10,
314   - eastStation0, eastStation10, outStation0, outStation10, userName, sd.format(new Date()));
315 407 }
  408 + } else {
  409 + throw new Exception();
316 410 }
317   - wb.close();
318   - fs.close();
319 411 } catch (Exception e) {
320 412 // TODO Auto-generated catch block
321 413 e.printStackTrace();
322   - return "文件导入失败";
  414 + return "文件导入失败" + msg;
323 415 } finally {
324 416 file.delete();
325 417 }
... ...
src/main/resources/static/pages/forms/statement/refuelUpload.html
... ... @@ -73,11 +73,13 @@
73 73 // alert("文件导入成功");
74 74 $('#uploadFile').modal('hide');
75 75 $('tr.filter .filter-submit').click();
  76 + $("#query").click();
76 77 }
77 78 },
78 79 error : function(data, status, e) {
79 80 layer.close(j);
80   - alert("文件导入失败");
  81 + alert(JSON.parse((data.responseText)).result);
  82 +// alert("文件导入失败");
81 83 }
82 84 })
83 85 });
... ...