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/StockInfoDetailController.cs | 259 ++++++++++++++++++++-------------------------------
1 files changed, 102 insertions(+), 157 deletions(-)
diff --git a/Code/WMS/WIDESEA_WMSServer/WIDESEA_WMSServer/Controllers/Stock/StockInfoDetailController.cs b/Code/WMS/WIDESEA_WMSServer/WIDESEA_WMSServer/Controllers/Stock/StockInfoDetailController.cs
index 1c4242a..1b3f335 100644
--- a/Code/WMS/WIDESEA_WMSServer/WIDESEA_WMSServer/Controllers/Stock/StockInfoDetailController.cs
+++ b/Code/WMS/WIDESEA_WMSServer/WIDESEA_WMSServer/Controllers/Stock/StockInfoDetailController.cs
@@ -5,10 +5,10 @@
using WIDESEA_DTO.MES;
using WIDESEA_IStockService;
using WIDESEA_IBasicService;
-using WIDESEA_IMesService;
using WIDESEA_ISystemService;
using WIDESEA_Model.Models;
-using System.Diagnostics;
+using WIDESEA_Common.Constants;
+using WIDESEA_Common.StockEnum;
namespace WIDESEA_WMSServer.Controllers.Stock
{
@@ -19,19 +19,25 @@
[ApiController]
public class StockInfoDetailController : ApiBaseController<IStockInfoDetailService, Dt_StockInfoDetail>
{
- private readonly IMesLogService _mesLogService;
private readonly IMesService _mesService;
private readonly ISys_DictionaryService _sysDictionaryService;
+ private readonly IStockInfoService _stockInfoService;
+ private readonly IMESDeviceConfigService _mesDeviceConfigService;
+ private readonly IMesUploadHelper _mesUploadHelper;
public StockInfoDetailController(
IStockInfoDetailService service,
- IMesLogService mesLogService,
IMesService mesService,
- ISys_DictionaryService sysDictionaryService) : base(service)
+ ISys_DictionaryService sysDictionaryService,
+ IStockInfoService stockInfoService,
+ IMESDeviceConfigService mesDeviceConfigService,
+ IMesUploadHelper mesUploadHelper) : base(service)
{
- _mesLogService = mesLogService;
_mesService = mesService;
_sysDictionaryService = sysDictionaryService;
+ _stockInfoService = stockInfoService;
+ _mesDeviceConfigService = mesDeviceConfigService;
+ _mesUploadHelper = mesUploadHelper;
}
/// <summary>
@@ -43,41 +49,36 @@
public async Task<WebResponseContent> BindContainer([FromBody] BindContainerRequestDto dto)
{
var response = new WebResponseContent();
- var stopwatch = Stopwatch.StartNew();
try
{
// 1. 鍙傛暟楠岃瘉
- if (string.IsNullOrWhiteSpace(dto.PalletCode))
- {
- return response.Error("鎵樼洏缂栧彿涓嶈兘涓虹┖");
- }
-
if (dto.SfcList == null || !dto.SfcList.Any())
{
return response.Error("鐢佃姱鐮佸垪琛ㄤ笉鑳戒负绌�");
}
// 2. 楠岃瘉鐢佃姱鐘舵�侊紙闈�'宸查攣瀹�'鐘舵�佸厑璁哥粦瀹氾級
- var stockDetails = await Service.Repository.QueryDataAsync(x => dto.SfcList.Contains(x.SerialNumber));
- if (stockDetails != null && stockDetails.Any(d => d.Status == 99))
+ var stockDetail = await Service.Repository.QueryFirstAsync(x => dto.SfcList.Contains(x.SerialNumber));
+ if (stockDetail != null && stockDetail.Status == 99)
{
return response.Error("褰撳墠搴撳瓨鏄庣粏鍖呭惈宸查攣瀹氱姸鎬侊紝涓嶅厑璁告墽琛岀粦瀹氭搷浣�");
}
+ var stockInfo = await _stockInfoService.Repository.QueryFirstAsync(x => stockDetail.StockId == x.Id);
- // 3. 鑾峰彇绯荤粺閰嶇疆
- var configs = _sysDictionaryService.GetVueDictionary(new[] { "MES_EquipmentCode", "MES_ResourceCode" });
+ // 3. 鍔ㄦ�佽幏鍙朚ES鍑瘉
+ var mesConfig = _mesDeviceConfigService.GetByDeviceName("缁勭洏鏈烘鎵�");
+ string equipmentCode = mesConfig?.EquipmentCode ?? StockConstants.MES_EQUIPMENT_CODE;
+ string resourceCode = mesConfig?.ResourceCode ?? StockConstants.MES_RESOURCE_CODE;
+ string token = mesConfig?.Token;
- string equipmentCode = GetConfigValue(configs, "MES_EquipmentCode", "WCS_001");
- string resourceCode = GetConfigValue(configs, "MES_ResourceCode", "RESOURCE_001");
-
- // 3. 鏋勯�燤ES璇锋眰 - 灏嗙數鑺垪琛ㄨ浆鎹负ContainerSfcItem鏍煎紡
+ // 4. 鏋勯�燤ES璇锋眰 - 灏嗙數鑺垪琛ㄨ浆鎹负ContainerSfcItem鏍煎紡
var mesRequest = new BindContainerRequest
{
EquipmentCode = equipmentCode,
ResourceCode = resourceCode,
LocalTime = DateTime.Now,
- ContainerCode = dto.PalletCode,
+ ContainerCode = stockInfo.PalletCode,
ContainerSfcList = dto.SfcList.Select(sfc => new ContainerSfcItem
{
Sfc = sfc,
@@ -88,46 +89,30 @@
string requestJson = System.Text.Json.JsonSerializer.Serialize(mesRequest);
- // 4. 璋冪敤MES鎺ュ彛锛堝悓姝ユ柟娉曪級
- var mesResult = _mesService.BindContainer(mesRequest);
- stopwatch.Stop();
+ // 5. 寮傛璋冪敤MES鎺ュ彛锛坒ire-and-forget锛�
+ _mesUploadHelper.FireAndForget(
+ stockInfo.PalletCode,
+ MesUploadStatusEnum.缁勭洏涓婁紶鎴愬姛,
+ "BindContainer",
+ requestJson,
+ () =>
+ {
+ var result = string.IsNullOrWhiteSpace(token)
+ ? _mesService.BindContainer(mesRequest)
+ : _mesService.BindContainer(mesRequest, token);
+ return (
+ result?.IsSuccess ?? false,
+ System.Text.Json.JsonSerializer.Serialize(result),
+ result?.ErrorMessage ?? "鏈煡閿欒"
+ );
+ },
+ App.User.UserName);
- // 5. 璁板綍鏃ュ織
- await _mesLogService.LogAsync(new MesApiLogDto
- {
- ApiType = "BindContainer",
- RequestJson = requestJson,
- ResponseJson = System.Text.Json.JsonSerializer.Serialize(mesResult),
- IsSuccess = mesResult.IsSuccess,
- ErrorMessage = mesResult.ErrorMessage,
- ElapsedMs = (int)stopwatch.ElapsedMilliseconds,
- Creator = App.User.UserName
- });
-
- // 6. 杩斿洖缁撴灉
- if (mesResult.IsSuccess)
- {
- return response.OK("鎵樼洏鐢佃姱缁戝畾鎴愬姛");
- }
- else
- {
- return response.Error($"MES鎺ュ彛璋冪敤澶辫触: {mesResult.ErrorMessage}");
- }
+ // 6. 绔嬪嵆杩斿洖鎴愬姛鍝嶅簲
+ return response.OK("鎵樼洏鐢佃姱缁戝畾鎴愬姛");
}
- catch (System.Exception ex)
+ catch (Exception ex)
{
- stopwatch.Stop();
-
- // 璁板綍閿欒鏃ュ織
- await _mesLogService.LogAsync(new MesApiLogDto
- {
- ApiType = "BindContainer",
- IsSuccess = false,
- ErrorMessage = ex.Message,
- ElapsedMs = (int)stopwatch.ElapsedMilliseconds,
- Creator = App.User.UserName
- });
-
return response.Error($"鎵樼洏鐢佃姱缁戝畾澶辫触: {ex.Message}");
}
}
@@ -141,86 +126,65 @@
public async Task<WebResponseContent> UnbindContainer([FromBody] UnbindContainerRequestDto dto)
{
var response = new WebResponseContent();
- var stopwatch = Stopwatch.StartNew();
try
{
// 1. 鍙傛暟楠岃瘉
- if (string.IsNullOrWhiteSpace(dto.PalletCode))
- {
- return response.Error("鎵樼洏缂栧彿涓嶈兘涓虹┖");
- }
-
if (dto.SfcList == null || !dto.SfcList.Any())
{
return response.Error("鐢佃姱鐮佸垪琛ㄤ笉鑳戒负绌�");
}
// 2. 楠岃瘉鐢佃姱鐘舵�侊紙闈�'宸查攣瀹�'鐘舵�佸厑璁歌В缁戯級
- var stockDetails = await Service.Repository.QueryDataAsync(x => dto.SfcList.Contains(x.SerialNumber));
- if (stockDetails != null && stockDetails.Any(d => d.Status == 99))
+ var stockDetail = await Service.Repository.QueryFirstAsync(x => dto.SfcList.Contains(x.SerialNumber));
+ if (stockDetail != null && stockDetail.Status == 99)
{
return response.Error("褰撳墠搴撳瓨鏄庣粏鍖呭惈宸查攣瀹氱姸鎬侊紝涓嶅厑璁告墽琛岃В缁戞搷浣�");
}
+ var stockInfo = await _stockInfoService.Repository.QueryFirstAsync(x => stockDetail.StockId == x.Id);
- // 3. 鑾峰彇绯荤粺閰嶇疆
- var configs = _sysDictionaryService.GetVueDictionary(new[] { "MES_EquipmentCode", "MES_ResourceCode" });
+ // 3. 鍔ㄦ�佽幏鍙朚ES鍑瘉
+ var mesConfig = _mesDeviceConfigService.GetByDeviceName("缁勭洏鏈烘鎵�");
+ string equipmentCode = mesConfig?.EquipmentCode ?? StockConstants.MES_EQUIPMENT_CODE;
+ string resourceCode = mesConfig?.ResourceCode ?? StockConstants.MES_RESOURCE_CODE;
+ string token = mesConfig?.Token;
- string equipmentCode = GetConfigValue(configs, "MES_EquipmentCode", "WCS_001");
- string resourceCode = GetConfigValue(configs, "MES_ResourceCode", "RESOURCE_001");
-
- // 3. 鏋勯�燤ES璇锋眰
+ // 4. 鏋勯�燤ES璇锋眰
var mesRequest = new UnBindContainerRequest
{
EquipmentCode = equipmentCode,
ResourceCode = resourceCode,
LocalTime = DateTime.Now,
- ContainCode = dto.PalletCode,
+ ContainCode = stockInfo.PalletCode,
SfcList = dto.SfcList
};
string requestJson = System.Text.Json.JsonSerializer.Serialize(mesRequest);
- // 4. 璋冪敤MES鎺ュ彛锛堝悓姝ユ柟娉曪級
- var mesResult = _mesService.UnBindContainer(mesRequest);
- stopwatch.Stop();
+ // 5. 寮傛璋冪敤MES鎺ュ彛锛坒ire-and-forget锛�
+ _mesUploadHelper.FireAndForget(
+ stockInfo.PalletCode,
+ MesUploadStatusEnum.鎷嗙洏涓婁紶鎴愬姛,
+ "UnbindContainer",
+ requestJson,
+ () =>
+ {
+ var result = string.IsNullOrWhiteSpace(token)
+ ? _mesService.UnBindContainer(mesRequest)
+ : _mesService.UnBindContainer(mesRequest, token);
+ return (
+ result?.IsSuccess ?? false,
+ System.Text.Json.JsonSerializer.Serialize(result),
+ result?.ErrorMessage ?? "鏈煡閿欒"
+ );
+ },
+ App.User.UserName);
- // 5. 璁板綍鏃ュ織
- await _mesLogService.LogAsync(new MesApiLogDto
- {
- ApiType = "UnbindContainer",
- RequestJson = requestJson,
- ResponseJson = System.Text.Json.JsonSerializer.Serialize(mesResult),
- IsSuccess = mesResult.IsSuccess,
- ErrorMessage = mesResult.ErrorMessage,
- ElapsedMs = (int)stopwatch.ElapsedMilliseconds,
- Creator = App.User.UserName
- });
-
- // 6. 杩斿洖缁撴灉
- if (mesResult.IsSuccess)
- {
- return response.OK("鎵樼洏鐢佃姱瑙g粦鎴愬姛");
- }
- else
- {
- return response.Error($"MES鎺ュ彛璋冪敤澶辫触: {mesResult.ErrorMessage}");
- }
+ // 6. 绔嬪嵆杩斿洖鎴愬姛鍝嶅簲
+ return response.OK("鎵樼洏鐢佃姱瑙g粦鎴愬姛");
}
- catch (System.Exception ex)
+ catch (Exception ex)
{
- stopwatch.Stop();
-
- // 璁板綍閿欒鏃ュ織
- await _mesLogService.LogAsync(new MesApiLogDto
- {
- ApiType = "UnbindContainer",
- IsSuccess = false,
- ErrorMessage = ex.Message,
- ElapsedMs = (int)stopwatch.ElapsedMilliseconds,
- Creator = App.User.UserName
- });
-
return response.Error($"鎵樼洏鐢佃姱瑙g粦澶辫触: {ex.Message}");
}
}
@@ -234,7 +198,6 @@
public async Task<WebResponseContent> ContainerNgReport([FromBody] ContainerNgReportRequestDto dto)
{
var response = new WebResponseContent();
- var stopwatch = Stopwatch.StartNew();
try
{
@@ -251,25 +214,25 @@
// 2. 楠岃瘉鐢佃姱鐘舵�侊紙闈�'宸查攣瀹�'鐘舵�佸厑璁窷G涓婃姤锛�
var sfcList = dto.NgSfcList.Select(x => x.Sfc).ToList();
- var stockDetails = await Service.Repository.QueryDataAsync(x => sfcList.Contains(x.SerialNumber));
- if (stockDetails != null && stockDetails.Any(d => d.Status == 99))
+ var stockDetail = await Service.Repository.QueryFirstAsync(x => sfcList.Contains(x.SerialNumber));
+ if (stockDetail != null && stockDetail.Status == 99)
{
return response.Error("褰撳墠搴撳瓨鏄庣粏鍖呭惈宸查攣瀹氱姸鎬侊紝涓嶅厑璁告墽琛孨G涓婃姤鎿嶄綔");
}
+ var stockInfo = await _stockInfoService.Repository.QueryFirstAsync(x => stockDetail.StockId == x.Id);
- // 3. 鑾峰彇绯荤粺閰嶇疆
- var configs = _sysDictionaryService.GetVueDictionary(new[] { "MES_EquipmentCode", "MES_ResourceCode" });
+ // 3. 鍔ㄦ�佽幏鍙朚ES鍑瘉
+ var mesConfig = _mesDeviceConfigService.GetByDeviceName("缁勭洏鏈烘鎵�");
+ string equipmentCode = mesConfig?.EquipmentCode ?? StockConstants.MES_EQUIPMENT_CODE;
+ string resourceCode = mesConfig?.ResourceCode ?? StockConstants.MES_RESOURCE_CODE;
- string equipmentCode = GetConfigValue(configs, "MES_EquipmentCode", "WCS_001");
- string resourceCode = GetConfigValue(configs, "MES_ResourceCode", "RESOURCE_001");
-
- // 3. 鏋勯�燤ES璇锋眰 - 灏咲TO鏍煎紡杞崲涓篗ES璇锋眰鏍煎紡
+ // 4. 鏋勯�燤ES璇锋眰 - 灏咲TO鏍煎紡杞崲涓篗ES璇锋眰鏍煎紡
var mesRequest = new ContainerNgReportRequest
{
EquipmentCode = equipmentCode,
ResourceCode = resourceCode,
LocalTime = DateTime.Now,
- ContainerCode = dto.PalletCode,
+ ContainerCode = stockInfo.PalletCode,
NgSfcList = dto.NgSfcList.Select(ng => new NgSfcItem
{
Sfc = ng.Sfc,
@@ -281,46 +244,28 @@
string requestJson = System.Text.Json.JsonSerializer.Serialize(mesRequest);
- // 4. 璋冪敤MES鎺ュ彛锛堝悓姝ユ柟娉曪級
- var mesResult = _mesService.ContainerNgReport(mesRequest);
- stopwatch.Stop();
+ // 5. 寮傛璋冪敤MES鎺ュ彛锛坒ire-and-forget锛�
+ _mesUploadHelper.FireAndForget(
+ stockInfo.PalletCode,
+ MesUploadStatusEnum.NG涓婃姤鎴愬姛,
+ "ContainerNgReport",
+ requestJson,
+ () =>
+ {
+ var result = _mesService.ContainerNgReport(mesRequest);
+ return (
+ result?.IsSuccess ?? false,
+ System.Text.Json.JsonSerializer.Serialize(result),
+ result?.ErrorMessage ?? "鏈煡閿欒"
+ );
+ },
+ App.User.UserName);
- // 5. 璁板綍鏃ュ織
- await _mesLogService.LogAsync(new MesApiLogDto
- {
- ApiType = "ContainerNgReport",
- RequestJson = requestJson,
- ResponseJson = System.Text.Json.JsonSerializer.Serialize(mesResult),
- IsSuccess = mesResult.IsSuccess,
- ErrorMessage = mesResult.ErrorMessage,
- ElapsedMs = (int)stopwatch.ElapsedMilliseconds,
- Creator = App.User.UserName
- });
-
- // 6. 杩斿洖缁撴灉
- if (mesResult.IsSuccess)
- {
- return response.OK("NG鐢佃姱涓婃姤鎴愬姛");
- }
- else
- {
- return response.Error($"MES鎺ュ彛璋冪敤澶辫触: {mesResult.ErrorMessage}");
- }
+ // 6. 绔嬪嵆杩斿洖鎴愬姛鍝嶅簲
+ return response.OK("NG鐢佃姱涓婃姤鎴愬姛");
}
- catch (System.Exception ex)
+ catch (Exception ex)
{
- stopwatch.Stop();
-
- // 璁板綍閿欒鏃ュ織
- await _mesLogService.LogAsync(new MesApiLogDto
- {
- ApiType = "ContainerNgReport",
- IsSuccess = false,
- ErrorMessage = ex.Message,
- ElapsedMs = (int)stopwatch.ElapsedMilliseconds,
- Creator = App.User.UserName
- });
-
return response.Error($"NG鐢佃姱涓婃姤澶辫触: {ex.Message}");
}
}
--
Gitblit v1.9.3