From 4b483d9d06bead231b88ca212fd799196668a057 Mon Sep 17 00:00:00 2001
From: wanshenmean <cathay_xy@163.com>
Date: 星期二, 28 四月 2026 22:58:27 +0800
Subject: [PATCH] fix(任务服务): 修复出库任务选择逻辑和货位分配问题

---
 Code/WMS/WIDESEA_WMSServer/WIDESEA_WMSServer/Controllers/Stock/StockInfoController.cs |  140 +++++++++++++---------------------------------
 1 files changed, 41 insertions(+), 99 deletions(-)

diff --git a/Code/WMS/WIDESEA_WMSServer/WIDESEA_WMSServer/Controllers/Stock/StockInfoController.cs b/Code/WMS/WIDESEA_WMSServer/WIDESEA_WMSServer/Controllers/Stock/StockInfoController.cs
index 02447f2..aeed570 100644
--- a/Code/WMS/WIDESEA_WMSServer/WIDESEA_WMSServer/Controllers/Stock/StockInfoController.cs
+++ b/Code/WMS/WIDESEA_WMSServer/WIDESEA_WMSServer/Controllers/Stock/StockInfoController.cs
@@ -1,17 +1,14 @@
-锘縰sing Microsoft.AspNetCore.Authorization;
-using Microsoft.AspNetCore.Http;
-using Microsoft.AspNetCore.Mvc;
-using WIDESEA_Core;
-using WIDESEA_Core.BaseController;
-using WIDESEA_DTO.Stock;
-using WIDESEA_DTO.MES;
-using WIDESEA_IStockService;
-using WIDESEA_IBasicService;
-using WIDESEA_ISystemService;
-using WIDESEA_Model.Models;
+锘縰sing Microsoft.AspNetCore.Mvc;
 using WIDESEA_Common.Constants;
 using WIDESEA_Common.StockEnum;
-using System.Diagnostics;
+using WIDESEA_Core;
+using WIDESEA_Core.BaseController;
+using WIDESEA_Core.Helper;
+using WIDESEA_DTO.MES;
+using WIDESEA_IBasicService;
+using WIDESEA_IStockService;
+using WIDESEA_ISystemService;
+using WIDESEA_Model.Models;
 
 namespace WIDESEA_WMSServer.Controllers.Stock
 {
@@ -22,25 +19,22 @@
     [ApiController]
     public class StockInfoController : ApiBaseController<IStockInfoService, Dt_StockInfo>
     {
-        private readonly IMesLogService _mesLogService;
         private readonly IMesService _mesService;
         private readonly IMESDeviceConfigService _mesDeviceConfigService;
         private readonly ISys_DictionaryService _sysDictionaryService;
-        private readonly IStockInfoService _stockInfoService;
+        private readonly IMesUploadHelper _mesUploadHelper;
 
         public StockInfoController(
             IStockInfoService service,
-            IMesLogService mesLogService,
             IMesService mesService,
             IMESDeviceConfigService mesDeviceConfigService,
             ISys_DictionaryService sysDictionaryService,
-            IStockInfoService stockInfoService) : base(service)
+            IMesUploadHelper mesUploadHelper) : base(service)
         {
-            _mesLogService = mesLogService;
             _mesService = mesService;
             _mesDeviceConfigService = mesDeviceConfigService;
             _sysDictionaryService = sysDictionaryService;
-            _stockInfoService = stockInfoService;
+            _mesUploadHelper = mesUploadHelper;
         }
 
         /// <summary>
@@ -106,50 +100,24 @@
                 string palletCode = stockInfo.PalletCode;
 
                 // 5. 寮傛鎵цMES璋冪敤锛坒ire-and-forget锛�
-                _ = Task.Run(async () =>
-                {
-                    var localStopwatch = Stopwatch.StartNew();
-                    try
+                _mesUploadHelper.FireAndForget(
+                    palletCode,
+                    MesUploadStatusEnum.杩涚珯涓婁紶鎴愬姛,
+                    "InboundInContainer",
+                    requestJson,
+                    () =>
                     {
                         var result = string.IsNullOrWhiteSpace(token)
                             ? _mesService.InboundInContainer(mesRequest)
                             : _mesService.InboundInContainer(mesRequest, token);
-                        localStopwatch.Stop();
-
-                        bool isSuccess = result?.IsSuccess ?? false;
-                        int status = isSuccess
-                            ? (int)MesUploadStatusEnum.杩涚珯涓婁紶鎴愬姛
-                            : (int)MesUploadStatusEnum.杩涚珯涓婁紶澶辫触;
-
-                        await _stockInfoService.UpdateMesUploadStatusAsync(palletCode, status);
-
-                        await _mesLogService.LogAsync(new MesApiLogDto
-                        {
-                            PalletCode = palletCode,
-                            ApiType = "InboundInContainer",
-                            RequestJson = requestJson,
-                            ResponseJson = System.Text.Json.JsonSerializer.Serialize(result),
-                            IsSuccess = isSuccess,
-                            ErrorMessage = result?.ErrorMessage ?? "鏈煡閿欒",
-                            ElapsedMs = (int)localStopwatch.ElapsedMilliseconds,
-                            Creator = App.User.UserName
-                        });
-                    }
-                    catch (Exception ex)
-                    {
-                        localStopwatch.Stop();
-                        await _stockInfoService.UpdateMesUploadStatusAsync(palletCode, (int)MesUploadStatusEnum.杩涚珯涓婁紶澶辫触);
-                        await _mesLogService.LogAsync(new MesApiLogDto
-                        {
-                            PalletCode = palletCode,
-                            ApiType = "InboundInContainer",
-                            IsSuccess = false,
-                            ErrorMessage = ex.Message,
-                            ElapsedMs = (int)localStopwatch.ElapsedMilliseconds,
-                            Creator = App.User.UserName
-                        });
-                    }
-                });
+                        return (
+                            result.Data?.IsSuccess ?? false,
+                            System.Text.Json.JsonSerializer.Serialize(result),
+                            result?.Data?.Msg ?? result?.ErrorMessage ?? "鏈煡閿欒",
+                            _mesService.BuildConfig(token ?? string.Empty).ToJson()
+                        );
+                    },
+                    App.User.UserName);
 
                 // 6. 绔嬪嵆杩斿洖鎴愬姛
                 return response.OK("鎵樼洏杩涚珯鎴愬姛");
