Commit 68ae79317f7da26d0647b0e2e28ed170acd12cfc

Authored by 胡翰林
1 parent 158d9f1f

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

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