From 266e4bf654c55ce2f7e9271048e4625f1b8b49f6 Mon Sep 17 00:00:00 2001
From: heshaofeng <heshaofeng@hnkhzn.com>
Date: 星期一, 29 十二月 2025 14:36:08 +0800
Subject: [PATCH] Merge branch 'htq20251215' of http://115.159.85.185:8098/r/ZhongRui/ALDbanyunxiangmu into htq20251215

---
 项目代码/WMS无仓储版/WIDESEA_WMSServer/WIDESEA_BasicService/InvokeMESService.cs |  145 ++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 133 insertions(+), 12 deletions(-)

diff --git "a/\351\241\271\347\233\256\344\273\243\347\240\201/WMS\346\227\240\344\273\223\345\202\250\347\211\210/WIDESEA_WMSServer/WIDESEA_BasicService/InvokeMESService.cs" "b/\351\241\271\347\233\256\344\273\243\347\240\201/WMS\346\227\240\344\273\223\345\202\250\347\211\210/WIDESEA_WMSServer/WIDESEA_BasicService/InvokeMESService.cs"
index 33ef1b4..362c13b 100644
--- "a/\351\241\271\347\233\256\344\273\243\347\240\201/WMS\346\227\240\344\273\223\345\202\250\347\211\210/WIDESEA_WMSServer/WIDESEA_BasicService/InvokeMESService.cs"
+++ "b/\351\241\271\347\233\256\344\273\243\347\240\201/WMS\346\227\240\344\273\223\345\202\250\347\211\210/WIDESEA_WMSServer/WIDESEA_BasicService/InvokeMESService.cs"
@@ -19,10 +19,12 @@
 using WIDESEA_Core;
 using WIDESEA_Core.BaseRepository;
 using WIDESEA_Core.Helper;
+using WIDESEA_Core.Util;
 using WIDESEA_DTO.Allocate;
 using WIDESEA_DTO.Basic;
 using WIDESEA_DTO.Inbound;
 using WIDESEA_DTO.Outbound;
+using WIDESEA_DTO.ReturnMES;
 using WIDESEA_IBasicService;
 using WIDESEA_IOutboundService;
 using WIDESEA_Model.Models;
@@ -41,19 +43,22 @@
         private readonly IRepository<Dt_StockInfoDetail> _stockInfoDetailRepository;
         private readonly IRepository<Dt_StockInfo> _stockInfoRepository;
         private readonly IRepository<Dt_InboundOrder> _inboundOrderRepository;
+        private readonly IRepository<Dt_InboundOrderDetail> _inboundOrderDetailRepository;
         private readonly IRepository<Dt_PickingRecord> _pickingRecoreRepository;
         private readonly IMaterialUnitService _materialUnitService;
         private readonly IOutboundOrderService _outboundOrderService;
         private readonly IOutboundOrderDetailService _outboundOrderDetailService;
         private readonly IOutStockLockInfoService _outStockLockInfoService;
         private readonly IRepository<Dt_InterfaceLog> _interfacelogRepository;
+        private readonly HttpClientHelper _httpClientHelper;
+        private readonly IUnitOfWorkManage _unitOfWorkManage;
 
         // 瀛樺偍璧勬簮ID鍙婂叾瀵瑰簲鐨勯攣瀵硅薄銆備娇鐢� ConcurrentDictionary 纭繚瀵瑰瓧鍏告搷浣滄湰韬殑绾跨▼瀹夊叏銆�
         private static readonly ConcurrentDictionary<string, object> _resourceLocks = new ConcurrentDictionary<string, object>();
 
         // 鍏ㄥ眬闈欐�侀攣锛氱敤浜庝繚鎶� _resourceLocks 瀛楀吀涓� GetOrAdd 鎴� TryRemove 鏃剁殑绔炰簤
         private static readonly object _globalLocker = new object();
