From 5621e29bd415c85fd719e0e6366eae813b970ea2 Mon Sep 17 00:00:00 2001
From: dengjunjie <dengjunjie@hnkhzn.com>
Date: 星期四, 30 十月 2025 20:16:11 +0800
Subject: [PATCH] 1

---
 新建文件夹/WIDESEA_WMSServer/WIDESEA_SquareCabinServices/Business.cs |  248 ++++++++++++++++++++++++++++--------------------
 1 files changed, 144 insertions(+), 104 deletions(-)

diff --git "a/\346\226\260\345\273\272\346\226\207\344\273\266\345\244\271/WIDESEA_WMSServer/WIDESEA_SquareCabinServices/Business.cs" "b/\346\226\260\345\273\272\346\226\207\344\273\266\345\244\271/WIDESEA_WMSServer/WIDESEA_SquareCabinServices/Business.cs"
index f475cd1..9eee10d 100644
--- "a/\346\226\260\345\273\272\346\226\207\344\273\266\345\244\271/WIDESEA_WMSServer/WIDESEA_SquareCabinServices/Business.cs"
+++ "b/\346\226\260\345\273\272\346\226\207\344\273\266\345\244\271/WIDESEA_WMSServer/WIDESEA_SquareCabinServices/Business.cs"
@@ -13,6 +13,9 @@
 using static WIDESEA_DTO.SquareCabin.TowcsDto;
 using WIDESEA_Common.WareHouseEnum;
 using WIDESEA_DTO.SquareCabin;