@@ -223,50 +191,24 @@
                 string palletCode = stockInfo.PalletCode;
 
                 // 5. 寮傛鎵цMES璋冪敤锛坒ire-and-forget锛�
-                _ = Task.Run(async () =>
-                {
-                    var localStopwatch = Stopwatch.StartNew();
-                    try
+                _mesUploadHelper.FireAndForget(
+                    palletCode,
+                    MesUploadStatusEnum.鍑虹珯涓婁紶鎴愬姛,
+                    "OutboundInContainer",
+                    requestJson,
+                    () =>
                     {
                         var result = string.IsNullOrWhiteSpace(token)
                             ? _mesService.OutboundInContainer(mesRequest)
                             : _mesService.OutboundInContainer(mesRequest, token);
-                        localStopwatch.Stop();
-
-                        bool isSuccess = result?.IsSuccess ?? false;
-                        int status = isSuccess
-                            ? (int)MesUploadStatusEnum.鍑虹珯涓婁紶鎴愬姛
-                            : (int)MesUploadStatusEnum.鍑虹珯涓婁紶澶辫触;
-
-                        await _stockInfoService.UpdateMesUploadStatusAsync(palletCode, status);
-
-                        await _mesLogService.LogAsync(new MesApiLogDto
-                        {
-                            PalletCode = palletCode,
-                            ApiType = "OutboundInContainer",
-                            RequestJson = requestJson,
-                            ResponseJson = System.Text.Json.JsonSerializer.Serialize(result),
-                            IsSuccess = isSuccess,
-                            ErrorMessage = result?.ErrorMessage ?? "鏈煡閿欒",
-                            ElapsedMs = (int)localStopwatch.ElapsedMilliseconds,
-                            Creator = App.User.UserName
-                        });
-                    }
-                    catch (Exception ex)
-                    {
-                        localStopwatch.Stop();
-                        await _stockInfoService.UpdateMesUploadStatusAsync(palletCode, (int)MesUploadStatusEnum.鍑虹珯涓婁紶澶辫触);
-                        await _mesLogService.LogAsync(new MesApiLogDto
-                        {
-                            PalletCode = palletCode,
-                            ApiType = "OutboundInContainer",
-                            IsSuccess = false,
-                            ErrorMessage = ex.Message,
-                            ElapsedMs = (int)localStopwatch.ElapsedMilliseconds,
-                            Creator = App.User.UserName
-                        });
-                    }
-                });
+                        return (
+                            result?.Data?.IsSuccess ?? false,
+                            System.Text.Json.JsonSerializer.Serialize(result),
+                            result?.Data?.Msg ?? result?.ErrorMessage ?? "鏈煡閿欒",
+                            _mesService.BuildConfig(token ?? string.Empty).ToJson()
+                        );
+                    },
+                    App.User.UserName);
 
                 // 6. 绔嬪嵆杩斿洖鎴愬姛
                 return response.OK("鎵樼洏鍑虹珯鎴愬姛");
@@ -322,4 +264,4 @@
             return defaultValue;
         }
     }
-}
+}
\ No newline at end of file

--
Gitblit v1.9.3