同步BUG修复

This commit is contained in:
dengjun 2024-03-05 17:09:09 +08:00
parent bdf6232e15
commit cdf82b1963
4 changed files with 1847 additions and 1786 deletions

View File

@ -36,6 +36,7 @@ import com.nuzar.rtops.log.dto.LogRecordDTO;
import com.nuzar.rtops.log.service.EsLogApprovalUtil; import com.nuzar.rtops.log.service.EsLogApprovalUtil;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
@ -61,6 +62,7 @@ import java.util.stream.Collectors;
@RequestMapping("/spz") @RequestMapping("/spz")
@Api(tags = "特保区") @Api(tags = "特保区")
@Validated @Validated
@Slf4j
public class FreeTradeHandler implements BaseHandler { public class FreeTradeHandler implements BaseHandler {
@Resource @Resource
@ -410,7 +412,7 @@ public class FreeTradeHandler implements BaseHandler {
@PostMapping("/import-to-add") @PostMapping("/import-to-add")
public Result<List<JSONObject>> upload( public Result<List<JSONObject>> upload(
@RequestParam(required = false, defaultValue = "0") String type, @RequestParam(required = false, defaultValue = "0") String type,
MultipartFile file) throws IOException { MultipartFile file) {
// 所有读取的数据 // 所有读取的数据
List<FreeTradeExcel> dataList = new ArrayList<>(); List<FreeTradeExcel> dataList = new ArrayList<>();
@ -420,96 +422,102 @@ public class FreeTradeHandler implements BaseHandler {
// 验证成功的数据 // 验证成功的数据
List<JSONObject> successDataList = new ArrayList<>(); List<JSONObject> successDataList = new ArrayList<>();
EasyExcel.read(file.getInputStream(), FreeTradeExcel.class, new ReadExcelListener<FreeTradeExcel>() { try {
@Override
protected void saveData(List<FreeTradeExcel> list) { // 保存数据 EasyExcel.read(file.getInputStream(), FreeTradeExcel.class, new ReadExcelListener<FreeTradeExcel>() {
dataList.addAll(list); @Override
protected void saveData(List<FreeTradeExcel> list) { // 保存数据
dataList.addAll(list);
}
}).sheet().doRead();
ValidatorFactory vf = Validation.buildDefaultValidatorFactory();
Validator validator = vf.getValidator();
List<FreeTradeExcel> validData = new ArrayList<>();
// 数据验证
dataList.stream().forEach(item -> {
Set<ConstraintViolation<FreeTradeExcel>> set = validator.validate(item);
if (CollectionUtils.isEmpty(set)) { // 验证通过的
validData.add(item);
} else { // 验证失败的
JSONObject o = JSONObject.from(item);
o.put("status", set.stream().map(p -> p.getMessage()).collect(Collectors.joining(",")));
errorDataList.add(o);
}
});
// 所有数据验证都不通过
if (CollectionUtils.isEmpty(validData)) {
return ResultUtil.failure(ErrorType.PARAMS_ERROR.id(), "导入失败", errorDataList);
} }
}).sheet().doRead();
ValidatorFactory vf = Validation.buildDefaultValidatorFactory(); // 是否有重复的车架号
Validator validator = vf.getValidator(); List<String> repeat = validData.stream().collect(Collectors.groupingBy(FreeTradeExcel::getVin, Collectors.counting()))
.entrySet().stream().filter(entry -> entry.getValue() > 1).map(entry -> entry.getKey()).collect(Collectors.toList());
List<FreeTradeExcel> validData = new ArrayList<>(); if (CollectionUtils.isNotEmpty(repeat)) {
errorDataList.addAll(validData.stream().filter(p -> repeat.contains(p.getVin())).map(p -> {
// 数据验证 JSONObject o = JSONObject.from(p);
dataList.stream().forEach(item -> { o.put("status", "车架号重复");
Set<ConstraintViolation<FreeTradeExcel>> set = validator.validate(item); return o;
if (CollectionUtils.isEmpty(set)) { // 验证通过的 }).collect(Collectors.toList()));
validData.add(item); return ResultUtil.failure(ErrorType.PARAMS_ERROR.id(), "导入失败", errorDataList);
} else { // 验证失败的
JSONObject o = JSONObject.from(item);
o.put("status", set.stream().map(p -> p.getMessage()).collect(Collectors.joining(",")));
errorDataList.add(o);
} }
});
// 所有数据验证都不通过 List<CustomerFreeTrade> collect = validData.stream().map(item -> {
if (CollectionUtils.isEmpty(validData)) { CustomerFreeTrade freeTrade = PoMapper.instance.excel2Entity(item);
return ResultUtil.failure(ErrorType.PARAMS_ERROR.id(), "导入失败", errorDataList); freeTrade.setApplicantId(UserContext.getUser().getUserId());
} freeTrade.setIsActivate(ActiveEnum.NO);
freeTrade.setApplyTime(new Date());
if (StringUtils.equals("1", type)) {
freeTrade.setCheckStatus(AuditEnum.AUDIT);
} else {
freeTrade.setCheckStatus(AuditEnum.SUBMIT);
}
return freeTrade;
}).collect(Collectors.toList());
// 是否有重复的车架号 List<String> vins = collect.stream().map(p -> p.getVin()).collect(Collectors.toList());
List<String> repeat = validData.stream().collect(Collectors.groupingBy(FreeTradeExcel::getVin, Collectors.counting()))
.entrySet().stream().filter(entry -> entry.getValue() > 1).map(entry -> entry.getKey()).collect(Collectors.toList());
if (CollectionUtils.isNotEmpty(repeat)) {
errorDataList.addAll(validData.stream().filter(p -> repeat.contains(p.getVin())).map(p -> {
JSONObject o = JSONObject.from(p);
o.put("status", "车架号重复");
return o;
}).collect(Collectors.toList()));
return ResultUtil.failure(ErrorType.PARAMS_ERROR.id(), "导入失败", errorDataList);
}
List<CustomerFreeTrade> collect = validData.stream().map(item -> { // 验证哪些车架号已经存在了
CustomerFreeTrade freeTrade = PoMapper.instance.excel2Entity(item); List<CustomerFreeTrade> existCargos = customerFreeTradeService.list(new LambdaQueryWrapper<CustomerFreeTrade>().in(CustomerFreeTrade::getVin, vins));
freeTrade.setApplicantId(UserContext.getUser().getUserId()); Map<String, ActiveEnum> existVins = existCargos.stream().collect(Collectors.toMap(CustomerFreeTrade::getVin, CustomerFreeTrade::getIsActivate));
freeTrade.setIsActivate(ActiveEnum.NO); Map<String, Long> existIdMap = existCargos.stream().collect(Collectors.toMap(CustomerFreeTrade::getVin, CustomerFreeTrade::getId));
freeTrade.setApplyTime(new Date());
if (StringUtils.equals("1", type)) { // 已激活不处理的数据
freeTrade.setCheckStatus(AuditEnum.AUDIT); List<FreeTradeExcel> activeData = validData.stream().filter(p -> existVins.containsKey(p.getVin()) && existVins.get(p.getVin()) == ActiveEnum.YES).collect(Collectors.toList());
} else { if (CollectionUtils.isNotEmpty(activeData)) {
freeTrade.setCheckStatus(AuditEnum.SUBMIT); errorDataList.addAll(activeData.stream().map(item -> {
JSONObject o = JSONObject.from(item);
o.put("status", "数据存在并激活");
return o;
}).collect(Collectors.toList()));
return ResultUtil.failure(ErrorType.PARAMS_ERROR.id(), "导入失败", errorDataList);
} }
return freeTrade;
}).collect(Collectors.toList());
List<String> vins = collect.stream().map(p -> p.getVin()).collect(Collectors.toList()); // 需要修改的数据
List<CustomerFreeTrade> update = collect.stream().filter(p -> existVins.containsKey(p.getVin()) && existVins.get(p.getVin()) == ActiveEnum.NO).map(p -> {
p.setId(existIdMap.get(p.getVin()));
p.setIsActivate(ActiveEnum.NO);
return p;
}).collect(Collectors.toList());
// 验证哪些车架号已经存在了 List<CustomerFreeTrade> save = collect.stream().filter(p -> !existIdMap.containsKey(p.getVin())).map(p -> {
List<CustomerFreeTrade> existCargos = customerFreeTradeService.list(new LambdaQueryWrapper<CustomerFreeTrade>().in(CustomerFreeTrade::getVin, vins)); p.setIsActivate(ActiveEnum.NO);
Map<String, ActiveEnum> existVins = existCargos.stream().collect(Collectors.toMap(CustomerFreeTrade::getVin, CustomerFreeTrade::getIsActivate)); return p;
Map<String, Long> existIdMap = existCargos.stream().collect(Collectors.toMap(CustomerFreeTrade::getVin, CustomerFreeTrade::getId)); }).collect(Collectors.toList());
// 已激活不处理的数据 customerFreeTradeService.updateBatchById(update);
List<FreeTradeExcel> activeData = validData.stream().filter(p -> existVins.containsKey(p.getVin()) && existVins.get(p.getVin()) == ActiveEnum.YES).collect(Collectors.toList());
if (CollectionUtils.isNotEmpty(activeData)) {
errorDataList.addAll(activeData.stream().map(item -> {
JSONObject o = JSONObject.from(item);
o.put("status", "数据存在并激活");
return o;
}).collect(Collectors.toList()));
return ResultUtil.failure(ErrorType.PARAMS_ERROR.id(), "导入失败", errorDataList); customerFreeTradeService.saveBatch(save);
return ResultUtil.success(errorDataList);
} catch (Exception e) {
log.error("错误信息", e);
return ResultUtil.failure(ErrorType.PARAMS_ERROR.id(), e.getMessage());
} }
// 需要修改的数据
List<CustomerFreeTrade> update = collect.stream().filter(p -> existVins.containsKey(p.getVin()) && existVins.get(p.getVin()) == ActiveEnum.NO).map(p -> {
p.setId(existIdMap.get(p.getVin()));
p.setIsActivate(ActiveEnum.NO);
return p;
}).collect(Collectors.toList());
List<CustomerFreeTrade> save = collect.stream().filter(p -> !existIdMap.containsKey(p.getVin())).map(p -> {
p.setIsActivate(ActiveEnum.NO);
return p;
}).collect(Collectors.toList());
customerFreeTradeService.updateBatchById(update);
customerFreeTradeService.saveBatch(save);
return ResultUtil.success(errorDataList);
} }
@ApiOperation("导入删除") @ApiOperation("导入删除")

View File

@ -847,7 +847,7 @@ public class CustomerServiceImpl implements CustomerService {
} }
} catch (Exception e) { } catch (Exception e) {
CustomerExportInSyncLog log = new CustomerExportInSyncLog(); CustomerExportInSyncLog log = new CustomerExportInSyncLog();
log.setBatchNo(StringUtils.joinWith(",", list.stream().map(p -> p.getBatchNo()).collect(Collectors.toList()))); log.setBatchNo(item.getBatchNo());
log.setRequestStr(JSONObject.toJSONString(req)); log.setRequestStr(JSONObject.toJSONString(req));
log.setResponseStr(e.getMessage()); log.setResponseStr(e.getMessage());
customerExportInSyncLogService.save(log); customerExportInSyncLogService.save(log);