Commit db7606e02d23fcaff4be733149ba9fe092170552

Authored by 胡翰林
1 parent 68ae7931

添加重复数据判断

... ... @@ -14,15 +14,18 @@ import com.ash.base.SpringJobBeanFactory;
14 14 import com.ash.base.excelOpt.ExcelCheck;
15 15 import com.ash.base.excelOpt.ExcelErrorMessage;
16 16 import com.ash.base.excelOpt.ValidateAnnotation;
  17 +import com.ash.entity.Case;
17 18 import com.ash.entity.dao.CaseMapper;
18 19 import com.ash.excelData.CaseExcelData;
19 20 import com.ash.service.CaseService;
  21 +import com.ash.util.ObjectValueOption;
20 22 import com.sun.javafx.binding.StringFormatter;
21 23 import javafx.beans.binding.StringExpression;
22 24 import lombok.Getter;
23 25 import lombok.extern.slf4j.Slf4j;
24 26 import org.apache.commons.collections.CollectionUtils;
25 27 import org.apache.commons.lang3.StringUtils;
  28 +import org.springframework.stereotype.Component;
26 29
27 30 import javax.annotation.Resource;
28 31 import java.lang.reflect.Field;
... ... @@ -151,6 +154,7 @@ public class CaseExcelListener extends AnalysisEventListener<CaseExcelData>
151 154
152 155 @Override
153 156 public void doAfterAllAnalysed(AnalysisContext analysisContext) {
  157 + checkRepeat();
154 158 if (CollectionUtils.isNotEmpty(errorList)) {
155 159 throw new ExcelAnalysisException("数据校验错误!");
156 160 }
... ... @@ -281,12 +285,16 @@ public class CaseExcelListener extends AnalysisEventListener<CaseExcelData>
281 285 public void checkRepeat() {
282 286 Map<Field, ValidateAnnotation> repeatFieldMap = fieldMap.entrySet().stream().filter(entry -> !entry.getValue().getExcelCheck().canRepeat())
283 287 .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
  288 + CaseService caseService = SpringJobBeanFactory.getBean(CaseService.class);
284 289 repeatFieldMap.forEach((field, annotation) -> {
285 290 //使用iterate方式构建流以获取行号
  291 + List<String> uniqueCodeList = new ArrayList<>();
286 292 Stream.iterate(0, i -> i + 1).limit(list.size()).collect(Collectors.groupingBy(i -> {
287 293 try {
288 294 Object value = field.get(list.get(i));
289   - return JSON.toJSONString(value);
  295 + String uc = ObjectValueOption.getObjString(value);
  296 + uniqueCodeList.add(uc);
  297 + return uc;
290 298 } catch (IllegalAccessException e) {
291 299 throw new RuntimeException(e);
292 300 }
... ... @@ -301,6 +309,21 @@ public class CaseExcelListener extends AnalysisEventListener<CaseExcelData>
301 309 }
302 310 }
303 311 });
  312 + if (CollectionUtils.isEmpty(errorList)) {
  313 + List<String> repeatDataList = caseService.listByFeild(field.getName(), uniqueCodeList);
  314 + for (int i = 0; i < list.size(); i++) {
  315 + try {
  316 + Object value = field.get(list.get(i));
  317 + String uc = ObjectValueOption.getObjString(value);
  318 + if (repeatDataList.contains(uc)) {
  319 + addError(i + 1, annotation.getHeadName() + "字段已存在!");
  320 + }
  321 +
  322 + } catch (IllegalAccessException e) {
  323 + throw new RuntimeException(e);
  324 + }
  325 + }
  326 + }
304 327 });
305 328 }
306 329
... ...
... ... @@ -17,6 +17,7 @@ import com.ash.base.excelOpt.ValidateAnnotation;
17 17 import com.ash.entity.dao.WarningInstanceMapper;
18 18 import com.ash.excelData.WarningInstanceExcelData;
19 19 import com.ash.service.WarningInstanceService;
  20 +import com.ash.util.ObjectValueOption;
