From 9b7acc479dc747c8c78eae84d3f83e1347aa478d Mon Sep 17 00:00:00 2001
From: dengjunjie <dengjunjie@hnkhzn.com>
Date: 星期二, 04 十一月 2025 12:53:04 +0800
Subject: [PATCH] 优化

---
 新建文件夹/WIDESEA_WMSServer/WIDESEA_SquareCabinServices/InventoryServices.cs |  694 +++++++++++++++++++++++++++++++++++++--------------------
 1 files changed, 452 insertions(+), 242 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 6f83567..00f08fa 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"
@@ -1,16 +1,19 @@
 锘縰sing HslCommunication;
 using Newtonsoft.Json;
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
+using SqlSugar;
+using WIDESEA_Common;
+using WIDESEA_Common.OrderEnum;
+using WIDESEA_Common.StockEnum;
+using WIDESEA_Common.TaskEnum;
+using WIDESEA_Common.WareHouseEnum;
 using WIDESEA_Core;
 using WIDESEA_Core.BaseRepository;
 using WIDESEA_Core.BaseServices;
+using WIDESEA_Core.Enums;
 using WIDESEA_Core.Helper;
 using WIDESEA_DTO.SquareCabin;
 using WIDESEA_ISquareCabinServices;
+using WIDESEA_IWMsInfoServices;
 using WIDESEA_Model.Models;
 using static WIDESEA_DTO.SquareCabin.OrderDto;
 using static WIDESEA_DTO.SquareCabin.TowcsDto;
