From 56827df8730c172c6c654fb78fed073ef8d694cb Mon Sep 17 00:00:00 2001
From: dengjunjie <dengjunjie@hnkhzn.com>
Date: 星期四, 23 十月 2025 19:22:39 +0800
Subject: [PATCH] 优化入库单处理

---
 新建文件夹/WIDESEA_WMSServer/WIDESEA_SquareCabinServices/InventoryServices.cs |  291 +++++++++++++++++++++++++++------------------------------
 1 files changed, 138 insertions(+), 153 deletions(-)

diff --git "a/\346\226\260\345\273\272\346\226\207\344\273\266\345\244\271/WIDESEA_WMSServer/WIDESEA_SquareCabinServices/InventoryServices.cs" "b/\346\226\260\345\273\272\346\226\207\344\273\266\345\244\271/WIDESEA_WMSServer/WIDESEA_SquareCabinServices/InventoryServices.cs"
index ee48bc6..1d72719 100644
--- "a/\346\226\260\345\273\272\346\226\207\344\273\266\345\244\271/WIDESEA_WMSServer/WIDESEA_SquareCabinServices/InventoryServices.cs"
+++ "b/\346\226\260\345\273\272\346\226\207\344\273\266\345\244\271/WIDESEA_WMSServer/WIDESEA_SquareCabinServices/InventoryServices.cs"
@@ -20,8 +20,10 @@
 {
     public class InventoryServices : ServiceBase<Dt_Inventory, IRepository<Dt_Inventory>>, IInventoryServices
     {
-        public InventoryServices(IRepository<Dt_Inventory> BaseDal) : base(BaseDal)
+        private readonly IDeliveryOrderServices _deliveryOrderServices;
+        public InventoryServices(IRepository<Dt_Inventory> BaseDal, IDeliveryOrderServices deliveryOrderServices) : base(BaseDal)
         {
+            _deliveryOrderServices = deliveryOrderServices;
         }
 
 
@@ -38,34 +40,33 @@
             try
             {
                 var url = "http://121.37.118.63/GYZ2/95fck/repositoryInfo";
-                // 鍙戣捣璇锋眰
-                var result = HttpHelper.Post(url, new { goods_no,batch_num}.ToJsonString());
+                var result = HttpHelper.Post(url, new { goods_no, batch_num }.ToJsonString());
 
-                // 鍙嶅簭鍒楀寲
                 var response = JsonConvert.DeserializeObject<UpstreamResponse<InventoryInfo>>(result);
 
-                if (response.resultCode!="0")
+                if (response.resultCode != "0")
                 {
-                    // 璋冪敤寮傚父鎺ュ彛
                     SendErrorToUpstream(8, "", response.resultMsg ?? "涓婃父鎺ュ彛杩斿洖澶辫触", "");
                     return responseContent.Error(response.resultMsg ?? "涓婃父鎺ュ彛杩斿洖澶辫触");
                 }
+
                 if (response.data == null || !response.data.Any())
                 {
                     return responseContent.OK("鏃犳柊搴撳瓨鏁版嵁");
                 }
+
                 Db.Ado.BeginTran();
                 foreach (var item in response.data)
                 {
-                    var Inver = new Dt_Inventory
-                    { 
-                        Goods_no = item.goods_no,
-                        Batch_num = item.batch_num,
-                        Exp_date=item.exp_date,
-                        Business_qty = item.business_qty,
-                        Actual_qty = item.actural_qty,
-                    };
-                    AddData(Inver);
+                    // 浣跨敤 FirstOrDefault 閬垮厤鎵句笉鍒拌褰曟椂鎶涘嚭寮傚父
+                    var Inver = Db.Queryable<Dt_Inventory_Batch>()
+                        .First(x => x.MaterielCode == item.goods_no && x.BatchNo == item.batch_num);
+
+                    if (Inver != null)
+                    {
+                        Inver.ERPStockQuantity = item.business_qty;
+                        Db.Updateable(Inver).ExecuteCommand();
+                    }
                 }
                 Db.Ado.CommitTran();
                 return responseContent.OK("搴撳瓨淇℃伅鍚屾瀹屾垚");
@@ -79,136 +80,49 @@
         }
 
 
+
+
+
         /// <summary>
-        /// wcs鍥炰紶缁欐垜璋冪敤鎴戠殑鏂规硶
+        /// wcs鍥炰紶缁欐垜璋冪敤鎴戠殑鏂规硶 涓嶇鏄叆搴� 鍑哄簱 鐩樼偣閮戒細璋冪敤杩欎釜鎺ュ彛
         /// </summary>
         /// <param name="request"></param>
         /// <returns></returns>
-        //public ApiResponse<Dt_Inventory> OrderFeedback(EdiOrderCallbackRequest request)
-        //{
-        //    try
-        //    {
-        //        if (request == null || request.details == null || !request.details.Any())
-        //        {
-        //            return new ApiResponse<Dt_Inventory> { code = "500", msg = "璇锋眰鍙傛暟鏃犳晥" };
-        //        }
-        //        BaseDal.Db.Ado.BeginTran();//寮�鍚簨鍔�
-        //        foreach (var detail in request.details)
-        //        {
-        //            // 鍏堟煡搴撳瓨鏄惁瀛樺湪锛堟寜鐓т骇鍝佺紪鍙峰拰鎵规鏉ュ鎵撅級
-        //            var entity = BaseDal.Db.Queryable<Dt_Inventory>()
-        //                           .First(x => x.Goods_no == detail.productCode && x.Batch_num == detail.batchNo);
-        //            //灏嗛泦鍚堜腑鐨勬暟閲忚繘琛岀疮鍔�
-        //            // 瀹夊叏杞崲
-
-        //            //姝e父鍑哄叆搴�
-        //            decimal orderQty = detail.orderDetails?.Sum(x => decimal.TryParse(x.quantity, out var q) ? q : 0) ?? 0;
-        //            //鐩樼偣鐩樹簭
-        //            decimal diffQty = detail.stocktakingDetails?.Sum(x => Convert.ToDecimal(x.differenceQuantity)) ?? 0;
-        //            if (entity == null)
-        //            {
-        //                // 濡傛灉涓嶅瓨鍦紝鏂板缓涓�鏉�
-        //                entity = new Dt_Inventory
-        //                {
-        //                    Goods_no = detail.productCode ?? detail.productName,
-        //                    Reservoirarea = "榛樿搴撳尯",
-        //                    Batch_num = detail.batchNo,
-        //                    //涓氬姟搴撳瓨
-        //                    Business_qty = 0,
-        //                    //瀹為檯搴撳瓨
-        //                    Actual_qty = 0
-        //                };
-        //            }
-
-        //            switch (request.orderType)
-        //            {
-        //                case "1": // 鍏ュ簱
-        //                    entity.Business_qty += orderQty;
-        //                    entity.Actual_qty += orderQty;
-        //                    break;
-
-        //                case "2": // 鍑哄簱
-        //                    entity.Business_qty -= orderQty;
-        //                    entity.Actual_qty -= orderQty;
-        //                    if (entity.Business_qty < 0) entity.Business_qty = 0; // 鍙�夛細闃叉璐熸暟
-        //                    if (entity.Actual_qty < 0) entity.Actual_qty = 0;
-        //                    break;
-
-        //                case "3": // 鐩樼偣
-        //                    if (detail.stocktakingDetails != null && detail.stocktakingDetails.Any())
-        //                    {
-        //                        foreach (var stock in detail.stocktakingDetails)
-        //                        {
-        //                            //姣忔閮藉彧鎷夸竴鏉℃槑缁嗕腑鐨勬暟閲�
-        //                            decimal diff = Convert.ToDecimal(stock.differenceQuantity);
-        //                            if (stock.IsProfit == "1") // 鐩樼泩
-        //                            {
-        //                                entity.Business_qty += diff;//濡傛灉鐩樼泩浜嗗浜嗭紝灏卞皢涓氬姟鏁伴噺+锛岀洏浜忓垯鐩稿弽
-        //                                entity.PalletCode = stock.palletCode;
-        //                            }
-        //                            else // 鐩樹簭
-        //                            {
-        //                                entity.Business_qty -= diff;
-        //                                entity.PalletCode = stock.palletCode;
-        //                            }
-        //                        }
-        //                    }
-        //                    break;
-        //            }
-
-        //            // 淇濆瓨鏁版嵁
-        //            if (entity.Id == 0) // 鏂板缓
-        //                BaseDal.Db.Insertable(entity).ExecuteCommand();
-        //            else // 鏇存柊
-        //                BaseDal.Db.Updateable(entity).ExecuteCommand();
-        //        }
-        //        BaseDal.Db.Ado.CommitTran(); // 鎻愪氦浜嬪姟
-        //        return new ApiResponse<Dt_Inventory> { code = "0", msg = "鎴愬姛" };
-        //    }
-        //    catch (Exception ex)
-        //    {
-        //        BaseDal.Db.Ado.RollbackTran();//鍥炴粴浜嬪姟
-        //        return new ApiResponse<Dt_Inventory> { code = "500", msg = ex.Message };
-        //    }
-        //}
-
-
         public ApiResponse<Dt_InventoryInfo> OrderFeedback(EdiOrderCallbackRequest request)
         {
             try
-            {
+            {  
+                // 1锔忊儯 鏍¢獙璇锋眰
                 if (request == null || request.details == null || !request.details.Any())
                 {
                     return new ApiResponse<Dt_InventoryInfo> { code = "500", msg = "璇锋眰鍙傛暟鏃犳晥" };
                 }
 
-                BaseDal.Db.Ado.BeginTran(); // 寮�鍚簨鍔�
+                //鏌ユ壘鍒板叏閮ㄧ殑
 
                 foreach (var detail in request.details)
                 {
-                    // 鍏ュ簱鏁伴噺锛堣嚜鍔ㄥ彇姝o級
+                    var goods = Db.Queryable<Dt_MaterielInfo>().Where(x => x.MaterielCode == detail.productCode).First();
+
+                    // 3锔� 璁$畻鍏ュ簱鏁伴噺锛堝彇姝o級閬嶅巻 orderDetails 涓殑姣忎釜 EdiOrderBoxDto 瀵硅薄
                     decimal orderQty = detail.orderDetails?
                         .Sum(x => decimal.TryParse(x.quantity, out var q) ? Math.Abs(q) : 0)
                         ?? 0;
 
-                    // 鐩樼偣宸紓鏁�
-                    decimal diffQty = detail.stocktakingDetails?
-                        .Sum(x => Convert.ToDecimal(x.differenceQuantity))
-                        ?? 0;
-
-                    // 鏌ヨ鐜版湁搴撳瓨鏄庣粏
+                    // 5锔忊儯 鏌ヨ搴撳瓨璇︽儏
                     var entity = BaseDal.Db.Queryable<Dt_InventoryInfo>()
-                        .First(x => x.MaterielCode == detail.productCode && x.BatchNo == detail.batchNo);
+                        .First(x => x.MaterielCode == detail.productCode && x.BatchNo == detail.batchNo && x.LocationCode == "绔嬪簱");
+                    //鏌ヨ鐗╂枡琛�
+                    var Goods = BaseDal.Db.Queryable<Dt_MaterielInfo>().First(x => x.MaterielCode == detail.productCode);
 
                     if (entity == null)
                     {
-                        //涓嶅瓨鍦ㄥ垯鏂板缓搴撳瓨鏄庣粏璁板綍
                         entity = new Dt_InventoryInfo
                         {
                             PalletCode = detail.orderDetails?.FirstOrDefault()?.palletCode ?? "",
-                            WarehouseId = 1,  // 榛樿搴撴埧缂栧彿
-                            LocationCode = "",
-                            StockStatus = 1,   // 绔嬪簱鍥哄畾涓� 1
+                            WarehouseCode = "001",
+                            LocationCode = "绔嬪簱",
+                            StockStatus = 1,
                             MaterielCode = detail.productCode ?? detail.productName,
                             MaterielName = detail.productName ?? "",
                             MaterielSpec = detail.productSpecifications ?? "",
@@ -216,6 +130,7 @@
                             StockQuantity = 0,
                             OutboundQuantity = 0,
                             SupplyQuantity = 0,
+                            AvailableQuantity = 0,
                             InDate = DateTime.Now,
                             ProductionDate = detail.finishDate.ToString("yyyy-MM-dd"),
                             ShelfLife = 0,
@@ -224,7 +139,7 @@
                         };
                     }
 
-                    // 鎵规琛ㄨ褰曪紙鐢ㄤ簬姹囨�伙級
+                    // 6锔忊儯 鏌ヨ搴撳瓨鎵规淇℃伅
                     var batch = BaseDal.Db.Queryable<Dt_Inventory_Batch>()
                         .First(x => x.MaterielCode == detail.productCode && x.BatchNo == detail.batchNo);
 
@@ -238,6 +153,7 @@
                             BatchNo = detail.batchNo,
                             StockQuantity = 0,
                             OutboundQuantity = 0,
+                            AvailableQuantity = 0,
                             SupplyQuantity = 0,
                             ERPStockQuantity = 0,
                             Status = false,
@@ -247,56 +163,65 @@
                         };
                     }
 
+                    // 7锔忊儯 鎸夎鍗曠被鍨嬫墽琛屼笉鍚岄�昏緫
                     switch (request.orderType)
                     {
-                        case "1": // 鍏ュ簱
-                            entity.StockQuantity += (float)orderQty;
+                        case "1": //鍏ュ簱
+                            entity.StockQuantity = orderQty; //瀹為檯搴撳瓨
+                            entity.AvailableQuantity = orderQty;
                             entity.InDate = DateTime.Now;
                             entity.Remark = "鍏ュ簱鍗曞洖浼�";
 
-                            batch.StockQuantity += (float)orderQty;
+
+                            batch.StockQuantity = orderQty;
+                            batch.AvailableQuantity = orderQty;
                             batch.Remark = "鍏ュ簱鍗曞洖浼�";
+                            UpdateInboundOrderDetailStatus(detail.batchNo, detail.productCode);
                             break;
 
-                        case "2": // 鍑哄簱
-                            entity.OutboundQuantity += (float)orderQty;
-                            entity.StockQuantity -= (float)orderQty;
+                        case "2": // 鍑哄簱  鎬�100  鍑�20
+                            decimal actualOutQty = entity.StockQuantity - orderQty;// 璁$畻鏈瀹為檯鍑哄簱鏁伴噺 20
+                            entity.StockQuantity=orderQty; //瀹為檯搴撳瓨鏁� 100
+                            entity.OutboundQuantity =Math.Max(0,entity.OutboundQuantity-actualOutQty); //寰呭嚭搴撴暟閲�
+                            entity.AvailableQuantity = entity.StockQuantity - entity.OutboundQuantity;//鍙敤搴撳瓨
                             if (entity.StockQuantity < 0) entity.StockQuantity = 0;
                             entity.Remark = "鍑哄簱鍗曞洖浼�";
 
-                            batch.OutboundQuantity += (float)orderQty;
-                            batch.StockQuantity -= (float)orderQty;
+
+                            batch.StockQuantity = orderQty;
+                            batch.OutboundQuantity =Math.Max(0,batch.OutboundQuantity-actualOutQty);//寰呭嚭搴撴暟閲�
+                            batch.AvailableQuantity = batch.StockQuantity - batch.OutboundQuantity; //鍙敤搴撳瓨
                             if (batch.StockQuantity < 0) batch.StockQuantity = 0;
                             batch.Remark = "鍑哄簱鍗曞洖浼�";
+                            UpdateOutboundOrderDetailStatus(detail.batchNo, detail.productCode);
+                            //璋冩嫧浠诲姟
+                            _deliveryOrderServices.CreateAllocatInOut(goods);
                             break;
 
-                        case "3": //鐩樼偣
-                            if (detail.stocktakingDetails != null && detail.stocktakingDetails.Any())
+                        case "3": // 鐩樼偣
+                            decimal diff = detail.ea ?? 0;             // 宸紓鏁�
+                            int flag = detail.isLossOrProfit ?? 3;     // 1=鐩樹簭, 2=鐩樼泩, 3=鐩樹腑
+                            if (flag == 1) // 鐩樹簭
                             {
-                                foreach (var stock in detail.stocktakingDetails)
-                                {
-                                    decimal diff = Convert.ToDecimal(stock.differenceQuantity);
-                                    if (stock.IsProfit == "1") // 鐩樼泩
-                                    {
-                                        entity.SupplyQuantity += (float)Math.Abs(diff);
-                                        batch.SupplyQuantity += (float)Math.Abs(diff);
-                                    }
-                                    else // 鐩樹簭
-                                    {
-                                        entity.SupplyQuantity -= (float)Math.Abs(diff);
-                                        batch.SupplyQuantity -= (float)Math.Abs(diff);
-                                        if (entity.SupplyQuantity < 0) entity.SupplyQuantity = 0;
-                                        if (batch.SupplyQuantity < 0) batch.SupplyQuantity = 0;
-                                    }
-                                    entity.PalletCode = stock.palletCode;
-                                    entity.Remark = "鐩樼偣鍗曞洖浼�";
-                                    batch.Remark = "鐩樼偣鍗曞洖浼�";
-                                }
+                                //搴撳瓨鏁�-宸紓鏁�=鐩樹簭
+                                batch.SupplyQuantity +=( batch.StockQuantity - diff);
+                                batch.Remark = "鐩樼偣鍗曞洖浼� - 鐩樹簭";
+                            }
+                            else if (flag == 2) // 鐩樼泩
+                            {
+                                //搴撳瓨鏁�+宸紓鏁�=鐩樼泩
+                                batch.SupplyQuantity +=(batch.StockQuantity + diff);
+                                batch.Remark = "鐩樼偣鍗曞洖浼� - 鐩樼泩";
+                            }
+                            else // 鐩樹腑
+                            {
+                                batch.Remark = "鐩樼偣鍗曞洖浼� - 鐩樹腑";
                             }
                             break;
                     }
-
-                    //淇濆瓨鍒版暟鎹簱
+                    // 2锔忊儯 寮�鍚簨鍔�
+                    BaseDal.Db.Ado.BeginTran();
+                    // 8锔忊儯 淇濆瓨鏁版嵁
                     if (entity.Id == 0)
                         BaseDal.Db.Insertable(entity).ExecuteCommand();
                     else
@@ -308,17 +233,77 @@
                         BaseDal.Db.Updateable(batch).ExecuteCommand();
                 }
 
+                // 9锔忊儯 鎻愪氦浜嬪姟
                 BaseDal.Db.Ado.CommitTran();
-                return new ApiResponse<Dt_InventoryInfo> { code = "0", msg = "鎴愬姛" };
+
+                return new ApiResponse<Dt_InventoryInfo>
+                {
+                    code = "0",
+                    msg = "鎴愬姛"
+                };
             }
             catch (Exception ex)
             {
+                // 馃敓 鍥炴粴浜嬪姟
                 BaseDal.Db.Ado.RollbackTran();
-                return new ApiResponse<Dt_InventoryInfo> { code = "500", msg = ex.Message };
+
+                return new ApiResponse<Dt_InventoryInfo>
+                {
+                    code = "500",
+                    msg = $"澶勭悊澶辫触: {ex.Message}"
+                };
             }
         }
 
 
+        /// <summary>
+        /// 鏇存柊鍏ュ簱鍗曡缁�
+        /// </summary>
+        /// <param name="batchNo">鎵规鍙�</param>
+        /// <param name="productCode">鐗╂枡鍚嶇紪鐮�/param>
+        public void UpdateInboundOrderDetailStatus(string batchNo,string productCode)
+        {
+            try
+            {
+                //鏌ヨ璇︽儏
+                var detail = BaseDal.Db.Queryable<Dt_CabinOrderDetail>()
+                .First(x => x.Goods_no == productCode && x.Batch_num == batchNo && x.OrderDetailStatus == "寮�濮�" && x.Status == 1);
+                //鏌ユ壘鍒板悗鏍规嵁鐗╂枡鐘舵�侊紙OrderDetailStatus==寮�濮嬶級 璇ョ姸鎬丼tatus=1淇敼璇ユ潯璇︽儏鐨凮rderDetailStatus==宸插畬鎴�
+                detail.OrderDetailStatus = "宸插畬鎴�";
+                BaseDal.Db.Updateable(detail).ExecuteCommand();
+            }
+            catch (Exception ex)
+            {
+
+                throw;
+            }
+
+        }
+
+        /// <summary>
+        ///  //鏌ユ壘鍒板悗鏍规嵁鐗╂枡鐘舵�侊紙OrderDetailStatus==寮�濮嬶級 璇ョ姸鎬丼tatus=1淇敼璇ユ潯璇︽儏鐨凮rderDetailStatus==宸插畬鎴�
+        /// </summary>
+        /// <param name="batchNo"></param>
+        /// <param name="productCode"></param>
+        public void UpdateOutboundOrderDetailStatus(string batchNo, string productCode)
+        {
+            try
+            {
+                //鏌ヨ璇︽儏
+                var detail = BaseDal.Db.Queryable<Dt_DeliveryOrderDetail>()
+                .First(x => x.Goods_no == productCode && x.Batch_num == batchNo && x.OotDetailStatus == "寮�濮�" && x.Status == 1);
+                detail.OotDetailStatus = "宸插畬鎴�";
+                BaseDal.Db.Updateable(detail).ExecuteCommand();
+            }
+            catch (Exception ex)
+            {
+
+                throw;
+            }
+         
+
+        }
+
 
         /// <summary>
         /// 鎺ㄩ�佸紓甯镐俊鎭粰涓婃父绯荤粺1.鍏ュ簱鍗曟帴鍙o紱2.鍏ュ簱鍗曟姤瀹屾垚鎺ュ彛锛�3.鍑哄簱鍗曟帴鍙o紱4.鍑哄簱鎶ュ畬鎴愭帴鍙o紱5.鑽搧鍩虹淇℃伅鍚屾鎺ュ彛锛�6.渚涘簲鍟嗕俊鎭帴鍙o紱7.瀹㈡埛淇℃伅鎺ュ彛锛�8.搴撳瓨

--
Gitblit v1.9.3