+using WIDESEA_Common;
+using System.Xml.Linq;
+using WIDESEA_Common.OrderEnum;
 
 namespace WIDESEA_WMSServer
 {
@@ -22,24 +25,16 @@
         private readonly ICabinOrderDetailServices _cabinDetailServices;
         private readonly IDeliveryOrderServices _deliveryOrderServices;
         private readonly IDeliveryOrderDetailServices _deliveryOrderDetailServices;
-        private readonly IBasicService _basicService;
+        private readonly IMessageInfoService _messageInfoService;
         private readonly IUnitOfWorkManage _unitOfWorkManage;
-        private readonly IInventory_BatchServices _inventory_BatchServices;
-        private readonly IInventoryInfoService _inventoryInfoService;
-        private readonly ISupplyTaskService _supplyTaskService;
-        private readonly ITacticsService _tacticsService;
-        public Business(IDeliveryOrderServices deliveryOrderServices, IBasicService basicService, IUnitOfWorkManage unitOfWorkManage, IInventory_BatchServices inventory_BatchServices, IInventoryInfoService inventoryInfoService, IDeliveryOrderDetailServices deliveryOrderDetailServices, ISupplyTaskService supplyTaskService, ICabinOrderServices cabinOrderServices, ITacticsService tacticsService, ICabinOrderDetailServices cabinDetailServices)
+        public Business(IDeliveryOrderServices deliveryOrderServices, IUnitOfWorkManage unitOfWorkManage, IDeliveryOrderDetailServices deliveryOrderDetailServices, ICabinOrderServices cabinOrderServices, ICabinOrderDetailServices cabinDetailServices, IMessageInfoService messageInfoService)
         {
             _deliveryOrderServices = deliveryOrderServices;
-            _basicService = basicService;
             _unitOfWorkManage = unitOfWorkManage;
-            _inventory_BatchServices = inventory_BatchServices;
-            _inventoryInfoService = inventoryInfoService;
             _deliveryOrderDetailServices = deliveryOrderDetailServices;
-            _supplyTaskService = supplyTaskService;
             _cabinOrderServices = cabinOrderServices;
-            _tacticsService = tacticsService;
             _cabinDetailServices = cabinDetailServices;
+            _messageInfoService = messageInfoService;
         }
         #region 鑾峰彇ERP鍏ュ簱鍗�
         static string SearchInDate = "2025-09-10 00:00:00";
@@ -71,7 +66,7 @@
                 // 鍙嶅簭鍒楀寲
                 var response = JsonConvert.DeserializeObject<UpstreamResponse<UpstreamOrderInfo>>(result);
 
-                if (response.resultCode != "0")
+                if (response == null || response.resultCode != "0" || requestData == null)
                 {
                     // 璋冪敤寮傚父鎺ュ彛
                     SendErrorToUpstream(1, "", response.resultMsg ?? "涓婃父鎺ュ彛杩斿洖澶辫触", "");
@@ -82,8 +77,10 @@
                 {
                     return responseContent.OK("鏃犳柊鍏ュ簱鍗曟暟鎹�");
                 }
+                var ordernos = response.data.Select(x => x.order_no).ToList();
                 // 鑾峰彇鎵�鏈夊凡瀛樺湪鐨勫叆搴撳崟鍙�
                 var existingOrderNos = _cabinOrderServices.Db.Queryable<Dt_CabinOrder>()
+                    .Where(x => ordernos.Contains(x.Order_no))
                     .Select(x => x.Order_no)
                     .Distinct().ToList();
 
@@ -92,28 +89,34 @@
                     .Where(order => !existingOrderNos.Contains(order.order_no))
                     .ToList();
 
-                if (!newOrders.Any())
+                if (newOrders.Count < 1)
                 {
                     return responseContent.OK("鎵�鏈夊叆搴撳崟宸插瓨鍦紝鏃犻渶鏂板");
                 }
 
-                _unitOfWorkManage.BeginTran();
-                try
+                //瀛樺偍鍏ュ簱鍗曞彿
+                List<string> orderNos = new List<string>();
+                string name = string.Empty;
+                MessageGroupByEnum messageGroupBy = MessageGroupByEnum.InOrderAlarm;
+                MessageStatusEnum messageStatus = MessageStatusEnum.Emergency;
+                foreach (var order in newOrders)
                 {
-                    List<Dt_CabinOrder> _CabinOrders = new List<Dt_CabinOrder>();
-                    //瀛樺偍鍏ュ簱鍗曞彿
-                    List<string> orderNos = new List<string>();
-
-                    foreach (var order in newOrders)
+                    try
                     {
+                        _unitOfWorkManage.BeginTran();
                         if (order.order_type == "1") //姝e父鍏ュ簱
                         {
+                            name = "鍒涘缓姝e父鍏ュ簱鍗�";
                             responseContent = _cabinOrderServices.CreateInboundOrder(order);
+                            if (!responseContent.Status) throw new Exception(responseContent.Message);
                             List<Dt_CabinOrder>? dt_CabinOrders = responseContent.Data as List<Dt_CabinOrder>;
-                            if (dt_CabinOrders != null) _CabinOrders.AddRange(dt_CabinOrders);
+                            if (dt_CabinOrders != null)
+                                _cabinOrderServices.Db.InsertNav(dt_CabinOrders).Include(x => x.Details).ExecuteCommand();
                         }
                         else if (order.order_type == "3")//鍏ュ簱閫�鏂�
                         {
+                            messageGroupBy = MessageGroupByEnum.OutOrderAlarm;
+                            name = "鍒涘缓鍏ュ簱閫�璐у崟";
                             //鍒涘缓鍑哄簱鍗�
                             #region 杞崲涓哄嚭搴撳崟
                             UpstramOutOrderInfo upstramOutOrderInfo = new UpstramOutOrderInfo()
@@ -134,28 +137,29 @@
                                 };
                                 upstramOutOrderInfo.details.Add(detail);
                             }
-                            _deliveryOrderServices.CreateOutboundOrder(upstramOutOrderInfo);
+                            var content = _deliveryOrderServices.CreateOutboundOrder(upstramOutOrderInfo);
+                            if (!content.Status) throw new Exception(content.Message);
                             #endregion
                         }
                         else if (order.order_type == "5")//鎶ユ孩鍏ュ簱
                         {
-                            _cabinOrderServices.CreateCheckInOrder(order);
+                            name = "鍒涘缓鐩樼泩鍏ュ簱鍗�";
+                            messageStatus = MessageStatusEnum.Undisposed;
+                            var content = _cabinOrderServices.CreateCheckInOrder(order);
+                            if (!content.Status) throw new Exception(content.Message);
                         }
-                    };
-                    _cabinOrderServices.Db.InsertNav(_CabinOrders).Include(x => x.Details).ExecuteCommand();
+                        _unitOfWorkManage.CommitTran();
+                    }
+                    catch (Exception ex)
+                    {
+                        _unitOfWorkManage.RollbackTran();
+                        _messageInfoService.AddMessageInfo(messageGroupBy, name, ex.Message, messageStatus);
+                        SendErrorToUpstream(1, "", ex.Message, "");
+                        //return responseContent.Error("鍚屾澶辫触: " + ex.Message);
+                    }
+                };
+                return responseContent.OK("鍚屾鍏ュ簱鍗曟垚鍔�");
 