20 21 import lombok.Getter;
21 22 import lombok.extern.slf4j.Slf4j;
22 23 import org.apache.commons.collections.CollectionUtils;
... ... @@ -150,6 +151,7 @@ public class WarningInstanceExcelListener extends AnalysisEventListener<WarningI
150 151
151 152 @Override
152 153 public void doAfterAllAnalysed(AnalysisContext analysisContext) {
  154 + checkRepeat();
153 155 if (CollectionUtils.isNotEmpty(errorList)) {
154 156 throw new ExcelAnalysisException("数据校验错误!");
155 157 }
... ... @@ -285,12 +287,16 @@ public class WarningInstanceExcelListener extends AnalysisEventListener<WarningI
285 287 public void checkRepeat() {
286 288 Map<Field, ValidateAnnotation> repeatFieldMap = fieldMap.entrySet().stream().filter(entry -> !entry.getValue().getExcelCheck().canRepeat())
287 289 .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
  290 + WarningInstanceService warningInstanceService = SpringJobBeanFactory.getBean(WarningInstanceService.class);
288 291 repeatFieldMap.forEach((field, annotation) -> {
289 292 //使用iterate方式构建流以获取行号
  293 + List<String> uniqueCodeList = new ArrayList<>();
290 294 Stream.iterate(0, i -> i + 1).limit(list.size()).collect(Collectors.groupingBy(i -> {
291 295 try {
292 296 Object value = field.get(list.get(i));
293   - return JSON.toJSONString(value);
  297 + String uc = ObjectValueOption.getObjString(value);
  298 + uniqueCodeList.add(uc);
  299 + return uc;
294 300 } catch (IllegalAccessException e) {
295 301 throw new RuntimeException(e);
296 302 }
... ... @@ -305,6 +311,21 @@ public class WarningInstanceExcelListener extends AnalysisEventListener<WarningI
305 311 }
306 312 }
307 313 });
  314 + if (CollectionUtils.isEmpty(errorList)) {
  315 + List<String> repeatDataList = warningInstanceService.listByFeild(field.getName(), uniqueCodeList);
  316 + for (int i = 0; i < list.size(); i++) {
  317 + try {
  318 + Object value = field.get(list.get(i));
  319 + String uc = ObjectValueOption.getObjString(value);
  320 + if (repeatDataList.contains(uc)) {
  321 + addError(i + 1, annotation.getHeadName() + "字段已存在!");
  322 + }
  323 +
  324 + } catch (IllegalAccessException e) {
  325 + throw new RuntimeException(e);
  326 + }
  327 + }
  328 + }
308 329 });
309 330 }
310 331
... ...
... ... @@ -16,6 +16,7 @@ import com.ash.listener.CaseExcelListener;
16 16 import com.ash.util.*;
17 17 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
18 18 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  19 +import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
19 20 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
20 21 import com.github.yulichang.query.MPJQueryWrapper;
21 22 import lombok.extern.slf4j.Slf4j;
... ... @@ -28,6 +29,8 @@ import org.springframework.util.CollectionUtils;
28 29 import org.springframework.web.multipart.MultipartFile;
29 30
30 31 import javax.annotation.Resource;
  32 +import java.lang.reflect.Field;
  33 +import java.lang.reflect.Method;
31 34 import java.text.ParseException;
32 35 import java.text.SimpleDateFormat;
33 36 import java.util.*;
... ... @@ -149,6 +152,23 @@ public class CaseService {
149 152 }
150 153
151 154
  155 + public List<String> listByFeild(String fieldName, List<String> values) {
  156 + QueryWrapper<Case> queryWrapper = new QueryWrapper<>();
  157 + queryWrapper.in(fieldName,values);
  158 + List<Case> cases = caseMapper.selectList(queryWrapper);
  159 + return Optional.ofNullable(cases).map(all -> all.stream().map(item -> {
  160 + try {
  161 + Field field = Case.class.getDeclaredField(fieldName);
  162 + field.setAccessible(true);
  163 + return String.valueOf(field.get(item));
  164 + } catch (Exception e) {
  165 + e.printStackTrace();
  166 + }
  167 + return "";
  168 + }).filter(StringUtils::isNotBlank).collect(Collectors.toList())).orElse(new ArrayList<>());
  169 + }
  170 +
  171 +
152 172 public List<String> listUnReview(List<String> exceptIds, String area) {
153 173 QueryWrapper<Case> queryWrapper = new QueryWrapper<>();
154 174 LambdaQueryWrapper<Case> lambda = queryWrapper.lambda();
... ...
... ... @@ -30,6 +30,7 @@ import org.springframework.util.CollectionUtils;
30 30 import org.springframework.web.multipart.MultipartFile;
31 31
32 32 import javax.annotation.Resource;
  33 +import java.lang.reflect.Field;
33 34 import java.text.ParseException;
34 35 import java.text.SimpleDateFormat;
35 36 import java.util.*;
... ... @@ -132,6 +133,22 @@ public class WarningInstanceService {
132 133 return warningInstanceMapper.selectList(queryWrapper);
133 134 }
134 135
  136 + public List<String> listByFeild(String fieldName, List<String> values) {
  137 + QueryWrapper<WarningInstance> queryWrapper = new QueryWrapper<>();
  138 + queryWrapper.in(fieldName,values);
  139 + List<WarningInstance> cases = warningInstanceMapper.selectList(queryWrapper);
  140 + return Optional.ofNullable(cases).map(all -> all.stream().map(item -> {
  141 + try {
  142 + Field field = WarningInstance.class.getDeclaredField(fieldName);
  143 + field.setAccessible(true);
  144 + return String.valueOf(field.get(item));
  145 + } catch (Exception e) {
  146 + e.printStackTrace();
  147 + }
  148 + return "";
  149 + }).filter(StringUtils::isNotBlank).collect(Collectors.toList())).orElse(new ArrayList<>());
  150 + }
  151 +
135 152 public List<String> listUnReview(List<String> exceptIds, String area) {
136 153 QueryWrapper<WarningInstance> queryWrapper = new QueryWrapper<>();
137 154 LambdaQueryWrapper<WarningInstance> lambda = queryWrapper.lambda();
... ...