From cdf82b196379e84a763c3be9135200632abfb23a Mon Sep 17 00:00:00 2001 From: dengjun Date: Tue, 5 Mar 2024 17:09:09 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=8C=E6=AD=A5BUG=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../rtosc/handler/ExportInHandler.java | 2310 +++++++++-------- .../rtosc/handler/ExportLoadHandler.java | 1155 +++++---- .../rtosc/handler/FreeTradeHandler.java | 166 +- .../service/impl/CustomerServiceImpl.java | 2 +- 4 files changed, 1847 insertions(+), 1786 deletions(-) diff --git a/nuzar-customer-controller/src/main/java/com/haitonggauto/rtosc/handler/ExportInHandler.java b/nuzar-customer-controller/src/main/java/com/haitonggauto/rtosc/handler/ExportInHandler.java index ebe98e5..60fe97c 100644 --- a/nuzar-customer-controller/src/main/java/com/haitonggauto/rtosc/handler/ExportInHandler.java +++ b/nuzar-customer-controller/src/main/java/com/haitonggauto/rtosc/handler/ExportInHandler.java @@ -48,10 +48,6 @@ import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.MapUtils; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; -import org.apache.poi.ss.usermodel.DataValidation; -import org.apache.poi.ss.usermodel.DataValidationConstraint; -import org.apache.poi.ss.usermodel.DataValidationHelper; -import org.apache.poi.ss.util.CellRangeAddressList; import org.apache.poi.xssf.usermodel.*; import org.springframework.core.io.ClassPathResource; import org.springframework.transaction.annotation.Transactional; @@ -80,6 +76,7 @@ import java.util.stream.Collectors; @RequestMapping("/ee") @Api(tags = "出口进场") @Validated +@Slf4j public class ExportInHandler implements BaseHandler { @Resource @@ -342,6 +339,7 @@ public class ExportInHandler implements BaseHandler { if (CollectionUtils.isNotEmpty(page.getRecords())) { QueryWrapper q = new QueryWrapper<>(); q.in("voyage_id", page.getRecords().stream().map(item -> item.getVoyageId()).collect(Collectors.toList())); + q.eq("check_status", AuditEnum.AUDIT_PASS); q.select("sum(quantity) as quantity, port_area_id, ship_id, voyage_id"); q.groupBy("port_area_id, ship_id, voyage_id"); @@ -1629,7 +1627,7 @@ public class ExportInHandler implements BaseHandler { @ApiOperation("车架号导入(数组形式)") @PostMapping("/import-vin-data") - public Result> importVinData(@RequestBody @Validated ImportVinVo vinList) throws IOException { + public Result> importVinData(@RequestBody @Validated ImportVinVo vinList) { // 船名,航次不存在 List noShipList = new ArrayList<>(); // 数量不一致 @@ -1642,120 +1640,127 @@ public class ExportInHandler implements BaseHandler { List validData = vinList.getData(); // 对不完整的数据进行筛选 - // 不能有重复的车架号 - List list = validData.stream().collect(Collectors.groupingBy(ExportVinExcel::getVin, Collectors.counting())) - .entrySet().stream().filter(entry -> entry.getValue() > 1).map(entry -> entry.getKey()).collect(Collectors.toList()); + try { - if (CollectionUtils.isNotEmpty(list)) { - return ResultUtil.failure(ErrorType.PARAMS_ERROR.id(), StringUtils.join(list, ",") + "车架号重复"); - } + // 不能有重复的车架号 + List list = validData.stream().collect(Collectors.groupingBy(ExportVinExcel::getVin, Collectors.counting())) + .entrySet().stream().filter(entry -> entry.getValue() > 1).map(entry -> entry.getKey()).collect(Collectors.toList()); - // 提单进行分组 - Map> collect = validData.stream() - .collect(Collectors.groupingBy(item -> item.getBillNo(), Collectors.toList())); - collect.entrySet().forEach(item -> { - String billNo = item.getKey(); - // 找到是否存在船名,航次,提单号的表头 - - CustomerExportIn exportIn = customerExportInService.getOne(new LambdaQueryWrapper() - .eq(CustomerExportIn::getId, vinList.getId()) - .eq(CustomerExportIn::getBillNum, billNo)); - if (exportIn == null) { - noShipList.addAll(item.getValue()); - return; + if (CollectionUtils.isNotEmpty(list)) { + return ResultUtil.failure(ErrorType.PARAMS_ERROR.id(), StringUtils.join(list, ",") + "车架号重复"); } - if (exportIn.getQuantity() != item.getValue().size()) { - noNumList.addAll(item.getValue()); - return; - } - List cargos = item.getValue().stream().map(p -> { - CustomerExportInCargo cargo = new CustomerExportInCargo(); - cargo.setExportInId(exportIn.getId()); - cargo.setBrand(exportIn.getBrand()); - cargo.setBrandId(exportIn.getBrandId()); - cargo.setCargoType(0); - cargo.setVin(p.getVin()); - cargo.setTermcd(exportIn.getPortAreaId()); - cargo.setVinStatus(1); - return cargo; - }).collect(Collectors.toList()); - List vins = cargos.stream().map(p -> p.getVin()).collect(Collectors.toList()); + // 提单进行分组 + Map> collect = validData.stream() + .collect(Collectors.groupingBy(item -> item.getBillNo(), Collectors.toList())); + collect.entrySet().forEach(item -> { + String billNo = item.getKey(); + // 找到是否存在船名,航次,提单号的表头 - // 验证哪些车架号已经存在了 - List existCargos = customerExportInCargoService.list(new LambdaQueryWrapper().in(CustomerExportInCargo::getVin, vins)); - List existVins = existCargos.stream().map(p -> p.getVin()).collect(Collectors.toList()); - - if (CollectionUtils.isNotEmpty(existCargos)) { - // 查询出对应的港区 - List sList = customerExportInService.lambdaQuery().in(CustomerExportIn::getId, existCargos.stream().map(ss -> ss.getExportInId()).collect(Collectors.toList())).list(); - Map portAreaMap = sList.stream().collect(Collectors.toMap(CustomerExportIn::getId, CustomerExportIn::getPortAreaId)); - // 通过接口再次验证 - List req = existCargos.stream().map(ss -> { - CheckVinReq v = new CheckVinReq(); - v.setIsRepetition(true); - v.setPamId(portAreaMap.get(ss.getExportInId())); - v.setVinCode(ss.getVin()); - return v; + CustomerExportIn exportIn = customerExportInService.getOne(new LambdaQueryWrapper() + .eq(CustomerExportIn::getId, vinList.getId()) + .eq(CustomerExportIn::getBillNum, billNo)); + if (exportIn == null) { + noShipList.addAll(item.getValue()); + return; + } + if (exportIn.getQuantity() != item.getValue().size()) { + noNumList.addAll(item.getValue()); + return; + } + List cargos = item.getValue().stream().map(p -> { + CustomerExportInCargo cargo = new CustomerExportInCargo(); + cargo.setExportInId(exportIn.getId()); + cargo.setBrand(exportIn.getBrand()); + cargo.setBrandId(exportIn.getBrandId()); + cargo.setCargoType(0); + cargo.setVin(p.getVin()); + cargo.setTermcd(exportIn.getPortAreaId()); + cargo.setVinStatus(1); + return cargo; }).collect(Collectors.toList()); - List rst = shpApi.checkVinRepeat(req); - existVins.clear(); - if (CollectionUtils.isNotEmpty(rst)) { - // 再次过滤出重复的 - List sCollect = rst.stream().filter(ss -> ss.getIsRepetition()).collect(Collectors.toList()); - if (CollectionUtils.isNotEmpty(sCollect)) { - existVins.addAll(sCollect.stream().map(ss -> ss.getVinCode()).collect(Collectors.toList())); + + List vins = cargos.stream().map(p -> p.getVin()).collect(Collectors.toList()); + + // 验证哪些车架号已经存在了 + List existCargos = customerExportInCargoService.list(new LambdaQueryWrapper().in(CustomerExportInCargo::getVin, vins)); + List existVins = existCargos.stream().map(p -> p.getVin()).collect(Collectors.toList()); + + if (CollectionUtils.isNotEmpty(existCargos)) { + // 查询出对应的港区 + List sList = customerExportInService.lambdaQuery().in(CustomerExportIn::getId, existCargos.stream().map(ss -> ss.getExportInId()).collect(Collectors.toList())).list(); + Map portAreaMap = sList.stream().collect(Collectors.toMap(CustomerExportIn::getId, CustomerExportIn::getPortAreaId)); + // 通过接口再次验证 + List req = existCargos.stream().map(ss -> { + CheckVinReq v = new CheckVinReq(); + v.setIsRepetition(true); + v.setPamId(portAreaMap.get(ss.getExportInId())); + v.setVinCode(ss.getVin()); + return v; + }).collect(Collectors.toList()); + List rst = shpApi.checkVinRepeat(req); + existVins.clear(); + if (CollectionUtils.isNotEmpty(rst)) { + // 再次过滤出重复的 + List sCollect = rst.stream().filter(ss -> ss.getIsRepetition()).collect(Collectors.toList()); + if (CollectionUtils.isNotEmpty(sCollect)) { + existVins.addAll(sCollect.stream().map(ss -> ss.getVinCode()).collect(Collectors.toList())); + } } } + + List existData = item.getValue().stream().filter(p -> existVins.contains(p.getVin())).collect(Collectors.toList()); + exists.addAll(existData); + + successList.addAll(item.getValue().stream().filter(p -> !existVins.contains(p.getVin())).collect(Collectors.toList())); + + List saveCargos = cargos.stream().filter(p -> !existVins.contains(p.getVin())).collect(Collectors.toList()); + if (CollectionUtils.isNotEmpty(saveCargos)) { // 保存新记录 + customerExportInCargoService.lambdaUpdate().eq(CustomerExportInCargo::getExportInId, exportIn.getId()).remove(); // 移除以前的 + customerExportInCargoService.saveBatch(saveCargos); + } + }); + + List rst = new ArrayList<>(); + + if (CollectionUtils.isNotEmpty(successList)) { + rst.addAll(successList.stream().map(item -> { + JSONObject o = JSONObject.from(item); + o.put("status", "导入成功"); + return o; + }).collect(Collectors.toList())); } - List existData = item.getValue().stream().filter(p -> existVins.contains(p.getVin())).collect(Collectors.toList()); - exists.addAll(existData); - - successList.addAll(item.getValue().stream().filter(p -> !existVins.contains(p.getVin())).collect(Collectors.toList())); - - List saveCargos = cargos.stream().filter(p -> !existVins.contains(p.getVin())).collect(Collectors.toList()); - if (CollectionUtils.isNotEmpty(saveCargos)) { // 保存新记录 - customerExportInCargoService.lambdaUpdate().eq(CustomerExportInCargo::getExportInId, exportIn.getId()).remove(); // 移除以前的 - customerExportInCargoService.saveBatch(saveCargos); + if (CollectionUtils.isNotEmpty(noShipList)) { + rst.addAll(noShipList.stream().map(item -> { + JSONObject o = JSONObject.from(item); + o.put("status", "提单号不匹配, 未导入"); + return o; + }).collect(Collectors.toList())); } - }); - List rst = new ArrayList<>(); + if (CollectionUtils.isNotEmpty(noNumList)) { + rst.addAll(noNumList.stream().map(item -> { + JSONObject o = JSONObject.from(item); + o.put("status", "数量不一致, 未导入"); + return o; + }).collect(Collectors.toList())); + } - if (CollectionUtils.isNotEmpty(successList)) { - rst.addAll(successList.stream().map(item -> { - JSONObject o = JSONObject.from(item); - o.put("status", "导入成功"); - return o; - }).collect(Collectors.toList())); + if (CollectionUtils.isNotEmpty(exists)) { + rst.addAll(exists.stream().map(item -> { + JSONObject o = JSONObject.from(item); + o.put("status", "数据已存在, 未导入"); + return o; + }).collect(Collectors.toList())); + } + + return ResultUtil.success(rst); + } catch (Exception e) { + log.error("错误信息", e); + return ResultUtil.failure(ErrorType.PARAMS_ERROR.id(), e.getMessage()); } - if (CollectionUtils.isNotEmpty(noShipList)) { - rst.addAll(noShipList.stream().map(item -> { - JSONObject o = JSONObject.from(item); - o.put("status", "提单号不匹配, 未导入"); - return o; - }).collect(Collectors.toList())); - } - - if (CollectionUtils.isNotEmpty(noNumList)) { - rst.addAll(noNumList.stream().map(item -> { - JSONObject o = JSONObject.from(item); - o.put("status", "数量不一致, 未导入"); - return o; - }).collect(Collectors.toList())); - } - - if (CollectionUtils.isNotEmpty(exists)) { - rst.addAll(exists.stream().map(item -> { - JSONObject o = JSONObject.from(item); - o.put("status", "数据已存在, 未导入"); - return o; - }).collect(Collectors.toList())); - } - - return ResultUtil.success(rst); } @ApiOperation("车架号导入模板下载") @@ -1799,165 +1804,170 @@ public class ExportInHandler implements BaseHandler { @ApiOperation("车架号导入(文件形式)") @PostMapping("/import-vin") - public Result> importVin(@RequestParam(required = false) @NotNull(message = "出口进场ID不能为空") Long id, MultipartFile file) throws IOException { - CustomerExportIn exportIn = customerExportInService.getById(id); + public Result> importVin(@RequestParam(required = false) @NotNull(message = "出口进场ID不能为空") Long id, MultipartFile file) { + try { + CustomerExportIn exportIn = customerExportInService.getById(id); - List havePlans = openApi.haveShipPlan(exportIn.getVoyageId()); - if (exportIn != null && CollectionUtils.isNotEmpty(havePlans)) { - Boolean c = havePlans.contains(exportIn.getId()); - if (c) { - return ResultUtil.failure(ErrorType.PARAMS_ERROR.id(), "已生成装船计划,不能导入"); - } - } - - // 所有读取的数据 - List dataList = new ArrayList<>(); - - // 有错误的数据 - List errorDataList = new ArrayList<>(); - - // 验证成功的数据 - List successDataList = new ArrayList<>(); - - // 要存储的数据 - List saveCargos = new ArrayList<>(); - - EasyExcel.read(file.getInputStream(), ExportVinExcel.class, new ReadExcelListener() { - @Override - protected void saveData(List list) { // 缓存数据 - dataList.addAll(list); - } - }).sheet().doRead(); - - ValidatorFactory vf = Validation.buildDefaultValidatorFactory(); - Validator validator = vf.getValidator(); - - List validData = new ArrayList<>(); - - // 数据验证 - dataList.stream().forEach(item -> { - Set> 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); - } - - // 是否有重复的车架号 - List repeat = validData.stream().collect(Collectors.groupingBy(ExportVinExcel::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); - } - - // 提单进行分组 - Map> collect = validData.stream() - .collect(Collectors.groupingBy(item -> item.getBillNo(), Collectors.toList())); - - collect.entrySet().forEach(item -> { - // 找到是否存在船名,航次,提单号的表头 - if (!StringUtils.equals(exportIn.getBillNum(), item.getKey())) { - errorDataList.addAll(item.getValue().stream().map(p -> { - JSONObject o = JSONObject.from(p); - o.put("status", "提单号不匹配"); - return o; - }).collect(Collectors.toList())); - return; - } - - if (exportIn.getQuantity() != item.getValue().size()) { - errorDataList.addAll(item.getValue().stream().map(p -> { - JSONObject o = JSONObject.from(p); - o.put("status", "数量不一致"); - return o; - }).collect(Collectors.toList())); - return; - } - - List cargos = item.getValue().stream().map(p -> { - CustomerExportInCargo cargo = new CustomerExportInCargo(); - cargo.setExportInId(exportIn.getId()); - cargo.setBrand(exportIn.getBrand()); - cargo.setBrandId(exportIn.getBrandId()); - cargo.setCargoType(0); - cargo.setVin(p.getVin()); - cargo.setTermcd(exportIn.getPortAreaId()); - cargo.setVinStatus(1); - return cargo; - }).collect(Collectors.toList()); - - List vins = cargos.stream().map(p -> p.getVin()).collect(Collectors.toList()); - - // 验证哪些车架号已经存在了 - List existCargos = customerExportInCargoService.list(new LambdaQueryWrapper().in(CustomerExportInCargo::getVin, vins).ne(CustomerExportInCargo::getExportInId, exportIn.getId())); - List existVins = existCargos.stream().map(p -> p.getVin()).collect(Collectors.toList()); - - if (CollectionUtils.isNotEmpty(existCargos)) { - // 查询出对应的港区 - List sList = customerExportInService.lambdaQuery().in(CustomerExportIn::getId, existCargos.stream().map(ss -> ss.getExportInId()).collect(Collectors.toList())).list(); - Map portAreaMap = sList.stream().collect(Collectors.toMap(CustomerExportIn::getId, CustomerExportIn::getPortAreaId)); - // 通过接口再次验证 - List req = existCargos.stream().map(ss -> { - CheckVinReq v = new CheckVinReq(); - v.setIsRepetition(true); - v.setPamId(portAreaMap.get(ss.getExportInId())); - v.setVinCode(ss.getVin()); - return v; - }).collect(Collectors.toList()); - List rst = shpApi.checkVinRepeat(req); - existVins.clear(); - if (CollectionUtils.isNotEmpty(rst)) { - // 再次过滤出重复的 - List sCollect = rst.stream().filter(ss -> ss.getIsRepetition()).collect(Collectors.toList()); - if (CollectionUtils.isNotEmpty(sCollect)) { - existVins.addAll(sCollect.stream().map(ss -> ss.getVinCode()).collect(Collectors.toList())); - } + List havePlans = openApi.haveShipPlan(exportIn.getVoyageId()); + if (exportIn != null && CollectionUtils.isNotEmpty(havePlans)) { + Boolean c = havePlans.contains(exportIn.getId()); + if (c) { + return ResultUtil.failure(ErrorType.PARAMS_ERROR.id(), "已生成装船计划,不能导入"); } } - List existData = item.getValue().stream().filter(p -> existVins.contains(p.getVin())).collect(Collectors.toList()); - if (CollectionUtils.isNotEmpty(existData)) { - errorDataList.addAll(existData.stream().map(p -> { - JSONObject o = JSONObject.from(p); - o.put("status", "数据已存在"); - return o; - }).collect(Collectors.toList())); + // 所有读取的数据 + List dataList = new ArrayList<>(); + + // 有错误的数据 + List errorDataList = new ArrayList<>(); + + // 验证成功的数据 + List successDataList = new ArrayList<>(); + + // 要存储的数据 + List saveCargos = new ArrayList<>(); + + EasyExcel.read(file.getInputStream(), ExportVinExcel.class, new ReadExcelListener() { + @Override + protected void saveData(List list) { // 缓存数据 + dataList.addAll(list); + } + }).sheet().doRead(); + + ValidatorFactory vf = Validation.buildDefaultValidatorFactory(); + Validator validator = vf.getValidator(); + + List validData = new ArrayList<>(); + + // 数据验证 + dataList.stream().forEach(item -> { + Set> 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); } - successDataList.addAll(item.getValue().stream().filter(p -> !existVins.contains(p.getVin())) - .map(p -> { + // 是否有重复的车架号 + List repeat = validData.stream().collect(Collectors.groupingBy(ExportVinExcel::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); + } + + // 提单进行分组 + Map> collect = validData.stream() + .collect(Collectors.groupingBy(item -> item.getBillNo(), Collectors.toList())); + + collect.entrySet().forEach(item -> { + // 找到是否存在船名,航次,提单号的表头 + if (!StringUtils.equals(exportIn.getBillNum(), item.getKey())) { + errorDataList.addAll(item.getValue().stream().map(p -> { JSONObject o = JSONObject.from(p); - o.put("status", "数据验证成功"); + o.put("status", "提单号不匹配"); return o; }).collect(Collectors.toList())); + return; + } - saveCargos.addAll(cargos.stream().filter(p -> !existVins.contains(p.getVin())).collect(Collectors.toList())); - }); + if (exportIn.getQuantity() != item.getValue().size()) { + errorDataList.addAll(item.getValue().stream().map(p -> { + JSONObject o = JSONObject.from(p); + o.put("status", "数量不一致"); + return o; + }).collect(Collectors.toList())); + return; + } - if (CollectionUtils.isNotEmpty(errorDataList)) { - errorDataList.addAll(successDataList); - return ResultUtil.failure(ErrorType.PARAMS_ERROR.id(), "导入失败", errorDataList); + List cargos = item.getValue().stream().map(p -> { + CustomerExportInCargo cargo = new CustomerExportInCargo(); + cargo.setExportInId(exportIn.getId()); + cargo.setBrand(exportIn.getBrand()); + cargo.setBrandId(exportIn.getBrandId()); + cargo.setCargoType(0); + cargo.setVin(p.getVin()); + cargo.setTermcd(exportIn.getPortAreaId()); + cargo.setVinStatus(1); + return cargo; + }).collect(Collectors.toList()); + + List vins = cargos.stream().map(p -> p.getVin()).collect(Collectors.toList()); + + // 验证哪些车架号已经存在了 + List existCargos = customerExportInCargoService.list(new LambdaQueryWrapper().in(CustomerExportInCargo::getVin, vins).ne(CustomerExportInCargo::getExportInId, exportIn.getId())); + List existVins = existCargos.stream().map(p -> p.getVin()).collect(Collectors.toList()); + + if (CollectionUtils.isNotEmpty(existCargos)) { + // 查询出对应的港区 + List sList = customerExportInService.lambdaQuery().in(CustomerExportIn::getId, existCargos.stream().map(ss -> ss.getExportInId()).collect(Collectors.toList())).list(); + Map portAreaMap = sList.stream().collect(Collectors.toMap(CustomerExportIn::getId, CustomerExportIn::getPortAreaId)); + // 通过接口再次验证 + List req = existCargos.stream().map(ss -> { + CheckVinReq v = new CheckVinReq(); + v.setIsRepetition(true); + v.setPamId(portAreaMap.get(ss.getExportInId())); + v.setVinCode(ss.getVin()); + return v; + }).collect(Collectors.toList()); + List rst = shpApi.checkVinRepeat(req); + existVins.clear(); + if (CollectionUtils.isNotEmpty(rst)) { + // 再次过滤出重复的 + List sCollect = rst.stream().filter(ss -> ss.getIsRepetition()).collect(Collectors.toList()); + if (CollectionUtils.isNotEmpty(sCollect)) { + existVins.addAll(sCollect.stream().map(ss -> ss.getVinCode()).collect(Collectors.toList())); + } + } + } + + List existData = item.getValue().stream().filter(p -> existVins.contains(p.getVin())).collect(Collectors.toList()); + if (CollectionUtils.isNotEmpty(existData)) { + errorDataList.addAll(existData.stream().map(p -> { + JSONObject o = JSONObject.from(p); + o.put("status", "数据已存在"); + return o; + }).collect(Collectors.toList())); + } + + successDataList.addAll(item.getValue().stream().filter(p -> !existVins.contains(p.getVin())) + .map(p -> { + JSONObject o = JSONObject.from(p); + o.put("status", "数据验证成功"); + return o; + }).collect(Collectors.toList())); + + saveCargos.addAll(cargos.stream().filter(p -> !existVins.contains(p.getVin())).collect(Collectors.toList())); + }); + + if (CollectionUtils.isNotEmpty(errorDataList)) { + errorDataList.addAll(successDataList); + return ResultUtil.failure(ErrorType.PARAMS_ERROR.id(), "导入失败", errorDataList); + } + + if (CollectionUtils.isNotEmpty(saveCargos)) { + // 删除原有的车架号 + customerExportInCargoService.lambdaUpdate().eq(CustomerExportInCargo::getExportInId, exportIn.getId()).remove(); + customerExportInCargoService.saveBatch(saveCargos); + } + + return ResultUtil.success(errorDataList); + } catch (Exception e) { + log.error("错误信息", e); + return ResultUtil.failure(ErrorType.PROGRAM_ERROR.id(), e.getMessage()); } - - if (CollectionUtils.isNotEmpty(saveCargos)) { - // 删除原有的车架号 - customerExportInCargoService.lambdaUpdate().eq(CustomerExportInCargo::getExportInId, exportIn.getId()).remove(); - customerExportInCargoService.saveBatch(saveCargos); - } - - return ResultUtil.success(errorDataList); } @ApiOperation("备件号导入模板下载") @@ -2001,149 +2011,154 @@ public class ExportInHandler implements BaseHandler { @ApiOperation("备件号导入") @PostMapping("/import-spare") - public Result> importSpare(@RequestParam(required = false) @NotNull(message = "出口进场ID不能为空") Long id, MultipartFile file) throws IOException { - CustomerExportIn exportIn = customerExportInService.getById(id); + public Result> importSpare(@RequestParam(required = false) @NotNull(message = "出口进场ID不能为空") Long id, MultipartFile file) { + try { + CustomerExportIn exportIn = customerExportInService.getById(id); - List havePlans = openApi.haveShipPlan(exportIn.getVoyageId()); - if (exportIn != null && CollectionUtils.isNotEmpty(havePlans)) { - Boolean c = havePlans.contains(exportIn.getId()); - if (c) { - return ResultUtil.failure(ErrorType.PARAMS_ERROR.id(), "已生成装船计划,不能导入"); - } - } - - // 所有读取的数据 - List dataList = new ArrayList<>(); - - // 有错误的数据 - List errorDataList = new ArrayList<>(); - - // 验证成功的数据 - List successDataList = new ArrayList<>(); - - // 要存储的数据 - List saveCargos = new ArrayList<>(); - - EasyExcel.read(file.getInputStream(), ExportInSpareExcel.class, new ReadExcelListener() { - @Override - protected void saveData(List list) { // 缓存数据 - dataList.addAll(list); - } - }).sheet().doRead(); - - ValidatorFactory vf = Validation.buildDefaultValidatorFactory(); - Validator validator = vf.getValidator(); - - List validData = new ArrayList<>(); - - // 数据验证 - dataList.stream().forEach(item -> { - Set> 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); - } - - // 是否有重复的车架号 - List repeat = validData.stream().collect(Collectors.groupingBy(ExportInSpareExcel::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); - } - - if (exportIn.getQuantity() != validData.size()) { - errorDataList.addAll(validData.stream().map(p -> { - JSONObject o = JSONObject.from(p); - o.put("status", "数量不一致"); - return o; - }).collect(Collectors.toList())); - return ResultUtil.failure(ErrorType.PARAMS_ERROR.id(), "导入失败", errorDataList); - } - - List cargos = validData.stream().map(p -> { - CustomerExportInCargo cargo = new CustomerExportInCargo(); - cargo.setExportInId(exportIn.getId()); - cargo.setBrand(exportIn.getBrand()); - cargo.setBrandId(exportIn.getBrandId()); - cargo.setCargoType(1); - cargo.setVin(p.getVin()); - cargo.setTermcd(exportIn.getPortAreaId()); - cargo.setVinStatus(1); - return cargo; - }).collect(Collectors.toList()); - - List vins = cargos.stream().map(p -> p.getVin()).collect(Collectors.toList()); - - // 验证哪些车架号已经存在了 - List existCargos = customerExportInCargoService.list(new LambdaQueryWrapper().in(CustomerExportInCargo::getVin, vins).ne(CustomerExportInCargo::getExportInId, exportIn.getId())); - List existVins = existCargos.stream().map(p -> p.getVin()).collect(Collectors.toList()); - - if (CollectionUtils.isNotEmpty(existCargos)) { - // 查询出对应的港区 - List sList = customerExportInService.lambdaQuery().in(CustomerExportIn::getId, existCargos.stream().map(ss -> ss.getExportInId()).collect(Collectors.toList())).list(); - Map portAreaMap = sList.stream().collect(Collectors.toMap(CustomerExportIn::getId, CustomerExportIn::getPortAreaId)); - // 通过接口再次验证 - List req = existCargos.stream().map(ss -> { - CheckVinReq v = new CheckVinReq(); - v.setIsRepetition(true); - v.setPamId(portAreaMap.get(ss.getExportInId())); - v.setVinCode(ss.getVin()); - return v; - }).collect(Collectors.toList()); - List rst = shpApi.checkVinRepeat(req); - existVins.clear(); - if (CollectionUtils.isNotEmpty(rst)) { - // 再次过滤出重复的 - List sCollect = rst.stream().filter(ss -> ss.getIsRepetition()).collect(Collectors.toList()); - if (CollectionUtils.isNotEmpty(sCollect)) { - existVins.addAll(sCollect.stream().map(ss -> ss.getVinCode()).collect(Collectors.toList())); + List havePlans = openApi.haveShipPlan(exportIn.getVoyageId()); + if (exportIn != null && CollectionUtils.isNotEmpty(havePlans)) { + Boolean c = havePlans.contains(exportIn.getId()); + if (c) { + return ResultUtil.failure(ErrorType.PARAMS_ERROR.id(), "已生成装船计划,不能导入"); } } - } - List existData = validData.stream().filter(p -> existVins.contains(p.getVin())).collect(Collectors.toList()); - if (CollectionUtils.isNotEmpty(existData)) { - errorDataList.addAll(existData.stream().map(p -> { - JSONObject o = JSONObject.from(p); - o.put("status", "数据已存在"); - return o; - }).collect(Collectors.toList())); - } + // 所有读取的数据 + List dataList = new ArrayList<>(); - successDataList.addAll(validData.stream().filter(p -> !existVins.contains(p.getVin())) - .map(p -> { + // 有错误的数据 + List errorDataList = new ArrayList<>(); + + // 验证成功的数据 + List successDataList = new ArrayList<>(); + + // 要存储的数据 + List saveCargos = new ArrayList<>(); + + EasyExcel.read(file.getInputStream(), ExportInSpareExcel.class, new ReadExcelListener() { + @Override + protected void saveData(List list) { // 缓存数据 + dataList.addAll(list); + } + }).sheet().doRead(); + + ValidatorFactory vf = Validation.buildDefaultValidatorFactory(); + Validator validator = vf.getValidator(); + + List validData = new ArrayList<>(); + + // 数据验证 + dataList.stream().forEach(item -> { + Set> 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); + } + + // 是否有重复的车架号 + List repeat = validData.stream().collect(Collectors.groupingBy(ExportInSpareExcel::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", "数据验证成功"); + o.put("status", "车架号重复"); return o; }).collect(Collectors.toList())); + return ResultUtil.failure(ErrorType.PARAMS_ERROR.id(), "导入失败", errorDataList); + } - saveCargos.addAll(cargos.stream().filter(p -> !existVins.contains(p.getVin())).collect(Collectors.toList())); + if (exportIn.getQuantity() != validData.size()) { + errorDataList.addAll(validData.stream().map(p -> { + JSONObject o = JSONObject.from(p); + o.put("status", "数量不一致"); + return o; + }).collect(Collectors.toList())); + return ResultUtil.failure(ErrorType.PARAMS_ERROR.id(), "导入失败", errorDataList); + } - if (CollectionUtils.isNotEmpty(errorDataList)) { - errorDataList.addAll(successDataList); - return ResultUtil.failure(ErrorType.PARAMS_ERROR.id(), "导入失败", errorDataList); + List cargos = validData.stream().map(p -> { + CustomerExportInCargo cargo = new CustomerExportInCargo(); + cargo.setExportInId(exportIn.getId()); + cargo.setBrand(exportIn.getBrand()); + cargo.setBrandId(exportIn.getBrandId()); + cargo.setCargoType(1); + cargo.setVin(p.getVin()); + cargo.setTermcd(exportIn.getPortAreaId()); + cargo.setVinStatus(1); + return cargo; + }).collect(Collectors.toList()); + + List vins = cargos.stream().map(p -> p.getVin()).collect(Collectors.toList()); + + // 验证哪些车架号已经存在了 + List existCargos = customerExportInCargoService.list(new LambdaQueryWrapper().in(CustomerExportInCargo::getVin, vins).ne(CustomerExportInCargo::getExportInId, exportIn.getId())); + List existVins = existCargos.stream().map(p -> p.getVin()).collect(Collectors.toList()); + + if (CollectionUtils.isNotEmpty(existCargos)) { + // 查询出对应的港区 + List sList = customerExportInService.lambdaQuery().in(CustomerExportIn::getId, existCargos.stream().map(ss -> ss.getExportInId()).collect(Collectors.toList())).list(); + Map portAreaMap = sList.stream().collect(Collectors.toMap(CustomerExportIn::getId, CustomerExportIn::getPortAreaId)); + // 通过接口再次验证 + List req = existCargos.stream().map(ss -> { + CheckVinReq v = new CheckVinReq(); + v.setIsRepetition(true); + v.setPamId(portAreaMap.get(ss.getExportInId())); + v.setVinCode(ss.getVin()); + return v; + }).collect(Collectors.toList()); + List rst = shpApi.checkVinRepeat(req); + existVins.clear(); + if (CollectionUtils.isNotEmpty(rst)) { + // 再次过滤出重复的 + List sCollect = rst.stream().filter(ss -> ss.getIsRepetition()).collect(Collectors.toList()); + if (CollectionUtils.isNotEmpty(sCollect)) { + existVins.addAll(sCollect.stream().map(ss -> ss.getVinCode()).collect(Collectors.toList())); + } + } + } + + List existData = validData.stream().filter(p -> existVins.contains(p.getVin())).collect(Collectors.toList()); + if (CollectionUtils.isNotEmpty(existData)) { + errorDataList.addAll(existData.stream().map(p -> { + JSONObject o = JSONObject.from(p); + o.put("status", "数据已存在"); + return o; + }).collect(Collectors.toList())); + } + + successDataList.addAll(validData.stream().filter(p -> !existVins.contains(p.getVin())) + .map(p -> { + JSONObject o = JSONObject.from(p); + o.put("status", "数据验证成功"); + return o; + }).collect(Collectors.toList())); + + saveCargos.addAll(cargos.stream().filter(p -> !existVins.contains(p.getVin())).collect(Collectors.toList())); + + if (CollectionUtils.isNotEmpty(errorDataList)) { + errorDataList.addAll(successDataList); + return ResultUtil.failure(ErrorType.PARAMS_ERROR.id(), "导入失败", errorDataList); + } + + if (CollectionUtils.isNotEmpty(saveCargos)) { + // 删除原有的车架号 + customerExportInCargoService.lambdaUpdate().eq(CustomerExportInCargo::getExportInId, exportIn.getId()).remove(); + customerExportInCargoService.saveBatch(saveCargos); + } + + return ResultUtil.success(errorDataList); + } catch (Exception e) { + log.error("错误信息", e); + return ResultUtil.failure(ErrorType.PARAMS_ERROR.id(), e.getMessage()); } - - if (CollectionUtils.isNotEmpty(saveCargos)) { - // 删除原有的车架号 - customerExportInCargoService.lambdaUpdate().eq(CustomerExportInCargo::getExportInId, exportIn.getId()).remove(); - customerExportInCargoService.saveBatch(saveCargos); - } - - return ResultUtil.success(errorDataList); } @ApiOperation("整船清单导入模板下载") @@ -2189,7 +2204,7 @@ public class ExportInHandler implements BaseHandler { @PostMapping("/import-to-add") public Result> upload( @ApiParam(name = "操作方式, 0为前端,1为审核端") @RequestParam(required = false, defaultValue = "0") String type, - MultipartFile file) throws IOException { + MultipartFile file) { // 所有读取的数据 List dataList = new ArrayList<>(); @@ -2202,61 +2217,63 @@ public class ExportInHandler implements BaseHandler { // 需要清空的 List ids = new ArrayList<>(); - // 要保存到数据库的数据 - List saveCargos = new ArrayList<>(); + try { - EasyExcel.read(file.getInputStream(), ExportInExcel.class, new ReadExcelListener() { - @Override - protected void saveData(List list) { // 保存数据 - dataList.addAll(list); + // 要保存到数据库的数据 + List saveCargos = new ArrayList<>(); + + EasyExcel.read(file.getInputStream(), ExportInExcel.class, new ReadExcelListener() { + @Override + protected void saveData(List list) { // 保存数据 + dataList.addAll(list); + } + }).sheet().doRead(); + + ValidatorFactory vf = Validation.buildDefaultValidatorFactory(); + Validator validator = vf.getValidator(); + + List validData = new ArrayList<>(); + + // 数据验证 + dataList.stream().forEach(item -> { + Set> 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(); + // 模型去空格处理 + validData.stream().forEach(item -> item.setModels(StringUtils.trim(item.getModels()))); - List validData = new ArrayList<>(); + // 首先按对船名,般次,提单进行分组 + Map> collect = validData.stream() + .collect(Collectors.groupingBy(item -> StringUtils.joinWith("#$#", item.getShipName(), item.getVoyage(), item.getBillNo(), item.getBrand(), item.getModels()), Collectors.toList())); - // 数据验证 - dataList.stream().forEach(item -> { - Set> 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); - } - }); + collect.entrySet().forEach(item -> { + String[] keys = StringUtils.split(item.getKey(), "#$#"); + String shipName = keys[0]; // 船名 + String voyage = keys[1]; // 航次 + String billNo = keys[2]; // 提单号 + String brand = keys[3]; // 品牌 + String models = keys[4]; // 型号 + // 找到是否存在船名,航次,提单号的表头 - // 所有数据验证都不通过 - if (CollectionUtils.isEmpty(validData)) { - return ResultUtil.failure(ErrorType.PARAMS_ERROR.id(), "导入失败", errorDataList); - } - - // 模型去空格处理 - validData.stream().forEach(item -> item.setModels(StringUtils.trim(item.getModels()))); - - // 首先按对船名,般次,提单进行分组 - Map> collect = validData.stream() - .collect(Collectors.groupingBy(item -> StringUtils.joinWith("#$#", item.getShipName(), item.getVoyage(), item.getBillNo(), item.getBrand(), item.getModels()), Collectors.toList())); - - collect.entrySet().forEach(item -> { - String[] keys = StringUtils.split(item.getKey(), "#$#"); - String shipName = keys[0]; // 船名 - String voyage = keys[1]; // 航次 - String billNo = keys[2]; // 提单号 - String brand = keys[3]; // 品牌 - String models = keys[4]; // 型号 - // 找到是否存在船名,航次,提单号的表头 - - LambdaQueryWrapper cQuery = new LambdaQueryWrapper<>(); - cQuery.eq(CustomerExportIn::getShipName, shipName) - .eq(CustomerExportIn::getVoyage, voyage) - .eq(CustomerExportIn::getBillNum, billNo) - .eq(CustomerExportIn::getBrand, brand) - .eq(CustomerExportIn::getModels, models) - .ne(CustomerExportIn::getCartType, "备件"); // 备件的不允许整船导入 + LambdaQueryWrapper cQuery = new LambdaQueryWrapper<>(); + cQuery.eq(CustomerExportIn::getShipName, shipName) + .eq(CustomerExportIn::getVoyage, voyage) + .eq(CustomerExportIn::getBillNum, billNo) + .eq(CustomerExportIn::getBrand, brand) + .eq(CustomerExportIn::getModels, models) + .ne(CustomerExportIn::getCartType, "备件"); // 备件的不允许整船导入 // if (StringUtils.equals(type, "0")) { // cQuery.and((wrapper) -> { // wrapper.eq(CustomerExportIn::getCheckStatus, AuditEnum.SUBMIT) @@ -2271,52 +2288,52 @@ public class ExportInHandler implements BaseHandler { // }); // } - // 同一个型号可能存在多个进港计划 + // 同一个型号可能存在多个进港计划 - List exportInList = customerExportInService.list(cQuery); + List exportInList = customerExportInService.list(cQuery); - if (CollectionUtils.isEmpty(exportInList)) { // 船名,航次等不存在 - errorDataList.addAll(item.getValue().stream().map(p -> { - JSONObject o = JSONObject.from(p); - o.put("status", "船名、航次、提单号、品牌或型号不存在"); - return o; - }).collect(Collectors.toList())); - return; - } - - // 计划总数量 - int planQuantity = exportInList.stream().mapToInt(p -> p.getQuantity()).sum(); - - // 找到和本次数量一致的 -// Optional first = exportInList.stream().filter(p -> p.getQuantity() == item.getValue().size()).findFirst(); - - CustomerExportIn exportIn = exportInList.get(0); - - if (planQuantity != item.getValue().size()) { - errorDataList.addAll(item.getValue().stream().map(p -> { - JSONObject o = JSONObject.from(p); - o.put("status", "数量不一致不允许导入"); - return o; - }).collect(Collectors.toList())); - return; - } - - ids.addAll(exportInList.stream().map(p -> p.getId()).collect(Collectors.toList())); -// ids.add(exportIn.getId()); - - // 已生成装船计划,不能导入 - List havePlans = openApi.haveShipPlan(exportIn.getVoyageId()); - if (exportIn != null && CollectionUtils.isNotEmpty(havePlans)) { - Boolean c = havePlans.contains(exportIn.getId()); - if (c) { + if (CollectionUtils.isEmpty(exportInList)) { // 船名,航次等不存在 errorDataList.addAll(item.getValue().stream().map(p -> { JSONObject o = JSONObject.from(p); - o.put("status", "已生成装船计划,不能导入"); + o.put("status", "船名、航次、提单号、品牌或型号不存在"); return o; }).collect(Collectors.toList())); return; } - } + + // 计划总数量 + int planQuantity = exportInList.stream().mapToInt(p -> p.getQuantity()).sum(); + + // 找到和本次数量一致的 +// Optional first = exportInList.stream().filter(p -> p.getQuantity() == item.getValue().size()).findFirst(); + + CustomerExportIn exportIn = exportInList.get(0); + + if (planQuantity != item.getValue().size()) { + errorDataList.addAll(item.getValue().stream().map(p -> { + JSONObject o = JSONObject.from(p); + o.put("status", "数量不一致不允许导入"); + return o; + }).collect(Collectors.toList())); + return; + } + + ids.addAll(exportInList.stream().map(p -> p.getId()).collect(Collectors.toList())); +// ids.add(exportIn.getId()); + + // 已生成装船计划,不能导入 + List havePlans = openApi.haveShipPlan(exportIn.getVoyageId()); + if (exportIn != null && CollectionUtils.isNotEmpty(havePlans)) { + Boolean c = havePlans.contains(exportIn.getId()); + if (c) { + errorDataList.addAll(item.getValue().stream().map(p -> { + JSONObject o = JSONObject.from(p); + o.put("status", "已生成装船计划,不能导入"); + return o; + }).collect(Collectors.toList())); + return; + } + } // if (exportIn.getCheckStatus() == AuditEnum.AUDIT_PASS) { // 审核 // errorDataList.addAll(item.getValue().stream().map(p -> { @@ -2335,24 +2352,24 @@ public class ExportInHandler implements BaseHandler { // }).collect(Collectors.toList())); // return; // } - // 按计划中的件数进行VIN的分配 - int j = 0; - List cargos = new ArrayList<>(); - for (CustomerExportIn e : exportInList) { - int n = e.getQuantity(); - for (int i= 0; i < n; i++) { - CustomerExportInCargo cargo = new CustomerExportInCargo(); - cargo.setBrandId(e.getBrandId()); - cargo.setBrand(e.getBrand()); - cargo.setExportInId(e.getId()); - cargo.setCargoType(0); - cargo.setVin(item.getValue().get(j++).getVin()); - cargo.setTermcd(e.getPortAreaId()); - cargo.setVinStatus(1); + // 按计划中的件数进行VIN的分配 + int j = 0; + List cargos = new ArrayList<>(); + for (CustomerExportIn e : exportInList) { + int n = e.getQuantity(); + for (int i = 0; i < n; i++) { + CustomerExportInCargo cargo = new CustomerExportInCargo(); + cargo.setBrandId(e.getBrandId()); + cargo.setBrand(e.getBrand()); + cargo.setExportInId(e.getId()); + cargo.setCargoType(0); + cargo.setVin(item.getValue().get(j++).getVin()); + cargo.setTermcd(e.getPortAreaId()); + cargo.setVinStatus(1); - cargos.add(cargo); + cargos.add(cargo); + } } - } // List cargos = item.getValue().stream().map(p -> { // CustomerExportInCargo cargo = new CustomerExportInCargo(); @@ -2366,74 +2383,78 @@ public class ExportInHandler implements BaseHandler { // return cargo; // }).collect(Collectors.toList()); - List vins = cargos.stream().map(p -> p.getVin()).collect(Collectors.toList()); + List vins = cargos.stream().map(p -> p.getVin()).collect(Collectors.toList()); - // 验证哪些车架号已经存在了 - List existCargos = customerExportInCargoService.list(new LambdaQueryWrapper().in(CustomerExportInCargo::getVin, vins).ne(CustomerExportInCargo::getExportInId, exportIn.getId())); - List existVins = existCargos.stream().map(p -> p.getVin()).collect(Collectors.toList()); + // 验证哪些车架号已经存在了 + List existCargos = customerExportInCargoService.list(new LambdaQueryWrapper().in(CustomerExportInCargo::getVin, vins).ne(CustomerExportInCargo::getExportInId, exportIn.getId())); + List existVins = existCargos.stream().map(p -> p.getVin()).collect(Collectors.toList()); - if (CollectionUtils.isNotEmpty(existCargos)) { - // 查询出对应的港区 - List sList = customerExportInService.lambdaQuery().in(CustomerExportIn::getId, existCargos.stream().map(ss -> ss.getExportInId()).collect(Collectors.toList())).list(); - Map portAreaMap = sList.stream().collect(Collectors.toMap(CustomerExportIn::getId, CustomerExportIn::getPortAreaId)); - // 通过接口再次验证 - List req = existCargos.stream().map(ss -> { - CheckVinReq v = new CheckVinReq(); - v.setIsRepetition(true); - v.setPamId(portAreaMap.get(ss.getExportInId())); - v.setVinCode(ss.getVin()); - return v; - }).collect(Collectors.toList()); - List rst = shpApi.checkVinRepeat(req); - existVins.clear(); // 先清空 - if (CollectionUtils.isNotEmpty(rst)) { - // 再次过滤出重复的 - List sCollect = rst.stream().filter(ss -> ss.getIsRepetition()).collect(Collectors.toList()); - if (CollectionUtils.isNotEmpty(sCollect)) { - existVins.addAll(sCollect.stream().map(ss -> ss.getVinCode()).collect(Collectors.toList())); + if (CollectionUtils.isNotEmpty(existCargos)) { + // 查询出对应的港区 + List sList = customerExportInService.lambdaQuery().in(CustomerExportIn::getId, existCargos.stream().map(ss -> ss.getExportInId()).collect(Collectors.toList())).list(); + Map portAreaMap = sList.stream().collect(Collectors.toMap(CustomerExportIn::getId, CustomerExportIn::getPortAreaId)); + // 通过接口再次验证 + List req = existCargos.stream().map(ss -> { + CheckVinReq v = new CheckVinReq(); + v.setIsRepetition(true); + v.setPamId(portAreaMap.get(ss.getExportInId())); + v.setVinCode(ss.getVin()); + return v; + }).collect(Collectors.toList()); + List rst = shpApi.checkVinRepeat(req); + existVins.clear(); // 先清空 + if (CollectionUtils.isNotEmpty(rst)) { + // 再次过滤出重复的 + List sCollect = rst.stream().filter(ss -> ss.getIsRepetition()).collect(Collectors.toList()); + if (CollectionUtils.isNotEmpty(sCollect)) { + existVins.addAll(sCollect.stream().map(ss -> ss.getVinCode()).collect(Collectors.toList())); + } } } + + List existData = item.getValue().stream().filter(p -> existVins.contains(p.getVin())).collect(Collectors.toList()); + if (CollectionUtils.isNotEmpty(existData)) { + errorDataList.addAll(existData.stream().map(p -> { + JSONObject o = JSONObject.from(p); + o.put("status", "数据已存在, 未导入"); + return o; + }).collect(Collectors.toList())); + successDataList.addAll(item.getValue().stream().filter(p -> !existVins.contains(p.getVin())) + .map(p -> { + JSONObject o = JSONObject.from(p); + o.put("status", "数据验证成功"); + return o; + }).collect(Collectors.toList())); + return; + } else { + successDataList.addAll(item.getValue().stream().filter(p -> !existVins.contains(p.getVin())) + .map(p -> { + JSONObject o = JSONObject.from(p); + o.put("status", "数据验证成功"); + return o; + }).collect(Collectors.toList())); + } + + saveCargos.addAll(cargos.stream().filter(p -> !existVins.contains(p.getVin())).collect(Collectors.toList())); + + }); + + if (CollectionUtils.isNotEmpty(errorDataList)) { + errorDataList.addAll(successDataList); + return ResultUtil.failure(ErrorType.PARAMS_ERROR.id(), "导入失败", errorDataList); } - List existData = item.getValue().stream().filter(p -> existVins.contains(p.getVin())).collect(Collectors.toList()); - if (CollectionUtils.isNotEmpty(existData)) { - errorDataList.addAll(existData.stream().map(p -> { - JSONObject o = JSONObject.from(p); - o.put("status", "数据已存在, 未导入"); - return o; - }).collect(Collectors.toList())); - successDataList.addAll(item.getValue().stream().filter(p -> !existVins.contains(p.getVin())) - .map(p -> { - JSONObject o = JSONObject.from(p); - o.put("status", "数据验证成功"); - return o; - }).collect(Collectors.toList())); - return; - } else { - successDataList.addAll(item.getValue().stream().filter(p -> !existVins.contains(p.getVin())) - .map(p -> { - JSONObject o = JSONObject.from(p); - o.put("status", "数据验证成功"); - return o; - }).collect(Collectors.toList())); + if (CollectionUtils.isNotEmpty(saveCargos)) { + // 删除原有的车架号 + customerExportInCargoService.lambdaUpdate().in(CustomerExportInCargo::getExportInId, ids).remove(); + customerExportInCargoService.saveBatch(saveCargos); } - saveCargos.addAll(cargos.stream().filter(p -> !existVins.contains(p.getVin())).collect(Collectors.toList())); - - }); - - if (CollectionUtils.isNotEmpty(errorDataList)) { - errorDataList.addAll(successDataList); - return ResultUtil.failure(ErrorType.PARAMS_ERROR.id(), "导入失败", errorDataList); + return ResultUtil.success(errorDataList); + } catch (Exception e) { + log.error("错误信息", e); + return ResultUtil.failure(ErrorType.PARAMS_ERROR.id(), e.getMessage()); } - - if (CollectionUtils.isNotEmpty(saveCargos)) { - // 删除原有的车架号 - customerExportInCargoService.lambdaUpdate().in(CustomerExportInCargo::getExportInId, ids).remove(); - customerExportInCargoService.saveBatch(saveCargos); - } - - return ResultUtil.success(errorDataList); } @ApiOperation("整票备件号导入模板下载") @@ -2479,7 +2500,7 @@ public class ExportInHandler implements BaseHandler { @PostMapping("/import-to-add/spare") public Result> uploadSpare( @ApiParam(name = "操作方式, 0为前端,1为审核端") @RequestParam(required = false, defaultValue = "0") String type, - MultipartFile file) throws IOException { + MultipartFile file) { // 所有读取的数据 List dataList = new ArrayList<>(); @@ -2495,206 +2516,212 @@ public class ExportInHandler implements BaseHandler { // 要保存到数据库的数据 List saveCargos = new ArrayList<>(); - EasyExcel.read(file.getInputStream(), ExportLoadExcel.class, new ReadExcelListener() { - @Override - protected void saveData(List list) { // 保存数据 - dataList.addAll(list); - } - }).sheet().doRead(); + try { - ValidatorFactory vf = Validation.buildDefaultValidatorFactory(); - Validator validator = vf.getValidator(); + EasyExcel.read(file.getInputStream(), ExportLoadExcel.class, new ReadExcelListener() { + @Override + protected void saveData(List list) { // 保存数据 + dataList.addAll(list); + } + }).sheet().doRead(); - List validData = new ArrayList<>(); + ValidatorFactory vf = Validation.buildDefaultValidatorFactory(); + Validator validator = vf.getValidator(); - // 数据验证 - dataList.stream().forEach(item -> { - Set> 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); - } - }); + List validData = new ArrayList<>(); - // 所有数据验证都不通过 - if (CollectionUtils.isEmpty(validData)) { - return ResultUtil.failure(ErrorType.PARAMS_ERROR.id(), "导入失败", errorDataList); - } + // 数据验证 + dataList.stream().forEach(item -> { + Set> 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); + } + }); - // 首先按对船名,般次,提单进行分组 - Map> collect = validData.stream() - .collect(Collectors.groupingBy(item -> StringUtils.joinWith("#$#", item.getShipName(), item.getVoyage(), item.getBillNo(), item.getBrand()), Collectors.toList())); - - collect.entrySet().forEach(item -> { - String[] keys = StringUtils.split(item.getKey(), "#$#"); - String shipName = keys[0]; // 船名 - String voyage = keys[1]; // 航次 - String billNo = keys[2]; // 提单号 - String brand = keys[3]; // 品牌 - // 找到是否存在船名,航次,提单号的表头 - - LambdaQueryWrapper cQuery = new LambdaQueryWrapper<>(); - cQuery.eq(CustomerExportIn::getShipName, shipName) - .eq(CustomerExportIn::getVoyage, voyage) - .eq(CustomerExportIn::getBillNum, billNo) - .eq(CustomerExportIn::getBrand, brand) - .eq(CustomerExportIn::getCartType, "备件"); // 备件的不允许整船导入 - if (StringUtils.equals(type, "0")) { - cQuery.and((wrapper) -> { - wrapper.eq(CustomerExportIn::getCheckStatus, AuditEnum.SUBMIT) - .or() - .eq(CustomerExportIn::getCheckStatus, AuditEnum.AUDIT_REJECT); - }); - } else { - cQuery.and((wrapper) -> { - wrapper.eq(CustomerExportIn::getCheckStatus, AuditEnum.AUDIT) - .or() - .eq(CustomerExportIn::getCheckStatus, AuditEnum.AUDIT_REJECT); - }); + // 所有数据验证都不通过 + if (CollectionUtils.isEmpty(validData)) { + return ResultUtil.failure(ErrorType.PARAMS_ERROR.id(), "导入失败", errorDataList); } - List exportInList = customerExportInService.list(cQuery); + // 首先按对船名,般次,提单进行分组 + Map> collect = validData.stream() + .collect(Collectors.groupingBy(item -> StringUtils.joinWith("#$#", item.getShipName(), item.getVoyage(), item.getBillNo(), item.getBrand()), Collectors.toList())); - if (CollectionUtils.isEmpty(exportInList)) { // 船名,航次等不存在 - errorDataList.addAll(item.getValue().stream().map(p -> { - JSONObject o = JSONObject.from(p); - o.put("status", "船名、航次、提单号、品牌不存在"); - return o; - }).collect(Collectors.toList())); - return; - } + collect.entrySet().forEach(item -> { + String[] keys = StringUtils.split(item.getKey(), "#$#"); + String shipName = keys[0]; // 船名 + String voyage = keys[1]; // 航次 + String billNo = keys[2]; // 提单号 + String brand = keys[3]; // 品牌 + // 找到是否存在船名,航次,提单号的表头 - // 找到和本次数量一致的 - Optional first = exportInList.stream().filter(p -> p.getQuantity() == item.getValue().size()).findFirst(); + LambdaQueryWrapper cQuery = new LambdaQueryWrapper<>(); + cQuery.eq(CustomerExportIn::getShipName, shipName) + .eq(CustomerExportIn::getVoyage, voyage) + .eq(CustomerExportIn::getBillNum, billNo) + .eq(CustomerExportIn::getBrand, brand) + .eq(CustomerExportIn::getCartType, "备件"); // 备件的不允许整船导入 + if (StringUtils.equals(type, "0")) { + cQuery.and((wrapper) -> { + wrapper.eq(CustomerExportIn::getCheckStatus, AuditEnum.SUBMIT) + .or() + .eq(CustomerExportIn::getCheckStatus, AuditEnum.AUDIT_REJECT); + }); + } else { + cQuery.and((wrapper) -> { + wrapper.eq(CustomerExportIn::getCheckStatus, AuditEnum.AUDIT) + .or() + .eq(CustomerExportIn::getCheckStatus, AuditEnum.AUDIT_REJECT); + }); + } - CustomerExportIn exportIn = first.isPresent() ? first.get() : null; + List exportInList = customerExportInService.list(cQuery); - if (exportIn == null) { - errorDataList.addAll(item.getValue().stream().map(p -> { - JSONObject o = JSONObject.from(p); - o.put("status", "数量不一致不允许导入"); - return o; - }).collect(Collectors.toList())); - return; - } - - ids.add(exportIn.getId()); - - // 已生成装船计划,不能导入 - List havePlans = openApi.haveShipPlan(exportIn.getVoyageId()); - if (exportIn != null && CollectionUtils.isNotEmpty(havePlans)) { - Boolean c = havePlans.contains(exportIn.getId()); - if (c) { + if (CollectionUtils.isEmpty(exportInList)) { // 船名,航次等不存在 errorDataList.addAll(item.getValue().stream().map(p -> { JSONObject o = JSONObject.from(p); - o.put("status", "已生成装船计划,不能导入"); + o.put("status", "船名、航次、提单号、品牌不存在"); return o; }).collect(Collectors.toList())); return; } - } - if (exportIn.getCheckStatus() == AuditEnum.AUDIT_PASS) { // 审核 - errorDataList.addAll(item.getValue().stream().map(p -> { - JSONObject o = JSONObject.from(p); - o.put("status", "审核通过状态的不可导入"); - return o; - }).collect(Collectors.toList())); - return; - } + // 找到和本次数量一致的 + Optional first = exportInList.stream().filter(p -> p.getQuantity() == item.getValue().size()).findFirst(); - if (exportIn.getEnterQuantity() != item.getValue().size()) { - errorDataList.addAll(item.getValue().stream().map(p -> { - JSONObject o = JSONObject.from(p); - o.put("status", "车架号数量和实际货物数量不符合,无法导入"); - return o; - }).collect(Collectors.toList())); - return; - } + CustomerExportIn exportIn = first.isPresent() ? first.get() : null; - List cargos = item.getValue().stream().map(p -> { - CustomerExportInCargo cargo = new CustomerExportInCargo(); - cargo.setBrandId(exportIn.getBrandId()); - cargo.setBrand(exportIn.getBrand()); - cargo.setExportInId(exportIn.getId()); - cargo.setCargoType(1); - cargo.setVin(p.getVin()); - cargo.setTermcd(exportIn.getPortAreaId()); - cargo.setVinStatus(1); - return cargo; - }).collect(Collectors.toList()); + if (exportIn == null) { + errorDataList.addAll(item.getValue().stream().map(p -> { + JSONObject o = JSONObject.from(p); + o.put("status", "数量不一致不允许导入"); + return o; + }).collect(Collectors.toList())); + return; + } - List vins = cargos.stream().map(p -> p.getVin()).collect(Collectors.toList()); + ids.add(exportIn.getId()); - // 验证哪些车架号已经存在了 - List existCargos = customerExportInCargoService.list(new LambdaQueryWrapper().in(CustomerExportInCargo::getVin, vins).ne(CustomerExportInCargo::getExportInId, exportIn.getId())); - List existVins = existCargos.stream().map(p -> p.getVin()).collect(Collectors.toList()); - - if (CollectionUtils.isNotEmpty(existCargos)) { - // 查询出对应的港区 - List sList = customerExportInService.lambdaQuery().in(CustomerExportIn::getId, existCargos.stream().map(ss -> ss.getExportInId()).collect(Collectors.toList())).list(); - Map portAreaMap = sList.stream().collect(Collectors.toMap(CustomerExportIn::getId, CustomerExportIn::getPortAreaId)); - // 通过接口再次验证 - List req = existCargos.stream().map(ss -> { - CheckVinReq v = new CheckVinReq(); - v.setIsRepetition(true); - v.setPamId(portAreaMap.get(ss.getExportInId())); - v.setVinCode(ss.getVin()); - return v; - }).collect(Collectors.toList()); - List rst = shpApi.checkVinRepeat(req); - existVins.clear(); // 先清空 - if (CollectionUtils.isNotEmpty(rst)) { - // 再次过滤出重复的 - List sCollect = rst.stream().filter(ss -> ss.getIsRepetition()).collect(Collectors.toList()); - if (CollectionUtils.isNotEmpty(sCollect)) { - existVins.addAll(sCollect.stream().map(ss -> ss.getVinCode()).collect(Collectors.toList())); + // 已生成装船计划,不能导入 + List havePlans = openApi.haveShipPlan(exportIn.getVoyageId()); + if (exportIn != null && CollectionUtils.isNotEmpty(havePlans)) { + Boolean c = havePlans.contains(exportIn.getId()); + if (c) { + errorDataList.addAll(item.getValue().stream().map(p -> { + JSONObject o = JSONObject.from(p); + o.put("status", "已生成装船计划,不能导入"); + return o; + }).collect(Collectors.toList())); + return; } } + + if (exportIn.getCheckStatus() == AuditEnum.AUDIT_PASS) { // 审核 + errorDataList.addAll(item.getValue().stream().map(p -> { + JSONObject o = JSONObject.from(p); + o.put("status", "审核通过状态的不可导入"); + return o; + }).collect(Collectors.toList())); + return; + } + + if (exportIn.getEnterQuantity() != item.getValue().size()) { + errorDataList.addAll(item.getValue().stream().map(p -> { + JSONObject o = JSONObject.from(p); + o.put("status", "车架号数量和实际货物数量不符合,无法导入"); + return o; + }).collect(Collectors.toList())); + return; + } + + List cargos = item.getValue().stream().map(p -> { + CustomerExportInCargo cargo = new CustomerExportInCargo(); + cargo.setBrandId(exportIn.getBrandId()); + cargo.setBrand(exportIn.getBrand()); + cargo.setExportInId(exportIn.getId()); + cargo.setCargoType(1); + cargo.setVin(p.getVin()); + cargo.setTermcd(exportIn.getPortAreaId()); + cargo.setVinStatus(1); + return cargo; + }).collect(Collectors.toList()); + + List vins = cargos.stream().map(p -> p.getVin()).collect(Collectors.toList()); + + // 验证哪些车架号已经存在了 + List existCargos = customerExportInCargoService.list(new LambdaQueryWrapper().in(CustomerExportInCargo::getVin, vins).ne(CustomerExportInCargo::getExportInId, exportIn.getId())); + List existVins = existCargos.stream().map(p -> p.getVin()).collect(Collectors.toList()); + + if (CollectionUtils.isNotEmpty(existCargos)) { + // 查询出对应的港区 + List sList = customerExportInService.lambdaQuery().in(CustomerExportIn::getId, existCargos.stream().map(ss -> ss.getExportInId()).collect(Collectors.toList())).list(); + Map portAreaMap = sList.stream().collect(Collectors.toMap(CustomerExportIn::getId, CustomerExportIn::getPortAreaId)); + // 通过接口再次验证 + List req = existCargos.stream().map(ss -> { + CheckVinReq v = new CheckVinReq(); + v.setIsRepetition(true); + v.setPamId(portAreaMap.get(ss.getExportInId())); + v.setVinCode(ss.getVin()); + return v; + }).collect(Collectors.toList()); + List rst = shpApi.checkVinRepeat(req); + existVins.clear(); // 先清空 + if (CollectionUtils.isNotEmpty(rst)) { + // 再次过滤出重复的 + List sCollect = rst.stream().filter(ss -> ss.getIsRepetition()).collect(Collectors.toList()); + if (CollectionUtils.isNotEmpty(sCollect)) { + existVins.addAll(sCollect.stream().map(ss -> ss.getVinCode()).collect(Collectors.toList())); + } + } + } + + List existData = item.getValue().stream().filter(p -> existVins.contains(p.getVin())).collect(Collectors.toList()); + if (CollectionUtils.isNotEmpty(existData)) { + errorDataList.addAll(existData.stream().map(p -> { + JSONObject o = JSONObject.from(p); + o.put("status", "数据已存在, 未导入"); + return o; + }).collect(Collectors.toList())); + successDataList.addAll(item.getValue().stream().filter(p -> !existVins.contains(p.getVin())) + .map(p -> { + JSONObject o = JSONObject.from(p); + o.put("status", "数据验证成功"); + return o; + }).collect(Collectors.toList())); + return; + } else { + successDataList.addAll(item.getValue().stream().filter(p -> !existVins.contains(p.getVin())) + .map(p -> { + JSONObject o = JSONObject.from(p); + o.put("status", "数据验证成功"); + return o; + }).collect(Collectors.toList())); + } + + saveCargos.addAll(cargos.stream().filter(p -> !existVins.contains(p.getVin())).collect(Collectors.toList())); + + }); + + if (CollectionUtils.isNotEmpty(errorDataList)) { + errorDataList.addAll(successDataList); + return ResultUtil.failure(ErrorType.PARAMS_ERROR.id(), "导入失败", errorDataList); } - List existData = item.getValue().stream().filter(p -> existVins.contains(p.getVin())).collect(Collectors.toList()); - if (CollectionUtils.isNotEmpty(existData)) { - errorDataList.addAll(existData.stream().map(p -> { - JSONObject o = JSONObject.from(p); - o.put("status", "数据已存在, 未导入"); - return o; - }).collect(Collectors.toList())); - successDataList.addAll(item.getValue().stream().filter(p -> !existVins.contains(p.getVin())) - .map(p -> { - JSONObject o = JSONObject.from(p); - o.put("status", "数据验证成功"); - return o; - }).collect(Collectors.toList())); - return; - } else { - successDataList.addAll(item.getValue().stream().filter(p -> !existVins.contains(p.getVin())) - .map(p -> { - JSONObject o = JSONObject.from(p); - o.put("status", "数据验证成功"); - return o; - }).collect(Collectors.toList())); + if (CollectionUtils.isNotEmpty(saveCargos)) { + // 删除原有的车架号 + customerExportInCargoService.lambdaUpdate().in(CustomerExportInCargo::getExportInId, ids).remove(); + customerExportInCargoService.saveBatch(saveCargos); } - saveCargos.addAll(cargos.stream().filter(p -> !existVins.contains(p.getVin())).collect(Collectors.toList())); - - }); - - if (CollectionUtils.isNotEmpty(errorDataList)) { - errorDataList.addAll(successDataList); - return ResultUtil.failure(ErrorType.PARAMS_ERROR.id(), "导入失败", errorDataList); + return ResultUtil.success(errorDataList); + } catch (Exception e) { + log.error("错误信息", e); + return ResultUtil.failure(ErrorType.PARAMS_ERROR.id(), e.getMessage()); } - - if (CollectionUtils.isNotEmpty(saveCargos)) { - // 删除原有的车架号 - customerExportInCargoService.lambdaUpdate().in(CustomerExportInCargo::getExportInId, ids).remove(); - customerExportInCargoService.saveBatch(saveCargos); - } - - return ResultUtil.success(errorDataList); } @ApiOperation("导入删除") @@ -3102,7 +3129,7 @@ public class ExportInHandler implements BaseHandler { @PostMapping("/exportIn/plan/import-to-add") public Result> exportInPlanUpload( @ApiParam(name = "操作方式, 0为前端,1为审核端") @RequestParam(required = false, defaultValue = "0") String type, - MultipartFile file) throws IOException { + MultipartFile file) { // 所有读取的数据 List dataList = new ArrayList<>(); @@ -3112,450 +3139,457 @@ public class ExportInHandler implements BaseHandler { // 验证通过的数据 List successDataList = new ArrayList<>(); - // 获取用户绑定的货代 - List userBindFreight = openApi.getUserBindFreight();; + try { - // 国家数据 - List countryList = openApi.getCountryList(); - // 港口基础数据 - List portList = dictHandler.getPortList(null).getData(); - // 船名基础数据 - List shipList = dictHandler.getAllShip(null).getData(); - // 港区基础数据 - List portAreaList = dictHandler.getPortAreaList(null).getData(); - // 品牌基础数据 - List brandList = dictHandler.getBrandList(null).getData(); - // 车型基础数据 - List carTypeList = dictHandler.getCartTypeList(null).getData(); - // 产地基础数据 - List originPlaceList = dictHandler.getAllCountryList(null).getData(); - // 运输模式 - List transportWayList = dictHandler.getTransportWayList(null).getData(); - // 操作模式 - List operateTypeList = dictHandler.getOperateTypeList(null).getData(); - // 特殊作业 - List specWorkList = dictHandler.getSpecWorkList().getData(); - // 新能源类型 - List energyTypeList = dictHandler.getEnergyTypeList().getData(); - // 根据港口ID 获取国家 -// Map portCountryList = new HashMap<>(); - // 货代列表 - Map companyMap = new HashMap<>(); - // 货物性质 - List goodsNature = dictHandler.getGoodsNature().getData(); - // 车型明细缓存 - Map> carDetailTypeList = new HashMap<>(); - // 航次缴存 - Map> voyageMap = new HashMap<>(); + // 获取用户绑定的货代 + List userBindFreight = openApi.getUserBindFreight(); + ; - EasyExcel.read(file.getInputStream(), ExportInPlanExcel.class, new ReadExcelListener() { - @Override - protected void saveData(List list) { // 保存数据 - dataList.addAll(list); - } - }).sheet().doRead(); + // 国家数据 + List countryList = openApi.getCountryList(); + // 港口基础数据 + List portList = dictHandler.getPortList(null).getData(); + // 船名基础数据 + List shipList = dictHandler.getAllShip(null).getData(); + // 港区基础数据 + List portAreaList = dictHandler.getPortAreaList(null).getData(); + // 品牌基础数据 + List brandList = dictHandler.getBrandList(null).getData(); + // 车型基础数据 + List carTypeList = dictHandler.getCartTypeList(null).getData(); + // 产地基础数据 + List originPlaceList = dictHandler.getAllCountryList(null).getData(); + // 运输模式 + List transportWayList = dictHandler.getTransportWayList(null).getData(); + // 操作模式 + List operateTypeList = dictHandler.getOperateTypeList(null).getData(); + // 特殊作业 + List specWorkList = dictHandler.getSpecWorkList().getData(); + // 新能源类型 + List energyTypeList = dictHandler.getEnergyTypeList().getData(); + // 根据港口ID 获取国家 + // Map portCountryList = new HashMap<>(); + // 货代列表 + Map companyMap = new HashMap<>(); + // 货物性质 + List goodsNature = dictHandler.getGoodsNature().getData(); + // 车型明细缓存 + Map> carDetailTypeList = new HashMap<>(); + // 航次缴存 + Map> voyageMap = new HashMap<>(); - ValidatorFactory vf = Validation.buildDefaultValidatorFactory(); - Validator validator = vf.getValidator(); + EasyExcel.read(file.getInputStream(), ExportInPlanExcel.class, new ReadExcelListener() { + @Override + protected void saveData(List list) { // 保存数据 + dataList.addAll(list); + } + }).sheet().doRead(); - List validData = new ArrayList<>(); + ValidatorFactory vf = Validation.buildDefaultValidatorFactory(); + Validator validator = vf.getValidator(); - // 需要保存的数据 - List saveData = new ArrayList<>(); + List validData = new ArrayList<>(); - // 数据验证 - dataList.stream().forEach(item -> { - Set> 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); - } - }); + // 需要保存的数据 + List saveData = new ArrayList<>(); - if (CollectionUtils.isEmpty(validData)) { // 数据完整性检验 - return ResultUtil.failure(ErrorType.PARAMS_ERROR.id(), "导入失败", errorDataList); - } + // 数据验证 + dataList.stream().forEach(item -> { + Set> 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); + } + }); - // 港区、船名、港口、品牌、车型 - validData.stream().forEach(item -> { - if (countryList.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getCtyCnname(), item.getCountry())).count() == 0) { - JSONObject o = JSONObject.from(item); - o.put("status", "国家不存在"); - errorDataList.add(o); - return; + if (CollectionUtils.isEmpty(validData)) { // 数据完整性检验 + return ResultUtil.failure(ErrorType.PARAMS_ERROR.id(), "导入失败", errorDataList); } - String tmpCountryId = countryList.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getCtyCnname(), item.getCountry())).findFirst().get().getCtyId(); - if (portList.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getText(), item.getPortName()) && StringUtils.equals(tmpCountryId, p.getExtra2())).count() == 0) { - JSONObject o = JSONObject.from(item); - o.put("status", "港口不存在"); - errorDataList.add(o); - return; + + // 港区、船名、港口、品牌、车型 + validData.stream().forEach(item -> { + if (countryList.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getCtyCnname(), item.getCountry())).count() == 0) { + JSONObject o = JSONObject.from(item); + o.put("status", "国家不存在"); + errorDataList.add(o); + return; + } + String tmpCountryId = countryList.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getCtyCnname(), item.getCountry())).findFirst().get().getCtyId(); + if (portList.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getText(), item.getPortName()) && StringUtils.equals(tmpCountryId, p.getExtra2())).count() == 0) { + JSONObject o = JSONObject.from(item); + o.put("status", "港口不存在"); + errorDataList.add(o); + return; + } + if (shipList.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getText(), item.getShipName())).count() == 0) { + JSONObject o = JSONObject.from(item); + o.put("status", "船名不存在"); + errorDataList.add(o); + return; + } + if (portAreaList.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getText(), item.getPortArea())).count() == 0) { + JSONObject o = JSONObject.from(item); + o.put("status", "港区不存在"); + errorDataList.add(o); + return; + } + if (brandList.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getText(), item.getBrand())).count() == 0) { + JSONObject o = JSONObject.from(item); + o.put("status", "品牌不存在"); + errorDataList.add(o); + return; + } + if (carTypeList.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getText(), item.getCartType())).count() == 0) { + JSONObject o = JSONObject.from(item); + o.put("status", "车型不存在"); + errorDataList.add(o); + return; + } + if (originPlaceList.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getCiyCnname(), item.getOriginPlace())).count() == 0) { + JSONObject o = JSONObject.from(item); + o.put("status", "产地错误"); + errorDataList.add(o); + return; + } + if (!companyMap.containsKey(item.getFreight())) { + CompanyDTO company = pubApi.getCompanyByName(item.getFreight()); + if (company == null || StringUtils.isEmpty(company.getCueId())) { + companyMap.put(item.getFreight(), ""); + } else { + companyMap.put(item.getFreight(), StringUtils.trim(company.getCueId())); + } + } + if (StringUtils.equals(type, "0")) {// 验证货代不一致 + if (CollectionUtils.isEmpty(userBindFreight) || userBindFreight.stream().filter(s -> StringUtils.equalsAny(item.getFreight(), s.getCueCnname(), s.getCueAbbreviation())).count() == 0) { + JSONObject o = JSONObject.from(item); + o.put("status", "登录账号绑定货代不一致"); + errorDataList.add(o); + return; + } + } + if (StringUtils.isEmpty(companyMap.get(item.getFreight()))) { + JSONObject o = JSONObject.from(item); + o.put("status", "货代不存在"); + errorDataList.add(o); + return; + } + // 航次必须是HT6、HTTC、HTLG中的一个,否则导入不成功,提示“航次错误 + if (!StringUtils.equalsAnyIgnoreCase(item.getVoyage(), "HT6", "HTTC", "HTLG")) { + JSONObject o = JSONObject.from(item); + o.put("status", "航次错误"); + errorDataList.add(o); + return; + } + // 运输方式必须是“板车运输、商品车自开、驳船,否则导入不成功,提示“运输方式错误” + if (transportWayList.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getText(), item.getTransportWay())).count() == 0) { + JSONObject o = JSONObject.from(item); + o.put("status", "运输方式错误"); + errorDataList.add(o); + return; + } + // 车型明细必须是车型关联的车型明细,否则导入不成功,提示“车型明细错误” + if (!carDetailTypeList.containsKey(item.getCartType())) { + List data = dictHandler.getCartTypeDetailList(carTypeList.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getText(), item.getCartType())).findFirst().get().getId(), null).getData(); + carDetailTypeList.put(item.getCartType(), data); + } + if (carDetailTypeList.get(item.getCartType()).stream().filter(p -> StringUtils.equalsIgnoreCase(p.getText(), item.getCartTypeDetail())).count() == 0) { + JSONObject o = JSONObject.from(item); + o.put("status", "车型明细错误"); + errorDataList.add(o); + return; + } + // 车型如果是车辆操作模式必须为“港机作业,客户自开”,如果是备件操作模式必须为“铲车、吊车、浮吊”,否则导入不成功,提示“操作模式错误” + if (operateTypeList.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getText(), item.getOperateType())).count() == 0) { + JSONObject o = JSONObject.from(item); + o.put("status", "操作模式错误"); + errorDataList.add(o); + return; + } + if (specWorkList.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getText(), item.getSpecWork())).count() == 0) { + JSONObject o = JSONObject.from(item); + o.put("status", "特殊作业错误"); + errorDataList.add(o); + return; + } + if (!StringUtils.equals("备件", item.getCartType())) { + if (StringUtils.isEmpty(item.getEnergyTypeName())) { + JSONObject o = JSONObject.from(item); + o.put("status", "能源类型不能为空"); + errorDataList.add(o); + return; + } + if (StringUtils.isEmpty(item.getSecondHand())) { + JSONObject o = JSONObject.from(item); + o.put("status", "是否二手车不能为空"); + errorDataList.add(o); + return; + } + if (energyTypeList.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getText(), item.getEnergyTypeName())).count() == 0) { + JSONObject o = JSONObject.from(item); + o.put("status", "能源类型不存在"); + errorDataList.add(o); + return; + } + } + if (goodsNature.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getPtrDesc(), item.getNatureFlagName())).count() == 0) { + JSONObject o = JSONObject.from(item); + o.put("status", "货物性质不存在"); + errorDataList.add(o); + return; + } + if (!StringUtils.equals(item.getNatureFlagName(), "正常")) { + if (shipList.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getText(), item.getTransferShipName())).count() == 0) { + JSONObject o = JSONObject.from(item); + o.put("status", "中转进口船名不存在"); + errorDataList.add(o); + return; + } + if (!voyageMap.containsKey(item.getTransferShipName())) { + List data = dictHandler.queryVoyageByKey(null, null, null, shipList.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getText(), item.getTransferShipName())).findFirst().get().getId(), null).getData(); + voyageMap.put(item.getTransferShipName(), data); + } + if (voyageMap.get(item.getTransferShipName()).stream().filter(p -> StringUtils.equalsIgnoreCase(p.getVvyName(), item.getTransferVoyage())).count() == 0) { + JSONObject o = JSONObject.from(item); + o.put("status", "中转进口航次不存在"); + errorDataList.add(o); + return; + } + } + Date bDate = DateUtils.parseDate(StringUtils.trim(item.getBeginEnterTime()), "yyyy/MM/dd"); + if (bDate == null) { + JSONObject o = JSONObject.from(item); + o.put("status", "进场开始日期格式为2023/09/08"); + errorDataList.add(o); + return; + } + Date eDate = DateUtils.parseDate(StringUtils.trim(item.getEndEnterTime()), "yyyy/MM/dd"); + if (eDate == null) { + JSONObject o = JSONObject.from(item); + o.put("status", "进场结束日期格式为2023/09/08"); + errorDataList.add(o); + return; + } + if (StringUtils.isNotEmpty(item.getEnterTime())) { + // 验证进场时间是否正确 + Date d = DateUtils.parseDate(StringUtils.trim(item.getEnterTime()), "yyyy/MM/dd HH:mm"); + + if (d == null) { + JSONObject o = JSONObject.from(item); + o.put("status", "进场时间格式必须为2023/09/08 13:00"); + errorDataList.add(o); + return; + } + } + + saveData.add(item); + }); + + if (CollectionUtils.isNotEmpty(errorDataList)) { + errorDataList.addAll(successDataList); + return ResultUtil.failure(ErrorType.PARAMS_ERROR.id(), "导入失败", errorDataList); } - if (shipList.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getText(), item.getShipName())).count() == 0) { - JSONObject o = JSONObject.from(item); - o.put("status", "船名不存在"); - errorDataList.add(o); - return; - } - if (portAreaList.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getText(), item.getPortArea())).count() == 0) { - JSONObject o = JSONObject.from(item); - o.put("status", "港区不存在"); - errorDataList.add(o); - return; - } - if (brandList.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getText(), item.getBrand())).count() == 0) { - JSONObject o = JSONObject.from(item); - o.put("status", "品牌不存在"); - errorDataList.add(o); - return; - } - if (carTypeList.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getText(), item.getCartType())).count() == 0) { - JSONObject o = JSONObject.from(item); - o.put("status", "车型不存在"); - errorDataList.add(o); - return; - } - if (originPlaceList.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getCiyCnname(), item.getOriginPlace())).count() == 0) { - JSONObject o = JSONObject.from(item); - o.put("status", "产地错误"); - errorDataList.add(o); - return; - } - if (!companyMap.containsKey(item.getFreight())) { - CompanyDTO company = pubApi.getCompanyByName(item.getFreight()); - if (company == null || StringUtils.isEmpty(company.getCueId())) { - companyMap.put(item.getFreight(), ""); + + // 对象转换 + List collect = saveData.stream().map(item -> { + CustomerExportIn in = PoMapper.instance.excel2Entity(item); + String batchNo = customerService.getSequenceNo("export_in_batch_no", "出口进场", "EI"); + in.setBatchNo(batchNo); + in.setApplyTime(new Date()); + // 处理国家 + in.setCountryId(countryList.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getCtyCnname(), item.getCountry())).findFirst().get().getCtyId()); + in.setPortId(portList.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getText(), item.getPortName()) && StringUtils.equals(p.getExtra2(), in.getCountryId())).findFirst().get().getId()); + + if (StringUtils.isNotEmpty(item.getEnterTime())) { + // 验证进场时间是否正确 + Date d = DateUtils.parseDate(StringUtils.trim(item.getEnterTime()), "yyyy/MM/dd HH:mm"); + in.setTmpEnterDate(d); + } + + in.setShipId(shipList.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getText(), item.getShipName())).findFirst().get().getId()); + in.setShipEnName(shipList.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getText(), item.getShipName())).findFirst().get().getExtra1()); + in.setPortAreaId(portAreaList.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getText(), item.getPortArea())).findFirst().get().getId()); + in.setBrandId(brandList.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getText(), item.getBrand())).findFirst().get().getId()); + in.setCartTypeId(carTypeList.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getText(), item.getCartType())).findFirst().get().getId()); + in.setVoyageId(in.getVoyage()); + in.setOriginPlaceId(originPlaceList.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getCiyCnname(), item.getOriginPlace())).findFirst().get().getCiyId()); + in.setTransportWayId(transportWayList.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getText(), item.getTransportWay())).findFirst().get().getId()); + in.setCartTypeDetailId(carDetailTypeList.get(in.getCartType()).stream().filter(p -> StringUtils.equalsIgnoreCase(p.getText(), item.getCartTypeDetail())).findFirst().get().getId()); + in.setOperateTypeId(operateTypeList.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getText(), item.getOperateType())).findFirst().get().getId()); + in.setSpecWorkId(specWorkList.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getText(), item.getSpecWork())).findFirst().get().getId()); + if (!StringUtils.equals("备件", item.getCartType())) { + in.setEnergyType(energyTypeList.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getText(), item.getEnergyTypeName())).findFirst().get().getId()); + } + in.setFreightId(companyMap.get(item.getFreight())); + in.setApplicantId(UserContext.getUser().getUserId()); + in.setTermcd(in.getPortAreaId()); + in.setVolume(in.getLength().multiply(in.getWidth()).multiply(in.getHeight()).setScale(4, RoundingMode.HALF_UP)); // 计算体积 + in.setNatureFlag(goodsNature.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getPtrDesc(), item.getNatureFlagName())).findFirst().get().getPtrCode()); + if (!StringUtils.equals(item.getNatureFlagName(), "正常")) { + in.setTransferShipId(shipList.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getText(), item.getTransferShipName())).findFirst().get().getId()); + in.setTransferVoyageId(voyageMap.get(in.getTransferShipName()).stream().filter(p -> StringUtils.equalsIgnoreCase(p.getVvyName(), item.getTransferVoyage())).findFirst().get().getVvyId()); + } + if (in.getEnterQuantity() == null) { + in.setEnterQuantity(in.getQuantity()); + } + + if (StringUtils.equals("1", type)) { + in.setCheckStatus(AuditEnum.AUDIT); } else { - companyMap.put(item.getFreight(), StringUtils.trim(company.getCueId())); + in.setCheckStatus(AuditEnum.SUBMIT); } - } - if (StringUtils.equals(type, "0")) {// 验证货代不一致 - if (CollectionUtils.isEmpty(userBindFreight) || userBindFreight.stream().filter(s -> StringUtils.equalsAny(item.getFreight(), s.getCueCnname(), s.getCueAbbreviation())).count() == 0) { - JSONObject o = JSONObject.from(item); - o.put("status", "登录账号绑定货代不一致"); - errorDataList.add(o); + return in; + }).collect(Collectors.toList()); + + // 首先按对船名,般次,提单进行分组 + Map> inMap = collect.stream() + .collect(Collectors.groupingBy(item -> StringUtils.joinWith("#$#", item.getShipId(), item.getVoyageId(), item.getBillNum()), Collectors.toList())); + + + inMap.entrySet().forEach(item -> { + String[] keys = StringUtils.split(item.getKey(), "#$#"); + String shipId = keys[0]; // 船名 + String voyageId = keys[1]; // 航次 + String billNo = keys[2]; // 提单号 + + // 查询出已存在的数据 + LambdaQueryWrapper cQuery = new LambdaQueryWrapper<>(); + cQuery.eq(CustomerExportIn::getShipId, shipId); + cQuery.eq(CustomerExportIn::getVoyageId, voyageId); + cQuery.eq(CustomerExportIn::getBillNum, billNo); + cQuery.ne(CustomerExportIn::getCheckStatus, AuditEnum.AUDIT_REJECT); + + List exportInList = customerExportInService.list(cQuery); + + // 单票件件,毛,体 + CustomerExportIn lastE = item.getValue().get(item.getValue().size() - 1); // 以最后一个为标准 + // 验证件,毛,体 + // 件数 + int totalQuantity = exportInList.stream().mapToInt(CustomerExportIn::getQuantity).sum() + item.getValue().stream().mapToInt(CustomerExportIn::getQuantity).sum(); + if (totalQuantity > lastE.getEachQuantity()) { + errorDataList.addAll(item.getValue().stream().map(p -> { + JSONObject o = JSONObject.from(p); + o.put("status", "数量不得超过单票件数"); + return o; + }).collect(Collectors.toList())); return; } - } - if (StringUtils.isEmpty(companyMap.get(item.getFreight()))) { - JSONObject o = JSONObject.from(item); - o.put("status", "货代不存在"); - errorDataList.add(o); - return; - } - // 航次必须是HT6、HTTC、HTLG中的一个,否则导入不成功,提示“航次错误 - if (!StringUtils.equalsAnyIgnoreCase(item.getVoyage(), "HT6", "HTTC", "HTLG")) { - JSONObject o = JSONObject.from(item); - o.put("status", "航次错误"); - errorDataList.add(o); - return; - } - // 运输方式必须是“板车运输、商品车自开、驳船,否则导入不成功,提示“运输方式错误” - if (transportWayList.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getText(), item.getTransportWay())).count() == 0) { - JSONObject o = JSONObject.from(item); - o.put("status", "运输方式错误"); - errorDataList.add(o); - return; - } - // 车型明细必须是车型关联的车型明细,否则导入不成功,提示“车型明细错误” - if (!carDetailTypeList.containsKey(item.getCartType())) { - List data = dictHandler.getCartTypeDetailList(carTypeList.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getText(), item.getCartType())).findFirst().get().getId(), null).getData(); - carDetailTypeList.put(item.getCartType(), data); - } - if (carDetailTypeList.get(item.getCartType()).stream().filter(p -> StringUtils.equalsIgnoreCase(p.getText(), item.getCartTypeDetail())).count() == 0) { - JSONObject o = JSONObject.from(item); - o.put("status", "车型明细错误"); - errorDataList.add(o); - return; - } - // 车型如果是车辆操作模式必须为“港机作业,客户自开”,如果是备件操作模式必须为“铲车、吊车、浮吊”,否则导入不成功,提示“操作模式错误” - if (operateTypeList.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getText(), item.getOperateType())).count() == 0) { - JSONObject o = JSONObject.from(item); - o.put("status", "操作模式错误"); - errorDataList.add(o); - return; - } - if (specWorkList.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getText(), item.getSpecWork())).count() == 0) { - JSONObject o = JSONObject.from(item); - o.put("status", "特殊作业错误"); - errorDataList.add(o); - return; - } - if (!StringUtils.equals("备件", item.getCartType())) { - if (StringUtils.isEmpty(item.getEnergyTypeName())) { - JSONObject o = JSONObject.from(item); - o.put("status", "能源类型不能为空"); - errorDataList.add(o); - return; + // 重量 + double totalWeight = exportInList.stream().map(p -> p.getWeight().doubleValue()).mapToDouble(p -> p).sum() + item.getValue().stream().map(p -> p.getWeight().doubleValue()).mapToDouble(p -> p).sum(); + if (new BigDecimal(totalWeight).compareTo(lastE.getEachWeight().divide(new BigDecimal(1000))) > 0) { + // errorDataList.addAll(item.getValue().stream().map(p -> { + // JSONObject o = JSONObject.from(p); + // o.put("status", "重量不得超过单票重量"); + // return o; + // }).collect(Collectors.toList())); + // return; } - if (StringUtils.isEmpty(item.getSecondHand())) { - JSONObject o = JSONObject.from(item); - o.put("status", "是否二手车不能为空"); - errorDataList.add(o); - return; + // 体积 + double totalVolume = exportInList.stream().map(p -> p.getVolume().doubleValue()).mapToDouble(p -> p).sum() + item.getValue().stream().map(p -> p.getVolume().doubleValue()).mapToDouble(p -> p).sum(); + if (new BigDecimal(totalVolume).compareTo(lastE.getEachVolume()) > 0) { + // errorDataList.addAll(item.getValue().stream().map(p -> { + // JSONObject o = JSONObject.from(p); + // o.put("status", "体积不得超过单票体积"); + // return o; + // }).collect(Collectors.toList())); + // return; } - if (energyTypeList.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getText(), item.getEnergyTypeName())).count() == 0) { - JSONObject o = JSONObject.from(item); - o.put("status", "能源类型不存在"); - errorDataList.add(o); - return; - } - } - if (goodsNature.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getPtrDesc(), item.getNatureFlagName())).count() == 0) { - JSONObject o = JSONObject.from(item); - o.put("status", "货物性质不存在"); - errorDataList.add(o); - return; - } - if (!StringUtils.equals(item.getNatureFlagName(), "正常")) { - if (shipList.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getText(), item.getTransferShipName())).count() == 0) { - JSONObject o = JSONObject.from(item); - o.put("status", "中转进口船名不存在"); - errorDataList.add(o); - return; - } - if (!voyageMap.containsKey(item.getTransferShipName())) { - List data = dictHandler.queryVoyageByKey(null, null, null, shipList.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getText(), item.getTransferShipName())).findFirst().get().getId(), null).getData(); - voyageMap.put(item.getTransferShipName(), data); - } - if (voyageMap.get(item.getTransferShipName()).stream().filter(p -> StringUtils.equalsIgnoreCase(p.getVvyName(), item.getTransferVoyage())).count() == 0) { - JSONObject o = JSONObject.from(item); - o.put("status", "中转进口航次不存在"); - errorDataList.add(o); - return; - } - } - Date bDate = DateUtils.parseDate(StringUtils.trim(item.getBeginEnterTime()), "yyyy/MM/dd"); - if (bDate == null) { - JSONObject o = JSONObject.from(item); - o.put("status", "进场开始日期格式为2023/09/08"); - errorDataList.add(o); - return; - } - Date eDate = DateUtils.parseDate(StringUtils.trim(item.getEndEnterTime()), "yyyy/MM/dd"); - if (eDate == null) { - JSONObject o = JSONObject.from(item); - o.put("status", "进场结束日期格式为2023/09/08"); - errorDataList.add(o); - return; - } - if (StringUtils.isNotEmpty(item.getEnterTime())) { - // 验证进场时间是否正确 - Date d = DateUtils.parseDate(StringUtils.trim(item.getEnterTime()), "yyyy/MM/dd HH:mm"); - if (d == null) { - JSONObject o = JSONObject.from(item); - o.put("status", "进场时间格式必须为2023/09/08 13:00"); - errorDataList.add(o); - return; - } - } - - saveData.add(item); - }); - - if (CollectionUtils.isNotEmpty(errorDataList)) { - errorDataList.addAll(successDataList); - return ResultUtil.failure(ErrorType.PARAMS_ERROR.id(), "导入失败", errorDataList); - } - - // 对象转换 - List collect = saveData.stream().map(item -> { - CustomerExportIn in = PoMapper.instance.excel2Entity(item); - String batchNo = customerService.getSequenceNo("export_in_batch_no", "出口进场", "EI"); - in.setBatchNo(batchNo); - in.setApplyTime(new Date()); - // 处理国家 - in.setCountryId(countryList.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getCtyCnname(), item.getCountry())).findFirst().get().getCtyId()); - in.setPortId(portList.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getText(), item.getPortName()) && StringUtils.equals(p.getExtra2(), in.getCountryId())).findFirst().get().getId()); - - if (StringUtils.isNotEmpty(item.getEnterTime())) { - // 验证进场时间是否正确 - Date d = DateUtils.parseDate(StringUtils.trim(item.getEnterTime()), "yyyy/MM/dd HH:mm"); - in.setTmpEnterDate(d); - } - - in.setShipId(shipList.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getText(), item.getShipName())).findFirst().get().getId()); - in.setShipEnName(shipList.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getText(), item.getShipName())).findFirst().get().getExtra1()); - in.setPortAreaId(portAreaList.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getText(), item.getPortArea())).findFirst().get().getId()); - in.setBrandId(brandList.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getText(), item.getBrand())).findFirst().get().getId()); - in.setCartTypeId(carTypeList.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getText(), item.getCartType())).findFirst().get().getId()); - in.setVoyageId(in.getVoyage()); - in.setOriginPlaceId(originPlaceList.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getCiyCnname(), item.getOriginPlace())).findFirst().get().getCiyId()); - in.setTransportWayId(transportWayList.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getText(), item.getTransportWay())).findFirst().get().getId()); - in.setCartTypeDetailId(carDetailTypeList.get(in.getCartType()).stream().filter(p -> StringUtils.equalsIgnoreCase(p.getText(), item.getCartTypeDetail())).findFirst().get().getId()); - in.setOperateTypeId(operateTypeList.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getText(), item.getOperateType())).findFirst().get().getId()); - in.setSpecWorkId(specWorkList.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getText(), item.getSpecWork())).findFirst().get().getId()); - if (!StringUtils.equals("备件", item.getCartType())) { - in.setEnergyType(energyTypeList.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getText(), item.getEnergyTypeName())).findFirst().get().getId()); - } - in.setFreightId(companyMap.get(item.getFreight())); - in.setApplicantId(UserContext.getUser().getUserId()); - in.setTermcd(in.getPortAreaId()); - in.setVolume(in.getLength().multiply(in.getWidth()).multiply(in.getHeight()).setScale(4, RoundingMode.HALF_UP)); // 计算体积 - in.setNatureFlag(goodsNature.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getPtrDesc(), item.getNatureFlagName())).findFirst().get().getPtrCode()); - if (!StringUtils.equals(item.getNatureFlagName(), "正常")) { - in.setTransferShipId(shipList.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getText(), item.getTransferShipName())).findFirst().get().getId()); - in.setTransferVoyageId(voyageMap.get(in.getTransferShipName()).stream().filter(p -> StringUtils.equalsIgnoreCase(p.getVvyName(), item.getTransferVoyage())).findFirst().get().getVvyId()); - } - if (in.getEnterQuantity() == null) { - in.setEnterQuantity(in.getQuantity()); - } - - if (StringUtils.equals("1", type)) { - in.setCheckStatus(AuditEnum.AUDIT); - } else { - in.setCheckStatus(AuditEnum.SUBMIT); - } - return in; - }).collect(Collectors.toList()); - - // 首先按对船名,般次,提单进行分组 - Map> inMap = collect.stream() - .collect(Collectors.groupingBy(item -> StringUtils.joinWith("#$#", item.getShipId(), item.getVoyageId(), item.getBillNum()), Collectors.toList())); - - - inMap.entrySet().forEach(item -> { - String[] keys = StringUtils.split(item.getKey(), "#$#"); - String shipId = keys[0]; // 船名 - String voyageId = keys[1]; // 航次 - String billNo = keys[2]; // 提单号 - - // 查询出已存在的数据 - LambdaQueryWrapper cQuery = new LambdaQueryWrapper<>(); - cQuery.eq(CustomerExportIn::getShipId, shipId); - cQuery.eq(CustomerExportIn::getVoyageId, voyageId); - cQuery.eq(CustomerExportIn::getBillNum, billNo); - cQuery.ne(CustomerExportIn::getCheckStatus, AuditEnum.AUDIT_REJECT); - - List exportInList = customerExportInService.list(cQuery); - - // 单票件件,毛,体 - CustomerExportIn lastE = item.getValue().get(item.getValue().size() - 1); // 以最后一个为标准 - // 验证件,毛,体 - // 件数 - int totalQuantity = exportInList.stream().mapToInt(CustomerExportIn::getQuantity).sum() + item.getValue().stream().mapToInt(CustomerExportIn::getQuantity).sum(); - if (totalQuantity > lastE.getEachQuantity()) { - errorDataList.addAll(item.getValue().stream().map(p -> { + successDataList.addAll(item.getValue().stream().map(p -> { JSONObject o = JSONObject.from(p); - o.put("status", "数量不得超过单票件数"); + o.put("status", "数据验证成功"); return o; }).collect(Collectors.toList())); - return; - } - // 重量 - double totalWeight = exportInList.stream().map(p -> p.getWeight().doubleValue()).mapToDouble(p -> p).sum() + item.getValue().stream().map(p -> p.getWeight().doubleValue()).mapToDouble(p -> p).sum(); - if (new BigDecimal(totalWeight).compareTo(lastE.getEachWeight().divide(new BigDecimal(1000))) > 0) { -// errorDataList.addAll(item.getValue().stream().map(p -> { -// JSONObject o = JSONObject.from(p); -// o.put("status", "重量不得超过单票重量"); -// return o; -// }).collect(Collectors.toList())); -// return; - } - // 体积 - double totalVolume = exportInList.stream().map(p -> p.getVolume().doubleValue()).mapToDouble(p -> p).sum() + item.getValue().stream().map(p -> p.getVolume().doubleValue()).mapToDouble(p -> p).sum(); - if (new BigDecimal(totalVolume).compareTo(lastE.getEachVolume()) > 0) { -// errorDataList.addAll(item.getValue().stream().map(p -> { -// JSONObject o = JSONObject.from(p); -// o.put("status", "体积不得超过单票体积"); -// return o; -// }).collect(Collectors.toList())); -// return; + }); + + if (CollectionUtils.isNotEmpty(errorDataList)) { + errorDataList.addAll(successDataList); + return ResultUtil.failure(ErrorType.PARAMS_ERROR.id(), "导入失败", errorDataList); } - successDataList.addAll(item.getValue().stream().map(p -> { - JSONObject o = JSONObject.from(p); - o.put("status", "数据验证成功"); - return o; - }).collect(Collectors.toList())); - }); + inMap.entrySet().forEach(item -> { + String[] keys = StringUtils.split(item.getKey(), "#$#"); + String shipId = keys[0]; // 船名 + String voyageId = keys[1]; // 航次 + String billNo = keys[2]; // 提单号 - if (CollectionUtils.isNotEmpty(errorDataList)) { - errorDataList.addAll(successDataList); - return ResultUtil.failure(ErrorType.PARAMS_ERROR.id(), "导入失败", errorDataList); + // 查询出已存在的数据 + LambdaQueryWrapper cQuery = new LambdaQueryWrapper<>(); + cQuery.eq(CustomerExportIn::getShipId, shipId); + cQuery.eq(CustomerExportIn::getVoyageId, voyageId); + cQuery.eq(CustomerExportIn::getBillNum, billNo); + + List exportInList = customerExportInService.list(cQuery); + + // 单票件件,毛,体 + CustomerExportIn lastE = item.getValue().get(item.getValue().size() - 1); // 以最后一个为标准 + // 如果是备件自动生成条码 + // 过滤出是备件 + item.getValue().stream().filter(ss -> StringUtils.equals(ss.getCartType(), "备件")).forEach(ss -> { + // 判断是否是国际中转航次ID + String vvyId = ss.getVoyageId(); + TransitPartRequest request = new TransitPartRequest(); + request.setVvyIds(Arrays.asList(vvyId)); + request.setPartAmount(ss.getQuantity()); + request.setMnfBl(ss.getBillNum()); + // Map> rst = openApi.getTransitPart(request); + + List vins = new ArrayList<>(); + if (StringUtils.equals(ss.getNatureFlagName(), "正常")) { // 需要自动生成 + // vins.addAll(rst.get(vvyId).stream().map(t -> t.getVinCode()).collect(Collectors.toList())); + // 出口进场计划备件条码生成的规则要变一下,BJ+年月日时分+5随机数,一共要17位 + String prefix = StringUtils.join("BJ", DateUtil.format(new Date(), "yyMMddHHmm")); + do { + String vin = prefix + RandomUtil.randomNumbers(5); + if (!vins.contains(vin)) { + vins.add(vin); + } + } while (vins.size() < ss.getQuantity()); + } + List spares = vins.stream().map(p -> { + CustomerExportInCargo entity = new CustomerExportInCargo(); + entity.setCargoType(1); + entity.setVinStatus(1); + entity.setVin(p); + entity.setTermcd(ss.getPortAreaId()); + return entity; + }).collect(Collectors.toList()); + + List times = new ArrayList<>(); + CustomerExportInTimes time = new CustomerExportInTimes(); + time.setEnterQuantity(ss.getQuantity()); + time.setEnterTime(ss.getTmpEnterDate() == null ? ss.getBeginEnterTime() : ss.getTmpEnterDate()); + times.add(time); + + ss.setEachVolume(lastE.getEachVolume()); + ss.setEachQuantity(lastE.getEachQuantity()); + ss.setEachWeight(lastE.getEachWeight()); + + customerService.saveExportIn(ss, null, times, spares); + + }); + + item.getValue().stream().filter(ss -> !StringUtils.equals(ss.getCartType(), "备件")).forEach(ss -> { + List times = new ArrayList<>(); + CustomerExportInTimes time = new CustomerExportInTimes(); + time.setEnterQuantity(ss.getQuantity()); + time.setEnterTime(ss.getTmpEnterDate() == null ? ss.getBeginEnterTime() : ss.getTmpEnterDate()); + times.add(time); + + ss.setEachVolume(lastE.getEachVolume()); + ss.setEachQuantity(lastE.getEachQuantity()); + ss.setEachWeight(lastE.getEachWeight()); + + customerService.saveExportIn(ss, null, times, null); + }); + }); + } catch (Exception e) { + log.error("上传错误", e); + return ResultUtil.failure(ErrorType.PROGRAM_ERROR.id(), e.getMessage()); } - inMap.entrySet().forEach(item -> { - String[] keys = StringUtils.split(item.getKey(), "#$#"); - String shipId = keys[0]; // 船名 - String voyageId = keys[1]; // 航次 - String billNo = keys[2]; // 提单号 - - // 查询出已存在的数据 - LambdaQueryWrapper cQuery = new LambdaQueryWrapper<>(); - cQuery.eq(CustomerExportIn::getShipId, shipId); - cQuery.eq(CustomerExportIn::getVoyageId, voyageId); - cQuery.eq(CustomerExportIn::getBillNum, billNo); - - List exportInList = customerExportInService.list(cQuery); - - // 单票件件,毛,体 - CustomerExportIn lastE = item.getValue().get(item.getValue().size() - 1); // 以最后一个为标准 - // 如果是备件自动生成条码 - // 过滤出是备件 - item.getValue().stream().filter(ss -> StringUtils.equals(ss.getCartType(), "备件")).forEach(ss -> { - // 判断是否是国际中转航次ID - String vvyId = ss.getVoyageId(); - TransitPartRequest request = new TransitPartRequest(); - request.setVvyIds(Arrays.asList(vvyId)); - request.setPartAmount(ss.getQuantity()); - request.setMnfBl(ss.getBillNum()); -// Map> rst = openApi.getTransitPart(request); - - List vins = new ArrayList<>(); - if (StringUtils.equals(ss.getNatureFlagName(), "正常")) { // 需要自动生成 -// vins.addAll(rst.get(vvyId).stream().map(t -> t.getVinCode()).collect(Collectors.toList())); - // 出口进场计划备件条码生成的规则要变一下,BJ+年月日时分+5随机数,一共要17位 - String prefix = StringUtils.join("BJ", DateUtil.format(new Date(), "yyMMddHHmm")); - do { - String vin = prefix + RandomUtil.randomNumbers(5); - if (!vins.contains(vin)) { - vins.add(vin); - } - } while (vins.size() < ss.getQuantity()); - } - List spares = vins.stream().map(p -> { - CustomerExportInCargo entity = new CustomerExportInCargo(); - entity.setCargoType(1); - entity.setVinStatus(1); - entity.setVin(p); - entity.setTermcd(ss.getPortAreaId()); - return entity; - }).collect(Collectors.toList()); - - List times = new ArrayList<>(); - CustomerExportInTimes time = new CustomerExportInTimes(); - time.setEnterQuantity(ss.getQuantity()); - time.setEnterTime(ss.getTmpEnterDate() == null ? ss.getBeginEnterTime() : ss.getTmpEnterDate()); - times.add(time); - - ss.setEachVolume(lastE.getEachVolume()); - ss.setEachQuantity(lastE.getEachQuantity()); - ss.setEachWeight(lastE.getEachWeight()); - - customerService.saveExportIn(ss, null, times, spares); - - }); - - item.getValue().stream().filter(ss -> !StringUtils.equals(ss.getCartType(), "备件")).forEach(ss -> { - List times = new ArrayList<>(); - CustomerExportInTimes time = new CustomerExportInTimes(); - time.setEnterQuantity(ss.getQuantity()); - time.setEnterTime(ss.getTmpEnterDate() == null ? ss.getBeginEnterTime() : ss.getTmpEnterDate()); - times.add(time); - - ss.setEachVolume(lastE.getEachVolume()); - ss.setEachQuantity(lastE.getEachQuantity()); - ss.setEachWeight(lastE.getEachWeight()); - - customerService.saveExportIn(ss, null, times, null); - }); - }); - return ResultUtil.success(errorDataList); } diff --git a/nuzar-customer-controller/src/main/java/com/haitonggauto/rtosc/handler/ExportLoadHandler.java b/nuzar-customer-controller/src/main/java/com/haitonggauto/rtosc/handler/ExportLoadHandler.java index 82d5cbd..c05b985 100644 --- a/nuzar-customer-controller/src/main/java/com/haitonggauto/rtosc/handler/ExportLoadHandler.java +++ b/nuzar-customer-controller/src/main/java/com/haitonggauto/rtosc/handler/ExportLoadHandler.java @@ -39,6 +39,7 @@ import com.nuzar.rtops.log.dto.LogRecordDTO; import com.nuzar.rtops.log.service.EsLogApprovalUtil; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; +import lombok.extern.slf4j.Slf4j; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.MapUtils; import org.apache.commons.lang3.StringUtils; @@ -66,6 +67,7 @@ import java.util.stream.Collectors; @RequestMapping("/el") @Api(tags = "出口装船") @Validated +@Slf4j public class ExportLoadHandler implements BaseHandler { @Resource @@ -1025,7 +1027,7 @@ public class ExportLoadHandler implements BaseHandler { @ApiOperation("外贸整船车辆导入") @PostMapping("/outside/import-to-add") - public Result> outsideUpload(@RequestParam(required = false) String type, MultipartFile file) throws IOException { + public Result> outsideUpload(@RequestParam(required = false) String type, MultipartFile file) { // 所有读取的数据 List dataList = new ArrayList<>(); @@ -1041,208 +1043,213 @@ public class ExportLoadHandler implements BaseHandler { // 要保存的明细 Map> details = new HashMap<>(); + try { - // 港口基础数据 - List portList = dictHandler.getPortList(null).getData(); + // 港口基础数据 + List portList = dictHandler.getPortList(null).getData(); - // 品牌基础数据 - List brandList = dictHandler.getBrandList(null).getData(); + // 品牌基础数据 + List brandList = dictHandler.getBrandList(null).getData(); - EasyExcel.read(file.getInputStream(), ExportLoadExcel.class, new ReadExcelListener() { - @Override - protected void saveData(List list) { // 保存数据 - dataList.addAll(list); + EasyExcel.read(file.getInputStream(), ExportLoadExcel.class, new ReadExcelListener() { + @Override + protected void saveData(List list) { // 保存数据 + dataList.addAll(list); + } + }).sheet().doRead(); + + ValidatorFactory vf = Validation.buildDefaultValidatorFactory(); + Validator validator = vf.getValidator(); + + List validData = new ArrayList<>(); + + // 数据验证 + dataList.stream().forEach(item -> { + Set> 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 validData = new ArrayList<>(); - - // 数据验证 - dataList.stream().forEach(item -> { - Set> 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); - } - - // 是否有重复的车架号 - List repeat = validData.stream().collect(Collectors.groupingBy(ExportLoadExcel::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); - } - - // 首先按对船名,般次,提单进行分组 - Map> collect = validData.stream() - .collect(Collectors.groupingBy(item -> StringUtils.joinWith("#$#", item.getShipName(), item.getVoyage(), item.getBillNo(), item.getBrand(), item.getDestPort()), Collectors.toList())); - collect.entrySet().forEach(item -> { - String[] keys = StringUtils.split(item.getKey(), "#$#"); - // 首先判断,船名,航次,提单号在出口进港申请中是否已经通过 - LambdaQueryWrapper iQuery = new LambdaQueryWrapper<>(); - iQuery.eq(CustomerExportIn::getShipName, keys[0]); - iQuery.eq(CustomerExportIn::getVoyage, keys[1]); - iQuery.eq(CustomerExportIn::getBillNum, keys[2]); - iQuery.eq(CustomerExportIn::getBrand, keys[3]); - iQuery.isNotNull(CustomerExportIn::getVoyageId); - iQuery.eq(CustomerExportIn::getCheckStatus, AuditEnum.AUDIT_PASS); - - CustomerExportIn exportIn = customerExportInService.getOne(iQuery, false); - if (exportIn == null || exportIn.getCheckStatus() != AuditEnum.AUDIT_PASS) { - errorDataList.addAll(item.getValue().stream().map(p -> { + // 是否有重复的车架号 + List repeat = validData.stream().collect(Collectors.groupingBy(ExportLoadExcel::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", "本船名航次下未找到审核通过的出口进港申请"); + o.put("status", "车架号重复"); return o; }).collect(Collectors.toList())); - return; + return ResultUtil.failure(ErrorType.PARAMS_ERROR.id(), "导入失败", errorDataList); } - if (portList.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getText(), keys[4])).count() == 0) { - JSONObject o = JSONObject.from(item); - o.put("status", "目的港信息错误"); - errorDataList.add(o); - return; - } + // 首先按对船名,般次,提单进行分组 + Map> collect = validData.stream() + .collect(Collectors.groupingBy(item -> StringUtils.joinWith("#$#", item.getShipName(), item.getVoyage(), item.getBillNo(), item.getBrand(), item.getDestPort()), Collectors.toList())); + collect.entrySet().forEach(item -> { + String[] keys = StringUtils.split(item.getKey(), "#$#"); + // 首先判断,船名,航次,提单号在出口进港申请中是否已经通过 + LambdaQueryWrapper iQuery = new LambdaQueryWrapper<>(); + iQuery.eq(CustomerExportIn::getShipName, keys[0]); + iQuery.eq(CustomerExportIn::getVoyage, keys[1]); + iQuery.eq(CustomerExportIn::getBillNum, keys[2]); + iQuery.eq(CustomerExportIn::getBrand, keys[3]); + iQuery.isNotNull(CustomerExportIn::getVoyageId); + iQuery.eq(CustomerExportIn::getCheckStatus, AuditEnum.AUDIT_PASS); - if (brandList.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getText(), keys[3])).count() == 0) { - JSONObject o = JSONObject.from(item); - o.put("status", "品牌不存在"); - errorDataList.add(o); - return; - } + CustomerExportIn exportIn = customerExportInService.getOne(iQuery, false); + if (exportIn == null || exportIn.getCheckStatus() != AuditEnum.AUDIT_PASS) { + errorDataList.addAll(item.getValue().stream().map(p -> { + JSONObject o = JSONObject.from(p); + o.put("status", "本船名航次下未找到审核通过的出口进港申请"); + return o; + }).collect(Collectors.toList())); + return; + } - List cargos = item.getValue().stream().map(p -> { - CustomerExportLoadCargo cargo = new CustomerExportLoadCargo(); - cargo.setCargoType(0); - cargo.setVinStatus(1); - cargo.setBrandId(brandList.stream().filter(s -> StringUtils.equalsIgnoreCase(s.getText(), keys[3])).findFirst().get().getId()); - cargo.setBrand(keys[3]); - cargo.setVin(p.getVin()); - cargo.setTermcd(exportIn.getPortAreaId()); - return cargo; - }).collect(Collectors.toList()); + if (portList.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getText(), keys[4])).count() == 0) { + JSONObject o = JSONObject.from(item); + o.put("status", "目的港信息错误"); + errorDataList.add(o); + return; + } - List vins = cargos.stream().map(p -> p.getVin()).collect(Collectors.toList()); + if (brandList.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getText(), keys[3])).count() == 0) { + JSONObject o = JSONObject.from(item); + o.put("status", "品牌不存在"); + errorDataList.add(o); + return; + } - // 导入时校验待提交、待审核、已通过状态中是否有重复的车架号,重复导入失败,提示”车架号已存在,请勿重复申请“ - List existCargos = customerExportLoadCargoService.list(new LambdaQueryWrapper() - .in(CustomerExportLoadCargo::getVin, vins) - .exists("select id from customer_export_load where customer_export_load.id = customer_export_load_cargo.export_load_id and customer_export_load.check_status <> 3")); - List existVins = existCargos.stream().map(p -> p.getVin()).collect(Collectors.toList()); - - // 查询出对应的港区 - if (CollectionUtils.isNotEmpty(existCargos)) { - List sList = customerExportLoadService.lambdaQuery().in(CustomerExportLoad::getId, existCargos.stream().map(ss -> ss.getExportLoadId()).collect(Collectors.toList())).list(); - Map portAreaMap = sList.stream().collect(Collectors.toMap(CustomerExportLoad::getId, CustomerExportLoad::getPortAreaId)); - // 通过接口再次验证 - List req = existCargos.stream().map(ss -> { - CheckVinReq v = new CheckVinReq(); - v.setIsRepetition(true); - v.setPamId(portAreaMap.get(ss.getExportLoadId())); - v.setVinCode(ss.getVin()); - return v; + List cargos = item.getValue().stream().map(p -> { + CustomerExportLoadCargo cargo = new CustomerExportLoadCargo(); + cargo.setCargoType(0); + cargo.setVinStatus(1); + cargo.setBrandId(brandList.stream().filter(s -> StringUtils.equalsIgnoreCase(s.getText(), keys[3])).findFirst().get().getId()); + cargo.setBrand(keys[3]); + cargo.setVin(p.getVin()); + cargo.setTermcd(exportIn.getPortAreaId()); + return cargo; }).collect(Collectors.toList()); - List rst = shpApi.checkVinRepeat(req); - existVins.clear(); - if (CollectionUtils.isNotEmpty(rst)) { - // 再次过滤出重复的 - List sCollect = rst.stream().filter(ss -> ss.getIsRepetition()).collect(Collectors.toList()); - if (CollectionUtils.isNotEmpty(sCollect)) { - existVins.addAll(sCollect.stream().map(ss -> ss.getVinCode()).collect(Collectors.toList())); + + List vins = cargos.stream().map(p -> p.getVin()).collect(Collectors.toList()); + + // 导入时校验待提交、待审核、已通过状态中是否有重复的车架号,重复导入失败,提示”车架号已存在,请勿重复申请“ + List existCargos = customerExportLoadCargoService.list(new LambdaQueryWrapper() + .in(CustomerExportLoadCargo::getVin, vins) + .exists("select id from customer_export_load where customer_export_load.id = customer_export_load_cargo.export_load_id and customer_export_load.check_status <> 3")); + List existVins = existCargos.stream().map(p -> p.getVin()).collect(Collectors.toList()); + + // 查询出对应的港区 + if (CollectionUtils.isNotEmpty(existCargos)) { + List sList = customerExportLoadService.lambdaQuery().in(CustomerExportLoad::getId, existCargos.stream().map(ss -> ss.getExportLoadId()).collect(Collectors.toList())).list(); + Map portAreaMap = sList.stream().collect(Collectors.toMap(CustomerExportLoad::getId, CustomerExportLoad::getPortAreaId)); + // 通过接口再次验证 + List req = existCargos.stream().map(ss -> { + CheckVinReq v = new CheckVinReq(); + v.setIsRepetition(true); + v.setPamId(portAreaMap.get(ss.getExportLoadId())); + v.setVinCode(ss.getVin()); + return v; + }).collect(Collectors.toList()); + List rst = shpApi.checkVinRepeat(req); + existVins.clear(); + if (CollectionUtils.isNotEmpty(rst)) { + // 再次过滤出重复的 + List sCollect = rst.stream().filter(ss -> ss.getIsRepetition()).collect(Collectors.toList()); + if (CollectionUtils.isNotEmpty(sCollect)) { + existVins.addAll(sCollect.stream().map(ss -> ss.getVinCode()).collect(Collectors.toList())); + } } } - } - List existData = item.getValue().stream().filter(p -> existVins.contains(p.getVin())).collect(Collectors.toList()); - if (CollectionUtils.isNotEmpty(existData)) { - errorDataList.addAll(existData.stream().map(p -> { - JSONObject o = JSONObject.from(p); - o.put("status", "车架号已存在,请勿重复申请"); - return o; - }).collect(Collectors.toList())); - successDataList.addAll(item.getValue().stream().filter(p -> !existVins.contains(p.getVin())) - .map(p -> { - JSONObject o = JSONObject.from(p); - o.put("status", "数据验证成功"); - return o; - }).collect(Collectors.toList())); - return; - } else { - successDataList.addAll(item.getValue().stream().filter(p -> !existVins.contains(p.getVin())) - .map(p -> { - JSONObject o = JSONObject.from(p); - o.put("status", "数据验证成功"); - return o; - }).collect(Collectors.toList())); - } - - // 生成新的装船记录 - List saveCargos = cargos.stream().filter(s -> !existVins.contains(s.getVin())).collect(Collectors.toList()); - if (CollectionUtils.isNotEmpty(saveCargos)) { - - CustomerExportLoad exportLoad = new CustomerExportLoad(); - - exportLoad.setTradType("W"); - exportLoad.setShipName(keys[0]); - exportLoad.setVoyage(keys[1]); - exportLoad.setBillNo(keys[2]); - exportLoad.setShipId(exportIn.getShipId()); - exportLoad.setShipEnName(exportIn.getShipEnName()); - exportLoad.setVoyageId(exportIn.getVoyageId()); - exportLoad.setBrandId(brandList.stream().filter(s -> StringUtils.equalsIgnoreCase(s.getText(), keys[3])).findFirst().get().getId()); - exportLoad.setBrand(keys[3]); - exportLoad.setDestPortId(portList.stream().filter(s -> StringUtils.equalsIgnoreCase(s.getText(), keys[4])).findFirst().get().getId()); - exportLoad.setDestPort(keys[4]); - exportLoad.setQuantity(saveCargos.size()); - exportLoad.setSpareQuantity(0); - exportLoad.setPortArea(exportIn.getPortArea()); - exportLoad.setPortAreaId(exportIn.getPortAreaId()); - exportLoad.setTermcd(exportIn.getPortAreaId()); - if (StringUtils.equals("1", type)) { - exportLoad.setCheckStatus(AuditEnum.AUDIT); + List existData = item.getValue().stream().filter(p -> existVins.contains(p.getVin())).collect(Collectors.toList()); + if (CollectionUtils.isNotEmpty(existData)) { + errorDataList.addAll(existData.stream().map(p -> { + JSONObject o = JSONObject.from(p); + o.put("status", "车架号已存在,请勿重复申请"); + return o; + }).collect(Collectors.toList())); + successDataList.addAll(item.getValue().stream().filter(p -> !existVins.contains(p.getVin())) + .map(p -> { + JSONObject o = JSONObject.from(p); + o.put("status", "数据验证成功"); + return o; + }).collect(Collectors.toList())); + return; } else { - exportLoad.setCheckStatus(AuditEnum.SUBMIT); + successDataList.addAll(item.getValue().stream().filter(p -> !existVins.contains(p.getVin())) + .map(p -> { + JSONObject o = JSONObject.from(p); + o.put("status", "数据验证成功"); + return o; + }).collect(Collectors.toList())); } - exportLoad.setApplyTime(new Date()); - exportLoad.setApplicantId(UserContext.getUser().getUserId()); - heads.put(item.getKey(), exportLoad); - details.put(item.getKey(), saveCargos); + // 生成新的装船记录 + List saveCargos = cargos.stream().filter(s -> !existVins.contains(s.getVin())).collect(Collectors.toList()); + if (CollectionUtils.isNotEmpty(saveCargos)) { + CustomerExportLoad exportLoad = new CustomerExportLoad(); + + exportLoad.setTradType("W"); + exportLoad.setShipName(keys[0]); + exportLoad.setVoyage(keys[1]); + exportLoad.setBillNo(keys[2]); + exportLoad.setShipId(exportIn.getShipId()); + exportLoad.setShipEnName(exportIn.getShipEnName()); + exportLoad.setVoyageId(exportIn.getVoyageId()); + exportLoad.setBrandId(brandList.stream().filter(s -> StringUtils.equalsIgnoreCase(s.getText(), keys[3])).findFirst().get().getId()); + exportLoad.setBrand(keys[3]); + exportLoad.setDestPortId(portList.stream().filter(s -> StringUtils.equalsIgnoreCase(s.getText(), keys[4])).findFirst().get().getId()); + exportLoad.setDestPort(keys[4]); + exportLoad.setQuantity(saveCargos.size()); + exportLoad.setSpareQuantity(0); + exportLoad.setPortArea(exportIn.getPortArea()); + exportLoad.setPortAreaId(exportIn.getPortAreaId()); + exportLoad.setTermcd(exportIn.getPortAreaId()); + if (StringUtils.equals("1", type)) { + exportLoad.setCheckStatus(AuditEnum.AUDIT); + } else { + exportLoad.setCheckStatus(AuditEnum.SUBMIT); + } + exportLoad.setApplyTime(new Date()); + exportLoad.setApplicantId(UserContext.getUser().getUserId()); + + heads.put(item.getKey(), exportLoad); + details.put(item.getKey(), saveCargos); + + } + + }); + + if (CollectionUtils.isNotEmpty(errorDataList)) { + errorDataList.addAll(successDataList); + return ResultUtil.failure(ErrorType.PARAMS_ERROR.id(), "导入失败", errorDataList); } - }); + if (MapUtils.isNotEmpty(heads)) { + heads.entrySet().forEach(item -> { + CustomerExportLoad exportLoad = item.getValue(); + String batchNo = customerService.getSequenceNo("export_load_batch_no", "出口装船", "EL"); + exportLoad.setBatchNo(batchNo); + customerService.saveExportLoad(exportLoad, details.get(item.getKey())); + }); + } - if (CollectionUtils.isNotEmpty(errorDataList)) { - errorDataList.addAll(successDataList); - return ResultUtil.failure(ErrorType.PARAMS_ERROR.id(), "导入失败", errorDataList); + return ResultUtil.success(errorDataList); + } catch (Exception e) { + log.error("错误信息", e); + return ResultUtil.failure(ErrorType.PARAMS_ERROR.id(), e.getMessage()); } - - if (MapUtils.isNotEmpty(heads)) { - heads.entrySet().forEach(item -> { - CustomerExportLoad exportLoad= item.getValue(); - String batchNo = customerService.getSequenceNo("export_load_batch_no", "出口装船", "EL"); - exportLoad.setBatchNo(batchNo); - customerService.saveExportLoad(exportLoad, details.get(item.getKey())); - }); - } - - return ResultUtil.success(errorDataList); } @@ -1287,7 +1294,7 @@ public class ExportLoadHandler implements BaseHandler { @ApiOperation("外贸整船备件导入") @PostMapping("/outside/spare/import-to-add") - public Result> outsideSpareUpload(@RequestParam(required = false) String type, MultipartFile file) throws IOException { + public Result> outsideSpareUpload(@RequestParam(required = false) String type, MultipartFile file) { // 所有读取的数据 List dataList = new ArrayList<>(); @@ -1300,131 +1307,137 @@ public class ExportLoadHandler implements BaseHandler { // 要保存的数据 List heads = new ArrayList<>(); - // 港口基础数据 - List portList = dictHandler.getPortList(null).getData(); + try { - // 品牌基础数据 - List brandList = dictHandler.getBrandList(null).getData(); + // 港口基础数据 + List portList = dictHandler.getPortList(null).getData(); - EasyExcel.read(file.getInputStream(), ExportLoadSpareExcel.class, new ReadExcelListener() { - @Override - protected void saveData(List list) { // 保存数据 - dataList.addAll(list); - } - }).sheet().doRead(); + // 品牌基础数据 + List brandList = dictHandler.getBrandList(null).getData(); - ValidatorFactory vf = Validation.buildDefaultValidatorFactory(); - Validator validator = vf.getValidator(); + EasyExcel.read(file.getInputStream(), ExportLoadSpareExcel.class, new ReadExcelListener() { + @Override + protected void saveData(List list) { // 保存数据 + dataList.addAll(list); + } + }).sheet().doRead(); - List validData = new ArrayList<>(); + ValidatorFactory vf = Validation.buildDefaultValidatorFactory(); + Validator validator = vf.getValidator(); - // 数据验证 - dataList.stream().forEach(item -> { - Set> 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); - } - }); + List validData = new ArrayList<>(); - if (CollectionUtils.isEmpty(validData)) { - return ResultUtil.failure(ErrorType.PARAMS_ERROR.id(), "导入失败", errorDataList); - } + // 数据验证 + dataList.stream().forEach(item -> { + Set> 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); + } + }); - // 首先按对船名,般次,提单进行分组 - Map> collect = validData.stream().collect(Collectors.groupingBy(item -> StringUtils.joinWith("#$#", item.getShipName(), item.getVoyage(), item.getBillNo(), item.getBrand(),item.getDestPort()), Collectors.toList())); - collect.entrySet().forEach(item -> { - String[] keys = StringUtils.split(item.getKey(), "#$#"); - - // 首先判断,船名,航次,提单号在出口进港申请中是否已经通过 - LambdaQueryWrapper iQuery = new LambdaQueryWrapper<>(); - iQuery.eq(CustomerExportIn::getShipName, keys[0]); - iQuery.eq(CustomerExportIn::getVoyage, keys[1]); - iQuery.eq(CustomerExportIn::getBillNum, keys[2]); - iQuery.eq(CustomerExportIn::getBrand, keys[3]); - iQuery.isNotNull(CustomerExportIn::getVoyageId); - iQuery.eq(CustomerExportIn::getCheckStatus, AuditEnum.AUDIT_PASS); - - CustomerExportIn exportIn = customerExportInService.getOne(iQuery, false); - if (exportIn == null || exportIn.getCheckStatus() != AuditEnum.AUDIT_PASS) { - errorDataList.addAll(item.getValue().stream().map(p -> { - JSONObject o = JSONObject.from(p); - o.put("status", "本船名航次下未找到审核通过的出口进港申请"); - return o; - }).collect(Collectors.toList())); - return; + if (CollectionUtils.isEmpty(validData)) { + return ResultUtil.failure(ErrorType.PARAMS_ERROR.id(), "导入失败", errorDataList); } - if (portList.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getText(), keys[4])).count() == 0) { - JSONObject o = JSONObject.from(item); - o.put("status", "目的港信息错误"); - errorDataList.add(o); - return; - } + // 首先按对船名,般次,提单进行分组 + Map> collect = validData.stream().collect(Collectors.groupingBy(item -> StringUtils.joinWith("#$#", item.getShipName(), item.getVoyage(), item.getBillNo(), item.getBrand(), item.getDestPort()), Collectors.toList())); + collect.entrySet().forEach(item -> { + String[] keys = StringUtils.split(item.getKey(), "#$#"); - if (brandList.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getText(), keys[3])).count() == 0) { - JSONObject o = JSONObject.from(item); - o.put("status", "品牌不存在"); - errorDataList.add(o); - return; - } + // 首先判断,船名,航次,提单号在出口进港申请中是否已经通过 + LambdaQueryWrapper iQuery = new LambdaQueryWrapper<>(); + iQuery.eq(CustomerExportIn::getShipName, keys[0]); + iQuery.eq(CustomerExportIn::getVoyage, keys[1]); + iQuery.eq(CustomerExportIn::getBillNum, keys[2]); + iQuery.eq(CustomerExportIn::getBrand, keys[3]); + iQuery.isNotNull(CustomerExportIn::getVoyageId); + iQuery.eq(CustomerExportIn::getCheckStatus, AuditEnum.AUDIT_PASS); - int num = item.getValue().stream().mapToInt(ExportLoadSpareExcel::getNum).sum(); - - successDataList.addAll(item.getValue().stream() - .map(p -> { + CustomerExportIn exportIn = customerExportInService.getOne(iQuery, false); + if (exportIn == null || exportIn.getCheckStatus() != AuditEnum.AUDIT_PASS) { + errorDataList.addAll(item.getValue().stream().map(p -> { JSONObject o = JSONObject.from(p); - o.put("status", "数据验证成功"); + o.put("status", "本船名航次下未找到审核通过的出口进港申请"); return o; }).collect(Collectors.toList())); + return; + } - CustomerExportLoad exportLoad = new CustomerExportLoad(); + if (portList.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getText(), keys[4])).count() == 0) { + JSONObject o = JSONObject.from(item); + o.put("status", "目的港信息错误"); + errorDataList.add(o); + return; + } - exportLoad.setTradType("W"); - exportLoad.setShipId(exportIn.getShipId()); - exportLoad.setShipName(keys[0]); - exportLoad.setShipEnName(exportIn.getShipEnName()); - exportLoad.setVoyage(keys[1]); - exportLoad.setVoyageId(exportIn.getVoyageId()); - exportLoad.setBillNo(keys[2]); - exportLoad.setBrandId(brandList.stream().filter(s -> StringUtils.equalsIgnoreCase(s.getText(), keys[3])).findFirst().get().getId()); - exportLoad.setBrand(keys[3]); - exportLoad.setDestPortId(portList.stream().filter(s -> StringUtils.equalsIgnoreCase(s.getText(), keys[4])).findFirst().get().getId()); - exportLoad.setDestPort(keys[4]); - exportLoad.setQuantity(0); - exportLoad.setSpareQuantity(num); - exportLoad.setPortArea(exportIn.getPortArea()); - exportLoad.setPortAreaId(exportIn.getPortAreaId()); - exportLoad.setTermcd(exportIn.getTermcd()); - if (StringUtils.equals("1", type)) { - exportLoad.setCheckStatus(AuditEnum.AUDIT); - } else { - exportLoad.setCheckStatus(AuditEnum.SUBMIT); - } - exportLoad.setApplyTime(new Date()); - exportLoad.setApplicantId(UserContext.getUser().getUserId()); + if (brandList.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getText(), keys[3])).count() == 0) { + JSONObject o = JSONObject.from(item); + o.put("status", "品牌不存在"); + errorDataList.add(o); + return; + } - heads.add(exportLoad); + int num = item.getValue().stream().mapToInt(ExportLoadSpareExcel::getNum).sum(); - }); + successDataList.addAll(item.getValue().stream() + .map(p -> { + JSONObject o = JSONObject.from(p); + o.put("status", "数据验证成功"); + return o; + }).collect(Collectors.toList())); - if (CollectionUtils.isNotEmpty(errorDataList)) { - errorDataList.addAll(successDataList); - return ResultUtil.failure(ErrorType.PARAMS_ERROR.id(), "导入失败", errorDataList); - } + CustomerExportLoad exportLoad = new CustomerExportLoad(); + + exportLoad.setTradType("W"); + exportLoad.setShipId(exportIn.getShipId()); + exportLoad.setShipName(keys[0]); + exportLoad.setShipEnName(exportIn.getShipEnName()); + exportLoad.setVoyage(keys[1]); + exportLoad.setVoyageId(exportIn.getVoyageId()); + exportLoad.setBillNo(keys[2]); + exportLoad.setBrandId(brandList.stream().filter(s -> StringUtils.equalsIgnoreCase(s.getText(), keys[3])).findFirst().get().getId()); + exportLoad.setBrand(keys[3]); + exportLoad.setDestPortId(portList.stream().filter(s -> StringUtils.equalsIgnoreCase(s.getText(), keys[4])).findFirst().get().getId()); + exportLoad.setDestPort(keys[4]); + exportLoad.setQuantity(0); + exportLoad.setSpareQuantity(num); + exportLoad.setPortArea(exportIn.getPortArea()); + exportLoad.setPortAreaId(exportIn.getPortAreaId()); + exportLoad.setTermcd(exportIn.getTermcd()); + if (StringUtils.equals("1", type)) { + exportLoad.setCheckStatus(AuditEnum.AUDIT); + } else { + exportLoad.setCheckStatus(AuditEnum.SUBMIT); + } + exportLoad.setApplyTime(new Date()); + exportLoad.setApplicantId(UserContext.getUser().getUserId()); + + heads.add(exportLoad); - if (CollectionUtils.isNotEmpty(heads)) { - heads.stream().forEach(item -> { - String batchNo = customerService.getSequenceNo("export_load_batch_no", "出口装船", "EL"); - item.setBatchNo(batchNo); }); - customerExportLoadService.saveBatch(heads); - } - return ResultUtil.success(errorDataList); + if (CollectionUtils.isNotEmpty(errorDataList)) { + errorDataList.addAll(successDataList); + return ResultUtil.failure(ErrorType.PARAMS_ERROR.id(), "导入失败", errorDataList); + } + + if (CollectionUtils.isNotEmpty(heads)) { + heads.stream().forEach(item -> { + String batchNo = customerService.getSequenceNo("export_load_batch_no", "出口装船", "EL"); + item.setBatchNo(batchNo); + }); + customerExportLoadService.saveBatch(heads); + } + + return ResultUtil.success(errorDataList); + } catch (Exception e) { + log.error("错误信息", e); + return ResultUtil.failure(ErrorType.PARAMS_ERROR.id(), e.getMessage()); + } } @ApiOperation("导入删除") @@ -1489,7 +1502,7 @@ public class ExportLoadHandler implements BaseHandler { @ApiOperation("内贸整船车辆导入") @PostMapping("/inside/import-to-add") - public Result> insideUpload(@RequestParam(required = false) String type, MultipartFile file) throws IOException { + public Result> insideUpload(@RequestParam(required = false) String type, MultipartFile file) { // 所有读取的数据 List dataList = new ArrayList<>(); @@ -1503,315 +1516,321 @@ public class ExportLoadHandler implements BaseHandler { Map heads = new LinkedHashMap<>(); Map> details = new HashMap<>(); - // 结算单位 - Map companyMap = new HashMap<>(); + try { + + // 结算单位 + Map companyMap = new HashMap<>(); // List companyList = pubApi.getCompanyList(Arrays.asList("7")); - // 品牌基础数据 - List brandList = dictHandler.getBrandList(null).getData(); - // 港口基础数据 - List portList = dictHandler.getPortList(null).getData(); - // 船名基础数据 - List shipList = dictHandler.getAllShip(null).getData(); - // 港区基础数据 - List portAreaList = dictHandler.getPortAreaList(null).getData(); - // 货物性质 - List goodsNature = dictHandler.getGoodsNature().getData(); - // 缓存航次信息 - Map voyageMap = new HashMap<>(); + // 品牌基础数据 + List brandList = dictHandler.getBrandList(null).getData(); + // 港口基础数据 + List portList = dictHandler.getPortList(null).getData(); + // 船名基础数据 + List shipList = dictHandler.getAllShip(null).getData(); + // 港区基础数据 + List portAreaList = dictHandler.getPortAreaList(null).getData(); + // 货物性质 + List goodsNature = dictHandler.getGoodsNature().getData(); + // 缓存航次信息 + Map voyageMap = new HashMap<>(); - // 缓存中转航次信息 - Map> transferVoyageMap = new HashMap<>(); + // 缓存中转航次信息 + Map> transferVoyageMap = new HashMap<>(); - EasyExcel.read(file.getInputStream(), ExportLoadInsideExcel.class, new ReadExcelListener() { - @Override - protected void saveData(List list) { // 保存数据 - dataList.addAll(list); - } - }).sheet().doRead(); - - ValidatorFactory vf = Validation.buildDefaultValidatorFactory(); - Validator validator = vf.getValidator(); - - List validData = new ArrayList<>(); - - // 数据验证 - dataList.stream().forEach(item -> { - Set> 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); - } - - // 是否有重复的车架号 - List repeat = validData.stream().collect(Collectors.groupingBy(ExportLoadInsideExcel::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 list = validData.stream().collect(Collectors.groupingBy(ExportLoadInsideExcel::getBillNo, Collectors.groupingBy(ExportLoadInsideExcel::getSettleCompName, Collectors.counting()))) - .entrySet().stream().filter(entry -> entry.getValue().size() > 1).map(entry -> entry.getKey()).collect(Collectors.toList()); - - validData.stream().forEach(item -> { - if (StringUtils.isNotEmpty(item.getDestPort()) && portList.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getText(), item.getDestPort())).count() == 0) { - JSONObject o = JSONObject.from(item); - o.put("status", "目的港信息错误"); - errorDataList.add(o); - return; - } - if (portAreaList.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getText(), item.getPortArea())).count() == 0) { - JSONObject o = JSONObject.from(item); - o.put("status", "港区不存在"); - errorDataList.add(o); - return; - } - if (shipList.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getText(), item.getShipName())).count() == 0) { - JSONObject o = JSONObject.from(item); - o.put("status", "船名不存在"); - errorDataList.add(o); - return; - } - if (!voyageMap.containsKey(StringUtils.join(item.getShipName(),item.getVoyage()))) { - VoyageReq req = new VoyageReq(); - req.setSpmName(item.getShipName()); - req.setVvyName(item.getVoyage()); - List resp = shpApi.queryVvyListByVvNameAndSpmName(Arrays.asList(req)); -// VoyageDTO v = shpApi.getVoyageNameByVvyName(item.getVoyage()); - if (CollectionUtils.isEmpty(resp)) { - voyageMap.put(StringUtils.join(item.getShipName(),item.getVoyage()), ""); - } else { - voyageMap.put(StringUtils.join(item.getShipName(),item.getVoyage()),StringUtils.trim(resp.get(0).getVvyId())); + EasyExcel.read(file.getInputStream(), ExportLoadInsideExcel.class, new ReadExcelListener() { + @Override + protected void saveData(List list) { // 保存数据 + dataList.addAll(list); } - } - if (StringUtils.isEmpty(voyageMap.get(StringUtils.join(item.getShipName(),item.getVoyage())))) { - JSONObject o = JSONObject.from(item); - o.put("status", "航次不存在"); - errorDataList.add(o); - return; - } - if (goodsNature.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getPtrDesc(), item.getNatureFlagName())).count() == 0) { - JSONObject o = JSONObject.from(item); - o.put("status", "货物性质不存在"); - errorDataList.add(o); - return; - } - if (!StringUtils.equals(item.getNatureFlagName(), "正常")) { - if (shipList.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getText(), item.getTransferShipName())).count() == 0) { + }).sheet().doRead(); + + ValidatorFactory vf = Validation.buildDefaultValidatorFactory(); + Validator validator = vf.getValidator(); + + List validData = new ArrayList<>(); + + // 数据验证 + dataList.stream().forEach(item -> { + Set> set = validator.validate(item); + if (CollectionUtils.isEmpty(set)) { // 验证通过的 + validData.add(item); + } else { // 验证失败的 JSONObject o = JSONObject.from(item); - o.put("status", "中转进口船名不存在"); + o.put("status", set.stream().map(p -> p.getMessage()).collect(Collectors.joining(","))); errorDataList.add(o); - return; - } - if (!transferVoyageMap.containsKey(item.getTransferShipName())) { - List data = dictHandler.queryVoyageByKey(null, null, null, shipList.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getText(), item.getTransferShipName())).findFirst().get().getId(), null).getData(); - transferVoyageMap.put(item.getTransferShipName(), data); - } - if (transferVoyageMap.get(item.getTransferShipName()).stream().filter(p -> StringUtils.equalsIgnoreCase(p.getVvyName(), item.getTransferVoyage())).count() == 0) { - JSONObject o = JSONObject.from(item); - o.put("status", "中转进口航次不存在"); - errorDataList.add(o); - return; } + }); + + if (CollectionUtils.isEmpty(validData)) { + return ResultUtil.failure(ErrorType.PARAMS_ERROR.id(), "导入失败", errorDataList); } - if (CollectionUtils.containsAny(list, item.getSettleCompName())) { - JSONObject o = JSONObject.from(item); - o.put("status", "相同提单号需保持结费单位相同"); - errorDataList.add(o); - return; - } - if (!companyMap.containsKey(item.getSettleCompName())) { - CompanyDTO company = pubApi.getCompanyByName(item.getSettleCompName()); - if (company == null || StringUtils.isEmpty(company.getCueId())) { - companyMap.put(item.getSettleCompName(), ""); - } else { - companyMap.put(item.getSettleCompName(), StringUtils.trim(company.getCueId())); - } - } - if (StringUtils.isEmpty(companyMap.get(item.getSettleCompName()))) { - JSONObject o = JSONObject.from(item); - o.put("status", "结算单位不存在"); - errorDataList.add(o); - return; - } - if (brandList.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getText(), item.getBrand())).count() == 0) { - JSONObject o = JSONObject.from(item); - o.put("status", "品牌不存在"); - errorDataList.add(o); - return; - } - }); - - if (CollectionUtils.isNotEmpty(errorDataList)) { - return ResultUtil.failure(ErrorType.PARAMS_ERROR.id(), "导入失败", errorDataList); - } - - // 首先按对船名,般次,提单进行分组 - Map> collect = validData.stream().collect(Collectors.groupingBy(item -> StringUtils.joinWith("#$#", item.getShipName(), item.getVoyage(), item.getBillNo(), item.getBrand()), Collectors.toList())); - collect.entrySet().forEach(item -> { - String[] keys = StringUtils.split(item.getKey(), "#$#"); - - // 相同的提单号,必须是相同的结算单位 - LambdaQueryWrapper lQuery = new LambdaQueryWrapper<>(); - lQuery.eq(CustomerExportLoad::getBillNo, keys[2]); - lQuery.ne(CustomerExportLoad::getSettleCompId, companyMap.get(item.getValue().get(0).getSettleCompName())); - - long count = customerExportLoadService.count(lQuery); - if (count > 0) { - errorDataList.addAll(item.getValue().stream().map(p -> { + // 是否有重复的车架号 + List repeat = validData.stream().collect(Collectors.groupingBy(ExportLoadInsideExcel::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", "相同提单号需保持结费单位相同"); + o.put("status", "车架号重复"); return o; }).collect(Collectors.toList())); - return; + return ResultUtil.failure(ErrorType.PARAMS_ERROR.id(), "导入失败", errorDataList); } - List cargos = item.getValue().stream().map(p -> { - CustomerExportLoadCargo cargo = new CustomerExportLoadCargo(); - cargo.setCargoType(0); - cargo.setVinStatus(1); - cargo.setBrandId(brandList.stream().filter(s -> StringUtils.equalsIgnoreCase(s.getText(), p.getBrand())).findFirst().get().getId()); - cargo.setBrand(p.getBrand()); - cargo.setVin(p.getVin()); - return cargo; - }).collect(Collectors.toList()); + // 结算单位不一致 + List list = validData.stream().collect(Collectors.groupingBy(ExportLoadInsideExcel::getBillNo, Collectors.groupingBy(ExportLoadInsideExcel::getSettleCompName, Collectors.counting()))) + .entrySet().stream().filter(entry -> entry.getValue().size() > 1).map(entry -> entry.getKey()).collect(Collectors.toList()); - List vins = cargos.stream().map(p -> p.getVin()).collect(Collectors.toList()); - - // 导入时校验待提交、待审核、已通过状态中是否有重复的车架号,重复导入失败,提示”车架号已存在,请勿重复申请“ - List existCargos = customerExportLoadCargoService.list(new LambdaQueryWrapper() - .in(CustomerExportLoadCargo::getVin, vins) - .exists("select id from customer_export_load where customer_export_load.id = customer_export_load_cargo.export_load_id and customer_export_load.check_status <> 3")); - List existVins = existCargos.stream().map(p -> p.getVin()).collect(Collectors.toList()); - - // 查询出对应的港区 - if (CollectionUtils.isNotEmpty(existCargos)) { - List sList = customerExportLoadService.lambdaQuery().in(CustomerExportLoad::getId, existCargos.stream().map(ss -> ss.getExportLoadId()).collect(Collectors.toList())).list(); - Map portAreaMap = sList.stream().collect(Collectors.toMap(CustomerExportLoad::getId, CustomerExportLoad::getPortAreaId)); - // 通过接口再次验证 - List req = existCargos.stream().map(ss -> { - CheckVinReq v = new CheckVinReq(); - v.setIsRepetition(true); - v.setPamId(portAreaMap.get(ss.getExportLoadId())); - v.setVinCode(ss.getVin()); - return v; - }).collect(Collectors.toList()); - List rst = shpApi.checkVinRepeat(req); - existVins.clear(); - if (CollectionUtils.isNotEmpty(rst)) { - // 再次过滤出重复的 - List sCollect = rst.stream().filter(ss -> ss.getIsRepetition()).collect(Collectors.toList()); - if (CollectionUtils.isNotEmpty(sCollect)) { - existVins.addAll(sCollect.stream().map(ss -> ss.getVinCode()).collect(Collectors.toList())); + validData.stream().forEach(item -> { + if (StringUtils.isNotEmpty(item.getDestPort()) && portList.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getText(), item.getDestPort())).count() == 0) { + JSONObject o = JSONObject.from(item); + o.put("status", "目的港信息错误"); + errorDataList.add(o); + return; + } + if (portAreaList.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getText(), item.getPortArea())).count() == 0) { + JSONObject o = JSONObject.from(item); + o.put("status", "港区不存在"); + errorDataList.add(o); + return; + } + if (shipList.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getText(), item.getShipName())).count() == 0) { + JSONObject o = JSONObject.from(item); + o.put("status", "船名不存在"); + errorDataList.add(o); + return; + } + if (!voyageMap.containsKey(StringUtils.join(item.getShipName(), item.getVoyage()))) { + VoyageReq req = new VoyageReq(); + req.setSpmName(item.getShipName()); + req.setVvyName(item.getVoyage()); + List resp = shpApi.queryVvyListByVvNameAndSpmName(Arrays.asList(req)); +// VoyageDTO v = shpApi.getVoyageNameByVvyName(item.getVoyage()); + if (CollectionUtils.isEmpty(resp)) { + voyageMap.put(StringUtils.join(item.getShipName(), item.getVoyage()), ""); + } else { + voyageMap.put(StringUtils.join(item.getShipName(), item.getVoyage()), StringUtils.trim(resp.get(0).getVvyId())); } } - } - - List existData = item.getValue().stream().filter(p -> existVins.contains(p.getVin())).collect(Collectors.toList()); - if (CollectionUtils.isNotEmpty(existData)) { - errorDataList.addAll(item.getValue().stream().map(p -> { - JSONObject o = JSONObject.from(p); - o.put("status", "车架号已存在,请勿重复申请"); - return o; - }).collect(Collectors.toList())); - successDataList.addAll(item.getValue().stream().filter(p -> !existVins.contains(p.getVin())) - .map(p -> { - JSONObject o = JSONObject.from(p); - o.put("status", "数据验证成功"); - return o; - }).collect(Collectors.toList())); - return; - } else { - successDataList.addAll(item.getValue().stream().filter(p -> !existVins.contains(p.getVin())) - .map(p -> { - JSONObject o = JSONObject.from(p); - o.put("status", "数据验证成功"); - return o; - }).collect(Collectors.toList())); - } - - - String key = UUID.randomUUID().toString(); - - // 生成新的装船记录 - List saveCargos = cargos.stream().filter(s -> !existVins.contains(s.getVin())).collect(Collectors.toList()); - - if (CollectionUtils.isNotEmpty(saveCargos)) { - - CustomerExportLoad exportLoad = new CustomerExportLoad(); - - exportLoad.setTradType("N"); - exportLoad.setShipId(shipList.stream().filter(s -> StringUtils.equalsIgnoreCase(s.getText(), keys[0])).findFirst().get().getId()); - exportLoad.setShipName(keys[0]); - exportLoad.setShipEnName(shipList.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getText(), keys[0])).findFirst().get().getExtra1()); - exportLoad.setVoyageId(voyageMap.get(StringUtils.join(keys[0],keys[1]))); - exportLoad.setVoyage(keys[1]); - exportLoad.setBillNo(keys[2]); - exportLoad.setBrandId(brandList.stream().filter(s -> StringUtils.equalsIgnoreCase(s.getText(), keys[3])).findFirst().get().getId()); - exportLoad.setBrand(keys[3]); - exportLoad.setQuantity(saveCargos.size()); - exportLoad.setSpareQuantity(0); - exportLoad.setSettleCompId(companyMap.get(item.getValue().get(0).getSettleCompName())); - exportLoad.setSettleCompName(item.getValue().get(0).getSettleCompName()); - exportLoad.setPortAreaId(portAreaList.stream().filter(s -> StringUtils.equals(s.getText(), item.getValue().get(0).getPortArea())).findFirst().get().getId()); - exportLoad.setPortArea(item.getValue().get(0).getPortArea()); - exportLoad.setContact(item.getValue().get(0).getContact()); - exportLoad.setContactPhone(item.getValue().get(0).getContactPhone()); - exportLoad.setNatureFlagName(item.getValue().get(0).getNatureFlagName()); - exportLoad.setNatureFlag(goodsNature.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getPtrDesc(), item.getValue().get(0).getNatureFlagName())).findFirst().get().getPtrCode()); - exportLoad.setTransferVoyage(item.getValue().get(0).getTransferVoyage()); - exportLoad.setTransferShipName(item.getValue().get(0).getTransferShipName()); - if (!StringUtils.equals(item.getValue().get(0).getNatureFlagName(), "正常")) { - exportLoad.setTransferShipId(shipList.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getText(), item.getValue().get(0).getTransferShipName())).findFirst().get().getId()); - exportLoad.setTransferVoyageId(transferVoyageMap.get(exportLoad.getTransferShipName()).stream().filter(p -> StringUtils.equalsIgnoreCase(p.getVvyName(), item.getValue().get(0).getTransferVoyage())).findFirst().get().getVvyId()); + if (StringUtils.isEmpty(voyageMap.get(StringUtils.join(item.getShipName(), item.getVoyage())))) { + JSONObject o = JSONObject.from(item); + o.put("status", "航次不存在"); + errorDataList.add(o); + return; } - if (StringUtils.isNotEmpty(item.getValue().get(0).getDestPort())) { // 内贸目的港可以为空 - exportLoad.setDestPort(item.getValue().get(0).getDestPort()); - exportLoad.setDestPortId(portList.stream().filter(s -> StringUtils.equals(s.getText(), item.getValue().get(0).getDestPort())).findFirst().get().getId()); + if (goodsNature.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getPtrDesc(), item.getNatureFlagName())).count() == 0) { + JSONObject o = JSONObject.from(item); + o.put("status", "货物性质不存在"); + errorDataList.add(o); + return; } - if (StringUtils.equals("1", type)) { - exportLoad.setCheckStatus(AuditEnum.AUDIT); - } else { - exportLoad.setCheckStatus(AuditEnum.SUBMIT); + if (!StringUtils.equals(item.getNatureFlagName(), "正常")) { + if (shipList.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getText(), item.getTransferShipName())).count() == 0) { + JSONObject o = JSONObject.from(item); + o.put("status", "中转进口船名不存在"); + errorDataList.add(o); + return; + } + if (!transferVoyageMap.containsKey(item.getTransferShipName())) { + List data = dictHandler.queryVoyageByKey(null, null, null, shipList.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getText(), item.getTransferShipName())).findFirst().get().getId(), null).getData(); + transferVoyageMap.put(item.getTransferShipName(), data); + } + if (transferVoyageMap.get(item.getTransferShipName()).stream().filter(p -> StringUtils.equalsIgnoreCase(p.getVvyName(), item.getTransferVoyage())).count() == 0) { + JSONObject o = JSONObject.from(item); + o.put("status", "中转进口航次不存在"); + errorDataList.add(o); + return; + } } - exportLoad.setApplyTime(new Date()); - exportLoad.setApplicantId(UserContext.getUser().getUserId()); - heads.put(key, exportLoad); - details.put(key, saveCargos); - } - - }); - - if (CollectionUtils.isNotEmpty(errorDataList)) { - errorDataList.addAll(successDataList); - return ResultUtil.failure(ErrorType.PARAMS_ERROR.id(), "导入失败", errorDataList); - } - - if (MapUtils.isNotEmpty(heads)) { - heads.entrySet().stream().forEach(item -> { - CustomerExportLoad exportLoad = item.getValue(); - String batchNo = customerService.getSequenceNo("export_load_batch_no", "出口装船", "EL"); - exportLoad.setBatchNo(batchNo); - customerService.saveExportLoad(exportLoad, details.get(item.getKey())); + if (CollectionUtils.containsAny(list, item.getSettleCompName())) { + JSONObject o = JSONObject.from(item); + o.put("status", "相同提单号需保持结费单位相同"); + errorDataList.add(o); + return; + } + if (!companyMap.containsKey(item.getSettleCompName())) { + CompanyDTO company = pubApi.getCompanyByName(item.getSettleCompName()); + if (company == null || StringUtils.isEmpty(company.getCueId())) { + companyMap.put(item.getSettleCompName(), ""); + } else { + companyMap.put(item.getSettleCompName(), StringUtils.trim(company.getCueId())); + } + } + if (StringUtils.isEmpty(companyMap.get(item.getSettleCompName()))) { + JSONObject o = JSONObject.from(item); + o.put("status", "结算单位不存在"); + errorDataList.add(o); + return; + } + if (brandList.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getText(), item.getBrand())).count() == 0) { + JSONObject o = JSONObject.from(item); + o.put("status", "品牌不存在"); + errorDataList.add(o); + return; + } }); - } - return ResultUtil.success(errorDataList); + if (CollectionUtils.isNotEmpty(errorDataList)) { + return ResultUtil.failure(ErrorType.PARAMS_ERROR.id(), "导入失败", errorDataList); + } + + // 首先按对船名,般次,提单进行分组 + Map> collect = validData.stream().collect(Collectors.groupingBy(item -> StringUtils.joinWith("#$#", item.getShipName(), item.getVoyage(), item.getBillNo(), item.getBrand()), Collectors.toList())); + collect.entrySet().forEach(item -> { + String[] keys = StringUtils.split(item.getKey(), "#$#"); + + // 相同的提单号,必须是相同的结算单位 + LambdaQueryWrapper lQuery = new LambdaQueryWrapper<>(); + lQuery.eq(CustomerExportLoad::getBillNo, keys[2]); + lQuery.ne(CustomerExportLoad::getSettleCompId, companyMap.get(item.getValue().get(0).getSettleCompName())); + + long count = customerExportLoadService.count(lQuery); + if (count > 0) { + errorDataList.addAll(item.getValue().stream().map(p -> { + JSONObject o = JSONObject.from(p); + o.put("status", "相同提单号需保持结费单位相同"); + return o; + }).collect(Collectors.toList())); + return; + } + + List cargos = item.getValue().stream().map(p -> { + CustomerExportLoadCargo cargo = new CustomerExportLoadCargo(); + cargo.setCargoType(0); + cargo.setVinStatus(1); + cargo.setBrandId(brandList.stream().filter(s -> StringUtils.equalsIgnoreCase(s.getText(), p.getBrand())).findFirst().get().getId()); + cargo.setBrand(p.getBrand()); + cargo.setVin(p.getVin()); + return cargo; + }).collect(Collectors.toList()); + + List vins = cargos.stream().map(p -> p.getVin()).collect(Collectors.toList()); + + // 导入时校验待提交、待审核、已通过状态中是否有重复的车架号,重复导入失败,提示”车架号已存在,请勿重复申请“ + List existCargos = customerExportLoadCargoService.list(new LambdaQueryWrapper() + .in(CustomerExportLoadCargo::getVin, vins) + .exists("select id from customer_export_load where customer_export_load.id = customer_export_load_cargo.export_load_id and customer_export_load.check_status <> 3")); + List existVins = existCargos.stream().map(p -> p.getVin()).collect(Collectors.toList()); + + // 查询出对应的港区 + if (CollectionUtils.isNotEmpty(existCargos)) { + List sList = customerExportLoadService.lambdaQuery().in(CustomerExportLoad::getId, existCargos.stream().map(ss -> ss.getExportLoadId()).collect(Collectors.toList())).list(); + Map portAreaMap = sList.stream().collect(Collectors.toMap(CustomerExportLoad::getId, CustomerExportLoad::getPortAreaId)); + // 通过接口再次验证 + List req = existCargos.stream().map(ss -> { + CheckVinReq v = new CheckVinReq(); + v.setIsRepetition(true); + v.setPamId(portAreaMap.get(ss.getExportLoadId())); + v.setVinCode(ss.getVin()); + return v; + }).collect(Collectors.toList()); + List rst = shpApi.checkVinRepeat(req); + existVins.clear(); + if (CollectionUtils.isNotEmpty(rst)) { + // 再次过滤出重复的 + List sCollect = rst.stream().filter(ss -> ss.getIsRepetition()).collect(Collectors.toList()); + if (CollectionUtils.isNotEmpty(sCollect)) { + existVins.addAll(sCollect.stream().map(ss -> ss.getVinCode()).collect(Collectors.toList())); + } + } + } + + List existData = item.getValue().stream().filter(p -> existVins.contains(p.getVin())).collect(Collectors.toList()); + if (CollectionUtils.isNotEmpty(existData)) { + errorDataList.addAll(item.getValue().stream().map(p -> { + JSONObject o = JSONObject.from(p); + o.put("status", "车架号已存在,请勿重复申请"); + return o; + }).collect(Collectors.toList())); + successDataList.addAll(item.getValue().stream().filter(p -> !existVins.contains(p.getVin())) + .map(p -> { + JSONObject o = JSONObject.from(p); + o.put("status", "数据验证成功"); + return o; + }).collect(Collectors.toList())); + return; + } else { + successDataList.addAll(item.getValue().stream().filter(p -> !existVins.contains(p.getVin())) + .map(p -> { + JSONObject o = JSONObject.from(p); + o.put("status", "数据验证成功"); + return o; + }).collect(Collectors.toList())); + } + + + String key = UUID.randomUUID().toString(); + + // 生成新的装船记录 + List saveCargos = cargos.stream().filter(s -> !existVins.contains(s.getVin())).collect(Collectors.toList()); + + if (CollectionUtils.isNotEmpty(saveCargos)) { + + CustomerExportLoad exportLoad = new CustomerExportLoad(); + + exportLoad.setTradType("N"); + exportLoad.setShipId(shipList.stream().filter(s -> StringUtils.equalsIgnoreCase(s.getText(), keys[0])).findFirst().get().getId()); + exportLoad.setShipName(keys[0]); + exportLoad.setShipEnName(shipList.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getText(), keys[0])).findFirst().get().getExtra1()); + exportLoad.setVoyageId(voyageMap.get(StringUtils.join(keys[0], keys[1]))); + exportLoad.setVoyage(keys[1]); + exportLoad.setBillNo(keys[2]); + exportLoad.setBrandId(brandList.stream().filter(s -> StringUtils.equalsIgnoreCase(s.getText(), keys[3])).findFirst().get().getId()); + exportLoad.setBrand(keys[3]); + exportLoad.setQuantity(saveCargos.size()); + exportLoad.setSpareQuantity(0); + exportLoad.setSettleCompId(companyMap.get(item.getValue().get(0).getSettleCompName())); + exportLoad.setSettleCompName(item.getValue().get(0).getSettleCompName()); + exportLoad.setPortAreaId(portAreaList.stream().filter(s -> StringUtils.equals(s.getText(), item.getValue().get(0).getPortArea())).findFirst().get().getId()); + exportLoad.setPortArea(item.getValue().get(0).getPortArea()); + exportLoad.setContact(item.getValue().get(0).getContact()); + exportLoad.setContactPhone(item.getValue().get(0).getContactPhone()); + exportLoad.setNatureFlagName(item.getValue().get(0).getNatureFlagName()); + exportLoad.setNatureFlag(goodsNature.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getPtrDesc(), item.getValue().get(0).getNatureFlagName())).findFirst().get().getPtrCode()); + exportLoad.setTransferVoyage(item.getValue().get(0).getTransferVoyage()); + exportLoad.setTransferShipName(item.getValue().get(0).getTransferShipName()); + if (!StringUtils.equals(item.getValue().get(0).getNatureFlagName(), "正常")) { + exportLoad.setTransferShipId(shipList.stream().filter(p -> StringUtils.equalsIgnoreCase(p.getText(), item.getValue().get(0).getTransferShipName())).findFirst().get().getId()); + exportLoad.setTransferVoyageId(transferVoyageMap.get(exportLoad.getTransferShipName()).stream().filter(p -> StringUtils.equalsIgnoreCase(p.getVvyName(), item.getValue().get(0).getTransferVoyage())).findFirst().get().getVvyId()); + } + if (StringUtils.isNotEmpty(item.getValue().get(0).getDestPort())) { // 内贸目的港可以为空 + exportLoad.setDestPort(item.getValue().get(0).getDestPort()); + exportLoad.setDestPortId(portList.stream().filter(s -> StringUtils.equals(s.getText(), item.getValue().get(0).getDestPort())).findFirst().get().getId()); + } + if (StringUtils.equals("1", type)) { + exportLoad.setCheckStatus(AuditEnum.AUDIT); + } else { + exportLoad.setCheckStatus(AuditEnum.SUBMIT); + } + exportLoad.setApplyTime(new Date()); + exportLoad.setApplicantId(UserContext.getUser().getUserId()); + + heads.put(key, exportLoad); + details.put(key, saveCargos); + } + + }); + + if (CollectionUtils.isNotEmpty(errorDataList)) { + errorDataList.addAll(successDataList); + return ResultUtil.failure(ErrorType.PARAMS_ERROR.id(), "导入失败", errorDataList); + } + + if (MapUtils.isNotEmpty(heads)) { + heads.entrySet().stream().forEach(item -> { + CustomerExportLoad exportLoad = item.getValue(); + String batchNo = customerService.getSequenceNo("export_load_batch_no", "出口装船", "EL"); + exportLoad.setBatchNo(batchNo); + customerService.saveExportLoad(exportLoad, details.get(item.getKey())); + }); + } + + return ResultUtil.success(errorDataList); + } catch (Exception e) { + log.error("错误信息", e); + return ResultUtil.failure(ErrorType.PARAMS_ERROR.id(), e.getMessage()); + } } /* diff --git a/nuzar-customer-controller/src/main/java/com/haitonggauto/rtosc/handler/FreeTradeHandler.java b/nuzar-customer-controller/src/main/java/com/haitonggauto/rtosc/handler/FreeTradeHandler.java index 5bf5033..a80390c 100644 --- a/nuzar-customer-controller/src/main/java/com/haitonggauto/rtosc/handler/FreeTradeHandler.java +++ b/nuzar-customer-controller/src/main/java/com/haitonggauto/rtosc/handler/FreeTradeHandler.java @@ -36,6 +36,7 @@ import com.nuzar.rtops.log.dto.LogRecordDTO; import com.nuzar.rtops.log.service.EsLogApprovalUtil; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; +import lombok.extern.slf4j.Slf4j; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.validation.annotation.Validated; @@ -61,6 +62,7 @@ import java.util.stream.Collectors; @RequestMapping("/spz") @Api(tags = "特保区") @Validated +@Slf4j public class FreeTradeHandler implements BaseHandler { @Resource @@ -410,7 +412,7 @@ public class FreeTradeHandler implements BaseHandler { @PostMapping("/import-to-add") public Result> upload( @RequestParam(required = false, defaultValue = "0") String type, - MultipartFile file) throws IOException { + MultipartFile file) { // 所有读取的数据 List dataList = new ArrayList<>(); @@ -420,96 +422,102 @@ public class FreeTradeHandler implements BaseHandler { // 验证成功的数据 List successDataList = new ArrayList<>(); - EasyExcel.read(file.getInputStream(), FreeTradeExcel.class, new ReadExcelListener() { - @Override - protected void saveData(List list) { // 保存数据 - dataList.addAll(list); + try { + + EasyExcel.read(file.getInputStream(), FreeTradeExcel.class, new ReadExcelListener() { + @Override + protected void saveData(List list) { // 保存数据 + dataList.addAll(list); + } + }).sheet().doRead(); + + ValidatorFactory vf = Validation.buildDefaultValidatorFactory(); + Validator validator = vf.getValidator(); + + List validData = new ArrayList<>(); + + // 数据验证 + dataList.stream().forEach(item -> { + Set> 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 validData = new ArrayList<>(); - - // 数据验证 - dataList.stream().forEach(item -> { - Set> 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); + // 是否有重复的车架号 + List 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); } - }); - // 所有数据验证都不通过 - if (CollectionUtils.isEmpty(validData)) { - return ResultUtil.failure(ErrorType.PARAMS_ERROR.id(), "导入失败", errorDataList); - } + List collect = validData.stream().map(item -> { + CustomerFreeTrade freeTrade = PoMapper.instance.excel2Entity(item); + 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 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 vins = collect.stream().map(p -> p.getVin()).collect(Collectors.toList()); - List collect = validData.stream().map(item -> { - CustomerFreeTrade freeTrade = PoMapper.instance.excel2Entity(item); - 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); + // 验证哪些车架号已经存在了 + List existCargos = customerFreeTradeService.list(new LambdaQueryWrapper().in(CustomerFreeTrade::getVin, vins)); + Map existVins = existCargos.stream().collect(Collectors.toMap(CustomerFreeTrade::getVin, CustomerFreeTrade::getIsActivate)); + Map existIdMap = existCargos.stream().collect(Collectors.toMap(CustomerFreeTrade::getVin, CustomerFreeTrade::getId)); + + // 已激活不处理的数据 + List 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); } - return freeTrade; - }).collect(Collectors.toList()); - List vins = collect.stream().map(p -> p.getVin()).collect(Collectors.toList()); + // 需要修改的数据 + List 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 existCargos = customerFreeTradeService.list(new LambdaQueryWrapper().in(CustomerFreeTrade::getVin, vins)); - Map existVins = existCargos.stream().collect(Collectors.toMap(CustomerFreeTrade::getVin, CustomerFreeTrade::getIsActivate)); - Map existIdMap = existCargos.stream().collect(Collectors.toMap(CustomerFreeTrade::getVin, CustomerFreeTrade::getId)); + List save = collect.stream().filter(p -> !existIdMap.containsKey(p.getVin())).map(p -> { + p.setIsActivate(ActiveEnum.NO); + return p; + }).collect(Collectors.toList()); - // 已激活不处理的数据 - List 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())); + customerFreeTradeService.updateBatchById(update); - 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 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 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("导入删除") diff --git a/nuzar-customer-controller/src/main/java/com/haitonggauto/rtosc/service/impl/CustomerServiceImpl.java b/nuzar-customer-controller/src/main/java/com/haitonggauto/rtosc/service/impl/CustomerServiceImpl.java index 4a51376..263eab9 100644 --- a/nuzar-customer-controller/src/main/java/com/haitonggauto/rtosc/service/impl/CustomerServiceImpl.java +++ b/nuzar-customer-controller/src/main/java/com/haitonggauto/rtosc/service/impl/CustomerServiceImpl.java @@ -847,7 +847,7 @@ public class CustomerServiceImpl implements CustomerService { } } catch (Exception e) { 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.setResponseStr(e.getMessage()); customerExportInSyncLogService.save(log);