-                    //鍦ㄤ笅鍙戠粰wcs 
-                    //EdiIn(); //鍙戠粰涓嬫父
-                    //濡傛灉杩欎釜鏂规硶鎴愬姛浜嗭紝閭d箞灏辫皟鐢–ompleteOrder鎺ュ彛锛岀劧鍚庢敼鍙�
-                    _unitOfWorkManage.CommitTran();
-                    return responseContent.OK("鍚屾鍏ュ簱鍗曟垚鍔�");
-                }
-                catch (Exception ex)
-                {
-                    _unitOfWorkManage.RollbackTran();
-                    SendErrorToUpstream(1, "", ex.Message, "");
-                    return responseContent.Error("鍚屾澶辫触: " + ex.Message);
-                }
             }
             catch (Exception ex)
             {
@@ -173,8 +177,8 @@
             var responseContent = new WebResponseContent();
             try
             {
-                //var url = "http://121.37.118.63:80/GYZ2/95fck/outOrder";
-                var url = "http://127.0.0.1:4523/m1/5660322-5340849-default/GYZ2/95fck/outOrder";
+                var url = "http://121.37.118.63:80/GYZ2/95fck/outOrder";
+                //var url = "http://127.0.0.1:4523/m1/5660322-5340849-default/GYZ2/95fck/outOrder";
                 if (string.IsNullOrEmpty(SearchOutDate)) SearchOutDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                 //// 璇锋眰鍙傛暟
                 var requestData = new
@@ -197,9 +201,10 @@
                 {
                     return responseContent.OK("鏃犳柊鍑哄簱鍗曟暟鎹�");
                 }
-
+                var ordernos = response.data.Select(x => x.order_no).ToList();
                 // 宸插瓨鍦ㄥ嚭搴撳崟鍙�
                 var existingOutOrderNos = _deliveryOrderServices.Db.Queryable<Dt_DeliveryOrder>()
+                    .Where(x => ordernos.Contains(x.Out_no))
                     .Select(x => x.Out_no)
                     .Distinct().ToList();
 
@@ -210,25 +215,31 @@
 
                 List<string> order_no = newOutOrders.Select(x => x.order_no).ToList();
 
-                if (!newOutOrders.Any())
+                if (newOutOrders.Count < 1)
                 {
                     return responseContent.OK("鎵�鏈夊嚭搴撳崟宸插瓨鍦紝鏃犻渶鏂板");
                 }
 
-                _unitOfWorkManage.BeginTran();
-                try
+                List<Dt_DeliveryOrder> _DeliveryOrders = new List<Dt_DeliveryOrder>();
+                List<string> orderNos = new List<string>();
+                string name = string.Empty;
+                MessageGroupByEnum messageGroupBy = MessageGroupByEnum.OutOrderAlarm;
+                MessageStatusEnum messageStatus = MessageStatusEnum.Emergency;
+                foreach (var outorder in newOutOrders)
                 {
-                    List<Dt_DeliveryOrder> _DeliveryOrders = new List<Dt_DeliveryOrder>();
-                    List<string> orderNos = new List<string>();
-
-                    foreach (var outorder in newOutOrders)
+                    try
                     {
+                        _unitOfWorkManage.BeginTran();
                         if (outorder.order_type == "1") // 姝e父鍑哄簱鍗�
                         {
-                            _deliveryOrderServices.CreateOutboundOrder(outorder);//鍒涘缓鍑哄簱鍗曘�佸鐞嗗簱瀛樸�佹坊鍔犲嚭搴撲换鍔�
+                            name = "鍒涘缓姝e父鍑哄簱鍗�";
+                            var content = _deliveryOrderServices.CreateOutboundOrder(outorder);
+                            if (!content.Status) throw new Exception(content.Message);
                         }
                         else if (outorder.order_type == "2")//鍑哄簱閫�璐�
                         {
+                            name = "鍒涘缓鍑哄簱閫�璐у崟";
+                            messageGroupBy = MessageGroupByEnum.InOrderAlarm;
                             // 鍒涘缓鍑哄簱閫�璐у崟 - 杩欓噷闇�瑕佹牴鎹笟鍔¢�昏緫瀹炵幇
                             // 鏆傛椂璺宠繃鎴栧疄鐜伴��璐ч�昏緫
                             //continue;
@@ -252,6 +263,7 @@
                                 order.details.Add(detail);
                             }
                             responseContent = _cabinOrderServices.CreateInboundOrder(order);
+                            if (!responseContent.Status) throw new Exception(responseContent.Message);
                             List<Dt_CabinOrder>? dt_CabinOrders = responseContent.Data as List<Dt_CabinOrder>;
                             if (dt_CabinOrders != null && dt_CabinOrders.Count > 0)
                                 _cabinOrderServices.Db.InsertNav(dt_CabinOrders).Include(x => x.Details).ExecuteCommand();
@@ -259,32 +271,22 @@
                         }
                         else if (outorder.order_type == "6")//鎶ユ崯鍑哄簱
                         {
-                            _deliveryOrderServices.CreateCheckOutOrder(outorder);
+                            name = "鍒涘缓鐩樻崯鍑哄簱鍗�";
+                            messageStatus = MessageStatusEnum.Undisposed;
+                            var content = _deliveryOrderServices.CreateCheckOutOrder(outorder);
+                            if (!content.Status) throw new Exception(content.Message);
                         }
+                        _unitOfWorkManage.CommitTran();
                     }
-
-                    // 鎵归噺鎻掑叆鍑哄簱鍗曞拰鏄庣粏
-                    //if (_DeliveryOrders.Any())
-                    //{
-                    //    BaseDal.Db.InsertNav(_DeliveryOrders).Include(x => x.Details).ExecuteCommand();
-                    //}
-                    //涓嬪彂鍑哄簱鍗曚换鍔$粰wcs
-
-
-                    _unitOfWorkManage.CommitTran();
-                    //var tex = CreateSupplyTask(order_no);
-                    //if (!tex)
-                    //{
-                    //    return responseContent.Error("鍒涘缓渚涘簲浠诲姟澶辫触");
-                    //}
-                    return responseContent.OK($"鍚屾鍑哄簱鍗曟垚鍔燂紝鍏眥_DeliveryOrders.Count}鏉�");
+                    catch (Exception ex)
+                    {
+                        _unitOfWorkManage.RollbackTran();
+                        _messageInfoService.AddMessageInfo(messageGroupBy, name, ex.Message, messageStatus);
+                        SendErrorToUpstream(3, "", ex.Message, "");
+                        //return responseContent.Error("鍚屾澶辫触: " + ex.Message);
+                    }
                 }
-                catch (Exception ex)
-                {
-                    _unitOfWorkManage.RollbackTran();
-                    SendErrorToUpstream(3, "", ex.Message, "");
-                    return responseContent.Error("鍚屾澶辫触: " + ex.Message);
-                }
+                return responseContent.OK($"鍚屾鍑哄簱鍗曟垚鍔燂紝鍏眥_DeliveryOrders.Count}鏉�");
             }
             catch (Exception ex)
             {
@@ -482,8 +484,8 @@
                         };
 
                         // 4. 璋冪敤鎺ュ彛
