Commit 68ae79317f7da26d0647b0e2e28ed170acd12cfc

Authored by 胡翰林
1 parent 158d9f1f

导入模板校验表头和空数据

@@ -5,9 +5,7 @@ import com.alibaba.excel.annotation.write.style.ColumnWidth; @@ -5,9 +5,7 @@ import com.alibaba.excel.annotation.write.style.ColumnWidth;
5 import com.alibaba.excel.annotation.write.style.ContentRowHeight; 5 import com.alibaba.excel.annotation.write.style.ContentRowHeight;
6 import com.alibaba.excel.annotation.write.style.ContentStyle; 6 import com.alibaba.excel.annotation.write.style.ContentStyle;
7 import com.alibaba.excel.annotation.write.style.HeadRowHeight; 7 import com.alibaba.excel.annotation.write.style.HeadRowHeight;
8 -import com.ash.base.excelOpt.DatetimeConverter;  
9 import com.ash.base.excelOpt.ExcelCheck; 8 import com.ash.base.excelOpt.ExcelCheck;
10 -import com.baomidou.mybatisplus.annotation.TableField;  
11 import lombok.AllArgsConstructor; 9 import lombok.AllArgsConstructor;
12 import lombok.Builder; 10 import lombok.Builder;
13 import lombok.Data; 11 import lombok.Data;
@@ -17,6 +17,8 @@ import com.ash.base.excelOpt.ValidateAnnotation; @@ -17,6 +17,8 @@ import com.ash.base.excelOpt.ValidateAnnotation;
17 import com.ash.entity.dao.CaseMapper; 17 import com.ash.entity.dao.CaseMapper;
18 import com.ash.excelData.CaseExcelData; 18 import com.ash.excelData.CaseExcelData;
19 import com.ash.service.CaseService; 19 import com.ash.service.CaseService;
  20 +import com.sun.javafx.binding.StringFormatter;
  21 +import javafx.beans.binding.StringExpression;
20 import lombok.Getter; 22 import lombok.Getter;
21 import lombok.extern.slf4j.Slf4j; 23 import lombok.extern.slf4j.Slf4j;
22 import org.apache.commons.collections.CollectionUtils; 24 import org.apache.commons.collections.CollectionUtils;
@@ -48,6 +50,7 @@ public class CaseExcelListener extends AnalysisEventListener<CaseExcelData> @@ -48,6 +50,7 @@ public class CaseExcelListener extends AnalysisEventListener<CaseExcelData>
48 private final Map<Field, ValidateAnnotation> fieldMap = new HashMap<>(); 50 private final Map<Field, ValidateAnnotation> fieldMap = new HashMap<>();
49 51
50 private boolean existValidate = true; 52 private boolean existValidate = true;
  53 + private boolean headCheck = true;
