Commit ecef11363422307c1c20dcbef669b11368d55bcc

Authored by 胡翰林
1 parent 6f6ab92f

导入增加时间格式判断

@@ -26,4 +26,9 @@ public @interface ExcelCheck { @@ -26,4 +26,9 @@ public @interface ExcelCheck {
26 * 长度校验,只对String生效 26 * 长度校验,只对String生效
27 */ 27 */
28 int length() default -1; 28 int length() default -1;
  29 +
  30 + /**
  31 + * 时间格式校验
  32 + */
  33 + String dateFormatValid() default "";
29 } 34 }
@@ -20,7 +20,7 @@ import java.util.Date; @@ -20,7 +20,7 @@ import java.util.Date;
20 @Builder 20 @Builder
21 @NoArgsConstructor 21 @NoArgsConstructor
22 @AllArgsConstructor 22 @AllArgsConstructor
23 -public class CaseExcelData implements Serializable { 23 +public class CaseExcelData implements Serializable {
24 private static final long serialVersionUID = 1L; 24 private static final long serialVersionUID = 1L;
25 25
26 @ExcelCheck(canEmpty = false, canRepeat = false) 26 @ExcelCheck(canEmpty = false, canRepeat = false)
@@ -47,13 +47,13 @@ public class CaseExcelData implements Serializable { @@ -47,13 +47,13 @@ public class CaseExcelData implements Serializable {
47 @ColumnWidth(25) 47 @ColumnWidth(25)
48 private String caseDetail; 48 private String caseDetail;
49 49
50 - @ExcelCheck(canEmpty = false) 50 + @ExcelCheck(canEmpty = false, dateFormatValid = "yyyy-MM-dd HH:mm:ss")
51 @ExcelProperty(value = "接警时间", index = 4) 51 @ExcelProperty(value = "接警时间", index = 4)
52 @ContentStyle(dataFormat = 49) 52 @ContentStyle(dataFormat = 49)
53 @ColumnWidth(25) 53 @ColumnWidth(25)
54 private Date alarmDate; 54 private Date alarmDate;
55 55
56 - @ExcelCheck(canEmpty = false) 56 + @ExcelCheck(canEmpty = false, dateFormatValid = "yyyy-MM-dd HH:mm:ss")
57 @ExcelProperty(value = "立案时间", index = 5) 57 @ExcelProperty(value = "立案时间", index = 5)
58 @ContentStyle(dataFormat = 49) 58 @ContentStyle(dataFormat = 49)
59 @ColumnWidth(25) 59 @ColumnWidth(25)
@@ -35,7 +35,7 @@ public class WarningInstanceExcelData implements Serializable { @@ -35,7 +35,7 @@ public class WarningInstanceExcelData implements Serializable {
35 @ColumnWidth(25) 35 @ColumnWidth(25)
36 private Date alarmDate; 36 private Date alarmDate;
37 37
38 - @ExcelCheck(canEmpty = false) 38 + @ExcelCheck(canEmpty = false, dateFormatValid = "yyyy-MM-dd HH:mm:ss")
39 @ExcelProperty(value = "报警电话", index = 2) 39 @ExcelProperty(value = "报警电话", index = 2)
40 @ContentStyle(dataFormat = 49) 40 @ContentStyle(dataFormat = 49)
41 @ColumnWidth(25) 41 @ColumnWidth(25)
@@ -22,6 +22,8 @@ import org.apache.commons.collections.CollectionUtils; @@ -22,6 +22,8 @@ import org.apache.commons.collections.CollectionUtils;
22 import org.apache.commons.lang3.StringUtils; 22 import org.apache.commons.lang3.StringUtils;
23 23
24 import java.lang.reflect.Field; 24 import java.lang.reflect.Field;
  25 +import java.text.ParseException;
  26 +import java.text.SimpleDateFormat;
25 import java.util.*; 27 import java.util.*;
26 import java.util.concurrent.*; 28 import java.util.concurrent.*;
27 import java.util.stream.Collectors; 29 import java.util.stream.Collectors;
@@ -29,7 +31,7 @@ import java.util.stream.Stream; @@ -29,7 +31,7 @@ import java.util.stream.Stream;
29 31
30 @Slf4j 32 @Slf4j
31 public class CaseExcelListener extends AnalysisEventListener<CaseExcelData> 33 public class CaseExcelListener extends AnalysisEventListener<CaseExcelData>
32 - implements ReadListener<CaseExcelData>{ 34 + implements ReadListener<CaseExcelData> {
33 35
34 private List<CaseExcelData> list = Collections.synchronizedList(new ArrayList<>()); 36 private List<CaseExcelData> list = Collections.synchronizedList(new ArrayList<>());
35 37
@@ -106,10 +108,13 @@ public class CaseExcelListener extends AnalysisEventListener<CaseExcelData> @@ -106,10 +108,13 @@ public class CaseExcelListener extends AnalysisEventListener<CaseExcelData>
106 try { 108 try {
107 // 校验是否空值 109 // 校验是否空值
108 checkEmpty(data, rowIndex); 110 checkEmpty(data, rowIndex);
  111 + // 校验时间类型
  112 + checkDate(data, rowIndex);
109 // 校验长度 113 // 校验长度
110 checkLength(data, rowIndex); 114 checkLength(data, rowIndex);
111 } catch (Exception e) { 115 } catch (Exception e) {
112 log.error("校验excel信息失败,", e); 116 log.error("校验excel信息失败,", e);
  117 + addError(0, "导入异常");
113 } 118 }
114 } 119 }
115 120
@@ -182,6 +187,35 @@ public class CaseExcelListener extends AnalysisEventListener<CaseExcelData> @@ -182,6 +187,35 @@ public class CaseExcelListener extends AnalysisEventListener<CaseExcelData>
182 })); 187 }));
183 } 188 }
184 189
  190 + /**
  191 + * 检验时间格式
  192 + *
  193 + * @param data 数据
  194 + * @param rowIndex 行索引
  195 + */
  196 + public void checkDate(CaseExcelData data, Integer rowIndex) {
  197 + fieldMap.forEach(((field, annotation) -> {
  198 + ExcelCheck excelCheck = annotation.getExcelCheck();
  199 + try {
  200 + Object value = field.get(data);
  201 + //校验非空
  202 + if (StringUtils.isNotBlank(excelCheck.dateFormatValid())) {
  203 + try {
  204 + SimpleDateFormat format = new SimpleDateFormat(excelCheck.dateFormatValid());
  205 + format.setLenient(false);
  206 + format.parse(format.format(value.toString()));
  207 +
  208 + } catch (Exception e) {
  209 + addError(rowIndex + 1, annotation.getHeadName() + "时间格式错误!");
  210 + }
  211 + }
  212 + } catch (IllegalAccessException e) {
  213 + log.error("校验excel信息失败,", e);
  214 + throw new RuntimeException(e);
  215 + }
  216 + }));
  217 + }
  218 +
