From dacd1cb8fddef0fea0ce3c2b2570d2c7996910cb Mon Sep 17 00:00:00 2001 From: Joshi <3040996759@qq.com> Date: Sat, 20 Dec 2025 16:44:19 +0800 Subject: [PATCH] =?UTF-8?q?refactor(wms):=20=E4=BC=98=E5=8C=96=E5=BA=93?= =?UTF-8?q?=E4=BD=8D=E6=8B=86=E5=88=86=E4=B8=8E=E5=90=88=E5=B9=B6=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 批量查询子库位以减少数据库访问次数 - 使用批量更新替代循环单条更新提高性能 - 优化复活子库位逻辑,支持批量操作 - 合并库位时增加占用状态检查 - 提取需要合并的库位进行针对性处理 - 子库位隐藏状态从1改为2以区分删除状态 --- .../impl/WmsActualWarehouseServiceImpl.java | 176 +++++++++++------- 1 file changed, 106 insertions(+), 70 deletions(-) diff --git a/klp-wms/src/main/java/com/klp/service/impl/WmsActualWarehouseServiceImpl.java b/klp-wms/src/main/java/com/klp/service/impl/WmsActualWarehouseServiceImpl.java index 4312ea4f..3c9e5b9c 100644 --- a/klp-wms/src/main/java/com/klp/service/impl/WmsActualWarehouseServiceImpl.java +++ b/klp-wms/src/main/java/com/klp/service/impl/WmsActualWarehouseServiceImpl.java @@ -455,80 +455,92 @@ public class WmsActualWarehouseServiceImpl implements IWmsActualWarehouseService throw new ServiceException("未找到列标识为 " + columnFlag + " 的库位"); } - // 检查是否已经有子库位存在(包括被删除的) - boolean hasExistingChildren = false; - for (WmsActualWarehouse location : columnLocations) { - List existingChildren = baseMapper.selectList(Wrappers.lambdaQuery() - .eq(WmsActualWarehouse::getParentId, location.getActualWarehouseId()) - .eq(WmsActualWarehouse::getActualWarehouseType, 4)); // 四级子库位 - if (CollUtil.isNotEmpty(existingChildren)) { - hasExistingChildren = true; - break; - } - } + // 批量查询所有子库位(一次性查询,避免循环查询) + List parentIds = columnLocations.stream() + .map(WmsActualWarehouse::getActualWarehouseId) + .collect(Collectors.toList()); + + List allChildren = baseMapper.selectList(Wrappers.lambdaQuery() + .in(WmsActualWarehouse::getParentId, parentIds) + .eq(WmsActualWarehouse::getActualWarehouseType, 4)); // 四级子库位 + + boolean hasExistingChildren = CollUtil.isNotEmpty(allChildren); if (hasExistingChildren) { // 如果已经有子库位,执行复活逻辑 - reviveExistingSubLocations(columnLocations, splitIds, splitType); + reviveExistingSubLocations(columnLocations, splitIds, splitType, allChildren); } else { // 如果没有子库位,执行创建逻辑 createNewSubLocationsWithOriginalLogic(columnLocations, splitIds, splitType, columnFlag); } - // 更新该列所有被拆分库位的状态 + // 批量更新该列所有被拆分库位的状态 + List toUpdate = new ArrayList<>(); for (WmsActualWarehouse location : columnLocations) { WmsActualWarehouse upd = new WmsActualWarehouse(); upd.setActualWarehouseId(location.getActualWarehouseId()); upd.setSplitStatus(1); upd.setSplitType(splitType); - baseMapper.updateById(upd); + toUpdate.add(upd); + } + if (!toUpdate.isEmpty()) { + baseMapper.updateBatchById(toUpdate); } } /** - * 复活已存在的子库位 + * 复活已存在的子库位(优化版:批量操作) */ - private void reviveExistingSubLocations(List columnLocations, Set splitIds, Integer splitType) { + private void reviveExistingSubLocations(List columnLocations, Set splitIds, Integer splitType, List allChildren) { + // 按父ID分组子库位 + Map> childrenByParent = allChildren.stream() + .sorted(Comparator.comparing(WmsActualWarehouse::getActualWarehouseId)) + .collect(Collectors.groupingBy(WmsActualWarehouse::getParentId, LinkedHashMap::new, Collectors.toList())); + + List toUpdate = new ArrayList<>(); + for (WmsActualWarehouse location : columnLocations) { boolean isSplitLocation = splitIds.contains(location.getActualWarehouseId()); + List children = childrenByParent.get(location.getActualWarehouseId()); + + if (CollUtil.isEmpty(children)) { + continue; + } - // 查询该库位下所有子库位(包括已删除的) - List allChildren = baseMapper.selectList(Wrappers.lambdaQuery() - .eq(WmsActualWarehouse::getParentId, location.getActualWarehouseId()) - .eq(WmsActualWarehouse::getActualWarehouseType, 4) // 四级子库位 - .orderByAsc(WmsActualWarehouse::getActualWarehouseId)); + // 根据是否为拆分库位决定需要复活的子库位数量 + int requiredCount = isSplitLocation ? 2 : 1; - if (CollUtil.isNotEmpty(allChildren)) { - // 根据是否为拆分库位决定需要复活的子库位数量 - int requiredCount = isSplitLocation ? 2 : 1; + // 复活前N个子库位 + for (int i = 0; i < Math.min(requiredCount, children.size()); i++) { + WmsActualWarehouse child = children.get(i); + if (child.getDelFlag() == 1) { // 只处理已删除的子库位 + WmsActualWarehouse childUpdate = new WmsActualWarehouse(); + childUpdate.setActualWarehouseId(child.getActualWarehouseId()); + childUpdate.setDelFlag(0); + childUpdate.setSplitStatus(1); + childUpdate.setSplitType(splitType); + toUpdate.add(childUpdate); + } + } - // 复活前N个子库位 - for (int i = 0; i < Math.min(requiredCount, allChildren.size()); i++) { - WmsActualWarehouse child = allChildren.get(i); - if (child.getDelFlag() == 1) { // 只处理已删除的子库位 + // 如果子库位数量超过需要的数量,隐藏多余的 + if (children.size() > requiredCount) { + for (int i = requiredCount; i < children.size(); i++) { + WmsActualWarehouse child = children.get(i); + if (child.getDelFlag() == 0) { // 只处理未删除的子库位 WmsActualWarehouse childUpdate = new WmsActualWarehouse(); childUpdate.setActualWarehouseId(child.getActualWarehouseId()); - childUpdate.setDelFlag(0); - childUpdate.setSplitStatus(1); - childUpdate.setSplitType(splitType); - baseMapper.updateById(childUpdate); - } - } - - // 如果子库位数量超过需要的数量,隐藏多余的 - if (allChildren.size() > requiredCount) { - for (int i = requiredCount; i < allChildren.size(); i++) { - WmsActualWarehouse child = allChildren.get(i); - if (child.getDelFlag() == 0) { // 只处理未删除的子库位 - WmsActualWarehouse childUpdate = new WmsActualWarehouse(); - childUpdate.setActualWarehouseId(child.getActualWarehouseId()); - childUpdate.setDelFlag(1); - baseMapper.updateById(childUpdate); - } + childUpdate.setDelFlag(1); + toUpdate.add(childUpdate); } } } } + + // 批量更新 + if (!toUpdate.isEmpty()) { + baseMapper.updateBatchById(toUpdate); + } } /** @@ -640,9 +652,7 @@ public class WmsActualWarehouseServiceImpl implements IWmsActualWarehouseService // 3. 批量更新编码 if (!toUpdate.isEmpty()) { - for (WmsActualWarehouse item : toUpdate) { - baseMapper.updateById(item); - } + baseMapper.updateBatchById(toUpdate); } } @@ -670,38 +680,56 @@ public class WmsActualWarehouseServiceImpl implements IWmsActualWarehouseService throw new ServiceException("未找到列标识为 " + columnFlag + " 的库位"); } - // 合并操作:隐藏子库位 - for (WmsActualWarehouse location : columnLocations) { - Integer currentSplitStatus = Optional.ofNullable(location.getSplitStatus()).orElse(0); - if (currentSplitStatus != 1) { - // 未拆分的库位无需合并 - continue; - } + // 过滤出需要合并的库位(splitStatus=1) + List locationsToMerge = columnLocations.stream() + .filter(loc -> Optional.ofNullable(loc.getSplitStatus()).orElse(0) == 1) + .collect(Collectors.toList()); - // 查询该库位下所有子库位(包括已删除的) - List allChildren = baseMapper.selectList(Wrappers.lambdaQuery() - .eq(WmsActualWarehouse::getParentId, location.getActualWarehouseId()) - .eq(WmsActualWarehouse::getActualWarehouseType, 4)); // 四级子库位 + if (CollUtil.isEmpty(locationsToMerge)) { + return; // 没有需要合并的库位 + } - if (CollUtil.isNotEmpty(allChildren)) { - // 检查子库位是否被占用(只检查未删除的子库位) - List activeChildren = allChildren.stream() + // 批量查询所有子库位 + List parentIds = locationsToMerge.stream() + .map(WmsActualWarehouse::getActualWarehouseId) + .collect(Collectors.toList()); + + List allChildren = baseMapper.selectList(Wrappers.lambdaQuery() + .in(WmsActualWarehouse::getParentId, parentIds) + .eq(WmsActualWarehouse::getActualWarehouseType, 4)); // 四级子库位 + + // 按父ID分组子库位 + Map> childrenByParent = allChildren.stream() + .collect(Collectors.groupingBy(WmsActualWarehouse::getParentId)); + + // 检查是否有子库位被占用 + for (WmsActualWarehouse location : locationsToMerge) { + List children = childrenByParent.get(location.getActualWarehouseId()); + if (CollUtil.isNotEmpty(children)) { + boolean anyOccupied = children.stream() .filter(c -> c.getDelFlag() == 0) - .collect(Collectors.toList()); - - boolean anyOccupied = activeChildren.stream() .anyMatch(c -> !Objects.equals(c.getIsEnabled(), 1)); if (anyOccupied) { throw new ServiceException("子库位被占用,不能合并:" + location.getActualWarehouseCode()); } + } + } - // 隐藏所有子库位(设置delFlag=1) - for (WmsActualWarehouse child : allChildren) { - if (child.getDelFlag() == 0) { // 只处理未删除的子库位 + // 批量更新子库位和父库位 + List childrenToUpdate = new ArrayList<>(); + List parentsToUpdate = new ArrayList<>(); + + for (WmsActualWarehouse location : locationsToMerge) { + List children = childrenByParent.get(location.getActualWarehouseId()); + + // 隐藏所有子库位 + if (CollUtil.isNotEmpty(children)) { + for (WmsActualWarehouse child : children) { + if (child.getDelFlag() == 0) { WmsActualWarehouse childUpdate = new WmsActualWarehouse(); childUpdate.setActualWarehouseId(child.getActualWarehouseId()); childUpdate.setDelFlag(2); - baseMapper.updateById(childUpdate); + childrenToUpdate.add(childUpdate); } } } @@ -711,7 +739,15 @@ public class WmsActualWarehouseServiceImpl implements IWmsActualWarehouseService parentUpdate.setActualWarehouseId(location.getActualWarehouseId()); parentUpdate.setSplitStatus(0); parentUpdate.setSplitType(0); - baseMapper.updateById(parentUpdate); + parentsToUpdate.add(parentUpdate); + } + + // 批量更新 + if (!childrenToUpdate.isEmpty()) { + baseMapper.updateBatchById(childrenToUpdate); + } + if (!parentsToUpdate.isEmpty()) { + baseMapper.updateBatchById(parentsToUpdate); } }