diff --git a/src/main/java/com/gaotao/modules/automatedWarehouse/service/WcsTaskService.java b/src/main/java/com/gaotao/modules/automatedWarehouse/service/WcsTaskService.java new file mode 100644 index 0000000..0cb3eea --- /dev/null +++ b/src/main/java/com/gaotao/modules/automatedWarehouse/service/WcsTaskService.java @@ -0,0 +1,45 @@ +package com.gaotao.modules.automatedWarehouse.service; + +import com.gaotao.modules.automatedWarehouse.entity.WcsCallbackTask; +import com.gaotao.modules.automatedWarehouse.entity.WmsOrderTask; + +import java.util.List; + +/** + * WCS任务处理服务接口 + */ +public interface WcsTaskService { + + /** + * 处理WCS回调任务 + * @param callbackTask WCS回调任务 + */ + void processWcsCallbackTask(WcsCallbackTask callbackTask); + + /** + * 获取待处理的WCS回调任务列表 + * @param site 站点 + * @return 待处理任务列表 + */ + List getPendingWcsCallbackTasks(String site); + + /** + * 处理立库调栈板出库任务 + * @param orderTask 订单任务 + */ + void processWcsPalletOutTask(WmsOrderTask orderTask); + + /** + * 获取待处理的立库调栈板出库任务列表 + * @param site 站点 + * @return 待处理任务列表 + */ + List getPendingWcsPalletOutTasks(String site); + + /** + * 构建移库请求参数 + * @param callbackTask WCS回调任务 + * @return 移库请求参数 + */ + Object buildWareHouseTransferRequest(WcsCallbackTask callbackTask); +} \ No newline at end of file diff --git a/src/main/java/com/gaotao/modules/automatedWarehouse/service/impl/WcsTaskServiceImpl.java b/src/main/java/com/gaotao/modules/automatedWarehouse/service/impl/WcsTaskServiceImpl.java new file mode 100644 index 0000000..018a199 --- /dev/null +++ b/src/main/java/com/gaotao/modules/automatedWarehouse/service/impl/WcsTaskServiceImpl.java @@ -0,0 +1,230 @@ +package com.gaotao.modules.automatedWarehouse.service.impl; + +import com.gaotao.modules.api.entity.WareHouseTransferRequest; +import com.gaotao.modules.api.service.WcsApiService; +import com.gaotao.modules.api.service.WmsMessageService; +import com.gaotao.modules.automatedWarehouse.entity.WcsCallbackTask; +import com.gaotao.modules.automatedWarehouse.entity.WmsOrderTask; +import com.gaotao.modules.automatedWarehouse.mapper.WcsIntegrationMapper; +import com.gaotao.modules.automatedWarehouse.service.WcsTaskService; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.Date; +import java.util.List; + +/** + * WCS任务处理服务实现类 + */ +@Slf4j +@Service +public class WcsTaskServiceImpl implements WcsTaskService { + + @Autowired + private WcsIntegrationMapper wcsIntegrationMapper; + + @Autowired + private WmsMessageService wmsMessageService; + + @Autowired + private WcsApiService wcsApiService; + + @Override + public List getPendingWcsCallbackTasks(String site) { + return wcsIntegrationMapper.getWcsCallbackTaskListWithStatus(site); + } + + @Override + public void processWcsCallbackTask(WcsCallbackTask callbackTask) { + try { + log.info("处理WCS回调任务:palletId={}, transType={}", callbackTask.getPalletId(), callbackTask.getTransTypeDesc()); + + // 1. 更新状态为处理中 + wcsIntegrationMapper.updateWcsCallbackTaskStatus(callbackTask.getId(), "处理中", new Date(), null, null); + + // 2. 构建移库请求参数 + WareHouseTransferRequest request = buildWareHouseTransferRequest(callbackTask); + + // 3. 调用通用移库方法(原样调用,不修改) + String result = wmsMessageService.doWareHouseForPallet(request); + + log.info("WCS回调任务处理成功:palletId={}, result={}", callbackTask.getPalletId(), result); + + // 4. 如果是入库,更新对应的wms_order_task状态 + if ("入库".equals(callbackTask.getTransTypeDesc())) { + updateOrderTaskStatusForInbound(callbackTask); + //如果更新wcs库位 + wcsIntegrationMapper.updatePalletWcsLocation(callbackTask.getSite(), callbackTask.getPalletId(), callbackTask.getToLocationId()); + } + if ("出库".equals(callbackTask.getTransTypeDesc())) { + wcsIntegrationMapper.updatePalletWcsLocation(callbackTask.getSite(), callbackTask.getPalletId(), ""); + } + + // 5. 更新任务状态为已完成 + wcsIntegrationMapper.updateWcsCallbackTaskStatus(callbackTask.getId(), "已完成", null, new Date(), null); + + } catch (Exception e) { + log.error("处理WCS回调任务失败:palletId={}, error={}", callbackTask.getPalletId(), e.getMessage()); + + // 更新重试次数 + int newRetryCount = (callbackTask.getRetryCount() == null ? 0 : callbackTask.getRetryCount()) + 1; + String status = newRetryCount >= 3 ? "处理失败" : "已录入"; // 重试3次后标记为失败 + + wcsIntegrationMapper.updateWcsCallbackTaskStatus(callbackTask.getId(), status, null, null, e.getMessage()); + wcsIntegrationMapper.updateWcsCallbackTaskRetryCount(callbackTask.getId(), newRetryCount); + + throw new RuntimeException("处理WCS回调任务失败: " + e.getMessage(), e); + } + } + + @Override + public List getPendingWcsPalletOutTasks(String site) { + return wcsIntegrationMapper.getWcsOrderTaskListForPalletOut(site); + } + + @Override + public void processWcsPalletOutTask(WmsOrderTask orderTask) { + try { + log.info("处理立库调栈板任务:taskNo={}, palletId={}", orderTask.getTaskNo(), orderTask.getPalletId()); + + // 1. 检查重试次数,避免无限重试 + int currentRetryCount = getCurrentRetryCount(orderTask); + if (currentRetryCount >= 3) { + log.warn("立库调栈板任务重试次数已达上限:taskNo={}, retryCount={}", orderTask.getTaskNo(), currentRetryCount); + wcsIntegrationMapper.updateOrderTaskStatusAndWmsStatus(orderTask.getId(), "处理失败", "重试次数超限"); + return; + } + + // 2. 更新状态为处理中 + wcsIntegrationMapper.updateOrderTaskStatusAndWmsStatus(orderTask.getId(), "处理中", "调用WCS中"); + + // 3. 调用WCS出库方法 + String result = callPalletOutWcs(orderTask); + + log.info("立库调栈板任务处理成功:taskNo={}, result={}", orderTask.getTaskNo(), result); + + // 4. 更新任务状态为已完成 + wcsIntegrationMapper.updateOrderTaskStatusAndWmsStatus(orderTask.getId(), "已完成", "WCS调用成功"); + + } catch (Exception e) { + log.error("处理立库调栈板任务失败:taskNo={}, error={}", orderTask.getTaskNo(), e.getMessage()); + + // 更新重试次数和错误信息 + int newRetryCount = getCurrentRetryCount(orderTask) + 1; + String status = newRetryCount >= 3 ? "处理失败" : "已创建"; // 重试3次后标记为失败 + String wmsStatus = newRetryCount >= 3 ? "重试次数超限" : "等待重试"; + + wcsIntegrationMapper.updateOrderTaskStatusAndWmsStatus(orderTask.getId(), status, wmsStatus); + wcsIntegrationMapper.updateOrderTaskErrorInfo(orderTask.getId(), "WCS_CALL_ERROR", e.getMessage()); + + throw new RuntimeException("处理立库调栈板任务失败: " + e.getMessage(), e); + } + } + + @Override + public WareHouseTransferRequest buildWareHouseTransferRequest(WcsCallbackTask callbackTask) { + WareHouseTransferRequest request = new WareHouseTransferRequest(); + + // 从回调任务中获取基本信息 + request.setSite(callbackTask.getSite()); + request.setPalletId(callbackTask.getPalletId()); + request.setToWarehouseId(callbackTask.getToWarehouseId()); + request.setToLocationId(callbackTask.getToLocationId()); + request.setTransType("移库"); + request.setToStation("*"); // 立库内的站点都为* + + // 根据transTypeDesc判断业务类型 + if ("入库".equals(callbackTask.getTransTypeDesc())) { + request.setBusinessType("立库入库"); + request.setRemark("WCS立库入库操作"); + } else if ("出库".equals(callbackTask.getTransTypeDesc())) { + request.setBusinessType("立库出库"); + request.setRemark("WCS立库出库操作"); + } else { + throw new RuntimeException("不支持的事务类型:" + callbackTask.getTransTypeDesc()); + } + + return request; + } + + /** + * 更新入库对应的订单任务状态 + * @param callbackTask WCS回调任务 + */ + private void updateOrderTaskStatusForInbound(WcsCallbackTask callbackTask) { + try { + // 查找对应的wms_order_task记录(source_type=组盘入库,pallet_id一致,status=已创建) + List orderTasks = wcsIntegrationMapper.findOrderTasksByPalletAndActionType( + callbackTask.getSite(), + callbackTask.getPalletId(), + "组盘入库", + "已创建" + ); + + if (orderTasks != null && !orderTasks.isEmpty()) { + for (WmsOrderTask orderTask : orderTasks) { + // 更新wms_order_task的status为已完成,wms_status为已完成 + wcsIntegrationMapper.updateOrderTaskStatusAndWmsStatus( + orderTask.getId(), "已完成", "已完成" + ); + + // 更新对应的wms_order_task_detail状态为已完成 + wcsIntegrationMapper.updateOrderTaskDetailStatusByTaskNo( + orderTask.getTaskNo(), "已完成", "已完成" + ); + + log.info("已更新订单任务状态:taskNo={}, palletId={}", + orderTask.getTaskNo(), callbackTask.getPalletId()); + } + } else { + log.warn("未找到对应的组盘入库订单任务:palletId={}", callbackTask.getPalletId()); + } + + } catch (Exception e) { + log.error("更新订单任务状态失败:palletId={}, error={}", + callbackTask.getPalletId(), e.getMessage()); + // 不抛出异常,避免影响主流程 + } + } + + /** + * 调用WCS出库方法 + * @param orderTask 订单任务 + * @return 调用结果 + */ + private String callPalletOutWcs(WmsOrderTask orderTask) { + try { + // 调用具体的WCS出库接口 + log.info("调用WCS出库接口:taskNo={}, palletId={}, fromLocation={}, toLocation={}", + orderTask.getTaskNo(), orderTask.getPalletId(), orderTask.getFromLocation(), orderTask.getToLocation()); + + wcsApiService.pushWCSTaskApi(orderTask); + return "WCS出库调用成功"; + + } catch (Exception e) { + log.error("调用WCS出库接口失败:taskNo={}, error={}", orderTask.getTaskNo(), e.getMessage()); + throw new RuntimeException("WCS出库调用失败:" + e.getMessage()); + } + } + + /** + * 获取当前任务的重试次数 + * @param orderTask 订单任务 + * @return 重试次数 + */ + private int getCurrentRetryCount(WmsOrderTask orderTask) { + try { + // 通过error_msg字段来判断重试次数,或者可以增加专门的重试次数字段 + String errorMsg = orderTask.getErrorMsg(); + if (errorMsg != null && errorMsg.contains("重试次数:")) { + String retryStr = errorMsg.substring(errorMsg.indexOf("重试次数:") + 5); + return Integer.parseInt(retryStr.split(",")[0]); + } + return 0; + } catch (Exception e) { + log.warn("获取重试次数失败,默认为0:taskNo={}", orderTask.getTaskNo()); + return 0; + } + } +} \ No newline at end of file diff --git a/src/main/resources/mapper/notify/NewIssureMapper.xml b/src/main/resources/mapper/notify/NewIssureMapper.xml index 67af3c6..8c296cb 100644 --- a/src/main/resources/mapper/notify/NewIssureMapper.xml +++ b/src/main/resources/mapper/notify/NewIssureMapper.xml @@ -144,7 +144,7 @@ update SOIssueNotifyHeader - set push_wcs_flag='Y' + set push_wcs_flag='已完成' where site=#{site} and notify_no=#{notifyNo}