51 54
52 private static final int CORE_POOL_SIZE = 5;// 核心线程数 55 private static final int CORE_POOL_SIZE = 5;// 核心线程数
53 private static final int MAX_POOL_SIZE = 10;// 最大线程数 56 private static final int MAX_POOL_SIZE = 10;// 最大线程数
@@ -75,6 +78,7 @@ public class CaseExcelListener extends AnalysisEventListener<CaseExcelData> @@ -75,6 +78,7 @@ public class CaseExcelListener extends AnalysisEventListener<CaseExcelData>
75 @Override 78 @Override
76 public void invokeHead(Map<Integer, ReadCellData<?>> headMap, AnalysisContext context) { 79 public void invokeHead(Map<Integer, ReadCellData<?>> headMap, AnalysisContext context) {
77 ExcelReadHeadProperty excelReadHeadProperty = context.currentReadHolder().excelReadHeadProperty(); 80 ExcelReadHeadProperty excelReadHeadProperty = context.currentReadHolder().excelReadHeadProperty();
  81 + StringBuilder headError = new StringBuilder();
78 for (Head head : excelReadHeadProperty.getHeadMap().values()) { 82 for (Head head : excelReadHeadProperty.getHeadMap().values()) {
79 Field field = head.getField(); 83 Field field = head.getField();
80 ExcelProperty excelProperty = field.getAnnotation(ExcelProperty.class); 84 ExcelProperty excelProperty = field.getAnnotation(ExcelProperty.class);
@@ -84,6 +88,24 @@ public class CaseExcelListener extends AnalysisEventListener<CaseExcelData> @@ -84,6 +88,24 @@ public class CaseExcelListener extends AnalysisEventListener<CaseExcelData>
84 field.setAccessible(true); 88 field.setAccessible(true);
85 fieldMap.put(field, new ValidateAnnotation(excelProperty, excelCheck, headName)); 89 fieldMap.put(field, new ValidateAnnotation(excelProperty, excelCheck, headName));
86 } 90 }
  91 +
  92 + //校验表头
  93 + if (excelProperty != null) {
  94 + int epIndex = excelProperty.index();
  95 + String headDescription = excelProperty.value()[0];
  96 + if (!headMap.containsKey(epIndex)) {
  97 + headError.append(String.format(" 第%s列缺失,应为:%s"
  98 + , epIndex + 1, headDescription));
  99 + } else if (!headMap.get(epIndex).getStringValue().equals(headDescription)) {
  100 + headError.append(String.format(" 第%s列错误,%s 应为:%s"
  101 + , epIndex + 1, headMap.get(epIndex).getStringValue(), headDescription));
  102 + }
  103 + }
  104 + }
  105 +
  106 + if (StringUtils.isNotBlank(headError.toString())) {
  107 + headCheck = false;
  108 + addError(0, "模板错误请使用下载的模板导入!" + headError);
87 } 109 }
88 if (fieldMap.isEmpty()) { 110 if (fieldMap.isEmpty()) {
89 existValidate = false; 111 existValidate = false;
@@ -103,6 +125,9 @@ public class CaseExcelListener extends AnalysisEventListener<CaseExcelData> @@ -103,6 +125,9 @@ public class CaseExcelListener extends AnalysisEventListener<CaseExcelData>
103 125
104 @Override 126 @Override
105 public void invoke(CaseExcelData data, AnalysisContext analysisContext) { 127 public void invoke(CaseExcelData data, AnalysisContext analysisContext) {
  128 + if (!headCheck) {
  129 + return;
  130 + }
106 log.info("接收案件信息" + data); 131 log.info("接收案件信息" + data);
107 if (data != null) { 132 if (data != null) {
108 if (existValidate) { 133 if (existValidate) {
@@ -129,6 +154,12 @@ public class CaseExcelListener extends AnalysisEventListener<CaseExcelData> @@ -129,6 +154,12 @@ public class CaseExcelListener extends AnalysisEventListener<CaseExcelData>
129 if (CollectionUtils.isNotEmpty(errorList)) { 154 if (CollectionUtils.isNotEmpty(errorList)) {
130 throw new ExcelAnalysisException("数据校验错误!"); 155 throw new ExcelAnalysisException("数据校验错误!");
131 } 156 }
  157 +
  158 + if (CollectionUtils.isEmpty(list)) {
  159 + addError(0, "模板数据为空!");
  160 + throw new ExcelAnalysisException("模板数据为空!");
  161 + }
  162 +
132 log.info("解析结束,开始插入数据"); 163 log.info("解析结束,开始插入数据");
133 ExecutorService executor = new ThreadPoolExecutor(CORE_POOL_SIZE, MAX_POOL_SIZE, 164 ExecutorService executor = new ThreadPoolExecutor(CORE_POOL_SIZE, MAX_POOL_SIZE,
134 KEEP_ALIVE_TIME, TimeUnit.SECONDS, 165 KEEP_ALIVE_TIME, TimeUnit.SECONDS,
@@ -149,7 +180,7 @@ public class CaseExcelListener extends AnalysisEventListener<CaseExcelData> @@ -149,7 +180,7 @@ public class CaseExcelListener extends AnalysisEventListener<CaseExcelData>
149 endPosition = (i + 1) * singleThreadDealcount; 180 endPosition = (i + 1) * singleThreadDealcount;
150 } 181 }
151 CaseMapper caseMapper = SpringJobBeanFactory.getBean(CaseMapper.class); 182 CaseMapper caseMapper = SpringJobBeanFactory.getBean(CaseMapper.class);
152 - CaseService caseService= SpringJobBeanFactory.getBean(CaseService.class); 183 + CaseService caseService = SpringJobBeanFactory.getBean(CaseService.class);
153 CaseThread thread = new CaseThread(count, caseMapper, caseService, list, startPosition, endPosition); 184 CaseThread thread = new CaseThread(count, caseMapper, caseService, list, startPosition, endPosition);
154 executor.execute(thread); 185 executor.execute(thread);
155 } 186 }
@@ -48,6 +48,7 @@ public class WarningInstanceExcelListener extends AnalysisEventListener<WarningI @@ -48,6 +48,7 @@ public class WarningInstanceExcelListener extends AnalysisEventListener<WarningI
48 private final Map<Field, ValidateAnnotation> fieldMap = new HashMap<>(); 48 private final Map<Field, ValidateAnnotation> fieldMap = new HashMap<>();
49 49
50 private boolean existValidate = true; 50 private boolean existValidate = true;
  51 + private boolean headCheck = true;
51 52
52 private static final int CORE_POOL_SIZE = 5;// 核心线程数 53 private static final int CORE_POOL_SIZE = 5;// 核心线程数
53 private static final int MAX_POOL_SIZE = 10;// 最大线程数 54 private static final int MAX_POOL_SIZE = 10;// 最大线程数
@@ -75,6 +76,7 @@ public class WarningInstanceExcelListener extends AnalysisEventListener<WarningI @@ -75,6 +76,7 @@ public class WarningInstanceExcelListener extends AnalysisEventListener<WarningI
75 @Override 76 @Override
76 public void invokeHead(Map<Integer, ReadCellData<?>> headMap, AnalysisContext context) { 77 public void invokeHead(Map<Integer, ReadCellData<?>> headMap, AnalysisContext context) {
77 ExcelReadHeadProperty excelReadHeadProperty = context.currentReadHolder().excelReadHeadProperty(); 78 ExcelReadHeadProperty excelReadHeadProperty = context.currentReadHolder().excelReadHeadProperty();
  79 + StringBuilder headError = new StringBuilder();
78 for (Head head : excelReadHeadProperty.getHeadMap().values()) { 80 for (Head head : excelReadHeadProperty.getHeadMap().values()) {
79 Field field = head.getField(); 81 Field field = head.getField();
80 ExcelProperty excelProperty = field.getAnnotation(ExcelProperty.class); 82 ExcelProperty excelProperty = field.getAnnotation(ExcelProperty.class);
@@ -84,7 +86,27 @@ public class WarningInstanceExcelListener extends AnalysisEventListener<WarningI @@ -84,7 +86,27 @@ public class WarningInstanceExcelListener extends AnalysisEventListener<WarningI
84 field.setAccessible(true); 86 field.setAccessible(true);
85 fieldMap.put(field, new ValidateAnnotation(excelProperty, excelCheck, headName)); 87 fieldMap.put(field, new ValidateAnnotation(excelProperty, excelCheck, headName));
86 } 88 }
  89 +
  90 + //校验表头
  91 + if (excelProperty != null) {
  92 + int epIndex = excelProperty.index();
  93 + String headDescription = excelProperty.value()[0];
  94 + if (!headMap.containsKey(epIndex)) {
  95 + headError.append(String.format(" 第%s列缺失,应为:%s"
  96 + , epIndex + 1, headDescription));
  97 + } else if (!headMap.get(epIndex).getStringValue().equals(headDescription)) {
  98 + headError.append(String.format(" 第%s列错误,%s 应为:%s"
  99 + , epIndex + 1, headMap.get(epIndex).getStringValue(), headDescription));
  100 + }
  101 + }
  102 +
  103 + }
  104 +
  105 + if (StringUtils.isNotBlank(headError.toString())) {
  106 + headCheck = false;
  107 + addError(0, "模板错误请使用下载的模板导入!" + headError);
87 } 108 }
  109 +
88 if (fieldMap.isEmpty()) { 110 if (fieldMap.isEmpty()) {
89 existValidate = false; 111 existValidate = false;
90 } 112 }
@@ -103,6 +125,9 @@ public class WarningInstanceExcelListener extends AnalysisEventListener<WarningI @@ -103,6 +125,9 @@ public class WarningInstanceExcelListener extends AnalysisEventListener<WarningI
103 125
104 @Override 126 @Override
105 public void invoke(WarningInstanceExcelData data, AnalysisContext analysisContext) { 127 public void invoke(WarningInstanceExcelData data, AnalysisContext analysisContext) {
  128 + if (!headCheck) {
  129 + return;
  130 + }
106 log.info("接收警情信息" + data); 131 log.info("接收警情信息" + data);
107 if (data != null) { 132 if (data != null) {
108 if (existValidate) { 133 if (existValidate) {
@@ -128,6 +153,13 @@ public class WarningInstanceExcelListener extends AnalysisEventListener<WarningI @@ -128,6 +153,13 @@ public class WarningInstanceExcelListener extends AnalysisEventListener<WarningI
128 if (CollectionUtils.isNotEmpty(errorList)) { 153 if (CollectionUtils.isNotEmpty(errorList)) {
129 throw new ExcelAnalysisException("数据校验错误!"); 154 throw new ExcelAnalysisException("数据校验错误!");
130 } 155 }
  156 +
  157 + if (CollectionUtils.isEmpty(list)) {
  158 + addError(0, "模板数据为空!");
  159 + throw new ExcelAnalysisException("模板数据为空!");
  160 + }
  161 +
  162 +
131 log.info("解析结束,开始插入数据"); 163 log.info("解析结束,开始插入数据");
132 ExecutorService executor = new ThreadPoolExecutor(CORE_POOL_SIZE, MAX_POOL_SIZE, 164 ExecutorService executor = new ThreadPoolExecutor(CORE_POOL_SIZE, MAX_POOL_SIZE,
133 KEEP_ALIVE_TIME, TimeUnit.SECONDS, 165 KEEP_ALIVE_TIME, TimeUnit.SECONDS,
@@ -3,6 +3,7 @@ package com.ash.service; @@ -3,6 +3,7 @@ package com.ash.service;
3 import com.alibaba.excel.EasyExcel; 3 import com.alibaba.excel.EasyExcel;
4 import com.alibaba.excel.exception.ExcelAnalysisException; 4 import com.alibaba.excel.exception.ExcelAnalysisException;
5 import com.alibaba.excel.exception.ExcelDataConvertException; 5 import com.alibaba.excel.exception.ExcelDataConvertException;
  6 +import com.alibaba.fastjson.JSON;
6 import com.alibaba.fastjson.JSONObject; 7 import com.alibaba.fastjson.JSONObject;
7 import com.ash.base.*; 8 import com.ash.base.*;
8 import com.ash.base.excelOpt.ExcelErrorMessage; 9 import com.ash.base.excelOpt.ExcelErrorMessage;
@@ -683,7 +684,9 @@ public class CaseService { @@ -683,7 +684,9 @@ public class CaseService {
683 return listener.getErrorList(); 684 return listener.getErrorList();
684 } catch (ExcelDataConvertException e) { 685 } catch (ExcelDataConvertException e) {
685 List<ExcelErrorMessage> errorList = new ArrayList<>(); 686 List<ExcelErrorMessage> errorList = new ArrayList<>();
686 - ExcelErrorMessage eem = new ExcelErrorMessage().setMessage("模板错误"); 687 + String error = String.format("第%s行,第%s列解析异常,数据为:%s", e.getRowIndex(),
  688 + e.getColumnIndex(), JSON.toJSONString(e.getCellData()));
  689 + ExcelErrorMessage eem = new ExcelErrorMessage().setMessage("数据类型错误!" + error);
687 errorList.add(eem); 690 errorList.add(eem);
688 return errorList; 691 return errorList;
689 } catch (Exception ex) { 692 } catch (Exception ex) {
@@ -3,6 +3,7 @@ package com.ash.service; @@ -3,6 +3,7 @@ package com.ash.service;
3 import com.alibaba.excel.EasyExcel; 3 import com.alibaba.excel.EasyExcel;
4 import com.alibaba.excel.exception.ExcelAnalysisException; 4 import com.alibaba.excel.exception.ExcelAnalysisException;
5 import com.alibaba.excel.exception.ExcelDataConvertException; 5 import com.alibaba.excel.exception.ExcelDataConvertException;
  6 +import com.alibaba.fastjson.JSON;
6 import com.alibaba.fastjson.JSONObject; 7 import com.alibaba.fastjson.JSONObject;
7 import com.ash.base.*; 8 import com.ash.base.*;
8 import com.ash.base.excelOpt.ExcelErrorMessage; 9 import com.ash.base.excelOpt.ExcelErrorMessage;
@@ -410,7 +411,9 @@ public class WarningInstanceService { @@ -410,7 +411,9 @@ public class WarningInstanceService {
410 return listener.getErrorList(); 411 return listener.getErrorList();
411 } catch (ExcelDataConvertException e) { 412 } catch (ExcelDataConvertException e) {
412 List<ExcelErrorMessage> errorList = new ArrayList<>(); 413 List<ExcelErrorMessage> errorList = new ArrayList<>();
413 - ExcelErrorMessage eem = new ExcelErrorMessage().setMessage("模板错误"); 414 + String error = String.format("第%s行,第%s列解析异常,数据为:%s", e.getRowIndex(),
  415 + e.getColumnIndex(), JSON.toJSONString(e.getCellData()));
  416 + ExcelErrorMessage eem = new ExcelErrorMessage().setMessage("数据类型错误!" + error);
414 errorList.add(eem); 417 errorList.add(eem);
415 return errorList; 418 return errorList;
416 } catch (Exception ex) { 419 } catch (Exception ex) {