-        public InvokeMESService(IHttpClientFactory httpClientFactory, ILogger<InvokeMESService> logger, IRepository<Dt_FeedbackToMes> feedbacktomesRepository, IRepository<Dt_StockInfoDetail> stockInfoDetailRepository, IRepository<Dt_StockInfo> stockInfoRepository, IRepository<Dt_InboundOrder> inboundOrderRepository, IOutboundOrderService outboundOrderService, IOutboundOrderDetailService outboundOrderDetailService, IOutStockLockInfoService outStockLockInfoService, IMaterialUnitService materialUnitService, IRepository<Dt_PickingRecord> pickingRecoreRepository, IRepository<Dt_InterfaceLog> interfacelogRepository)
+        public InvokeMESService(IHttpClientFactory httpClientFactory, ILogger<InvokeMESService> logger, IRepository<Dt_FeedbackToMes> feedbacktomesRepository, IRepository<Dt_StockInfoDetail> stockInfoDetailRepository, IRepository<Dt_StockInfo> stockInfoRepository, IRepository<Dt_InboundOrder> inboundOrderRepository, IOutboundOrderService outboundOrderService, IOutboundOrderDetailService outboundOrderDetailService, IOutStockLockInfoService outStockLockInfoService, IMaterialUnitService materialUnitService, IRepository<Dt_PickingRecord> pickingRecoreRepository, IRepository<Dt_InterfaceLog> interfacelogRepository, IRepository<Dt_InboundOrderDetail> inboundOrderDetailRepository, HttpClientHelper httpClientHelper, IUnitOfWorkManage unitOfWorkManage)
         {
             _httpClientFactory = httpClientFactory;
             _logger = logger;
@@ -67,6 +72,9 @@
             _materialUnitService = materialUnitService;
             _pickingRecoreRepository = pickingRecoreRepository;
             _interfacelogRepository = interfacelogRepository;
+            _inboundOrderDetailRepository = inboundOrderDetailRepository;
+            _httpClientHelper = httpClientHelper;
+            _unitOfWorkManage = unitOfWorkManage;
         }
 
         /// <summary>
@@ -245,7 +253,7 @@
         public async Task<WebResponseContent> BatchOrderFeedbackToMes(List<string> orderNos, int inout)
         {
             try
-            {         
+            {
 
                 if (inout == 1)
                 {
@@ -260,6 +268,15 @@
                             var unreports = stockinfos.Where(x => !feeds.Contains(x.PalletCode)).ToList();
                             if (unreports != null && !unreports.Any())
                             {
+                                _inboundOrderRepository.Db.Updateable<Dt_InboundOrder>().SetColumns(it => new Dt_InboundOrder { ReturnToMESStatus = 1, Remark = "" })
+                                                 .Where(it => it.InboundOrderNo == orderNo).ExecuteCommand();
+                                var inboundOrder = _inboundOrderRepository.Db.Queryable<Dt_InboundOrder>().First(x => x.InboundOrderNo == orderNo);
+                                if (inboundOrder != null)
+                                {
+                                    _inboundOrderDetailRepository.Db.Updateable<Dt_InboundOrderDetail>().SetColumns(it => new Dt_InboundOrderDetail { ReturnToMESStatus = 1 })
+                                         .Where(it => it.OrderId == inboundOrder.Id).ExecuteCommand();
+                                }
+
                                 return WebResponseContent.Instance.Error("娌℃湁闇�瑕佸洖浼犵殑鏁版嵁");
                             }
                             foreach (var item in unreports)
@@ -277,7 +294,7 @@
                                             {
                                                 ReqCode = Guid.NewGuid().ToString(),
                                                 ReqTime = DateTime.Now.ToString(),
-                                                BusinessType = "2",
+                                                BusinessType = "3",
                                                 FactoryArea = inboundOrder.FactoryArea,
                                                 OperationType = 1,
                                                 Operator = inboundOrder.Operator,
@@ -307,8 +324,10 @@
                                                }).ToList();
                                             allocatefeedmodel.Details = groupedData;
 
-                                            var result = await FeedbackAllocate(allocatefeedmodel);
-                                            if (result != null && result.code == 200)
+                                            //var result = await FeedbackAllocate(allocatefeedmodel);
+                                            var result = responseModel(inboundOrder, 3, null, allocatefeedmodel);
+                                            //if (response != null && response.code == 200)
+                                            if (result != null && result.IsSuccess)
                                             {
                                                 _feedbacktomesRepository.Db.Insertable(new Dt_FeedbackToMes { OrderNo = orderNo, PalletCode = item.PalletCode, ReportStatus = 1 }).ExecuteCommand();
                                             }
@@ -347,10 +366,38 @@
                                                    }).ToList()
                                                }).ToList();
                                             feedmodel.details = groupedData;