@@ -20,10 +23,19 @@
 {
     public class InventoryServices : ServiceBase<Dt_Inventory, IRepository<Dt_Inventory>>, IInventoryServices
     {
-        public InventoryServices(IRepository<Dt_Inventory> BaseDal) : base(BaseDal)
+        private readonly ICabinOrderServices _cabinOrderServices;
+        private readonly IDeliveryOrderServices _deliveryOrderServices;
+        private readonly ISupplyTaskService _supplyTaskService;
+        private readonly IInventory_BatchServices _inventoryBatchServices;
+        private readonly IMessageInfoService _infoService;
+        public InventoryServices(IRepository<Dt_Inventory> BaseDal, ICabinOrderServices cabinOrderServices, IDeliveryOrderServices deliveryOrderServices, ISupplyTaskService supplyTaskService, IInventory_BatchServices inventoryBatchServices, IMessageInfoService infoService) : base(BaseDal)
         {
+            _cabinOrderServices = cabinOrderServices;
+            _deliveryOrderServices = deliveryOrderServices;
+            _supplyTaskService = supplyTaskService;
+            _inventoryBatchServices = inventoryBatchServices;
+            _infoService = infoService;
         }
-
 
         /// <summary>
         /// 鑾峰彇涓婃父搴撳瓨淇℃伅
@@ -32,293 +44,491 @@
         /// <param name="batch_num">鎵瑰彿</param>
         /// <returns></returns>
         /// <exception cref="NotImplementedException"></exception>
-        public WebResponseContent GetInventoryList(string goods_no, string batch_num)
+        public WebResponseContent GetInventoryList(int[] keys)
         {
             var responseContent = new WebResponseContent();
             try
             {
-                var url = "http://121.37.118.63/GYZ2/95fck/repositoryInfo";
-                // 鍙戣捣璇锋眰
-                var result = HttpHelper.Post(url, new { goods_no,batch_num}.ToJsonString());
+                //鏍规嵁id鏌ヨ搴撳瓨鎵规淇℃伅琛紝鑾峰彇鍒颁粬浠殑鍟嗗搧缂栫爜鍜屾壒娆″彿
+                var inventoryBatches = _inventoryBatchServices.Repository.QueryData(x => keys.Contains(x.Id)); //鏌ュ叏閮�
 
-                // 鍙嶅簭鍒楀寲
-                var response = JsonConvert.DeserializeObject<UpstreamResponse<InventoryInfo>>(result);
+                var url = "http://121.37.118.63:80/GYZ2/95fck/repositoryInfo";
+                foreach (var item in inventoryBatches)
+                {
+                    var result = HttpHelper.Post(url, new { goods_no = item.MaterielCode, batch_num = item.BatchNo }.ToJsonString());
 
-                if (response.resultCode!="0")
-                {
-                    // 璋冪敤寮傚父鎺ュ彛
-                    SendErrorToUpstream(8, "", response.resultMsg ?? "涓婃父鎺ュ彛杩斿洖澶辫触", "");
-                    return responseContent.Error(response.resultMsg ?? "涓婃父鎺ュ彛杩斿洖澶辫触");
+                    var response = JsonConvert.DeserializeObject<UpstreamResponse<InventoryInfo>>(result);
+                    if (response == null) continue;
+                    var data = response.data.FirstOrDefault();
+                    if (data != null)
+                        item.ERPStockQuantity = data.business_qty;
                 }
-                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);
-                }
-                Db.Ado.CommitTran();
+                _inventoryBatchServices.UpdateData(inventoryBatches);
                 return responseContent.OK("搴撳瓨淇℃伅鍚屾瀹屾垚");
+
             }
             catch (Exception ex)
             {
-                Db.Ado.RollbackTran();
-                SendErrorToUpstream(8, "", ex.Message, "");
                 return responseContent.Error("鍚屾澶辫触: " + ex.Message);
             }
         }
 
-
-        /// <summary>
-        /// 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
             {
+                if (request == null || request.details == null || request.details.Count < 1) throw new Exception("璇锋眰鍙傛暟鏃犳晥");
+                var Warecode = WarehouseEnum.绔嬪簱.ObjToInt().ToString("000");
+                switch (request.orderType)
+                {
+                    case "1": //鍏ュ簱
+                        {
+                            Dt_CabinOrder cabinOrder = _cabinOrderServices.Db.Queryable<Dt_CabinOrder>().Where(x => x.Order_no == request.externalOrderNo && x.Warehouse_no == Warecode).Includes(x => x.Details).First();
+                            if (cabinOrder == null) throw new Exception($"鏈壘鍒板叆搴撳崟鍙枫�恵request.externalOrderNo}銆戠殑鍏ュ簱鍗曚俊鎭�");
+                            if (cabinOrder.OdrderStatus == "宸插畬鎴�")
+                                return new ApiResponse<Dt_InventoryInfo> { code = "0", msg = $"鎴愬姛" };
+                            WebResponseContent content = _cabinOrderServices.CompleteLKInOrder(cabinOrder, request);
+                            if (!content.Status) throw new Exception(content.Message);
+                        }
+                        break;
+                    case "2":
+                        {
+                            Dt_DeliveryOrder deliveryOrder = _deliveryOrderServices.Db.Queryable<Dt_DeliveryOrder>().Where(x => x.Out_no == request.externalOrderNo && x.Warehouse_no == Warecode).Includes(x => x.Details).First();
+                            if (deliveryOrder == null) throw new Exception($"鏈壘鍒板嚭搴撳崟鍙枫�恵request.externalOrderNo}銆戠殑鍑哄簱鍗曚俊鎭�");
+                            if (deliveryOrder.OutStatus == "宸插畬鎴�")
+                                return new ApiResponse<Dt_InventoryInfo> { code = "0", msg = $"鎴愬姛" };
+                            WebResponseContent content = _deliveryOrderServices.CompleteLKOutOrder(deliveryOrder, request);
+                            if (!content.Status) throw new Exception(content.Message);
+                        }
+                        break;
+                    case "3":
+                        {
+                            #region 鎵剧洏鐐逛换鍔�
+                            List<Dt_SupplyTask> supplyTasks = _supplyTaskService.Repository.QueryData(x => x.OrderNo == request.externalOrderNo && x.WarehouseCode == Warecode && x.TaskStatus == SupplyStatusEnum.NewCheck.ObjToInt());
+                            foreach (var detail in request.details)
+                            {
+                                Dt_SupplyTask? supplyTask = supplyTasks.Where(x => x.MaterielCode == detail.productCode && x.BatchNo == detail.batchNo).FirstOrDefault();
+                                if (supplyTask == null) throw new Exception($"鏈壘鍒拌鍗曞彿銆恵request.externalOrderNo}銆戠墿鏂欑紪鍙枫�恵detail.productCode}銆戠殑鐩樼偣浠诲姟");
+                                if (detail.isLossOrProfit == 1) // 鐩樹簭
+                                {
+                                    supplyTask.SupplyQuantity = supplyTask.StockQuantity - detail.ea;
+                                }
+                                else if (detail.isLossOrProfit == 2) // 鐩樼泩
+                                {
+                                    supplyTask.SupplyQuantity = supplyTask.StockQuantity + detail.ea;
+                                }
+                                else // 鐩樹腑
+                                {
+                                    supplyTask.SupplyQuantity = supplyTask.StockQuantity;
+                                }
+                                var content = _deliveryOrderServices.CheckTaskFinish(supplyTask);
+                                if (!content.Status) throw new Exception(content.Message);
+                            }
+                            #endregion
+                        }
+                        break;
+                    default:
+                        throw new Exception($"鏈畾涔夌殑绫诲瀷銆恵request.orderType}銆�");
+                }
+                return new ApiResponse<Dt_InventoryInfo>
+                {
+                    code = "0",
+                    msg = $"鎴愬姛"
+                };
+            }
+            catch (Exception ex)
+            {
+                return new ApiResponse<Dt_InventoryInfo>
+                {
+                    code = "500",
+                    msg = $"澶勭悊澶辫触: {ex.Message}"
+                };
+            }
+        }
+
+        /// <summary>
+        /// wcs鍥炰紶缁欐垜璋冪敤鎴戠殑鏂规硶 涓嶇鏄叆搴� 鍑哄簱 鐩樼偣閮戒細璋冪敤杩欎釜鎺ュ彛
+        /// </summary>
+        /// <param name="request"></param>
+        /// <returns></returns>
+        public ApiResponse<Dt_InventoryInfo> OrderFeedback1(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(); // 寮�鍚簨鍔�
-
+                var reslut = WarehouseEnum.绔嬪簱.ObjToInt().ToString("000");
+                // 2锔忊儯 寮�鍚簨鍔�
+                //BaseDal.Db.Ado.BeginTran();
                 foreach (var detail in request.details)
                 {
-                    // 鍏ュ簱鏁伴噺锛堣嚜鍔ㄥ彇姝o級
+                    #region 鏌ヨ鐗╂枡淇℃伅銆佽绠楁�诲拰
+                    //鏌ヨ鐗╂枡琛�
+                    var goods = Db.Queryable<Dt_MaterielInfo>().Where(x => x.MaterielCode == detail.productCode).First();
+                    if (goods == null) throw new Exception($"鏈壘鍒扮墿鏂欑紪鐮併�恵detail.productCode}銆戠殑鐗╂枡淇℃伅");
+                    // 3锔� 璁$畻鍏ュ簱鏁伴噺锛堝彇姝o級閬嶅巻 orderDetails 涓殑姣忎釜 EdiOrderBoxDto 瀵硅薄
                     decimal orderQty = detail.orderDetails?
                         .Sum(x => decimal.TryParse(x.quantity, out var q) ? Math.Abs(q) : 0)
                         ?? 0;
+                    #endregion
 
-                    // 鐩樼偣宸紓鏁�
-                    decimal diffQty = detail.stocktakingDetails?
-                        .Sum(x => Convert.ToDecimal(x.differenceQuantity))
-                        ?? 0;
-
-                    // 鏌ヨ鐜版湁搴撳瓨鏄庣粏
-                    var entity = BaseDal.Db.Queryable<Dt_InventoryInfo>()
-                        .First(x => x.MaterielCode == detail.productCode && x.BatchNo == detail.batchNo);
-
-                    if (entity == null)
-                    {
-                        //涓嶅瓨鍦ㄥ垯鏂板缓搴撳瓨鏄庣粏璁板綍
-                        entity = new Dt_InventoryInfo
-                        {
-                            PalletCode = detail.orderDetails?.FirstOrDefault()?.palletCode ?? "",
-                            WarehouseCode = 1.ToString(),  // 榛樿搴撴埧缂栧彿
-                            LocationCode = "",
-                            StockStatus = 1,   // 绔嬪簱鍥哄畾涓� 1
-                            MaterielCode = detail.productCode ?? detail.productName,
-                            MaterielName = detail.productName ?? "",
-                            MaterielSpec = detail.productSpecifications ?? "",
-                            BatchNo = detail.batchNo,
-                            StockQuantity = 0,
-                            OutboundQuantity = 0,
-                            SupplyQuantity = 0,
-                            InDate = DateTime.Now,
-                            ProductionDate = detail.finishDate.ToString("yyyy-MM-dd"),
-                            ShelfLife = 0,
-                            ValidityPeriod = "",
-                            Remark = "WCS鍥炰紶鍒涘缓"
-                        };
-                    }
-
-                    // 鎵规琛ㄨ褰曪紙鐢ㄤ簬姹囨�伙級
-                    var batch = BaseDal.Db.Queryable<Dt_Inventory_Batch>()
-                        .First(x => x.MaterielCode == detail.productCode && x.BatchNo == detail.batchNo);
-
-                    if (batch == null)
-                    {
-                        batch = new Dt_Inventory_Batch
-                        {
-                            MaterielCode = detail.productCode ?? detail.productName,
-                            MaterielName = detail.productName ?? "",
-                            MaterielSpec = detail.productSpecifications ?? "",
-                            BatchNo = detail.batchNo,
-                            StockQuantity = 0,
-                            OutboundQuantity = 0,
-                            SupplyQuantity = 0,
-                            ERPStockQuantity = 0,
-                            Status = false,
-                            ProductionDate = detail.finishDate.ToString("yyyy-MM-dd"),
-                            ValidityPeriod = DateTime.Now.AddYears(1).ToString("yyyy-MM-dd"),
-                            Remark = "鑷姩鍒涘缓"
-                        };
-                    }
-
+                    // 7锔忊儯 鎸夎鍗曠被鍨嬫墽琛屼笉鍚岄�昏緫
                     switch (request.orderType)
                     {
-                        case "1": // 鍏ュ簱
-                            entity.StockQuantity += orderQty;
-                            entity.InDate = DateTime.Now;
-                            entity.Remark = "鍏ュ簱鍗曞洖浼�";
-
-                            batch.StockQuantity += orderQty;
-                            batch.Remark = "鍏ュ簱鍗曞洖浼�";
-                            break;
-
-                        case "2": // 鍑哄簱
-                            entity.OutboundQuantity += orderQty;
-                            entity.StockQuantity -= orderQty;
-                            if (entity.StockQuantity < 0) entity.StockQuantity = 0;
-                            entity.Remark = "鍑哄簱鍗曞洖浼�";
-
-                            batch.OutboundQuantity += orderQty;
-                            batch.StockQuantity -= orderQty;
-                            if (batch.StockQuantity < 0) batch.StockQuantity = 0;
-                            batch.Remark = "鍑哄簱鍗曞洖浼�";
-                            break;
-
-                        case "3": //鐩樼偣
-                            if (detail.stocktakingDetails != null && detail.stocktakingDetails.Any())
+                        case "1": //鍏ュ簱
                             {
-                                foreach (var stock in detail.stocktakingDetails)
+                                #region 澶勭悊鍏ュ簱璁㈠崟
+
+                                Dt_CabinOrder cabinOrder = Db.Queryable<Dt_CabinOrder>().Where(x => x.Order_no == request.externalOrderNo && x.Warehouse_no == reslut).Includes(x => x.Details).First();
+                                if (cabinOrder == null) throw new Exception($"鏈壘鍒板叆搴撳崟鍙枫�恵request.externalOrderNo}銆戠殑鍏ュ簱鍗曚俊鎭�");
+                                #region 鍒ゆ柇鏄惁涓虹洏鐩堝叆搴撳崟
+                                if (cabinOrder.Order_type == "5")
                                 {
-                                    decimal diff = Convert.ToDecimal(stock.differenceQuantity);
-                                    if (stock.IsProfit == "1") // 鐩樼泩
+                                    foreach (var item in cabinOrder.Details)
                                     {
-                                        entity.SupplyQuantity += Math.Abs(diff);
-                                        batch.SupplyQuantity += Math.Abs(diff);
+                                        Dt_InventoryInfo inventoryInfo = BaseDal.Db.Queryable<Dt_InventoryInfo>()
+                                        .First(x => x.MaterielCode == item.Goods_no && x.BatchNo == item.Batch_num && x.LocationCode == reslut);
+                                        var qty = inventoryInfo.SupplyQuantity - inventoryInfo.StockQuantity;
+                                        if (orderQty != qty)
+                                            throw new Exception($"鐩樼泩鍏ュ簱鏁伴噺涓嶄竴鑷�,涓婁紶鏁伴噺銆恵orderQty}銆�,搴撳瓨鐩樼泩鏁伴噺銆恵qty}銆�");
+                                        inventoryInfo.StockQuantity = inventoryInfo.SupplyQuantity;
+                                        inventoryInfo.SupplyQuantity = 0;
+                                        inventoryInfo.StockStatus = StockStatusEmun.鍏ュ簱瀹屾垚.ObjToInt();
+                                        #region 娣诲姞鐩樼泩鍏ュ簱浠诲姟
+                                        Dt_SupplyTask_Hty supplyTask_Hty = new Dt_SupplyTask_Hty()
+                                        {
+                                            WarehouseCode = inventoryInfo.WarehouseCode,
+                                            OperateType = OperateTypeEnum.鑷姩瀹屾垚.ToString(),
+                                            InsertTime = DateTime.Now,
+                                            TaskStatus = SupplyStatusEnum.InFinish.ObjToInt(),
+                                            BatchNo = inventoryInfo.BatchNo,
+                                            MaterielName = inventoryInfo.MaterielName,
+                                            MaterielCode = inventoryInfo.MaterielCode,
+                                            MaterielSpec = inventoryInfo.MaterielSpec,
+                                            TaskType = TaskTypeEnum.ChenckIn.ObjToInt(),
+                                            CreateDate = DateTime.Now,
+                                            Creater = App.User.UserName,
+                                            LocationCode = inventoryInfo.LocationCode,
+                                            OrderNo = cabinOrder.Order_no,
+                                            StockQuantity = orderQty,
+                                            SupplyQuantity = 0,
+                                            Remark = "鐩樼泩鍏ュ簱"
+                                        };
+                                        #endregion
+                                        var inventory_Batch = BaseDal.Db.Queryable<Dt_Inventory_Batch>()
+                                        .First(x => x.MaterielCode == inventoryInfo.MaterielCode && x.BatchNo == inventoryInfo.BatchNo);
+                                        inventory_Batch.StockQuantity = inventory_Batch.SupplyQuantity;
+                                        inventory_Batch.SupplyQuantity = 0;
+                                        item.OrderDetailStatus = "宸插畬鎴�";
+                                        BaseDal.Db.Updateable(item).ExecuteCommand();
+                                        BaseDal.Db.Updateable(inventoryInfo).ExecuteCommand();
+                                        BaseDal.Db.Updateable(inventory_Batch).ExecuteCommand();
+                                        BaseDal.Db.Insertable(supplyTask_Hty).ExecuteCommand();
                                     }
-                                    else // 鐩樹簭
+                                    var cabinOrder1 = BaseDal.Db.Queryable<Dt_CabinOrder>().Where(x => x.Id == cabinOrder.Id).Includes(x => x.Details).First();
+                                    if (!cabinOrder1.Details.Where(x => x.OrderDetailStatus != "宸插畬鎴�").Any())
+                                        cabinOrder.OdrderStatus = "宸插畬鎴�";
+                                    BaseDal.Db.Updateable(cabinOrder).ExecuteCommand();
+                                }
+                                #endregion
+                                else
+                                {
+                                    if (cabinOrder.OdrderStatus == "宸插畬鎴�")
+                                        return new ApiResponse<Dt_InventoryInfo>
+                                        {
+                                            code = "0",
+                                            msg = "鎴愬姛"
+                                        };
+                                    Dt_CabinOrderDetail cabinOrderDetail = cabinOrder.Details.Where(x => x.Goods_no == detail.productCode && x.Batch_num == detail.batchNo && x.Status == 1).First();
+                                    if (cabinOrderDetail == null || cabinOrderDetail.OrderDetailStatus == "宸插畬鎴�")
+                                        throw new Exception($"鍏ュ簱鍗曟槑缁嗗凡瀹屾垚");
+                                    cabinOrderDetail.Order_Inqty += orderQty;
+                                    if (cabinOrderDetail.Order_Inqty > cabinOrderDetail.Order_qty)
+                                        throw new Exception($"鍏ュ簱鏁伴噺涓嶅彲瓒呭嚭鍗曟嵁鏁伴噺");
+                                    cabinOrder.OdrderStatus = "寮�濮�";
+                                    cabinOrderDetail.OrderDetailStatus = "寮�濮�";
+                                    if (cabinOrderDetail.Order_Inqty == cabinOrderDetail.Order_qty)
+                                        cabinOrderDetail.OrderDetailStatus = "宸插畬鎴�";
+                                    BaseDal.Db.Updateable(cabinOrderDetail).ExecuteCommand();
+                                    var cabinOrder1 = BaseDal.Db.Queryable<Dt_CabinOrder>().Where(x => x.Order_no == cabinOrder.Order_no && x.Warehouse_no == reslut).Includes(x => x.Details).First();
+                                    if (!cabinOrder1.Details.Where(x => x.OrderDetailStatus != "宸插畬鎴�").Any())
+                                        cabinOrder.OdrderStatus = "宸插畬鎴�";
+                                    BaseDal.Db.Updateable(cabinOrder).ExecuteCommand();
+                                    #endregion
+
+                                    #region 鍒涘缓搴撳瓨淇℃伅
+                                    // 5锔忊儯 鏌ヨ搴撳瓨璇︽儏
+                                    var inventoryInfo = BaseDal.Db.Queryable<Dt_InventoryInfo>()
+                                        .First(x => x.MaterielCode == cabinOrderDetail.Goods_no && x.BatchNo == cabinOrderDetail.Batch_num && x.LocationCode == "绔嬪簱");
+                                    if (inventoryInfo != null)
                                     {
-                                        entity.SupplyQuantity -= Math.Abs(diff);
-                                        batch.SupplyQuantity -= Math.Abs(diff);
-                                        if (entity.SupplyQuantity < 0) entity.SupplyQuantity = 0;
-                                        if (batch.SupplyQuantity < 0) batch.SupplyQuantity = 0;
+                                        inventoryInfo.StockQuantity += orderQty;
+                                        inventoryInfo.AvailableQuantity += orderQty;
+                                        inventoryInfo.InDate = DateTime.Now;
+                                        BaseDal.Db.Updateable(inventoryInfo).ExecuteCommand();
                                     }
-                                    entity.PalletCode = stock.palletCode;
-                                    entity.Remark = "鐩樼偣鍗曞洖浼�";
-                                    batch.Remark = "鐩樼偣鍗曞洖浼�";
+                                    else
+                                    {
+                                        inventoryInfo = new Dt_InventoryInfo
+                                        {
+                                            PalletCode = detail.orderDetails?.FirstOrDefault()?.palletCode ?? "",
+                                            WarehouseCode = "001",
+                                            LocationCode = "绔嬪簱",
+                                            StockStatus = StockStatusEmun.鍏ュ簱瀹屾垚.ObjToInt(),
+                                            MaterielCode = goods.MaterielCode,
+                                            MaterielName = goods.MaterielName,
+                                            MaterielSpec = goods.MaterielSpec,
+                                            BatchNo = detail.batchNo,
+                                            StockQuantity = orderQty,
+                                            OutboundQuantity = 0,
+                                            SupplyQuantity = 0,
+                                            AvailableQuantity = orderQty,
+                                            InDate = DateTime.Now,
+                                            ProductionDate = detail.finishDate.ToString("yyyy-MM-dd"),
+                                            ShelfLife = 0,
+                                            ValidityPeriod = cabinOrderDetail.Exp_date,
+                                            Remark = "鏅鸿兘绔嬪簱"
+                                        };
+                                        BaseDal.Db.Insertable(inventoryInfo).ExecuteCommand();
+                                    }
+                                    #endregion
+
+                                    #region 鍒涘缓搴撳瓨鎵规淇℃伅  鍙湁涓嶆槸璋冩嫧鍏ュ簱鍗曪紙115锛夋墠瀵规壒娆′俊鎭繘琛屼慨鏀�
+                                    if (cabinOrder.Order_type != InOrderTypeEnum.Allocat.ObjToInt().ToString())
+                                    {
+                                        // 6锔忊儯 鏌ヨ搴撳瓨鎵规淇℃伅
+                                        var inventory_Batch = BaseDal.Db.Queryable<Dt_Inventory_Batch>()
+                                            .First(x => x.MaterielCode == inventoryInfo.MaterielCode && x.BatchNo == inventoryInfo.BatchNo);
+
+                                        if (inventory_Batch != null)
+                                        {
+                                            inventory_Batch.StockQuantity += orderQty;
+                                            inventory_Batch.AvailableQuantity += orderQty;
+                                            BaseDal.Db.Updateable(inventory_Batch).ExecuteCommand();
+                                        }
+                                        else
+                                        {
+                                            inventory_Batch = new Dt_Inventory_Batch
+                                            {
+                                                MaterielCode = inventoryInfo.MaterielCode,
+                                                MaterielName = inventoryInfo.MaterielName,
+                                                MaterielSpec = inventoryInfo.MaterielSpec,
+                                                BatchNo = inventoryInfo.BatchNo,
+                                                StockQuantity = inventoryInfo.StockQuantity,
+                                                OutboundQuantity = inventoryInfo.OutboundQuantity,
+                                                AvailableQuantity = inventoryInfo.AvailableQuantity,
+                                                SupplyQuantity = inventoryInfo.SupplyQuantity,
+                                                ERPStockQuantity = 0,
+                                                Status = false,
+                                                ProductionDate = detail.finishDate.ToString("yyyy-MM-dd"),
+                                                ValidityPeriod = inventoryInfo.ValidityPeriod.ObjToDate(),
+                                                Remark = "鑷姩鍒涘缓"
+                                            };
+                                            BaseDal.Db.Insertable(inventory_Batch).ExecuteCommand();
+                                        }
+                                    }
+                                    #endregion
+
+                                    #region 娣诲姞鍏ュ簱浠诲姟鍘嗗彶
+                                    Dt_SupplyTask_Hty supplyTask_Hty = new Dt_SupplyTask_Hty()
+                                    {
+                                        WarehouseCode = inventoryInfo.WarehouseCode,
+                                        TaskNum = cabinOrderDetail.Id,
+                                        OperateType = OperateTypeEnum.鑷姩瀹屾垚.ToString(),
+                                        InsertTime = DateTime.Now,
+                                        TaskStatus = SupplyStatusEnum.InFinish.ObjToInt(),
+                                        BatchNo = inventoryInfo.BatchNo,
+                                        MaterielName = inventoryInfo.MaterielName,
+                                        MaterielCode = inventoryInfo.MaterielCode,
+                                        MaterielSpec = inventoryInfo.MaterielSpec,
+                                        TaskType = cabinOrder.Order_type == "1" ? TaskTypeEnum.In.ObjToInt() : TaskTypeEnum.OutReturn.ObjToInt(),
+                                        CreateDate = DateTime.Now,
+                                        Creater = App.User.UserName,
+                                        LocationCode = inventoryInfo.LocationCode,
+                                        OrderNo = cabinOrder.Order_no,
+                                        StockQuantity = orderQty,
+                                        SupplyQuantity = 0,
+                                        Remark = "鍏ュ簱"
+                                    };
+                                    //杩欓噷杩欓噷鐨勫叆搴撹皟鎷ㄥ崟鐘舵�佹槸115锛屽氨灏嗚皟鎷ㄤ换鍔$姸鎬佹敼涓�7
+                                    if (cabinOrder.Order_type == InOrderTypeEnum.Allocat.ObjToInt().ToString()) supplyTask_Hty.TaskType = TaskTypeEnum.AllocatIn.ObjToInt();
+                                    BaseDal.Db.Insertable(supplyTask_Hty).ExecuteCommand();
+                                    #endregion
                                 }
                             }
                             break;
+                        case "2":
+                            {
+                                Dt_DeliveryOrder cabinOrder = Db.Queryable<Dt_DeliveryOrder>().Where(x => x.Out_no == request.externalOrderNo && x.Warehouse_no == reslut).Includes(x => x.Details).First();
+                                if (cabinOrder == null) throw new Exception($"鏈壘鍒板嚭搴撳崟鍙枫�恵request.externalOrderNo}銆戠殑鍑哄簱鍗曚俊鎭�");
+
+                                if (cabinOrder.Out_type == "6")
+                                {
+                                    foreach (var item in cabinOrder.Details)
+                                    {
+                                        Dt_InventoryInfo inventoryInfo = BaseDal.Db.Queryable<Dt_InventoryInfo>()
+                                        .First(x => x.MaterielCode == item.Goods_no && x.BatchNo == item.Batch_num && x.LocationCode == reslut);
+                                        var qty = inventoryInfo.StockQuantity - inventoryInfo.SupplyQuantity;
+                                        if (orderQty != qty)
+                                            throw new Exception($"鐩樹簭鍑哄簱鏁伴噺涓嶄竴鑷�,涓婁紶鏁伴噺銆恵orderQty}銆�,搴撳瓨鐩樹簭鏁伴噺銆恵qty}銆�");
+                                        inventoryInfo.StockQuantity = inventoryInfo.SupplyQuantity;
+                                        inventoryInfo.SupplyQuantity = 0;
+                                        inventoryInfo.StockStatus = StockStatusEmun.鍏ュ簱瀹屾垚.ObjToInt();
+                                        #region 娣诲姞鐩樹簭鍑哄簱浠诲姟
+                                        Dt_SupplyTask_Hty supplyTask_Hty = new Dt_SupplyTask_Hty()
+                                        {
+                                            WarehouseCode = inventoryInfo.WarehouseCode,
+                                            OperateType = OperateTypeEnum.鑷姩瀹屾垚.ToString(),
+                                            InsertTime = DateTime.Now,
+                                            TaskStatus = SupplyStatusEnum.OutFinish.ObjToInt(),
+                                            BatchNo = inventoryInfo.BatchNo,
+                                            MaterielName = inventoryInfo.MaterielName,
+                                            MaterielCode = inventoryInfo.MaterielCode,
+                                            MaterielSpec = inventoryInfo.MaterielSpec,
+                                            TaskType = TaskTypeEnum.ChenckOut.ObjToInt(),
+                                            CreateDate = DateTime.Now,
+                                            Creater = App.User.UserName,
+                                            LocationCode = inventoryInfo.LocationCode,
+                                            OrderNo = cabinOrder.Out_no,
+                                            StockQuantity = orderQty,
+                                            SupplyQuantity = 0,
+                                            Remark = "鐩樹簭鍑哄簱"
+                                        };
+                                        #endregion
+                                        var inventory_Batch = BaseDal.Db.Queryable<Dt_Inventory_Batch>()
+                                        .First(x => x.MaterielCode == inventoryInfo.MaterielCode && x.BatchNo == inventoryInfo.BatchNo);
+                                        inventory_Batch.StockQuantity = inventory_Batch.SupplyQuantity;
+                                        inventory_Batch.SupplyQuantity = 0;
+                                        item.OotDetailStatus = "宸插畬鎴�";
+                                        BaseDal.Db.Updateable(item).ExecuteCommand();
+                                        BaseDal.Db.Updateable(inventoryInfo).ExecuteCommand();
+                                        BaseDal.Db.Updateable(inventory_Batch).ExecuteCommand();
+                                        BaseDal.Db.Insertable(supplyTask_Hty).ExecuteCommand();
+                                    }
+                                    var cabinOrder1 = BaseDal.Db.Queryable<Dt_DeliveryOrder>().Where(x => x.Id == cabinOrder.Id).Includes(x => x.Details).First();
+                                    if (!cabinOrder1.Details.Where(x => x.OotDetailStatus != "宸插畬鎴�").Any())
+                                        cabinOrder.OutStatus = "宸插畬鎴�";
+                                    BaseDal.Db.Updateable(cabinOrder).ExecuteCommand();
+                                }
+                                else
+                                {
+                                    #region 鎵惧嚭搴撲换鍔�
+                                    Dt_SupplyTask supplyTask = Db.Queryable<Dt_SupplyTask>().Where(x => x.MaterielCode == detail.productCode && x.BatchNo == detail.batchNo && x.OrderNo == request.externalOrderNo && x.WarehouseCode == reslut && x.TaskStatus == SupplyStatusEnum.NewOut.ObjToInt()).First();
+                                    if (supplyTask == null) throw new Exception($"鏈壘鍒拌鍗曞彿銆恵request.externalOrderNo}銆戠墿鏂欑紪鍙枫�恵detail.productCode}銆戠殑鍑哄簱浠诲姟");
+                                    if (supplyTask.StockQuantity != orderQty) throw new Exception($"璁㈠崟鍙枫�恵request.orderNo}銆戠墿鏂欑紪鍙枫�恵detail.productCode}銆戠殑鍑哄簱鏁伴噺涓庡嚭搴撲换鍔℃暟閲忎笉鐩哥瓑");
+                                    #endregion
+
+                                    #region 澶勭悊鍑哄簱鍗曘�佸簱瀛樹俊鎭�佸嚭搴撲换鍔°�佽皟鎷ㄤ换鍔�
+                                    var content = _deliveryOrderServices.OutTaskFinish(supplyTask);
+                                    if (!content.Status) throw new Exception(content.Message);
+                                    #endregion
+                                }
+                            }
+                            break;
+                        case "3": // 鐩樼偣
+                            {
+                                var WarehouseLK = WarehouseEnum.绔嬪簱.ObjToInt().ToString("000");
+                                #region 鎵剧洏鐐逛换鍔�
+                                Dt_SupplyTask supplyTask = Db.Queryable<Dt_SupplyTask>().Where(x => x.MaterielCode == detail.productCode && x.BatchNo == detail.batchNo && x.OrderNo == request.externalOrderNo && x.WarehouseCode == WarehouseLK && x.TaskStatus == SupplyStatusEnum.NewCheck.ObjToInt()).First();
+                                if (supplyTask == null) throw new Exception($"鏈壘鍒拌鍗曞彿銆恵request.externalOrderNo}銆戠墿鏂欑紪鍙枫�恵detail.productCode}銆戠殑鐩樼偣浠诲姟");
+                                #endregion
+                                if (detail.isLossOrProfit == 1) // 鐩樹簭
+                                {
+                                    supplyTask.SupplyQuantity = supplyTask.StockQuantity - detail.ea;
+                                }
+                                else if (detail.isLossOrProfit == 2) // 鐩樼泩
+                                {
+                                    supplyTask.SupplyQuantity = supplyTask.StockQuantity + detail.ea;
+                                }
+                                else // 鐩樹腑
+                                {
+                                    supplyTask.SupplyQuantity = supplyTask.StockQuantity;
+                                }
+                                var content = _deliveryOrderServices.CheckTaskFinish(supplyTask);
+                                if (!content.Status) throw new Exception(content.Message);
+                            }
+                            break;
                     }
-
-                    //淇濆瓨鍒版暟鎹簱
-                    if (entity.Id == 0)
-                        BaseDal.Db.Insertable(entity).ExecuteCommand();
-                    else
-                        BaseDal.Db.Updateable(entity).ExecuteCommand();
-
-                    if (batch.Id == 0)
-                        BaseDal.Db.Insertable(batch).ExecuteCommand();
-                    else
-                        BaseDal.Db.Updateable(batch).ExecuteCommand();
                 }
 
-                BaseDal.Db.Ado.CommitTran();
-                return new ApiResponse<Dt_InventoryInfo> { code = "0", msg = "鎴愬姛" };
+                // 9锔忊儯 鎻愪氦浜嬪姟
+                //BaseDal.Db.Ado.CommitTran();
+
+                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 };
+                // 馃敓 鍥炴粴浜嬪姟
+                //BaseDal.Db.Ado.RollbackTran();
+
+                return new ApiResponse<Dt_InventoryInfo>
+                {
+                    code = "500",
+                    msg = $"澶勭悊澶辫触: {ex.Message}"
+                };
+
+                //Console.WriteLine(ex.Message);
             }
         }
 
 
+        /// <summary>
+        /// 鏇存柊鍏ュ簱鍗曡缁�
+        /// </summary>
+        /// <param name="batchNo">鎵规鍙�</param>
+        /// <param name="productCode">鐗╂枡鍚嶇紪鐮�/param>
+        public void UpdateInboundOrderDetailStatus(string orderNo, 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