wanshenmean
10 小时以前 57feefd4c9cd84de47b21b811638054373d4a46e
refactor(StockInfoDetailController): MES调用改为Task.Run异步执行

- BindContainer/UnbindContainer/ContainerNgReport改为fire-and-forget
- MES调用在后台Task.Run中执行,不阻塞响应
- 成功/失败状态通过UpdateMesUploadStatusAsync更新
- 立即返回成功响应给客户端

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
已修改1个文件
169 ■■■■■ 文件已修改
Code/WMS/WIDESEA_WMSServer/WIDESEA_WMSServer/Controllers/Stock/StockInfoDetailController.cs 169 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Code/WMS/WIDESEA_WMSServer/WIDESEA_WMSServer/Controllers/Stock/StockInfoDetailController.cs
@@ -7,6 +7,7 @@
using WIDESEA_IBasicService;
using WIDESEA_ISystemService;
using WIDESEA_Model.Models;
using WIDESEA_Common.StockEnum;
using System.Diagnostics;
namespace WIDESEA_WMSServer.Controllers.Stock
@@ -45,7 +46,6 @@
        public async Task<WebResponseContent> BindContainer([FromBody] BindContainerRequestDto dto)
        {
            var response = new WebResponseContent();
            var stopwatch = Stopwatch.StartNew();
            try
            {
@@ -80,46 +80,55 @@
                string requestJson = System.Text.Json.JsonSerializer.Serialize(mesRequest);
                // 4. 调用MES接口(同步方法)
                var mesResult = _mesService.BindContainer(mesRequest);
                stopwatch.Stop();
                // 4. 异步调用MES接口(fire-and-forget)
                _ = Task.Run(async () =>
                {
                    var localStopwatch = Stopwatch.StartNew();
                    try
                    {
                        var result = _mesService.BindContainer(mesRequest);
                        localStopwatch.Stop();
                // 5. 记录日志
                        bool isSuccess = result?.IsSuccess ?? false;
                        int status = isSuccess
                            ? (int)MesUploadStatusEnum.组盘上传成功
                            : (int)MesUploadStatusEnum.组盘上传失败;
                        await _stockInfoService.UpdateMesUploadStatusAsync(stockInfo.PalletCode, status);
                await _mesLogService.LogAsync(new MesApiLogDto
                {
                            PalletCode = stockInfo.PalletCode,
                    ApiType = "BindContainer",
                    RequestJson = requestJson,
                    ResponseJson = System.Text.Json.JsonSerializer.Serialize(mesResult),
                    IsSuccess = mesResult.IsSuccess,
                    ErrorMessage = mesResult.ErrorMessage,
                    ElapsedMs = (int)stopwatch.ElapsedMilliseconds,
                            ResponseJson = System.Text.Json.JsonSerializer.Serialize(result),
                            IsSuccess = isSuccess,
                            ErrorMessage = result?.ErrorMessage ?? "未知错误",
                            ElapsedMs = (int)localStopwatch.ElapsedMilliseconds,
                    Creator = App.User.UserName
                });
                // 6. 返回结果
                if (mesResult.IsSuccess)
                {
                    return response.OK("托盘电芯绑定成功");
                }
                else
                    catch (Exception ex)
                {
                    return response.Error($"MES接口调用失败: {mesResult.ErrorMessage}");
                }
            }
            catch (System.Exception ex)
            {
                stopwatch.Stop();
                // 记录错误日志
                        localStopwatch.Stop();
                        await _stockInfoService.UpdateMesUploadStatusAsync(stockInfo.PalletCode, (int)MesUploadStatusEnum.组盘上传失败);
                await _mesLogService.LogAsync(new MesApiLogDto
                {
                            PalletCode = stockInfo.PalletCode,
                    ApiType = "BindContainer",
                    IsSuccess = false,
                    ErrorMessage = ex.Message,
                    ElapsedMs = (int)stopwatch.ElapsedMilliseconds,
                            ElapsedMs = (int)localStopwatch.ElapsedMilliseconds,
                    Creator = App.User.UserName
                });
                    }
                });
                // 5. 立即返回成功响应
                return response.OK("托盘电芯绑定成功");
            }
            catch (Exception ex)
            {
                return response.Error($"托盘电芯绑定失败: {ex.Message}");
            }
        }