-                                            var result = await FeedbackInbound(feedmodel);
-                                            if (result != null && result.code == 200)
+                                            //var result = await FeedbackInbound(feedmodel);
+                                            var response = responseModel(inboundOrder, 2, feedmodel);
+                                            //if (result != null && result.code == 200)
+                                            if (response != null && response.IsSuccess)
                                             {
                                                 _feedbacktomesRepository.Db.Insertable(new Dt_FeedbackToMes { OrderNo = orderNo, PalletCode = item.PalletCode, ReportStatus = 1 }).ExecuteCommand();
+
+                                                var feedstockinfos = _stockInfoRepository.Db.Queryable<Dt_StockInfo>("info").Where(info => info.StockStatus == 6)
+                                                   .Where(it => SqlFunc.Subqueryable<Dt_StockInfoDetail>().Where(s => s.StockId == it.Id && s.OrderNo == orderNo).Any())
+                                                   .ToList();
+                                                var feedstomes = _feedbacktomesRepository.Db.Queryable<Dt_FeedbackToMes>().Where(x => x.OrderNo == orderNo && x.ReportStatus == 1).Select(o => o.PalletCode).ToList();
+                                                var feedunreports = feedstockinfos.Where(x => !feedstomes.Contains(x.PalletCode)).ToList();
+                                                if (feedunreports != null && !feedunreports.Any())
+                                                {
+                                                    _inboundOrderRepository.Db.Updateable<Dt_InboundOrder>().SetColumns(it => new Dt_InboundOrder { ReturnToMESStatus = 1, Remark = "" })
+                                                     .Where(it => it.InboundOrderNo == orderNo).ExecuteCommand();
+                                                    var feedinboundOrder = _inboundOrderRepository.Db.Queryable<Dt_InboundOrder>().First(x => x.InboundOrderNo == orderNo);
+                                                    if (feedinboundOrder != null)
+                                                    {
+                                                        _inboundOrderDetailRepository.Db.Updateable<Dt_InboundOrderDetail>().SetColumns(it => new Dt_InboundOrderDetail { ReturnToMESStatus = 1 })
+                                                             .Where(it => it.OrderId == feedinboundOrder.Id).ExecuteCommand();
+                                                    }
+                                                }
+                                                return WebResponseContent.Instance.Error("鍥炰紶鎴愬姛锛�");
+                                            }
+                                            else
+                                            {
+                                                _inboundOrderRepository.Db.Updateable<Dt_InboundOrder>().SetColumns(it => new Dt_InboundOrder { ReturnToMESStatus = 2 })
+                                                .Where(it => it.Id == inboundOrder.Id).ExecuteCommand();
+                                                _inboundOrderDetailRepository.Db.Updateable<Dt_InboundOrderDetail>().SetColumns(it => new Dt_InboundOrderDetail { ReturnToMESStatus = 2 })
+                                              .Where(it => it.OrderId == inboundOrder.Id).ExecuteCommand();
+                                                return WebResponseContent.Instance.Error("鍥炰紶寮傚父锛�");
                                             }
                                         }
                                     }
@@ -402,6 +449,61 @@
                 // MemoryLockManager.ReleaseLock(orderNos[0]);
             }
             return WebResponseContent.Instance.OK();
+        }
+
+        public HttpResponseResult<MesResponseDTO> responseModel(Dt_InboundOrder order, int InterfaceType, FeedbackInboundRequestModel model = null, AllocateDto allocateDto = null)
+        {
+            HttpResponseResult<MesResponseDTO> httpResponseResult = new HttpResponseResult<MesResponseDTO>();
+            string reqCode = Guid.NewGuid().ToString();
+            string reqTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
+            string requestData = string.Empty;
+            string apiUrl = string.Empty;
+            if (model != null)
+            {
+                apiUrl = AppSettings.GetValue("AldMaterialWarehousing");
+                httpResponseResult = _httpClientHelper.Post<MesResponseDTO>(apiUrl, model.Serialize());
+                requestData = model.Serialize();
+            }
+            else
+            {
+                apiUrl = AppSettings.GetValue("AldAllocationOperation");
+                httpResponseResult = _httpClientHelper.Post<MesResponseDTO>(apiUrl, allocateDto.Serialize());
+                requestData = allocateDto.Serialize();
+            }
+            httpResponseResult.ApiUrl = apiUrl;
+            bool isSuccess = httpResponseResult.IsSuccess && httpResponseResult.Data.Code == "200";
+            string message = "鎴愬姛";
+            if (!isSuccess)
+            {
+                if (!httpResponseResult.IsSuccess)
+                {
+                    message = $"MES鎺ュ彛杩斿洖閿欒锛孒TTP浠g爜锛歿httpResponseResult.StatusCode}锛屼俊鎭細{httpResponseResult.ErrorMessage}";
+                }
+                else if (httpResponseResult?.Data?.Code != "200")
+                {
+                    message = $"璋冪敤MES鎺ュ彛澶辫触锛屼唬鐮侊細{httpResponseResult?.Data?.Code}锛屼俊鎭細{httpResponseResult?.Data?.Message}";
+                }
+            }
+            Dt_MesReturnRecord mesReturnRecord = new Dt_MesReturnRecord()
+            {
+                ApiUrl = httpResponseResult.ApiUrl,
+                InterfaceType = InterfaceType,
+                OrderId = order.Id,
+                OrderNo = order.InboundOrderNo,
+                RequestCode = reqCode,
+                RequestData = requestData,
+                FailureReason = message,
+                LastReturnTime = DateTime.Now,
+                HttpStatusCode = httpResponseResult.StatusCode.ObjToInt(),
+                ResponseData = httpResponseResult.Content,
+                ReturnType = 0,
+                ReturnCount = 1,
+                ReturnStatus = httpResponseResult.IsSuccess ? 1 : 2,
+                SuccessTime = httpResponseResult.IsSuccess ? DateTime.Now : null
+            };
+            _unitOfWorkManage.Db.Insertable(mesReturnRecord).ExecuteCommand();
+
+            return httpResponseResult;
         }
 
         private async Task<WebResponseContent> HandleOutboundOrderBatchToMESCompletion(Dt_OutboundOrder outboundOrder, string orderNo)
