From 663d1ec7cd68ec40b27f889daec747f1b534c6f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B8=B8=E7=86=9F=E5=90=B4=E5=BD=A6=E7=A5=96?= Date: Thu, 2 Oct 2025 01:25:15 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B9=90=E8=A7=82=E9=94=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mapper/WcsIntegrationMapper.java | 13 ++++++++ .../service/impl/WcsTaskServiceImpl.java | 31 ++++++++++++++----- .../WcsIntegrationMapper.xml | 10 ++++++ 3 files changed, 46 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/gaotao/modules/automatedWarehouse/mapper/WcsIntegrationMapper.java b/src/main/java/com/gaotao/modules/automatedWarehouse/mapper/WcsIntegrationMapper.java index bec9562..bd27c93 100644 --- a/src/main/java/com/gaotao/modules/automatedWarehouse/mapper/WcsIntegrationMapper.java +++ b/src/main/java/com/gaotao/modules/automatedWarehouse/mapper/WcsIntegrationMapper.java @@ -237,6 +237,19 @@ public interface WcsIntegrationMapper { @Param("processStartTime") Date processStartTime, @Param("processEndTime") Date processEndTime, @Param("errorMsg") String errorMsg); + + /** + * 原子性更新WCS回调任务状态(乐观锁,防止重复处理) - AI制作 + * @param id 任务ID + * @param oldStatus 原状态(必须匹配才能更新) + * @param newStatus 新状态 + * @param processStartTime 处理开始时间 + * @return 影响的行数(0=更新失败,1=更新成功) + */ + int updateWcsCallbackTaskStatusWithLock(@Param("id") Long id, + @Param("oldStatus") String oldStatus, + @Param("newStatus") String newStatus, + @Param("processStartTime") Date processStartTime); void updatePalletWcsLocation(@Param("site") String site, @Param("palletId") String palletId, @Param("location") String location); /** 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 index 3722b8a..e9e88c3 100644 --- a/src/main/java/com/gaotao/modules/automatedWarehouse/service/impl/WcsTaskServiceImpl.java +++ b/src/main/java/com/gaotao/modules/automatedWarehouse/service/impl/WcsTaskServiceImpl.java @@ -39,20 +39,35 @@ public class WcsTaskServiceImpl implements WcsTaskService { @Override public void processWcsCallbackTask(WcsCallbackTask callbackTask) { try { - log.info("处理WCS回调任务:palletId={}, transType={}", callbackTask.getPalletId(), callbackTask.getTransTypeDesc()); + log.info("处理WCS回调任务:palletId={}, transType={}, currentStatus={}", + callbackTask.getPalletId(), callbackTask.getTransTypeDesc(), callbackTask.getStatus()); + + // 1. 使用乐观锁更新状态为处理中(防止重复处理) + int updateCount = wcsIntegrationMapper.updateWcsCallbackTaskStatusWithLock( + callbackTask.getId(), + callbackTask.getStatus(), // 原状态(已录入 or 处理失败) + "处理中", + new Date() + ); + + // 2. 如果更新失败,说明已被其他线程处理,直接返回 + if (updateCount == 0) { + log.warn("任务已被其他线程处理,跳过:id={}, palletId={}, status={}", + callbackTask.getId(), callbackTask.getPalletId(), callbackTask.getStatus()); + return; // 跳过,不抛异常 + } - // 1. 更新状态为处理中 - wcsIntegrationMapper.updateWcsCallbackTaskStatus(callbackTask.getId(), "处理中", new Date(), null, null); + log.info("成功锁定任务,开始处理:palletId={}", callbackTask.getPalletId()); - // 2. 构建移库请求参数 + // 3. 构建移库请求参数 WareHouseTransferRequest request = buildWareHouseTransferRequest(callbackTask); - // 3. 调用通用移库方法(原样调用,不修改) + // 4. 调用通用移库方法(原样调用,不修改) String result = wmsMessageService.doWareHouseForPallet(request); log.info("WCS回调任务处理成功:palletId={}, result={}", callbackTask.getPalletId(), result); - // 4. 如果是入库,更新对应的wms_order_task状态 + // 5. 如果是入库,更新对应的wms_order_task状态 if ("入库".equals(callbackTask.getTransTypeDesc())) { updateOrderTaskStatusForInbound(callbackTask); //如果更新wcs库位 @@ -62,11 +77,11 @@ public class WcsTaskServiceImpl implements WcsTaskService { wcsIntegrationMapper.updatePalletWcsLocation(callbackTask.getSite(), callbackTask.getPalletId(), ""); } - // 5. 更新任务状态为已完成 + // 6. 更新任务状态为已完成 wcsIntegrationMapper.updateWcsCallbackTaskStatus(callbackTask.getId(), "已完成", null, new Date(), null); } catch (Exception e) { - log.error("处理WCS回调任务失败:palletId={}, error={}", callbackTask.getPalletId(), e.getMessage()); + log.error("处理WCS回调任务失败:palletId={}", callbackTask.getPalletId(), e); // 记录完整堆栈 // 更新重试次数 int newRetryCount = (callbackTask.getRetryCount() == null ? 0 : callbackTask.getRetryCount()) + 1; diff --git a/src/main/resources/mapper/automatedWarehouse/WcsIntegrationMapper.xml b/src/main/resources/mapper/automatedWarehouse/WcsIntegrationMapper.xml index bfd664c..926cce1 100644 --- a/src/main/resources/mapper/automatedWarehouse/WcsIntegrationMapper.xml +++ b/src/main/resources/mapper/automatedWarehouse/WcsIntegrationMapper.xml @@ -551,6 +551,16 @@ where id = #{id} + + + + UPDATE wcs_callback_task + SET status = #{newStatus}, + process_start_time = #{processStartTime} + WHERE id = #{id} + AND status = #{oldStatus} + + update pallet set location_code = #{location},updated_by='wms_sys',updated_time=getdate() where site = #{site} and pallet_id = #{palletId}