-                        //var url = "http://172.16.1.2:9357/file-admin/api/out/ediOut";
-                        var url = "http://127.0.0.1:4523/m2/5660322-5340849-default/363076920";
+                        var url = "http://172.16.1.2:9357/file-admin/api/out/ediOut";
+                        //var url = "http://127.0.0.1:4523/m2/5660322-5340849-default/363076920";
 
                         var result = HttpHelper.Post(url, ediDto.ToJsonString());
                         var resp = JsonConvert.DeserializeObject<TowcsDto.TowcsResponse<object>>(result);
@@ -538,37 +540,56 @@
             var responseContent = new WebResponseContent();
             try
             {
+                _unitOfWorkManage.BeginTran();
                 #region 鏌ユ壘鎵�鏈夊凡瀹屾垚鍏ュ簱鍗�
                 var inorders = _cabinOrderServices.Repository.QueryData(x => x.OdrderStatus == "宸插畬鎴�").Select(x => x.Order_no).Distinct().ToList();
                 foreach (var inorder in inorders)
                 {
-                    //var Orders = BaseDal.QueryData(x => x.Order_no == inorder);
                     var Orders = _cabinOrderServices.Db.Queryable<Dt_CabinOrder>().Where(x => x.Order_no == inorder).Includes(x => x.Details).ToList();
+                    
                     if (!Orders.Where(x => x.OdrderStatus != "宸插畬鎴�").Any())
                     {
-                        _unitOfWorkManage.BeginTran();
-                        _cabinOrderServices.Repository.DeleteAndMoveIntoHty(Orders, OperateTypeEnum.鑷姩瀹屾垚);
-                        foreach (var item in Orders)
+                        if (Orders.FirstOrDefault().Order_type == InOrderTypeEnum.Allocat.ObjToInt().ToString())
                         {
-                            _cabinDetailServices.Repository.DeleteAndMoveIntoHty(item.Details, OperateTypeEnum.鑷姩瀹屾垚);
-                        }
-                        // 璋冪敤涓婃父鎺ュ彛
-                        var url = "http://121.37.118.63:80/GYZ2/95fck/inOrderOk";
-                        var result = HttpHelper.Post(url, new { order_no = inorder }.ToJsonString());
-                        var response = JsonConvert.DeserializeObject<UpstreamOrderResponse>(result);
-                        if (response.resultCode == "0")
-                        {
-                            // 鎻愪氦浜嬪姟
-                            _unitOfWorkManage.CommitTran();
+                            foreach (var item in Orders)
+                            {
+                                if (item.Details != null)
+                                    _cabinDetailServices.Repository.DeleteAndMoveIntoHty(item.Details, OperateTypeEnum.鑷姩瀹屾垚);
+                                item.Details = null;
+                            }
+                            _cabinOrderServices.Repository.DeleteAndMoveIntoHty(Orders, OperateTypeEnum.鑷姩瀹屾垚);
                         }
                         else
                         {
-                            _unitOfWorkManage.RollbackTran();
-                            SendErrorToUpstream(2, "", $"涓婃父鎺ュ彛杩斿洖澶辫触: {response.resultMsg}", inorder);
+                            // 璋冪敤涓婃父鎺ュ彛
+                            var url = "http://121.37.118.63:80/GYZ2/95fck/inOrderOk";
+
+
+
+                            var result = HttpHelper.Post(url, new { order_no = inorder }.ToJsonString());
+                            var response = JsonConvert.DeserializeObject<UpstreamOrderResponse>(result);
+                            if (response.resultCode == "0")
+                            {
+                                foreach (var item in Orders)
+                                {
+                                    if (item.Details != null)
+                                        _cabinDetailServices.Repository.DeleteAndMoveIntoHty(item.Details, OperateTypeEnum.鑷姩瀹屾垚);
+                                    item.Details = null;
+                                }
+                                _cabinOrderServices.Repository.DeleteAndMoveIntoHty(Orders, OperateTypeEnum.鑷姩瀹屾垚);
+                                // 鎻愪氦浜嬪姟
+                            }
+                            else
+                            {
+                                //_unitOfWorkManage.RollbackTran();
+                                SendErrorToUpstream(2, "", $"涓婃父鎺ュ彛杩斿洖澶辫触: {response.resultMsg}", inorder);
+                                continue;
+                            }
                         }
                     }
                 }
                 #endregion
+                _unitOfWorkManage.CommitTran();
                 return responseContent.OK();
             }
             catch (Exception ex)