185 219
186 /** 220 /**
187 * 校验长度 221 * 校验长度
@@ -23,6 +23,7 @@ import org.apache.commons.lang3.StringUtils; @@ -23,6 +23,7 @@ import org.apache.commons.lang3.StringUtils;
23 import org.springframework.stereotype.Service; 23 import org.springframework.stereotype.Service;
24 24
25 import java.lang.reflect.Field; 25 import java.lang.reflect.Field;
  26 +import java.text.SimpleDateFormat;
26 import java.util.*; 27 import java.util.*;
27 import java.util.concurrent.*; 28 import java.util.concurrent.*;
28 import java.util.stream.Collectors; 29 import java.util.stream.Collectors;
@@ -108,6 +109,8 @@ public class WarningInstanceExcelListener extends AnalysisEventListener<WarningI @@ -108,6 +109,8 @@ public class WarningInstanceExcelListener extends AnalysisEventListener<WarningI
108 try { 109 try {
109 // 校验是否空值 110 // 校验是否空值
110 checkEmpty(data, rowIndex); 111 checkEmpty(data, rowIndex);
  112 + // 校验时间类型
  113 + checkDate(data, rowIndex);
111 // 校验长度 114 // 校验长度
112 checkLength(data, rowIndex); 115 checkLength(data, rowIndex);
113 } catch (Exception e) { 116 } catch (Exception e) {
@@ -189,6 +192,36 @@ public class WarningInstanceExcelListener extends AnalysisEventListener<WarningI @@ -189,6 +192,36 @@ public class WarningInstanceExcelListener extends AnalysisEventListener<WarningI
189 192
190 193
191 /** 194 /**
  195 + * 检验时间格式
  196 + *
  197 + * @param data 数据
  198 + * @param rowIndex 行索引
  199 + */
  200 + public void checkDate(WarningInstanceExcelData data, Integer rowIndex) {
  201 + fieldMap.forEach(((field, annotation) -> {
  202 + ExcelCheck excelCheck = annotation.getExcelCheck();
  203 + try {
  204 + Object value = field.get(data);
  205 + //校验非空
  206 + if (StringUtils.isNotBlank(excelCheck.dateFormatValid())) {
  207 + try {
  208 + SimpleDateFormat format = new SimpleDateFormat(excelCheck.dateFormatValid());
  209 + format.setLenient(false);
  210 + format.parse(format.format(value.toString()));
  211 +
  212 + } catch (Exception e) {
  213 + addError(rowIndex + 1, annotation.getHeadName() + "时间格式错误!");
  214 + }
  215 + }
  216 + } catch (IllegalAccessException e) {
  217 + log.error("校验excel信息失败,", e);
  218 + throw new RuntimeException(e);
  219 + }
  220 + }));
  221 + }
  222 +
  223 +
  224 + /**
192 * 校验长度 225 * 校验长度
193 * 226 *
194 * @param data 数据 227 * @param data 数据