diff --git a/202504-Wms-MengYang-box/wms_serve_mengyang/src/main/java/com/wms_main/service/business/serviceImpl/StackerTaskServiceImpl.java b/202504-Wms-MengYang-box/wms_serve_mengyang/src/main/java/com/wms_main/service/business/serviceImpl/StackerTaskServiceImpl.java index c161e7b..69e12d7 100644 --- a/202504-Wms-MengYang-box/wms_serve_mengyang/src/main/java/com/wms_main/service/business/serviceImpl/StackerTaskServiceImpl.java +++ b/202504-Wms-MengYang-box/wms_serve_mengyang/src/main/java/com/wms_main/service/business/serviceImpl/StackerTaskServiceImpl.java @@ -127,6 +127,8 @@ public class StackerTaskServiceImpl implements IStackerTaskService { .eq(TAppLocation::getEquipmentId, equipmentId); // 查询库位 List locationList = appLocationService.list(queryWrapper); + // 计算当前设备的最大深度(默认2) + int maxDepth = locationList.stream().mapToInt(TAppLocation::getLDepth).max().orElse(2); List candidateLocationList = locationList.stream().filter(item -> { boolean filterResult = item.getIsWorking() == 0 && item.getIsLock() == 0 && item.getIsOccupy() == 0; if (locationFilter != null) { @@ -141,8 +143,9 @@ public class StackerTaskServiceImpl implements IStackerTaskService { } } return filterResult; - }).sorted(Comparator.comparingInt(TAppLocation::getLDepth).reversed()) - .sorted(Comparator.comparingInt(location -> location.getLCol() + location.getLLayer())) + }) + .sorted(Comparator.comparingInt(TAppLocation::getLLayer).reversed() + .thenComparingInt(TAppLocation::getLCol).reversed()) .toList(); // 判断是否输入了subArea if (locationFilter != null && StringUtils.isNotEmpty(locationFilter.getSubArea())) { @@ -154,26 +157,59 @@ public class StackerTaskServiceImpl implements IStackerTaskService { candidateLocationList = new ArrayList<>(firstList);// 先添加符合subArea的库位 candidateLocationList.addAll(lastList);// 再添加不符合subArea的库位 } - // 找一个空闲的库位 + + // 第一轮:只尝试最大深度(例如 2 深度)库位 for (TAppLocation candidateLocation : candidateLocationList) { + if (!Objects.equals(candidateLocation.getLDepth(), maxDepth)) { + continue; // 只处理最大深度 + } if (candidateLocation.getIsWorking() == 1 || candidateLocation.getIsLock() == 1 || candidateLocation.getIsOccupy() == 1) { continue; } if (isMaxDepthAvailable(locationList, candidateLocation)) { try { - // 尝试锁定库位,锁定成功就返回 + // 申请当前库位,如果成功则返回 if (appLocationService.update(new LambdaUpdateWrapper() .set(TAppLocation::getIsOccupy, 1) .set(TAppLocation::getVehicleId, requestVehicleId) .eq(TAppLocation::getLocationId, candidateLocation.getLocationId()) .eq(TAppLocation::getIsOccupy, 0))) { - // 当前库位可用 targetLocation = candidateLocation; break; } } catch (Exception e) { - log.error("锁定库位{}失败。", candidateLocation.getLocationId()); + log.error("请求库位{}失败.", candidateLocation.getLocationId()); + } + } + } + if (targetLocation != null) { + // 找到了目标库位 + break; + } + + // 第二轮:若最大深度库位都不可用,再尝试其它深度(如 1 深度)库位 + for (TAppLocation candidateLocation : candidateLocationList) { + if (Objects.equals(candidateLocation.getLDepth(), maxDepth)) { + continue; // 第一轮已经尝试 + } + if (candidateLocation.getIsWorking() == 1 || candidateLocation.getIsLock() == 1 + || candidateLocation.getIsOccupy() == 1) { + continue; + } + if (isMaxDepthAvailable(locationList, candidateLocation)) { + try { + // 申请当前库位,如果成功则返回 + if (appLocationService.update(new LambdaUpdateWrapper() + .set(TAppLocation::getIsOccupy, 1) + .set(TAppLocation::getVehicleId, requestVehicleId) + .eq(TAppLocation::getLocationId, candidateLocation.getLocationId()) + .eq(TAppLocation::getIsOccupy, 0))) { + targetLocation = candidateLocation; + break; + } + } catch (Exception e) { + log.error("请求库位{}失败.", candidateLocation.getLocationId()); } } } @@ -430,14 +466,14 @@ public class StackerTaskServiceImpl implements IStackerTaskService { // int times = 0; // MesApiResponse response = null; // do { - // Thread.sleep(15000L * times); - // response = externalApiService.invokeOrderInCB(orderInCBReq); - // times++; + // Thread.sleep(15000L * times); + // response = externalApiService.invokeOrderInCB(orderInCBReq); + // times++; // } while (response.getCode() != 0 && times <= 10); // if (response.getCode() != 0) { - // log.error("[wms]上报失败"); + // log.error("[wms]上报失败"); // } else { - // log.info("[wms]上报成功"); + // log.info("[wms]上报成功"); // } } } @@ -545,24 +581,24 @@ public class StackerTaskServiceImpl implements IStackerTaskService { } } // for (TAppOrderOut orderOut : orderOuts) { - // OrderOutCBReq orderOutCBReq = new OrderOutCBReq(); - // orderOutCBReq.setTaskId(orderOut.getOrderId()); - // orderOutCBReq.setVehicleNo(vehicleId); - // orderOutCBReq.setResult(OrderOutCBEnums.COMPLETE.getCode()); - // orderOutCBReq.setResultMessage("出库完成"); + // OrderOutCBReq orderOutCBReq = new OrderOutCBReq(); + // orderOutCBReq.setTaskId(orderOut.getOrderId()); + // orderOutCBReq.setVehicleNo(vehicleId); + // orderOutCBReq.setResult(OrderOutCBEnums.COMPLETE.getCode()); + // orderOutCBReq.setResultMessage("出库完成"); - // int times = 0; - // MesApiResponse response = null; - // do { - // response = externalApiService.invokeOrderOutCB(orderOutCBReq); - // times++; - // Thread.sleep(15000L * times); - // } while (response.getCode() != 0 && times <= 10); - // if (response.getCode() != 0) { - // log.error("[WMS]上报失败"); - // } else { - // log.info("[WMS]上报成功"); - // } + // int times = 0; + // MesApiResponse response = null; + // do { + // response = externalApiService.invokeOrderOutCB(orderOutCBReq); + // times++; + // Thread.sleep(15000L * times); + // } while (response.getCode() != 0 && times <= 10); + // if (response.getCode() != 0) { + // log.error("[WMS]上报失败"); + // } else { + // log.info("[WMS]上报成功"); + // } // } } }