@@ -133,7 +142,6 @@
        public async Task<WebResponseContent> UnbindContainer([FromBody] UnbindContainerRequestDto dto)
        {
            var response = new WebResponseContent();
            var stopwatch = Stopwatch.StartNew();
            try
            {
@@ -163,46 +171,55 @@
                string requestJson = System.Text.Json.JsonSerializer.Serialize(mesRequest);
                // 4. 调用MES接口(同步方法)
                var mesResult = _mesService.UnBindContainer(mesRequest);
                stopwatch.Stop();
                // 4. 异步调用MES接口(fire-and-forget)
                _ = Task.Run(async () =>
                {
                    var localStopwatch = Stopwatch.StartNew();
                    try
                    {
                        var result = _mesService.UnBindContainer(mesRequest);
                        localStopwatch.Stop();
                // 5. 记录日志
                        bool isSuccess = result?.IsSuccess ?? false;
                        int status = isSuccess
                            ? (int)MesUploadStatusEnum.拆盘上传成功
                            : (int)MesUploadStatusEnum.拆盘上传失败;
                        await _stockInfoService.UpdateMesUploadStatusAsync(stockInfo.PalletCode, status);
                await _mesLogService.LogAsync(new MesApiLogDto
                {
                            PalletCode = stockInfo.PalletCode,
                    ApiType = "UnbindContainer",
                    RequestJson = requestJson,
                    ResponseJson = System.Text.Json.JsonSerializer.Serialize(mesResult),
                    IsSuccess = mesResult.IsSuccess,
                    ErrorMessage = mesResult.ErrorMessage,
                    ElapsedMs = (int)stopwatch.ElapsedMilliseconds,
                            ResponseJson = System.Text.Json.JsonSerializer.Serialize(result),
                            IsSuccess = isSuccess,
                            ErrorMessage = result?.ErrorMessage ?? "未知错误",
                            ElapsedMs = (int)localStopwatch.ElapsedMilliseconds,
                    Creator = App.User.UserName
                });
                // 6. 返回结果
                if (mesResult.IsSuccess)
                {
                    return response.OK("托盘电芯解绑成功");
                }
                else
                    catch (Exception ex)
                {
                    return response.Error($"MES接口调用失败: {mesResult.ErrorMessage}");
                }
            }
            catch (System.Exception ex)
            {
                stopwatch.Stop();
                // 记录错误日志
                        localStopwatch.Stop();
                        await _stockInfoService.UpdateMesUploadStatusAsync(stockInfo.PalletCode, (int)MesUploadStatusEnum.拆盘上传失败);
                await _mesLogService.LogAsync(new MesApiLogDto
                {
                            PalletCode = stockInfo.PalletCode,
                    ApiType = "UnbindContainer",
                    IsSuccess = false,
                    ErrorMessage = ex.Message,
                    ElapsedMs = (int)stopwatch.ElapsedMilliseconds,
                            ElapsedMs = (int)localStopwatch.ElapsedMilliseconds,
                    Creator = App.User.UserName
                });
                    }
                });
                // 5. 立即返回成功响应
                return response.OK("托盘电芯解绑成功");
            }
            catch (Exception ex)
            {
                return response.Error($"托盘电芯解绑失败: {ex.Message}");
            }
        }
@@ -216,7 +233,6 @@
        public async Task<WebResponseContent> ContainerNgReport([FromBody] ContainerNgReportRequestDto dto)
        {
            var response = new WebResponseContent();
            var stopwatch = Stopwatch.StartNew();
            try
            {
@@ -258,46 +274,55 @@
                string requestJson = System.Text.Json.JsonSerializer.Serialize(mesRequest);
                // 4. 调用MES接口(同步方法)
                var mesResult = _mesService.ContainerNgReport(mesRequest);
                stopwatch.Stop();
                // 4. 异步调用MES接口(fire-and-forget)
                _ = Task.Run(async () =>
                {
                    var localStopwatch = Stopwatch.StartNew();
                    try
                    {
                        var result = _mesService.ContainerNgReport(mesRequest);
                        localStopwatch.Stop();
                // 5. 记录日志
                        bool isSuccess = result?.IsSuccess ?? false;
                        int status = isSuccess
                            ? (int)MesUploadStatusEnum.NG上报成功
                            : (int)MesUploadStatusEnum.NG上报失败;
                        await _stockInfoService.UpdateMesUploadStatusAsync(stockInfo.PalletCode, status);
                await _mesLogService.LogAsync(new MesApiLogDto
                {
                            PalletCode = stockInfo.PalletCode,
                    ApiType = "ContainerNgReport",
                    RequestJson = requestJson,
                    ResponseJson = System.Text.Json.JsonSerializer.Serialize(mesResult),
                    IsSuccess = mesResult.IsSuccess,
                    ErrorMessage = mesResult.ErrorMessage,
                    ElapsedMs = (int)stopwatch.ElapsedMilliseconds,
                            ResponseJson = System.Text.Json.JsonSerializer.Serialize(result),
                            IsSuccess = isSuccess,
                            ErrorMessage = result?.ErrorMessage ?? "未知错误",
                            ElapsedMs = (int)localStopwatch.ElapsedMilliseconds,
                    Creator = App.User.UserName
                });
                // 6. 返回结果
                if (mesResult.IsSuccess)
                {
                    return response.OK("NG电芯上报成功");
                }
                else
                    catch (Exception ex)
                {
                    return response.Error($"MES接口调用失败: {mesResult.ErrorMessage}");
                }
            }
            catch (System.Exception ex)
            {
                stopwatch.Stop();
                // 记录错误日志
                        localStopwatch.Stop();
                        await _stockInfoService.UpdateMesUploadStatusAsync(stockInfo.PalletCode, (int)MesUploadStatusEnum.NG上报失败);
                await _mesLogService.LogAsync(new MesApiLogDto
                {
                            PalletCode = stockInfo.PalletCode,
                    ApiType = "ContainerNgReport",
                    IsSuccess = false,
                    ErrorMessage = ex.Message,
                    ElapsedMs = (int)stopwatch.ElapsedMilliseconds,
                            ElapsedMs = (int)localStopwatch.ElapsedMilliseconds,
                    Creator = App.User.UserName
                });
                    }
                });
                // 5. 立即返回成功响应
                return response.OK("NG电芯上报成功");
            }
            catch (Exception ex)
            {
                return response.Error($"NG电芯上报失败: {ex.Message}");
            }
        }