@@ -590,36 +611,55 @@
             var responseContent = new WebResponseContent();
             try
             {
+                _unitOfWorkManage.BeginTran();
                 #region 鏌ユ壘鎵�鏈夊凡瀹屾垚鍑哄簱鍗�
                 var outorders = _deliveryOrderServices.Repository.QueryData(x => x.OutStatus == "宸插畬鎴�").Select(x => x.Out_no).Distinct().ToList();
                 foreach (var outorder in outorders)
                 {
                     var Orders = _deliveryOrderServices.Db.Queryable<Dt_DeliveryOrder>().Where(x => x.Out_no == outorder).Includes(x => x.Details).ToList();
+
                     if (!Orders.Where(x => x.OutStatus != "宸插畬鎴�").Any())
                     {
-                        _unitOfWorkManage.BeginTran();
-                        _deliveryOrderServices.Repository.DeleteAndMoveIntoHty(Orders, OperateTypeEnum.鑷姩瀹屾垚);
-                        foreach (var item in Orders)
+                        if (Orders.FirstOrDefault().Out_type == OutOrderTypeEnum.Allocate.ObjToInt().ToString())
                         {
-                            _deliveryOrderDetailServices.Repository.DeleteAndMoveIntoHty(item.Details, OperateTypeEnum.鑷姩瀹屾垚);
+                            foreach (var item in Orders)
+                            {
+                                if (item.Details != null)
+                                    _deliveryOrderDetailServices.Repository.DeleteAndMoveIntoHty(item.Details, OperateTypeEnum.鑷姩瀹屾垚);
+                                item.Details = null;
+                            }
+                            _deliveryOrderServices.Repository.DeleteAndMoveIntoHty(Orders, OperateTypeEnum.鑷姩瀹屾垚);
                         }
-                        var url = "http://121.37.118.63:80/GYZ2/95fck/outOrderOk";
-                        var requestDate = new
-                        {
-                            order_no = outorder
-                        };
-                        var result = HttpHelper.Post(url, requestDate.ToJsonString());
-                        var response = JsonConvert.DeserializeObject<UpstreamOrderResponse>(result);
-                        if (response.resultCode == "0") _unitOfWorkManage.CommitTran();
                         else
                         {
-                            _unitOfWorkManage.RollbackTran();
-                            SendErrorToUpstream(4, "", $"涓婃父鎺ュ彛杩斿洖澶辫触: {response.resultMsg}", outorder);
+                            var url = "http://121.37.118.63:80/GYZ2/95fck/outOrderOk";
+                            var requestDate = new
+                            {
+                                order_no = outorder
+                            };
+                            var result = HttpHelper.Post(url, requestDate.ToJsonString());
+                            var response = JsonConvert.DeserializeObject<UpstreamOrderResponse>(result);
+                            if (response.resultCode == "0")
+                            {
+                                foreach (var item in Orders)
+                                {
+                                    if (item.Details != null)
+                                        _deliveryOrderDetailServices.Repository.DeleteAndMoveIntoHty(item.Details, OperateTypeEnum.鑷姩瀹屾垚);
+                                    item.Details = null;
+                                }
+                                _deliveryOrderServices.Repository.DeleteAndMoveIntoHty(Orders, OperateTypeEnum.鑷姩瀹屾垚);
+                            }
+                            else
+                            {
+                                //_unitOfWorkManage.RollbackTran();
+                                SendErrorToUpstream(4, "", $"涓婃父鎺ュ彛杩斿洖澶辫触: {response.resultMsg}", outorder);
+                                continue;
+                            }
                         }
                     }
                 }
                 #endregion
-
+                _unitOfWorkManage.CommitTran();
                 return responseContent.OK();
             }
             catch (Exception ex)

--
Gitblit v1.9.3