From 96adc295cb04fd135d63d3a907f2732274f90965 Mon Sep 17 00:00:00 2001
From: wanshenmean <cathay_xy@163.com>
Date: 星期二, 21 四月 2026 01:11:21 +0800
Subject: [PATCH] feat: 添加MES异步上传辅助服务并重构相关代码

---
 Code/WMS/WIDESEA_WMSServer/WIDESEA_WMSServer/Controllers/Stock/StockInfoController.cs |  236 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 234 insertions(+), 2 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 62af775..c04b982 100644
--- a/Code/WMS/WIDESEA_WMSServer/WIDESEA_WMSServer/Controllers/Stock/StockInfoController.cs
+++ b/Code/WMS/WIDESEA_WMSServer/WIDESEA_WMSServer/Controllers/Stock/StockInfoController.cs
@@ -4,8 +4,13 @@
 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;
+using WIDESEA_Common.Constants;
+using WIDESEA_Common.StockEnum;
 
 namespace WIDESEA_WMSServer.Controllers.Stock
 {
@@ -16,8 +21,22 @@
     [ApiController]
     public class StockInfoController : ApiBaseController<IStockInfoService, Dt_StockInfo>
     {
-        public StockInfoController(IStockInfoService service) : base(service)
+        private readonly IMesService _mesService;
+        private readonly IMESDeviceConfigService _mesDeviceConfigService;
+        private readonly ISys_DictionaryService _sysDictionaryService;
+        private readonly IMesUploadHelper _mesUploadHelper;
+
+        public StockInfoController(
+            IStockInfoService service,
+            IMesService mesService,
+            IMESDeviceConfigService mesDeviceConfigService,
+            ISys_DictionaryService sysDictionaryService,
+            IMesUploadHelper mesUploadHelper) : base(service)
         {
+            _mesService = mesService;
+            _mesDeviceConfigService = mesDeviceConfigService;
+            _sysDictionaryService = sysDictionaryService;
+            _mesUploadHelper = mesUploadHelper;
         }
 
         /// <summary>
@@ -29,7 +48,220 @@
         public async Task<WebResponseContent> Get3DLayout(int warehouseId)
         {
             var result = await Service.Get3DLayoutAsync(warehouseId);
-            return WebResponseContent.Instance.OK(result);
+            return WebResponseContent.Instance.OK(data: result);
+        }
+
+        /// <summary>
+        /// 鎵樼洏杩涚珯 - 璋冪敤MES鎺ュ彛
+        /// </summary>
+        /// <param name="dto">杩涚珯璇锋眰鍙傛暟</param>
+        /// <returns>鎿嶄綔缁撴灉</returns>
+        [HttpPost("inboundInContainer")]
+        public async Task<WebResponseContent> InboundInContainer([FromBody] InboundInContainerRequestDto dto)
+        {
+            var response = new WebResponseContent();
+
+            try
+            {
+                // 1. 鍙傛暟楠岃瘉
+                if (string.IsNullOrWhiteSpace(dto.PalletCode))
+                {
+                    return response.Error("鎵樼洏缂栧彿涓嶈兘涓虹┖");
+                }
+
+                // 2. 鏌ヨ搴撳瓨淇℃伅 - 浣跨敤鍗曟潯璁板綍鏌ヨ鏂规硶鎻愰珮鏁堢巼
+                var stockInfo = await Service.Repository.QueryDataNavFirstAsync(x => x.Id == dto.StockId);
+
+                if (stockInfo == null)
+                {
+                    return response.Error("搴撳瓨淇℃伅涓嶅瓨鍦�");
+                }
+
+                // 3. 楠岃瘉搴撳瓨鐘舵�侊紙浠�"鍏ュ簱瀹屾垚"鐘舵�佸厑璁歌繘绔欙級
+                if (stockInfo.StockStatus != StockStatusEmun.鍏ュ簱瀹屾垚.GetHashCode())
+                {
+                    return response.Error($"褰撳墠搴撳瓨鐘舵�佷笉鍏佽杩涚珯鎿嶄綔锛屽綋鍓嶇姸鎬侊細{stockInfo.StockStatus}");
+                }
+
+                // 4. 鍔ㄦ�佽幏鍙朚ES鍑瘉
+                string deviceName = stockInfo.Remark == "GW_1" ? "楂樻俯闈欑疆1"
+                    : stockInfo.Remark == "GW_2" ? "楂樻俯闈欑疆2"
+                    : "甯告俯闈欑疆1";
+                var mesConfig = _mesDeviceConfigService.GetByDeviceName(deviceName);
+
+                var mesRequest = new InboundInContainerRequest
+                {
+                    EquipmentCode = mesConfig?.EquipmentCode ?? StockConstants.MES_EQUIPMENT_CODE,
+                    ResourceCode = mesConfig?.ResourceCode ?? StockConstants.MES_RESOURCE_CODE,
+                    LocalTime = DateTime.Now,
+                    ContainerCode = dto.PalletCode
+                };
+
+                string token = mesConfig?.Token;
+                string requestJson = System.Text.Json.JsonSerializer.Serialize(mesRequest);
+                string palletCode = stockInfo.PalletCode;
+
+                // 5. 寮傛鎵цMES璋冪敤锛坒ire-and-forget锛�
+                _mesUploadHelper.FireAndForget(
+                    palletCode,
+                    MesUploadStatusEnum.杩涚珯涓婁紶鎴愬姛,
+                    "InboundInContainer",
+                    requestJson,
+                    () =>
+                    {
+                        var result = string.IsNullOrWhiteSpace(token)
+                            ? _mesService.InboundInContainer(mesRequest)
+                            : _mesService.InboundInContainer(mesRequest, token);
+                        return (
+                            result?.IsSuccess ?? false,
+                            System.Text.Json.JsonSerializer.Serialize(result),
+                            result?.ErrorMessage ?? "鏈煡閿欒"
+                        );
+                    },
+                    App.User.UserName);
+
+                // 6. 绔嬪嵆杩斿洖鎴愬姛
+                return response.OK("鎵樼洏杩涚珯鎴愬姛");
+            }
+            catch (System.Exception ex)
+            {
+                return response.Error($"鎵樼洏杩涚珯澶辫触: {ex.Message}");
+            }
+        }
+
+        /// <summary>
+        /// 鎵樼洏鍑虹珯 - 璋冪敤MES鎺ュ彛
+        /// </summary>
+        /// <param name="dto">鍑虹珯璇锋眰鍙傛暟</param>
+        /// <returns>鎿嶄綔缁撴灉</returns>
+        [HttpPost("outboundInContainer")]
+        public async Task<WebResponseContent> OutboundInContainer([FromBody] OutboundInContainerRequestDto dto)
+        {
+            var response = new WebResponseContent();
+
+            try
+            {
+                // 1. 鍙傛暟楠岃瘉
+                if (string.IsNullOrWhiteSpace(dto.PalletCode))
+                {
+                    return response.Error("鎵樼洏缂栧彿涓嶈兘涓虹┖");
+                }
+
+                // 2. 鏌ヨ搴撳瓨淇℃伅 - 浣跨敤鍗曟潯璁板綍鏌ヨ鏂规硶鎻愰珮鏁堢巼
+                var stockInfo = await Service.Repository.QueryDataNavFirstAsync(x => x.Id == dto.StockId);
+
+                if (stockInfo == null)
+                {
+                    return response.Error("搴撳瓨淇℃伅涓嶅瓨鍦�");
+                }
+
+                // 3. 楠岃瘉搴撳瓨鐘舵�侊紙"鍑哄簱閿佸畾"鎴�"鍑哄簱瀹屾垚"鐘舵�佸厑璁稿嚭绔欙級
+                var allowedStatuses = new[]
+                {
+                    StockStatusEmun.鍑哄簱閿佸畾.GetHashCode(),
+                    StockStatusEmun.鍑哄簱瀹屾垚.GetHashCode()
+                };
+
+                if (!allowedStatuses.Contains(stockInfo.StockStatus))
+                {
+                    return response.Error($"褰撳墠搴撳瓨鐘舵�佷笉鍏佽鍑虹珯鎿嶄綔锛屽綋鍓嶇姸鎬侊細{stockInfo.StockStatus}");
+                }
+
+                // 4. 鍔ㄦ�佽幏鍙朚ES鍑瘉
+                string deviceName = stockInfo.Remark == "GW_1" ? "楂樻俯闈欑疆1"
+                    : stockInfo.Remark == "GW_2" ? "楂樻俯闈欑疆2"
+                    : "甯告俯闈欑疆1";
+                var mesConfig = _mesDeviceConfigService.GetByDeviceName(deviceName);
+
+                var mesRequest = new OutboundInContainerRequest
+                {
+                    EquipmentCode = mesConfig?.EquipmentCode ?? StockConstants.MES_EQUIPMENT_CODE,
+                    ResourceCode = mesConfig?.ResourceCode ?? StockConstants.MES_RESOURCE_CODE,
+                    LocalTime = DateTime.Now,
+                    ContainerCode = dto.PalletCode,
+                    ParamList = dto.ParamList?.Select(p => new ParamItem
+                    {
+                        ParamCode = p.ParamCode,
+                        ParamValue = p.ParamValue,
+                        CollectionTime = DateTime.TryParse(p.CollectionTime, out var ct) ? ct : DateTime.Now
+                    }).ToList()
+                };
+
+                string token = mesConfig?.Token;
+                string requestJson = System.Text.Json.JsonSerializer.Serialize(mesRequest);
+                string palletCode = stockInfo.PalletCode;
+
+                // 5. 寮傛鎵цMES璋冪敤锛坒ire-and-forget锛�
+                _mesUploadHelper.FireAndForget(
+                    palletCode,
+                    MesUploadStatusEnum.鍑虹珯涓婁紶鎴愬姛,
+                    "OutboundInContainer",
+                    requestJson,
+                    () =>
+                    {
+                        var result = string.IsNullOrWhiteSpace(token)
+                            ? _mesService.OutboundInContainer(mesRequest)
+                            : _mesService.OutboundInContainer(mesRequest, token);
+                        return (
+                            result?.IsSuccess ?? false,
+                            System.Text.Json.JsonSerializer.Serialize(result),
+                            result?.ErrorMessage ?? "鏈煡閿欒"
+                        );
+                    },
+                    App.User.UserName);
+
+                // 6. 绔嬪嵆杩斿洖鎴愬姛
+                return response.OK("鎵樼洏鍑虹珯鎴愬姛");
+            }
+            catch (System.Exception ex)
+            {
+                return response.Error($"鎵樼洏鍑虹珯澶辫触: {ex.Message}");
+            }
+        }
+
+        /// <summary>
+        /// 浠庨厤缃瓧鍏镐腑鑾峰彇閰嶇疆鍊�
+        /// </summary>
+        /// <param name="configs">閰嶇疆瀛楀吀鍒楄〃</param>
+        /// <param name="key">閰嶇疆閿�</param>
+        /// <param name="defaultValue">榛樿鍊�</param>
+        /// <returns>閰嶇疆鍊�</returns>
+        private string GetConfigValue(System.Collections.Generic.List<WIDESEA_DTO.System.VueDictionaryDTO> configs, string key, string defaultValue = "")
+        {
+            if (configs != null)
+            {
+                var config = configs.FirstOrDefault(c => c.DicNo == key);
+                if (config != null && config.Data != null)
+                {
+                    // Data鏄痙ynamic绫诲瀷锛屽皾璇曡幏鍙栫涓�涓厓绱犵殑value灞炴��
+                    try
+                    {
+                        // 浣跨敤dynamic鏉ヨ闂尶鍚嶇被鍨嬬殑灞炴��
+                        dynamic data = config.Data;
+                        if (data != null)
+                        {
+                            // data鍙兘鏄疘Enumerable鎴栬�呭崟涓璞�
+                            var enumerable = data as System.Collections.IEnumerable;
+                            if (enumerable != null)
+                            {
+                                foreach (var item in enumerable)
+                                {
+                                    // 鑾峰彇绗竴涓厓绱�
+                                    dynamic firstItem = item;
+                                    var value = firstItem.value;
+                                    return value?.ToString() ?? defaultValue;
+                                }
+                            }
+                        }
+                    }
+                    catch
+                    {
+                        // 濡傛灉鏃犳硶鑾峰彇锛岃繑鍥為粯璁ゅ��
+                        return defaultValue;
+                    }
+                }
+            }
+            return defaultValue;
         }
     }
 }

--
Gitblit v1.9.3