diff --git a/emis-biz/src/main/java/com/xdadan/erp/emis/service/impl/EmisTmsSiteBatchDtlServiceImpl.java b/emis-biz/src/main/java/com/xdadan/erp/emis/service/impl/EmisTmsSiteBatchDtlServiceImpl.java index 5e207b355..57dbfe8ef 100644 --- a/emis-biz/src/main/java/com/xdadan/erp/emis/service/impl/EmisTmsSiteBatchDtlServiceImpl.java +++ b/emis-biz/src/main/java/com/xdadan/erp/emis/service/impl/EmisTmsSiteBatchDtlServiceImpl.java @@ -161,11 +161,13 @@ public class EmisTmsSiteBatchDtlServiceImpl implements IEmisTmsSiteBatchDtlServi EmisTransPlanRule emisTransPlanRule = new EmisTransPlanRule(); emisTransPlanRule.setStartSiteCode(startSiteCode); emisTransPlanRule.setNextSiteCode(nextSiteCode); - emisTransPlanRule.setSupplierCode(supplierCode); +// emisTransPlanRule.setSupplierCode(supplierCode); emisTransPlanRule.setLineCode(emisWaybill.getTransLineType()); emisTransPlanRule.setProductType(emisWaybill.getProductType()); - List rules = emisTransPlanRuleMapper.checkTransPlanRule(emisTransPlanRule); + List queryRules = emisTransPlanRuleMapper.checkTransPlanRule(emisTransPlanRule); + List rules = queryRules.stream() + .filter(i -> i.getSupplierCode().contains(supplierCode)).collect(Collectors.toList()); // 根据起始网点、下一网点和运单号查询TMS组批次 List existingBatches = emisTmsSiteBatchMapper.selectTmsSiteBatchBySiteAndBillCode( @@ -218,32 +220,60 @@ public class EmisTmsSiteBatchDtlServiceImpl implements IEmisTmsSiteBatchDtlServi } } - // 将existingBatches根据起始网点、下一网点、批次重量、件数、体积分组去重 - Map> groupedBatches = existingBatches.stream() - .filter(Objects::nonNull) // 过滤空值 - .collect(Collectors.groupingBy(batch -> { - // 构建分组键,包含所有相关字段 - BigDecimal totalWeight = batch.getTotalWeight() != null ? batch.getTotalWeight() : BigDecimal.ZERO; - Integer totalParcelQty = batch.getTotalParcelQty() != null ? batch.getTotalParcelQty() : 0; - BigDecimal totalVolume = batch.getTotalVolume() != null ? batch.getTotalVolume() : BigDecimal.ZERO; + // 先过滤出包含多个供应商编码的rules + List multiSupplierRules = queryRules.stream() + .filter(transPlanRule -> StringUtils.isNotBlank(transPlanRule.getSupplierCode()) + && transPlanRule.getSupplierCode().contains(",")) + .collect(Collectors.toList()); - return batch.getStartSiteCode() + "_" + batch.getNextSiteCode() + "_" + - totalWeight + "_" + totalParcelQty + "_" + totalVolume; - })); + // 处理多供应商规则,将匹配的批次分组 + List> matchedBatchGroups = new ArrayList<>(); + for (EmisTransPlanRule transPlanRule : multiSupplierRules) { + String[] supplierCodes = transPlanRule.getSupplierCode().split(","); + List supplierCodeList = Arrays.asList(supplierCodes); + + // 找出allBatches中包含指定供应商编码的批次 + List matchedBatches = existingBatches.stream() + .filter(batch -> supplierCodeList.contains(batch.getSupplierCode())) + .collect(Collectors.toList()); + + if (!matchedBatches.isEmpty()) { + matchedBatchGroups.add(matchedBatches); + } + } + + // 如果有匹配的批次组,进行处理 + if (!matchedBatchGroups.isEmpty()) { + // 将多个list合一去重生成listALL + List listALL = matchedBatchGroups.stream() + .flatMap(List::stream) + .distinct() + .collect(Collectors.toList()); + + // 从allBatches中去除包含在listALL中的元素 + existingBatches.removeAll(listALL); + + // 将每个list中留一个元素插入allBatches中 + for (List batchGroup : matchedBatchGroups) { + if (!batchGroup.isEmpty()) { + existingBatches.add(batchGroup.get(0)); + } + } + } // 计算去重后的批次总重量、总件数、总体积 - BigDecimal totalWeight = groupedBatches.values().stream() - .map(batchList -> batchList.get(0).getTotalWeight()) + BigDecimal totalWeight = existingBatches.stream() + .map(EmisTmsSiteBatch::getTotalWeight) .filter(Objects::nonNull) .reduce(BigDecimal.ZERO, BigDecimal::add); - Integer totalParcelQty = groupedBatches.values().stream() - .map(batchList -> batchList.get(0).getTotalParcelQty()) + Integer totalParcelQty = existingBatches.stream() + .map(EmisTmsSiteBatch::getTotalParcelQty) .filter(Objects::nonNull) .reduce(0, Integer::sum); - BigDecimal totalVolume = groupedBatches.values().stream() - .map(batchList -> batchList.get(0).getTotalVolume()) + BigDecimal totalVolume = existingBatches.stream() + .map(EmisTmsSiteBatch::getTotalVolume) .filter(Objects::nonNull) .reduce(BigDecimal.ZERO, BigDecimal::add); diff --git a/emis-biz/src/main/java/com/xdadan/erp/emis/service/impl/EmisTmsSiteBatchServiceImpl.java b/emis-biz/src/main/java/com/xdadan/erp/emis/service/impl/EmisTmsSiteBatchServiceImpl.java index 99969f255..13ab82371 100644 --- a/emis-biz/src/main/java/com/xdadan/erp/emis/service/impl/EmisTmsSiteBatchServiceImpl.java +++ b/emis-biz/src/main/java/com/xdadan/erp/emis/service/impl/EmisTmsSiteBatchServiceImpl.java @@ -156,13 +156,13 @@ public class EmisTmsSiteBatchServiceImpl extends EmisBaseService implements IEmi EmisWaybill emisWaybill = emisWaybillMapper.selectEmisWaybillByBillCode(itemDtl.getBillCode()); checkVirtual(itemDtl.getBillCode(), emisWaybill); - EmisTransPlanRule rule = checkTransPlanRule(oldSiteBatch, emisWaybill); - checkItemDtl(oldSiteBatch, itemDtl, emisWaybill, rule); + List rules = checkTransPlanRule(oldSiteBatch, emisWaybill); + checkItemDtl(oldSiteBatch, itemDtl, emisWaybill, rules); } private void checkItemDtl(EmisTmsSiteBatch oldSiteBatch, EmisTmsSiteBatchDtl itemDtl, EmisWaybill emisWaybill, - EmisTransPlanRule rule) throws EmisBizError { + List rules) throws EmisBizError { // 安全比较:检查批次数据是否与运单数据完全一致 boolean isParcelQtyEqual = Objects.equals(emisWaybill.getParcelQty(), itemDtl.getBatchParcelQty()); // 重量比较:使用compareTo方法比较大小是否相等 @@ -174,7 +174,7 @@ public class EmisTmsSiteBatchServiceImpl extends EmisBaseService implements IEmi if (isParcelQtyEqual && isWeightEqual && isVolumeEqual) { checkUnique(oldSiteBatch, itemDtl.getBillCode()); - compareWaybillAndBatch(oldSiteBatch, itemDtl, emisWaybill, rule, false); + compareWaybillAndBatch(oldSiteBatch, itemDtl, emisWaybill, rules, false); return; } @@ -191,16 +191,19 @@ public class EmisTmsSiteBatchServiceImpl extends EmisBaseService implements IEmi throw new EmisBizError(EmisBizErrorType.FAIL, itemDtl.getBillCode() + "的批次体积必须大于0"); } - compareWaybillAndBatch(oldSiteBatch, itemDtl, emisWaybill, rule, true); + compareWaybillAndBatch(oldSiteBatch, itemDtl, emisWaybill, rules, true); } private void compareWaybillAndBatch(EmisTmsSiteBatch oldSiteBatch, EmisTmsSiteBatchDtl itemDtl, - EmisWaybill emisWaybill, EmisTransPlanRule rule, boolean isBatch) throws EmisBizError { + EmisWaybill emisWaybill, List rules, boolean isBatch) throws EmisBizError { // 根据起始网点、下一网点和运单号查询TMS组批次 List existingBatches = selectTmsSiteBatchBySiteAndBillCode( oldSiteBatch.getStartSiteCode(), oldSiteBatch.getNextSiteCode(), itemDtl.getBillCode(), itemDtl.getId()); + EmisTransPlanRule rule = rules.stream() + .filter(i -> i.getSupplierCode().contains(oldSiteBatch.getSupplierCode())).collect(Collectors.toList()) + .get(0); // 检查rule.supplierCode是否包含多个供应商编码(用逗号隔开) if (StringUtils.isNotBlank(rule.getSupplierCode()) && rule.getSupplierCode().contains(",")) { @@ -251,34 +254,62 @@ public class EmisTmsSiteBatchServiceImpl extends EmisBaseService implements IEmi currentBatch.setTotalVolume(itemDtl.getBatchVolume()); allBatches.add(currentBatch); - // 将allBatches根据起始网点、下一网点、批次重量、件数、体积分组去重 - Map> groupedBatches = allBatches.stream() - .collect(Collectors.groupingBy(batch -> { - // 去除重量和体积的多余0 - String weightStr = batch.getTotalWeight() != null - ? batch.getTotalWeight().stripTrailingZeros().toPlainString() - : "0"; - String volumeStr = batch.getTotalVolume() != null - ? batch.getTotalVolume().stripTrailingZeros().toPlainString() - : "0"; + List countBatches = new ArrayList<>(allBatches); - return batch.getStartSiteCode() + "_" + batch.getNextSiteCode() + "_" + - weightStr + "_" + batch.getTotalParcelQty() + "_" + volumeStr; - })); + // 先过滤出包含多个供应商编码的rules + List multiSupplierRules = rules.stream() + .filter(transPlanRule -> StringUtils.isNotBlank(transPlanRule.getSupplierCode()) + && transPlanRule.getSupplierCode().contains(",")) + .collect(Collectors.toList()); + + // 处理多供应商规则,将匹配的批次分组 + List> matchedBatchGroups = new ArrayList<>(); + for (EmisTransPlanRule transPlanRule : multiSupplierRules) { + String[] supplierCodes = transPlanRule.getSupplierCode().split(","); + List supplierCodeList = Arrays.asList(supplierCodes); + + // 找出countBatches中包含指定供应商编码的批次 + List matchedBatches = countBatches.stream() + .filter(batch -> supplierCodeList.contains(batch.getSupplierCode())) + .collect(Collectors.toList()); + + if (!matchedBatches.isEmpty()) { + matchedBatchGroups.add(matchedBatches); + } + } + + // 如果有匹配的批次组,进行处理 + if (!matchedBatchGroups.isEmpty()) { + // 将多个list合一去重生成listALL + List listALL = matchedBatchGroups.stream() + .flatMap(List::stream) + .distinct() + .collect(Collectors.toList()); + + // 从countBatches中去除包含在listALL中的元素 + countBatches.removeAll(listALL); + + // 将每个list中留一个元素插入countBatches中 + for (List batchGroup : matchedBatchGroups) { + if (!batchGroup.isEmpty()) { + countBatches.add(batchGroup.get(0)); + } + } + } // 计算去重后的批次总重量、总件数、总体积 - BigDecimal totalWeight = groupedBatches.values().stream() - .map(batchList -> batchList.get(0).getTotalWeight()) + BigDecimal totalWeight = countBatches.stream() + .map(EmisTmsSiteBatch::getTotalWeight) .filter(Objects::nonNull) .reduce(BigDecimal.ZERO, BigDecimal::add); - Integer totalParcelQty = groupedBatches.values().stream() - .map(batchList -> batchList.get(0).getTotalParcelQty()) + Integer totalParcelQty = countBatches.stream() + .map(EmisTmsSiteBatch::getTotalParcelQty) .filter(Objects::nonNull) .reduce(0, Integer::sum); - BigDecimal totalVolume = groupedBatches.values().stream() - .map(batchList -> batchList.get(0).getTotalVolume()) + BigDecimal totalVolume = countBatches.stream() + .map(EmisTmsSiteBatch::getTotalVolume) .filter(Objects::nonNull) .reduce(BigDecimal.ZERO, BigDecimal::add); @@ -372,19 +403,21 @@ public class EmisTmsSiteBatchServiceImpl extends EmisBaseService implements IEmi } } - private EmisTransPlanRule checkTransPlanRule(EmisTmsSiteBatch oldSiteBatch, EmisWaybill emisWaybill) + private List checkTransPlanRule(EmisTmsSiteBatch oldSiteBatch, EmisWaybill emisWaybill) throws EmisBizError { EmisTransPlanRule emisTransPlanRule = new EmisTransPlanRule(); emisTransPlanRule.setStartSiteCode(oldSiteBatch.getStartSiteCode()); emisTransPlanRule.setNextSiteCode(oldSiteBatch.getNextSiteCode()); - emisTransPlanRule.setSupplierCode(oldSiteBatch.getSupplierCode()); + // emisTransPlanRule.setSupplierCode(oldSiteBatch.getSupplierCode()); emisTransPlanRule.setLineCode(emisWaybill.getTransLineType()); emisTransPlanRule.setProductType(emisWaybill.getProductType()); List list = emisTransPlanRuleMapper.checkTransPlanRule(emisTransPlanRule); - if (CollectionUtil.isEmpty(list)) { + List filterList = list.stream() + .filter(i -> i.getSupplierCode().contains(oldSiteBatch.getSupplierCode())).collect(Collectors.toList()); + if (CollectionUtil.isEmpty(filterList)) { throw new EmisBizError(EmisBizErrorType.FAIL, emisWaybill.getBillCode() + "组批次起始网点、下一网点、供应商与规则不匹配"); } - return list.get(0); + return list; } private void checkUnique(EmisTmsSiteBatch emisTmsSiteBatch, String billCode) throws EmisBizError {