@@ -640,7 +742,16 @@
             var mesResult = await FeedbackOutbound(feedModel);
             if (mesResult == null || mesResult.code != 200)
             {
+                var messages = mesResult?.message ?? "";
 
+                await _outboundOrderService.Db.Updateable<Dt_OutboundOrder>()
+                   .SetColumns(x => new Dt_OutboundOrder
+                   {
+                       ReturnToMESStatus = 2,
+                       Remark = messages,
+                   })
+                   .Where(x => x.OrderNo == orderNo)
+                   .ExecuteCommandAsync();
                 // 鏇存柊鏄庣粏涓哄洖浼犲け璐ワ紙ReturnToMESStatus=2锛�
                 await _outboundOrderDetailService.Db.Updateable<Dt_OutboundOrderDetail>()
                     .SetColumns(it => new Dt_OutboundOrderDetail
@@ -653,10 +764,7 @@
 
                 return (flowControl: false, value: WebResponseContent.Instance.Error($"鍥炰紶MES澶辫触"));
             }
-            foreach (var record in pickingRecords.Where(x => detailIds.Contains(x.OrderDetailId)).ToList())
-            {
-                record.ReturnToMESStatus = 1;
-            }
+
             var updates = pickingRecords.Where(x => detailIds.Contains(x.OrderDetailId)).ToList();
             updates.ForEach(x =>
             {
@@ -687,6 +795,7 @@
                     .SetColumns(x => new Dt_OutboundOrder
                     {
                         ReturnToMESStatus = 1,
+                        Remark = "",
                         OrderStatus = newStatus
                     })
                     .Where(x => x.OrderNo == orderNo)
@@ -717,6 +826,7 @@
                         .SetColumns(it => new Dt_OutboundOrder
                         {
                             ReturnToMESStatus = 1,
+                            Remark = "",
                             OrderStatus = OutOrderStatusEnum.鍑哄簱瀹屾垚.ObjToInt(),
                         })
                         .Where(x => x.OrderNo == orderNo)
@@ -846,7 +956,7 @@
                             .ExecuteCommandAsync();
 
                         await _outboundOrderService.Db.Updateable<Dt_OutboundOrder>()
-                            .SetColumns(x => x.ReturnToMESStatus == 1)
+                            .SetColumns(it => new Dt_OutboundOrder { ReturnToMESStatus = 1, Remark = "" })
                             .Where(x => x.OrderNo == orderNo)
                             .ExecuteCommandAsync();
 
@@ -861,6 +971,17 @@
                     {
                         var errorMsg = $"OrderNo: {orderNo} 鍥炰紶MES澶辫触锛岄敊璇爜锛歿result.code}锛岄敊璇俊鎭細{result.message ?? "鏃�"}";
                         _logger.LogError(errorMsg);
+
+                        await _outboundOrderDetailService.Db.Updateable<Dt_OutboundOrderDetail>()
+                          .SetColumns(x => x.ReturnToMESStatus == 2)
+                          .Where(x => x.OrderId == outboundOrder.Id)
+                          .ExecuteCommandAsync();
+
+                        await _outboundOrderService.Db.Updateable<Dt_OutboundOrder>()
+                            .SetColumns(it => new Dt_OutboundOrder { ReturnToMESStatus = 2, Remark = result.message })
+                            .Where(x => x.OrderNo == orderNo)
+                            .ExecuteCommandAsync();
+
                         return WebResponseContent.Instance.Error(errorMsg);
                     }
                 }

--
Gitblit v1.9.3