From 63ca4ac71443473a0ff72758e1ae3739b7640a68 Mon Sep 17 00:00:00 2001
From: wanshenmean <cathay_xy@163.com>
Date: 星期二, 21 四月 2026 00:32:22 +0800
Subject: [PATCH] refactor(TaskService_Inbound): InboundInContainer改为Task.Run异步执行
---
Code/WMS/WIDESEA_WMSServer/WIDESEA_StockService/StockSerivce.cs | 177 +++++++++++++++++++++++++++++++++++++++++++++++++++++------
1 files changed, 159 insertions(+), 18 deletions(-)
diff --git a/Code/WMS/WIDESEA_WMSServer/WIDESEA_StockService/StockSerivce.cs b/Code/WMS/WIDESEA_WMSServer/WIDESEA_StockService/StockSerivce.cs
index d2f1044..81914e7 100644
--- a/Code/WMS/WIDESEA_WMSServer/WIDESEA_StockService/StockSerivce.cs
+++ b/Code/WMS/WIDESEA_WMSServer/WIDESEA_StockService/StockSerivce.cs
@@ -1,8 +1,10 @@
锘縰sing Newtonsoft.Json;
using SqlSugar;
+using System.Diagnostics;
using WIDESEA_Common.Constants;
using WIDESEA_Common.StockEnum;
using WIDESEA_Core;
+using WIDESEA_Core.Helper;
using WIDESEA_DTO.MES;
using WIDESEA_DTO.Stock;
using WIDESEA_IBasicService;
@@ -51,6 +53,8 @@
/// </summary>
public IMesService _mesService { get; }
+ private readonly IMesLogService _mesLogService;
+
/// <summary>
/// 鏋勯�犲嚱鏁�
/// </summary>
@@ -65,7 +69,8 @@
IStockInfo_HtyService stockInfo_HtyService,
IMesService mesService,
IWarehouseService warehouseService,
- ISqlSugarClient sqlSugarClient)
+ ISqlSugarClient sqlSugarClient,
+ IMesLogService mesLogService)
{
StockInfoDetailService = stockInfoDetailService;
StockInfoService = stockInfoService;
@@ -74,6 +79,7 @@
_mesService = mesService;
_warehouseService = warehouseService;
SqlSugarClient = sqlSugarClient;
+ _mesLogService = mesLogService;
}
/// <summary>
@@ -392,8 +398,9 @@
/// 鎵归噺鎷嗙洏纭 - 涓�娆℃�ц皟鐢∕ES瑙g粦鏁翠釜鎵樼洏
/// </summary>
/// <param name="palletCode">婧愭墭鐩樺彿</param>
+ /// <param name="deviceName">璁惧鍚嶇О锛堢敤浜庡姩鎬丮ES鍑瘉鏌ヨ锛�</param>
/// <returns>鎿嶄綔缁撴灉</returns>
- public async Task<WebResponseContent> SplitPalletConfirmAsync(string palletCode)
+ public async Task<WebResponseContent> SplitPalletConfirmAsync(string palletCode, string deviceName)
{
WebResponseContent content = new WebResponseContent();
try
@@ -412,22 +419,70 @@
if (sfcList == null || !sfcList.Any())
return content.Error("涓存椂琛ㄤ腑鐢佃姱鍒楄〃涓虹┖");
- // 2. 璋冪敤MES瑙g粦
+ // 2. 鑾峰彇MES璁惧閰嶇疆锛堝姩鎬佸嚟璇侊級
+ string equipmentCode = StockConstants.MES_EQUIPMENT_CODE;
+ string resourceCode = StockConstants.MES_RESOURCE_CODE;
+ string token = null;
+
+ if (!string.IsNullOrWhiteSpace(deviceName))
+ {
+ var mesConfig = ResolveMesConfig(deviceName, palletCode);
+ if (mesConfig != null)
+ {
+ equipmentCode = mesConfig.EquipmentCode;
+ resourceCode = mesConfig.ResourceCode;
+ token = mesConfig.Token;
+ }
+ }
+
+ // 3. Fire-and-forget寮傛璋冪敤MES瑙g粦
var unbindRequest = new UnBindContainerRequest
{
- EquipmentCode = StockConstants.MES_EQUIPMENT_CODE,
- ResourceCode = StockConstants.MES_RESOURCE_CODE,
+ EquipmentCode = equipmentCode,
+ ResourceCode = resourceCode,
LocalTime = DateTime.Now,
ContainCode = palletCode,
SfcList = sfcList
};
- var unbindResult = _mesService.UnBindContainer(unbindRequest);
- if (unbindResult == null || unbindResult.Data == null || !unbindResult.Data.IsSuccess)
+ _ = Task.Run(() =>
{
- return content.Error($"MES瑙g粦澶辫触: {unbindResult?.Data?.Msg ?? unbindResult?.ErrorMessage ?? "鏈煡閿欒"}");
- }
+ var stopwatch = Stopwatch.StartNew();
+ try
+ {
+ var unbindResult = string.IsNullOrWhiteSpace(token)
+ ? _mesService.UnBindContainer(unbindRequest)
+ : _mesService.UnBindContainer(unbindRequest, token);
+ stopwatch.Stop();
- // 3. 鍒犻櫎涓存椂琛ㄨ褰�
+ bool isSuccess = unbindResult?.Data?.IsSuccess ?? false;
+ int status = isSuccess
+ ? (int)MesUploadStatusEnum.鎷嗙洏涓婁紶鎴愬姛
+ : (int)MesUploadStatusEnum.鎷嗙洏涓婁紶澶辫触;
+
+ // 鏇存柊MES涓婁紶鐘舵��
+ StockInfoService.UpdateMesUploadStatusAsync(palletCode, status).ConfigureAwait(false);
+
+ // 璁板綍MES鏃ュ織
+ _mesLogService.LogAsync(new MesApiLogDto
+ {
+ PalletCode = palletCode,
+ ApiType = "UnBindContainer",
+ RequestJson = unbindRequest.ToJson(),
+ ResponseJson = System.Text.Json.JsonSerializer.Serialize(unbindResult),
+ IsSuccess = isSuccess,
+ ErrorMessage = unbindResult?.Data?.Msg ?? unbindResult?.ErrorMessage ?? "鏈煡閿欒",
+ ElapsedMs = (int)stopwatch.ElapsedMilliseconds,
+ Creator = "System"
+ }).ConfigureAwait(false);
+ }
+ catch (Exception ex)
+ {
+ // 璋冪敤澶辫触
+ StockInfoService.UpdateMesUploadStatusAsync(palletCode, (int)MesUploadStatusEnum.鎷嗙洏涓婁紶澶辫触).ConfigureAwait(false);
+ }
+ });
+
+ // 4. 鍒犻櫎涓存椂琛ㄨ褰�
await SqlSugarClient.Deleteable<Dt_SplitTemp>().Where(t => t.PalletCode == palletCode).ExecuteCommandAsync();
return content.OK("鎵归噺鎷嗙洏纭鎴愬姛");
@@ -442,10 +497,12 @@
/// 鎵归噺缁勭洏纭 - 涓�娆℃�ц皟鐢∕ES缁戝畾鏁翠釜鎵樼洏
/// </summary>
/// <param name="palletCode">鐩爣鎵樼洏鍙�</param>
+ /// <param name="deviceName">璁惧鍚嶇О锛堢敤浜庡姩鎬丮ES鍑瘉鏌ヨ锛�</param>
/// <returns>鎿嶄綔缁撴灉</returns>
- public async Task<WebResponseContent> GroupPalletConfirmAsync(string palletCode)
+ public async Task<WebResponseContent> GroupPalletConfirmAsync(string palletCode, string deviceName)
{
WebResponseContent content = new WebResponseContent();
+ var stopwatch = Stopwatch.StartNew();
try
{
if (string.IsNullOrWhiteSpace(palletCode))
@@ -460,12 +517,28 @@
if (details == null || !details.Any())
return content.Error("鎵樼洏涓嬫棤鐢佃姱鏁版嵁");
- // 2. 璋冪敤MES缁戝畾
+ // 2. 鑾峰彇MES璁惧閰嶇疆锛堝姩鎬佸嚟璇侊級
+ string equipmentCode = StockConstants.MES_EQUIPMENT_CODE;
+ string resourceCode = StockConstants.MES_RESOURCE_CODE;
+ string token = null;
+
+ if (!string.IsNullOrWhiteSpace(deviceName))
+ {
+ var mesConfig = ResolveMesConfig(deviceName, palletCode);
+ if (mesConfig != null)
+ {
+ equipmentCode = mesConfig.EquipmentCode;
+ resourceCode = mesConfig.ResourceCode;
+ token = mesConfig.Token;
+ }
+ }
+
+ // 3. 璋冪敤MES缁戝畾
var bindRequest = new BindContainerRequest
{
ContainerCode = palletCode,
- EquipmentCode = StockConstants.MES_EQUIPMENT_CODE,
- ResourceCode = StockConstants.MES_RESOURCE_CODE,
+ EquipmentCode = equipmentCode,
+ ResourceCode = resourceCode,
LocalTime = DateTime.Now,
OperationType = StockConstants.MES_BIND_OPERATION_TYPE,
ContainerSfcList = details.Select(d => new ContainerSfcItem
@@ -474,11 +547,45 @@
Location = d.InboundOrderRowNo.ToString()
}).ToList()
};
- var bindResult = _mesService.BindContainer(bindRequest);
- if (bindResult == null || bindResult.Data == null || !bindResult.Data.IsSuccess)
+ string requestJson = bindRequest.ToJson();
+ // 3. Fire-and-forget寮傛璋冪敤MES缁戝畾
+ _ = Task.Run(() =>
{
- return content.Error($"MES缁戝畾澶辫触: {bindResult?.Data?.Msg ?? bindResult?.ErrorMessage ?? "鏈煡閿欒"}");
- }
+ var stopwatch = Stopwatch.StartNew();
+ try
+ {
+ var bindResult = string.IsNullOrWhiteSpace(token)
+ ? _mesService.BindContainer(bindRequest)
+ : _mesService.BindContainer(bindRequest, token);
+ stopwatch.Stop();
+
+ bool isSuccess = bindResult?.Data?.IsSuccess ?? false;
+ int status = isSuccess
+ ? (int)MesUploadStatusEnum.缁勭洏涓婁紶鎴愬姛
+ : (int)MesUploadStatusEnum.缁勭洏涓婁紶澶辫触;
+
+ // 鏇存柊MES涓婁紶鐘舵��
+ StockInfoService.UpdateMesUploadStatusAsync(palletCode, status).ConfigureAwait(false);
+
+ // 璁板綍MES鏃ュ織
+ _mesLogService.LogAsync(new MesApiLogDto
+ {
+ PalletCode = palletCode,
+ ApiType = "BindContainer",
+ RequestJson = requestJson,
+ ResponseJson = System.Text.Json.JsonSerializer.Serialize(bindResult),
+ IsSuccess = isSuccess,
+ ErrorMessage = bindResult?.ErrorMessage ?? "鏈煡閿欒",
+ ElapsedMs = (int)stopwatch.ElapsedMilliseconds,
+ Creator = "System"
+ }).ConfigureAwait(false);
+ }
+ catch (Exception ex)
+ {
+ // 璋冪敤澶辫触
+ StockInfoService.UpdateMesUploadStatusAsync(palletCode, (int)MesUploadStatusEnum.缁勭洏涓婁紶澶辫触).ConfigureAwait(false);
+ }
+ });
return content.OK("鎵归噺缁勭洏纭鎴愬姛");
}
@@ -487,5 +594,39 @@
return content.Error($"鎵归噺缁勭洏纭澶辫触: {ex.Message}");
}
}
+
+ /// <summary>
+ /// 鏍规嵁璁惧鍚嶇О鍜屾墭鐩樺彿瑙f瀽MES璁惧閰嶇疆
+ /// </summary>
+ /// <param name="deviceName">璁惧鍚嶇О</param>
+ /// <param name="palletCode">鎵樼洏鍙凤紙鐢ㄤ簬鏌ヨ浠撳簱缂栫爜锛�</param>
+ /// <returns>MES璁惧閰嶇疆锛屼笉瀛樺湪鏃惰繑鍥瀗ull</returns>
+ private Dt_MESDeviceConfig ResolveMesConfig(string deviceName, string palletCode)
+ {
+ // 鎹㈢洏鏈烘鎵嬮渶瑕佸尯鍒嗕粨搴�
+ if (deviceName.Contains("鎹㈢洏"))
+ {
+ // 鏌ヨ鎵樼洏瀵瑰簲鐨勪粨搴撶紪鐮�
+ var stockInfo = StockInfoService.Repository.QueryFirst(s => s.PalletCode == palletCode);
+ if (stockInfo != null && stockInfo.WarehouseId > 0)
+ {
+ var warehouse = _warehouseService.Repository.QureyDataById(stockInfo.WarehouseId);
+ if (warehouse != null && !string.IsNullOrWhiteSpace(warehouse.WarehouseCode))
+ {
+ // 鍏堟寜璁惧鍚�+浠撳簱缂栫爜鏌ヨ
+ var config = SqlSugarClient.Queryable<Dt_MESDeviceConfig>()
+ .Where(c => c.DeviceName == deviceName && c.WarehouseCode == warehouse.WarehouseCode)
+ .First();
+ if (config != null)
+ return config;
+ }
+ }
+ }
+
+ // 鎸夎澶囧悕绉版煡璇紙閫傜敤浜庣粍鐩樻満姊版墜鎴栨崲鐩樻満姊版墜鏈壘鍒颁粨搴撳尮閰嶇殑鎯呭喌锛�
+ return SqlSugarClient.Queryable<Dt_MESDeviceConfig>()
+ .Where(c => c.DeviceName == deviceName)
+ .First();
+ }
}
}
\ No newline at end of file
--
Gitblit v1.9.3