From c30ac3ad95328c1a34661ecde471e4215df9dab3 Mon Sep 17 00:00:00 2001
From: dengjunjie <dengjunjie@hnkhzn.com>
Date: 星期五, 17 十月 2025 19:54:03 +0800
Subject: [PATCH] 添加盘点功能、添加立方智能库补货功能(入库平库调拨出库,立库调拨入库)

---
 新建文件夹/WIDESEA_WMSServer/WIDESEA_SquareCabinServices/DeliveryOrderServices.cs |  652 ++++++++++++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 574 insertions(+), 78 deletions(-)

diff --git "a/\346\226\260\345\273\272\346\226\207\344\273\266\345\244\271/WIDESEA_WMSServer/WIDESEA_SquareCabinServices/DeliveryOrderServices.cs" "b/\346\226\260\345\273\272\346\226\207\344\273\266\345\244\271/WIDESEA_WMSServer/WIDESEA_SquareCabinServices/DeliveryOrderServices.cs"
index 820ca9a..9b662bd 100644
--- "a/\346\226\260\345\273\272\346\226\207\344\273\266\345\244\271/WIDESEA_WMSServer/WIDESEA_SquareCabinServices/DeliveryOrderServices.cs"
+++ "b/\346\226\260\345\273\272\346\226\207\344\273\266\345\244\271/WIDESEA_WMSServer/WIDESEA_SquareCabinServices/DeliveryOrderServices.cs"
@@ -5,12 +5,18 @@
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
+using WIDESEA_Common.LocationEnum;
+using WIDESEA_Common.OrderEnum;
+using WIDESEA_Common.TaskEnum;
 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_IBasicService;
 using WIDESEA_ISquareCabinServices;
+using WIDESEA_IWMsInfoServices;
 using WIDESEA_Model.Models;
 using static WIDESEA_DTO.SquareCabin.OrderDto;
 
@@ -18,12 +24,26 @@
 
 namespace WIDESEA_SquareCabinServices
 {
-    public class DeliveryOrderServices : ServiceBase<Dt_DeliveryOrder, IRepository<Dt_DeliveryOrder>>, IDeliveryOrderServices
+    public partial class DeliveryOrderServices : ServiceBase<Dt_DeliveryOrder, IRepository<Dt_DeliveryOrder>>, IDeliveryOrderServices
     {
         static string SearchDate = "";
-
-        public DeliveryOrderServices(IRepository<Dt_DeliveryOrder> BaseDal) : base(BaseDal)
+        private readonly IBasicService _basicService;
+        private readonly IUnitOfWorkManage _unitOfWorkManage;
+        private readonly IInventory_BatchServices _inventory_BatchServices;
+        private readonly IInventoryInfoService _inventoryInfoService;
+        private readonly IDeliveryOrderDetailServices _deliveryOrderDetailServices;
+        private readonly ISupplyTaskService _supplyTaskService;
+        private readonly ICabinOrderServices _cabinOrderServices;
+        public IRepository<Dt_DeliveryOrder> Repository => BaseDal;
+        public DeliveryOrderServices(IRepository<Dt_DeliveryOrder> BaseDal, IBasicService basicService, IUnitOfWorkManage unitOfWorkManage, IInventory_BatchServices inventory_BatchServices, IInventoryInfoService inventoryInfoService, IDeliveryOrderDetailServices deliveryOrderDetailServices, ISupplyTaskService supplyTaskService, ICabinOrderServices cabinOrderServices) : base(BaseDal)
         {
+            _basicService = basicService;
+            _unitOfWorkManage = unitOfWorkManage;
+            _deliveryOrderDetailServices = deliveryOrderDetailServices;
+            _supplyTaskService = supplyTaskService;
+            _inventory_BatchServices = inventory_BatchServices;
+            _inventoryInfoService = inventoryInfoService;
+            _cabinOrderServices = cabinOrderServices;
         }
 
         /// <summary>
@@ -37,15 +57,16 @@
         //    try
         //    {
         //        // 璇锋眰鍦板潃
-        //        var url = "http://127.0.0.1:9000/GYZ2/95fck/outOrder";
+        //        var url = "http://121.37.118.63:80/GYZ2/95fck/outOrder";
 
-        //        if (string.IsNullOrEmpty(SearchDate)) SearchDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
+        //        //if (string.IsNullOrEmpty(SearchDate)) SearchDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
         //        //// 璇锋眰鍙傛暟
         //        var requestData = new
         //        {
-        //            searchDate = SearchDate
+        //            //searchDate = SearchDate
+        //            searchDate = "2022-10-10 20:45:16"  // 姝g‘鐨勬牸寮�
         //        };
-        //        SearchDate = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd HH:mm:ss");
+        //        //SearchDate = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd HH:mm:ss");
         //        // 鍙戣捣璇锋眰
         //        var result = HttpHelper.Post(url, requestData.ToJsonString());
 
@@ -62,75 +83,111 @@
         //        {
         //            return responseContent.OK("鏃犳柊鍑哄簱鍗曟暟鎹�");
         //        }
+        //        // 鑾峰彇鎵�鏈夊凡瀛樺湪鐨勫嚭搴撳崟鍙�
+        //        var existingOutOrderNos = BaseDal.Db.Queryable<Dt_DeliveryOrder>()
+        //            .Select(x => x.Out_no)
+        //            .ToList();
 
-        //        Db.Ado.BeginTran();
+        //        // 杩囨护鎺夊凡瀛樺湪鐨勫嚭搴撳崟
+        //        var newOutOrders = response.data
+        //            .Where(outorder => !existingOutOrderNos.Contains(outorder.order_no))//order_no鍑哄簱鍗曞彿
+        //            .ToList();
 
-        //        foreach (var outorder in response.data)
+        //        if (!newOutOrders.Any())
         //        {
-        //            try
+        //            return responseContent.OK("鎵�鏈夊嚭搴撳崟宸插瓨鍦紝鏃犻渶鏂板");
+        //        }
+        //        Db.Ado.BeginTran();
+        //        try
+        //        {
+        //            List<Dt_DeliveryOrder> _DeliveryOrders = new List<Dt_DeliveryOrder>();
+        //            ////瀛樺偍鍑哄簱鍗曞彿
+        //            //List<string> orderNos = new List<string>();
+
+        //            foreach (var outorder in newOutOrders)
         //            {
-        //                // 鎻掑叆鍑哄簱鍗�
         //                var entityOrder = new Dt_DeliveryOrder
         //                {
-        //                    Out_no = outorder.out_no,
-        //                    Out_type = outorder.out_type,
+        //                    Out_no = outorder.order_no,
+        //                    Out_type = outorder.order_type,
         //                    Client_no = outorder.client_no,
-        //                    Client_name=outorder.client_name,
+        //                    Client_name = outorder.client_name,
         //                    Account_time = outorder.account_time,
-        //                    OutStatus= "鏈畬鎴�",
+        //                    Warehouse_no=outorder.warehouse_no,
+        //                    OutStatus = "鏈畬鎴�",
+        //                    //Details = outorder.details.Select(d => new Dt_DeliveryOrderDetail
+        //                    //{
+        //                    //    // InsertNav 浼氳嚜鍔ㄨ缃叧鑱斿瓧娈� DeliveryOrderId
+        //                    //    Goods_no = d.goods_no,
+        //                    //    Order_qty = d.order_qty,
+        //                    //    Batch_num = d.batch_num,
+        //                    //    Exp_date = d.exp_date,
+        //                    //    OotDetailStatus = "鏂板缓",
+        //                    //    Status = outorder.warehouse_no == "001" ? 0 : 2, //濡傛灉鏄�001鎴块偅涔堝皢鐘舵�佽缃负0锛�2涓轰汉宸ュ鐞�
+        //                    //}).ToList()
         //                };
-        //                var outorderId = BaseDal.Db.Insertable(entityOrder).ExecuteReturnIdentity();
-
-        //                // 鎻掑叆鍑哄簱鍗曟槑缁�
-        //                var detailEntities = outorder.details.Select(d => new Dt_DeliveryOrderDetail
+        //                #region 鏍规嵁鍑哄簱鍗曡鎯呮煡鎵剧墿鏂欏悕绉般�佹壒娆$殑搴撳瓨淇℃伅(Dt_InventoryInfo)锛涙牴鎹厛鍏ュ厛鍑�,CreateDate
+        //                //鏍规嵁鐗╂枡鍚嶇О鏌ユ壘鐗╂枡淇℃伅
+        //                foreach (var item in outorder.details)
         //                {
-        //                    DeliveryOrderId = outorderId,
-        //                    Goods_no = d.goods_no,
-        //                    Order_qty = d.out_qty,
-        //                    Batch_num = d.batch_num,
-        //                    Exp_date = d.exp_date,
-        //                    OotDetailStatus="鏂板缓"
-        //                }).ToList();
-        //                BaseDal.Db.Insertable(detailEntities).ExecuteCommand();
-        //            }
-        //            catch (Exception ex)
-        //            {
-        //                SendErrorToUpstream(3, outorder.out_no, ex.Message, "");
-        //                throw; // 璁╁灞傛崟鑾峰苟鍥炴粴
-        //            }
-        //        }
+        //                    // 灏嗕笂娓稿嚭搴撴暟閲忚浆涓烘鏁�
+        //                    item.order_qty = Math.Abs(item.order_qty);
+        //                    var medication = BaseDal.Db.Queryable<Dt_MedicineGoods>()
+        //                        .Where(m => m.Goods_no == item.goods_no)
+        //                        .First();
 
-        //        Db.Ado.CommitTran();
-        //        return responseContent.OK("鍚屾鍑哄簱鍗曟垚鍔�");
+
+        //                }
+
+        //                //鏍规嵁绠辫鍒ゆ柇鏄惁鏈夋暎浠讹紝璁$畻鍑烘暎浠舵暟閲忓拰鏁翠欢鏁�
+
+        //                //鏍规嵁鍏ュ簱鏃堕棿鍒嗙粍锛屾湁鏁d欢灏变紭鍏堝垎閰嶇珛搴撴暟閲忥紝鏁翠欢浼樺厛鍒嗛厤骞冲簱鏁伴噺
+
+
+        //                #endregion
+        //                _DeliveryOrders.Add(entityOrder);
+
+        //            };
+
+        //            // 浣跨敤 InsertNav 涓�娆℃�ф彃鍏ヤ富琛ㄥ拰瀛愯〃鏁版嵁
+        //            BaseDal.Db.InsertNav(_DeliveryOrders).Include(x => x.Details).ExecuteCommand();
+
+        //            // 杩欓噷鍙互娣诲姞涓嬪彂鍒� WCS 鐨勯�昏緫
+        //            // var result = EdiOut(); // 鍙戠粰涓嬫父
+
+        //            Db.Ado.CommitTran();
+        //            return responseContent.OK("鍚屾鍑哄簱鍗曟垚鍔�");
+        //        }
+        //        catch (Exception ex)
+        //        {
+        //            Db.Ado.RollbackTran();
+        //            SendErrorToUpstream(3, "", ex.Message, "");
+        //            return responseContent.Error("鍚屾澶辫触: " + ex.Message);
+        //        }
         //    }
         //    catch (Exception ex)
         //    {
+        //        // 鍏ㄥ眬寮傚父鏃讹紝涔熸帹閫佸紓甯哥粰涓婃父
         //        SendErrorToUpstream(3, "", ex.Message, "");
-        //        Db.Ado.RollbackTran();
         //        return responseContent.Error("鍚屾澶辫触: " + ex.Message);
         //    }
         //}
+
+
 
         public WebResponseContent GetUpstreamOutOrder()
         {
             var responseContent = new WebResponseContent();
             try
             {
-                // 璇锋眰鍦板潃
                 var url = "http://121.37.118.63:80/GYZ2/95fck/outOrder";
 
-                //if (string.IsNullOrEmpty(SearchDate)) SearchDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
-                //// 璇锋眰鍙傛暟
                 var requestData = new
                 {
-                    //searchDate = SearchDate
-                    searchDate = "2022-10-10 20:45:16"  // 姝g‘鐨勬牸寮�
+                    searchDate = "2022-10-10 20:45:16"
                 };
-                //SearchDate = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd HH:mm:ss");
-                // 鍙戣捣璇锋眰
-                var result = HttpHelper.Post(url, requestData.ToJsonString());
 
-                // 鍙嶅簭鍒楀寲
+                var result = HttpHelper.Post(url, requestData.ToJsonString());
                 var response = JsonConvert.DeserializeObject<UpstreamResponse<UpstramOutOrderInfo>>(result);
 
                 if (response.resultCode != "0")
@@ -143,26 +200,26 @@
                 {
                     return responseContent.OK("鏃犳柊鍑哄簱鍗曟暟鎹�");
                 }
-                // 鑾峰彇鎵�鏈夊凡瀛樺湪鐨勫嚭搴撳崟鍙�
+
+                // 宸插瓨鍦ㄥ嚭搴撳崟鍙�
                 var existingOutOrderNos = BaseDal.Db.Queryable<Dt_DeliveryOrder>()
                     .Select(x => x.Out_no)
                     .ToList();
 
-                // 杩囨护鎺夊凡瀛樺湪鐨勫嚭搴撳崟
+                // 鏂板嚭搴撳崟
                 var newOutOrders = response.data
-                    .Where(outorder => !existingOutOrderNos.Contains(outorder.order_no))//order_no鍑哄簱鍗曞彿
+                    .Where(outorder => !existingOutOrderNos.Contains(outorder.order_no))
                     .ToList();
 
                 if (!newOutOrders.Any())
                 {
                     return responseContent.OK("鎵�鏈夊嚭搴撳崟宸插瓨鍦紝鏃犻渶鏂板");
                 }
+
                 Db.Ado.BeginTran();
                 try
                 {
                     List<Dt_DeliveryOrder> _DeliveryOrders = new List<Dt_DeliveryOrder>();
-                    ////瀛樺偍鍑哄簱鍗曞彿
-                    //List<string> orderNos = new List<string>();
 
                     foreach (var outorder in newOutOrders)
                     {
@@ -173,37 +230,209 @@
                             Client_no = outorder.client_no,
                             Client_name = outorder.client_name,
                             Account_time = outorder.account_time,
-                            Warehouse_no=outorder.warehouse_no,
+                            Warehouse_no = outorder.warehouse_no,
                             OutStatus = "鏈畬鎴�",
-                            Details = outorder.details.Select(d => new Dt_DeliveryOrderDetail
-                            {
-                                // InsertNav 浼氳嚜鍔ㄨ缃叧鑱斿瓧娈� DeliveryOrderId
-                                Goods_no = d.goods_no,
-                                Order_qty = d.out_qty,
-                                Batch_num = d.batch_num,
-                                Exp_date = d.exp_date,
-                                OotDetailStatus = "鏂板缓",
-                                Status = outorder.warehouse_no == "001" ? 0 : 2, //濡傛灉鏄�001鎴块偅涔堝皢鐘舵�佽缃负0锛�2涓轰汉宸ュ鐞�
-                            }).ToList()
+                            Details = new List<Dt_DeliveryOrderDetail>()
                         };
-                        #region 鏍规嵁鍑哄簱鍗曡鎯呮煡鎵剧墿鏂欏悕绉般�佹壒娆$殑搴撳瓨淇℃伅(Dt_InventoryInfo)锛涙牴鎹厛鍏ュ厛鍑�,CreateDate
-                        //鏍规嵁鐗╂枡鍚嶇О鏌ユ壘鐗╂枡淇℃伅
 
-                        //鏍规嵁绠辫鍒ゆ柇鏄惁鏈夋暎浠讹紝璁$畻鍑烘暎浠舵暟閲忓拰鏁翠欢鏁�
+                        // 閬嶅巻鍑哄簱鏄庣粏
+                        foreach (var item in outorder.details)
+                        {
+                            //  1锔忊儯 杞负姝f暟锛堜笂娓稿彲鑳戒紶璐熸暟锛�
+                            item.order_qty = Math.Abs(item.order_qty);
+                            decimal orderQty = (decimal)item.order_qty;
 
-                        //鏍规嵁鍏ュ簱鏃堕棿鍒嗙粍锛屾湁鏁d欢灏变紭鍏堝垎閰嶇珛搴撴暟閲忥紝鏁翠欢浼樺厛鍒嗛厤骞冲簱鏁伴噺
+                            //  2锔忊儯 鏌ョ墿鏂欏熀纭�淇℃伅锛堣幏鍙栫瑙勶級
+                            var medication = BaseDal.Db.Queryable<Dt_MedicineGoods>()
+                                .Where(m => m.Goods_no == item.goods_no)
+                                .First();
 
+                            if (medication == null)
+                            {
+                                SendErrorToUpstream(3, "", $"鎵句笉鍒扮墿鏂欎俊鎭細{item.goods_no}", "");
+                                continue;
+                            }
 
-                        #endregion
+                            decimal boxQty = medication.BoxQty <= 0 ? 1 : medication.BoxQty;
+
+                            //  3锔忊儯 璁$畻鏁翠欢涓庢暎浠舵暟閲�
+                            var fullBoxes = (int)(orderQty / boxQty);   // 鏁翠欢绠辨暟
+                            var partialQty = orderQty % boxQty;         // 鏁d欢鏁伴噺
+
+                            // 鑻ヤ笂娓哥粰浜嗘壒娆″彿锛屽垯浼樺厛鍖归厤
+                            string requestedBatch = string.IsNullOrEmpty(item.batch_num) ? null : item.batch_num;
+
+                            // helper: 鏌ヨ搴撳瓨鍑芥暟锛堟寜鍏ュ簱鏃堕棿鍗囧簭锛�
+                            Func<int, List<Dt_InventoryInfo>> queryInventoryByStockStatus = (stockStatus) =>
+                            {
+                                var q = BaseDal.Db.Queryable<Dt_InventoryInfo>()
+                                    .Where(i => i.MaterielCode == item.goods_no &&
+                                                (i.StockQuantity - i.OutboundQuantity) > 0 &&
+                                                i.StockStatus == stockStatus);
+
+                                if (!string.IsNullOrEmpty(requestedBatch))
+                                    q = q.Where(i => i.BatchNo == requestedBatch);
+
+                                return q.OrderBy(i => i.InDate).ToList();
+                            };
+
+                            // 4锔忊儯鍒嗛厤鏁d欢锛堜紭鍏堢珛搴� Status=1锛�
+                            decimal remainingPartial = partialQty;
+                            if (remainingPartial > 0)
+                            {
+                                var invList_ly = queryInventoryByStockStatus(1);
+                                foreach (var inv in invList_ly)
+                                {
+                                    if (remainingPartial <= 0) break;
+
+                                    decimal available = (decimal)(inv.StockQuantity - inv.OutboundQuantity);
+                                    if (available <= 0) continue;
+
+                                    decimal use = Math.Min(available, remainingPartial);
+
+                                    var detail = new Dt_DeliveryOrderDetail
+                                    {
+                                        Goods_no = item.goods_no,
+                                        Order_qty = use,
+                                        Batch_num = inv.BatchNo,
+                                        Exp_date = inv.ValidityPeriod,
+                                        OotDetailStatus = "鏂板缓",
+                                        Status = 0, //绔嬪簱
+                                        Reservoirarea = inv.LocationCode
+                                    };
+                                    entityOrder.Details.Add(detail);
+
+                                    inv.OutboundQuantity += use;
+                                    BaseDal.Db.Updateable(inv).ExecuteCommand();
+
+                                    remainingPartial -= use;
+                                }
+
+                                // 绔嬪簱涓嶅 鈫� 骞冲簱琛� (Status=2)
+                                if (remainingPartial > 0)
+                                {
+                                    var invList_pk = queryInventoryByStockStatus(2);
+                                    foreach (var inv in invList_pk)
+                                    {
+                                        if (remainingPartial <= 0) break;
+
+                                        decimal available = (decimal)(inv.StockQuantity - inv.OutboundQuantity);
+                                        if (available <= 0) continue;
+
+                                        decimal use = Math.Min(available, remainingPartial);
+
+                                        var detail = new Dt_DeliveryOrderDetail
+                                        {
+                                            Goods_no = item.goods_no,
+                                            Order_qty = use,
+                                            Batch_num = inv.BatchNo,
+                                            Exp_date = inv.ValidityPeriod,
+                                            OotDetailStatus = "鏂板缓",
+                                            Status = 2, //骞冲簱
+                                            Reservoirarea = inv.LocationCode
+                                        };
+                                        entityOrder.Details.Add(detail);
+
+                                        inv.OutboundQuantity += use;
+                                        BaseDal.Db.Updateable(inv).ExecuteCommand();
+
+                                        remainingPartial -= use;
+                                    }
+                                }
+
+                                if (remainingPartial > 0)
+                                {
+                                    SendErrorToUpstream(3, "", $"鍑哄簱鍗昜{outorder.order_no}]鐗╂枡[{item.goods_no}]鏁d欢搴撳瓨涓嶈冻锛屾湭鍒嗛厤锛歿remainingPartial}", "");
+                                }
+                            }
+
+                            //鍒嗛厤鏁翠欢锛堜紭鍏堝钩搴� Status=2锛�
+                            int remainingFullBoxes = fullBoxes;
+                            if (remainingFullBoxes > 0)
+                            {
+                                var invList_pk = queryInventoryByStockStatus(2);
+                                foreach (var inv in invList_pk)
+                                {
+                                    if (remainingFullBoxes <= 0) break;
+
+                                    decimal available = (decimal)(inv.StockQuantity - inv.OutboundQuantity);
+                                    if (available < boxQty) continue;
+
+                                    int canProvideBoxes = (int)(available / boxQty);
+                                    if (canProvideBoxes <= 0) continue;
+
+                                    int useBoxes = Math.Min(canProvideBoxes, remainingFullBoxes);
+                                    decimal useQty = useBoxes * boxQty;
+
+                                    var detail = new Dt_DeliveryOrderDetail
+                                    {
+                                        Goods_no = item.goods_no,
+                                        Order_qty = useQty,
+                                        Batch_num = inv.BatchNo,
+                                        Exp_date = inv.ValidityPeriod,
+                                        OotDetailStatus = "鏂板缓",
+                                        Status = 2, //骞冲簱
+                                        Reservoirarea = inv.LocationCode
+                                    };
+                                    entityOrder.Details.Add(detail);
+
+                                    inv.OutboundQuantity += useQty;
+                                    BaseDal.Db.Updateable(inv).ExecuteCommand();
+
+                                    remainingFullBoxes -= useBoxes;
+                                }
+
+                                // 骞冲簱涓嶅 鈫� 绔嬪簱琛� (Status=1)
+                                if (remainingFullBoxes > 0)
+                                {
+                                    var invList_ly = queryInventoryByStockStatus(1);
+                                    foreach (var inv in invList_ly)
+                                    {
+                                        if (remainingFullBoxes <= 0) break;
+
+                                        decimal available = (decimal)(inv.StockQuantity - inv.OutboundQuantity);
+                                        if (available < boxQty) continue;
+
+                                        int canProvideBoxes = (int)(available / boxQty);
+                                        if (canProvideBoxes <= 0) continue;
+
+                                        int useBoxes = Math.Min(canProvideBoxes, remainingFullBoxes);
+                                        decimal useQty = useBoxes * boxQty;
+
+                                        var detail = new Dt_DeliveryOrderDetail
+                                        {
+                                            Goods_no = item.goods_no,
+                                            Order_qty = useQty,
+                                            Batch_num = inv.BatchNo,
+                                            Exp_date = inv.ValidityPeriod,
+                                            OotDetailStatus = "鏂板缓",
+                                            Status = 0, //绔嬪簱锛堣ˉ鏁寸锛�
+                                            Reservoirarea = inv.LocationCode
+                                        };
+                                        entityOrder.Details.Add(detail);
+
+                                        inv.OutboundQuantity += useQty;
+                                        BaseDal.Db.Updateable(inv).ExecuteCommand();
+
+                                        remainingFullBoxes -= useBoxes;
+                                    }
+                                }
+
+                                if (remainingFullBoxes > 0)
+                                {
+                                    decimal unfilledQty = remainingFullBoxes * boxQty;
+                                    SendErrorToUpstream(3, "", $"鍑哄簱鍗昜{outorder.order_no}]鐗╂枡[{item.goods_no}]鏁寸搴撳瓨涓嶈冻锛屾湭鍒嗛厤鏁伴噺锛歿unfilledQty}", "");
+                                }
+                            }
+                        }
+
                         _DeliveryOrders.Add(entityOrder);
-                        //orderNos.Add(outorder.out_no);
-                    };
+                    }
 
-                    // 浣跨敤 InsertNav 涓�娆℃�ф彃鍏ヤ富琛ㄥ拰瀛愯〃鏁版嵁
-                    BaseDal.Db.InsertNav(_DeliveryOrders).Include(x => x.Details).ExecuteCommand();
-
-                    // 杩欓噷鍙互娣诲姞涓嬪彂鍒� WCS 鐨勯�昏緫
-                    // var result = EdiOut(); // 鍙戠粰涓嬫父
+                    // 鎻掑叆涓昏〃+鏄庣粏
+                    BaseDal.Db.InsertNav(_DeliveryOrders)
+                        .Include(x => x.Details)
+                        .ExecuteCommand();
 
                     Db.Ado.CommitTran();
                     return responseContent.OK("鍚屾鍑哄簱鍗曟垚鍔�");
@@ -217,7 +446,6 @@
             }
             catch (Exception ex)
             {
-                // 鍏ㄥ眬寮傚父鏃讹紝涔熸帹閫佸紓甯哥粰涓婃父
                 SendErrorToUpstream(3, "", ex.Message, "");
                 return responseContent.Error("鍚屾澶辫触: " + ex.Message);
             }
@@ -303,7 +531,7 @@
 
                             // 鏇存柊鏄庣粏鐘舵��
                             BaseDal.Db.Updateable<Dt_DeliveryOrderDetail>()
-                              .SetColumns(d => new Dt_DeliveryOrderDetail { Status = 1,OotDetailStatus="宸插畬鎴�" })
+                              .SetColumns(d => new Dt_DeliveryOrderDetail { Status = 1, OotDetailStatus = "宸插畬鎴�" })
                               .Where(d => d.DeliveryOrderId == order.Id && d.Status == 0)
                               .ExecuteCommand();
 
@@ -467,6 +695,8 @@
         //        return new WebResponseContent { Status = false, Message = ex.Message };
         //    }
         //}
+
+
         public WebResponseContent InventoryGood(string externalOrderNo)
         {
             try
@@ -492,7 +722,6 @@
                     .ToList();
 
                 var inventoryDict = inventoryList.ToDictionary(i => i.Goods_no, i => i);
-
                 string url = "http://172.16.1.2:9357/file-admin/api/out/ediOut";
                 bool allSuccess = true;
 
@@ -888,6 +1117,273 @@
             }
         }
 
-       
+        public WebResponseContent GetPdDeliveryOrders(SaveModel saveModel)
+        {
+            WebResponseContent content = new WebResponseContent();
+            try
+            {
+                int pageNo = saveModel.MainData["pageNo"].ObjToInt();
+                string warehouseCode = saveModel.MainData["warehouseId"].ToString();
+                string orderNo = saveModel.MainData["orderNo"].ToString();
+                List<Dt_DeliveryOrder> dt_ReceiveOrders = new List<Dt_DeliveryOrder>();
+                if (string.IsNullOrEmpty(orderNo))
+                {
+                    dt_ReceiveOrders = Db.Queryable<Dt_DeliveryOrder>().Where(x => (x.OutStatus == "鏂板缓" || x.OutStatus == "寮�濮�") && x.Warehouse_no == warehouseCode && x.Out_type == "3").Includes(x => x.Details).OrderByDescending(x => x.CreateDate).ToPageList(pageNo, 5);
+                }
+                else
+                {
+                    dt_ReceiveOrders = Db.Queryable<Dt_DeliveryOrder>().Where(x => (x.Out_no.Contains(orderNo) || x.Client_no.Contains(orderNo)) && (x.OutStatus == "鏂板缓" || x.OutStatus == "寮�濮�" && x.Out_type == "3") && x.Warehouse_no == warehouseCode).OrderByDescending(x => x.CreateDate).Includes(x => x.Details).ToPageList(pageNo, 5);
+                }
+
+                content.OK(data: dt_ReceiveOrders);
+            }
+            catch (Exception)
+            {
+
+                throw;
+            }
+            return content;
+        }
+
+        public WebResponseContent GetDeliveryOrders(SaveModel saveModel)
+        {
+            WebResponseContent content = new WebResponseContent();
+            try
+            {
+                int pageNo = saveModel.MainData["pageNo"].ObjToInt();
+                string warehouseCode = saveModel.MainData["warehouseId"].ToString();
+                string orderNo = saveModel.MainData["orderNo"].ToString();
+                List<Dt_DeliveryOrder> dt_ReceiveOrders = new List<Dt_DeliveryOrder>();
+                if (string.IsNullOrEmpty(orderNo))
+                {
+                    dt_ReceiveOrders = Db.Queryable<Dt_DeliveryOrder>().Where(x => (x.OutStatus == "鏂板缓" || x.OutStatus == "寮�濮�") && x.Warehouse_no == warehouseCode && x.Out_type != "3").Includes(x => x.Details).OrderByDescending(x => x.CreateDate).ToPageList(pageNo, 5);
+                }
+                else
+                {
+                    dt_ReceiveOrders = Db.Queryable<Dt_DeliveryOrder>().Where(x => (x.Out_no.Contains(orderNo) || x.Client_no.Contains(orderNo)) && (x.OutStatus == "鏂板缓" || x.OutStatus == "寮�濮�" && x.Out_type != "3") && x.Warehouse_no == warehouseCode).OrderByDescending(x => x.CreateDate).Includes(x => x.Details).ToPageList(pageNo, 5);
+                }
+
+                content.OK(data: dt_ReceiveOrders);
+            }
+            catch (Exception)
+            {
+
+                throw;
+            }
+            return content;
+        }
+
+        public WebResponseContent GetDeliveryOrderDetail(int pageNo, string orderNo, bool isPick)
+        {
+            WebResponseContent content = new WebResponseContent();
+            Dt_DeliveryOrder cabinOrder = new Dt_DeliveryOrder();
+            if (isPick)
+                cabinOrder = Db.Queryable<Dt_DeliveryOrder>().Includes(x => x.Details).First(x => x.Out_no == orderNo && x.Out_type == "3");
+            else
+                cabinOrder = Db.Queryable<Dt_DeliveryOrder>().Includes(x => x.Details).First(x => x.Out_no == orderNo && x.Out_type != "3");
+            List<Dt_DeliveryOrderDetail>? cabinOrderDetails = cabinOrder.Details?.Where(x => x.Reservoirarea == pageNo.ToString()).ToList();
+            content.OK(data: cabinOrderDetails);
+            return content;
+        }
+        public WebResponseContent MatPicking(SaveModel saveModel)
+        {
+            WebResponseContent content = new WebResponseContent();
+            try
+            {
+                var LocationCode = saveModel.MainData["LocationCode"].ToString();
+                var orderNo = saveModel.MainData["orderNo"].ToString();
+                var batchNo = saveModel.MainData["batchNo"].ToString();
+                var Inqty = saveModel.MainData["Inqty"].ObjToInt();
+                var warehouseCode = saveModel.MainData["warehouseCode"].ToString();
+                Dt_DeliveryOrder cabinOrder = BaseDal.Db.Queryable<Dt_DeliveryOrder>().Where(x => x.Out_no == orderNo && x.Warehouse_no == warehouseCode && x.Out_type == "3").Includes(x => x.Details).First();
+                if (cabinOrder == null) return WebResponseContent.Instance.Error($"鐩樼偣鍗曞凡瀹屾垚");
+                Dt_DeliveryOrderDetail cabinOrderDetail = cabinOrder.Details.Where(x => x.Batch_num == batchNo && x.Reservoirarea == warehouseCode).First();
+                if (cabinOrderDetail == null) return WebResponseContent.Instance.Error($"鐩樼偣鍗曟槑缁嗗凡瀹屾垚");
+                Dt_MaterielInfo materielInfo = _basicService.MaterielInfoService.Repository.QueryFirst(x => x.MaterielCode == cabinOrderDetail.Goods_no);
+                if (materielInfo == null) return WebResponseContent.Instance.Error($"璇风淮鎶ょ墿鏂欑紪鍙枫�恵cabinOrderDetail.Goods_no}銆戠殑鐗╂枡淇℃伅");
+                cabinOrderDetail.Order_Outqty += Inqty;
+                if (cabinOrderDetail.Order_Outqty > cabinOrderDetail.Order_qty)
+                    return WebResponseContent.Instance.Error($"瀹炵洏鏁伴噺涓嶅彲瓒呭嚭璐﹂潰鏁伴噺");
+
+                #region 澶勭悊鍑哄簱鍗曪紝璐т綅锛屽簱瀛橈紝搴撳瓨鎵规淇℃伅
+                _unitOfWorkManage.BeginTran();
+
+                #region 鍑哄簱鍗�
+                cabinOrder.OutStatus = "寮�濮�";
+                cabinOrderDetail.OotDetailStatus = "寮�濮�";
+                if (cabinOrderDetail.Order_Outqty == cabinOrderDetail.Order_qty)
+                {
+                    cabinOrderDetail.OotDetailStatus = "宸插畬鎴�";
+                    _deliveryOrderDetailServices.Repository.DeleteAndMoveIntoHty(cabinOrderDetail, OperateTypeEnum.鑷姩瀹屾垚);
+                }
+                else
+                {
+                    _deliveryOrderDetailServices.Repository.UpdateData(cabinOrderDetail);
+                }
+                var cabinOrder1 = BaseDal.Db.Queryable<Dt_DeliveryOrder>().Where(x => x.Out_no == cabinOrder.Out_no && x.Out_type == "3").Includes(x => x.Details).First();
+                if (cabinOrder1.Details == null || cabinOrder1.Details.Count < 1) cabinOrder.OutStatus = "宸插畬鎴�";
+                if (cabinOrder.OutStatus == "宸插畬鎴�")
+                    Repository.DeleteAndMoveIntoHty(cabinOrder, OperateTypeEnum.鑷姩瀹屾垚);
+                else
+                    Repository.UpdateData(cabinOrder);
+                #endregion
+
+                #region 搴撳瓨
+                Dt_InventoryInfo inventoryInfo = _inventoryInfoService.Repository.QueryFirst(x => x.BatchNo == cabinOrderDetail.Batch_num && x.MaterielCode == cabinOrderDetail.Goods_no && x.LocationCode == LocationCode);
+                if (inventoryInfo == null) return WebResponseContent.Instance.Error($"鏈壘鍒拌揣浣嶃�恵LocationCode}銆戠殑搴撳瓨淇℃伅");
+                inventoryInfo.SupplyQuantity += Inqty;
+                _inventoryInfoService.UpdateData(inventoryInfo);
+                #endregion
+
+                #region 浠诲姟璁板綍
+                Dt_SupplyTask supplyTask = new Dt_SupplyTask()
+                {
+                    WarehouseCode = cabinOrderDetail.Reservoirarea,
+                    BatchNo = inventoryInfo.BatchNo,
+                    MaterielName = inventoryInfo.MaterielName,
+                    MaterielCode = inventoryInfo.MaterielCode,
+                    MaterielSpec = inventoryInfo.MaterielSpec,
+                    TaskType = TaskTypeEnum.OutInventory.ObjToInt(),
+                    TaskStatus = SupplyStatusEnum.CheckFinish.ObjToInt(),
+                    CreateDate = DateTime.Now,
+                    Creater = App.User.UserName,
+                    LocationCode = LocationCode,
+                    OrderNo = cabinOrder.Out_no,
+                    StockQuantity = inventoryInfo.StockQuantity,
+                    SupplyQuantity = Inqty,
+                    Remark = "鐩樼偣"
+                };
+                _supplyTaskService.AddData(supplyTask);
+                #endregion
+
+                #region 搴撳瓨鎵规
+                Dt_Inventory_Batch inventory_Batch = _inventory_BatchServices.Repository.QueryFirst(x => x.BatchNo == inventoryInfo.BatchNo && x.MaterielCode == inventoryInfo.MaterielCode);
+                if (inventory_Batch != null)
+                {
+                    inventory_Batch.SupplyQuantity += Inqty;
+                    _inventory_BatchServices.UpdateData(inventory_Batch);
+                }
+                #endregion
+
+                _unitOfWorkManage.CommitTran();
+                #endregion
+
+                content.OK(cabinOrderDetail.Order_Outqty.ToString());
+            }
+            catch (Exception ex)
+            {
+                _unitOfWorkManage.RollbackTran();
+                content.Error(ex.Message);
+            }
+            return content;
+        }
+        public WebResponseContent FeedbackOut(SaveModel saveModel)
+        {
+            WebResponseContent content = new WebResponseContent();
+            try
+            {
+                var LocationCode = saveModel.MainData["LocationCode"].ToString();
+                var orderNo = saveModel.MainData["orderNo"].ToString();
+                var batchNo = saveModel.MainData["batchNo"].ToString();
+                var Inqty = saveModel.MainData["Inqty"].ObjToInt();
+                var warehouseCode = saveModel.MainData["warehouseCode"].ToString();
+                Dt_DeliveryOrder cabinOrder = BaseDal.Db.Queryable<Dt_DeliveryOrder>().Where(x => x.Out_no == orderNo && x.Warehouse_no == warehouseCode && x.Out_type != "3").Includes(x => x.Details).First();
+                if (cabinOrder == null) return WebResponseContent.Instance.Error($"鍑哄簱鍗曞凡瀹屾垚");
+                Dt_DeliveryOrderDetail cabinOrderDetail = cabinOrder.Details.Where(x => x.Batch_num == batchNo && x.Reservoirarea == warehouseCode).First();
+                if (cabinOrderDetail == null) return WebResponseContent.Instance.Error($"鍑哄簱鍗曟槑缁嗗凡瀹屾垚");
+                Dt_MaterielInfo materielInfo = _basicService.MaterielInfoService.Repository.QueryFirst(x => x.MaterielCode == cabinOrderDetail.Goods_no);
+                if (materielInfo == null) return WebResponseContent.Instance.Error($"璇风淮鎶ょ墿鏂欑紪鍙枫�恵cabinOrderDetail.Goods_no}銆戠殑鐗╂枡淇℃伅");
+                cabinOrderDetail.Order_Outqty += Inqty;
+                if (cabinOrderDetail.Order_Outqty > cabinOrderDetail.Order_qty)
+                    return WebResponseContent.Instance.Error($"鍑哄簱鏁伴噺涓嶅彲瓒呭嚭鍗曟嵁鏁伴噺");
+
+                #region 澶勭悊鍑哄簱鍗曪紝璐т綅锛屽簱瀛橈紝搴撳瓨鎵规淇℃伅
+                _unitOfWorkManage.BeginTran();
+
+                #region 鍑哄簱鍗�
+                cabinOrder.OutStatus = "寮�濮�";
+                cabinOrderDetail.OotDetailStatus = "寮�濮�";
+                if (cabinOrderDetail.Order_Outqty == cabinOrderDetail.Order_qty)
+                {
+                    cabinOrderDetail.OotDetailStatus = "宸插畬鎴�";
+                    _deliveryOrderDetailServices.Repository.DeleteAndMoveIntoHty(cabinOrderDetail, OperateTypeEnum.鑷姩瀹屾垚);
+                }
+                else
+                {
+                    _deliveryOrderDetailServices.Repository.UpdateData(cabinOrderDetail);
+                }
+                var cabinOrder1 = BaseDal.Db.Queryable<Dt_DeliveryOrder>().Where(x => x.Out_no == cabinOrder.Out_no && x.Out_type != "3").Includes(x => x.Details).First();
+                if (cabinOrder1.Details == null || cabinOrder1.Details.Count < 1) cabinOrder.OutStatus = "宸插畬鎴�";
+                Repository.UpdateData(cabinOrder);
+                #endregion
+
+                #region 搴撳瓨
+                Dt_InventoryInfo inventoryInfo = _inventoryInfoService.Repository.QueryFirst(x => x.BatchNo == cabinOrderDetail.Batch_num && x.MaterielCode == cabinOrderDetail.Goods_no && x.LocationCode == LocationCode);
+                if (inventoryInfo == null) return WebResponseContent.Instance.Error($"鏈壘鍒拌揣浣嶃�恵LocationCode}銆戠殑搴撳瓨淇℃伅");
+                inventoryInfo.StockQuantity -= Inqty;
+                if (inventoryInfo.StockQuantity <= 0)
+                    _inventoryInfoService.DeleteData(inventoryInfo);
+                else
+                    _inventoryInfoService.UpdateData(inventoryInfo);
+                #endregion
+
+                #region 璐т綅
+                var location = _basicService.LocationInfoService.Repository.QueryFirst(x => x.LocationCode == LocationCode);
+                if (location == null) return WebResponseContent.Instance.Error($"璇风淮鎶よ揣浣嶇紪鍙枫�恵LocationCode}銆戠殑璐т綅淇℃伅");
+                if (location.EnableStatus == EnableStatusEnum.Disable.ObjToInt())
+                    return WebResponseContent.Instance.Error($"璐т綅缂栧彿銆恵LocationCode}銆戝凡绂佺敤锛岃鎭㈠姝e父鍐嶄娇鐢�");
+                Dt_InventoryInfo inventoryInfo1 = _inventoryInfoService.Repository.QueryFirst(x => x.LocationCode == LocationCode);
+                if (inventoryInfo1 == null)
+                {
+                    location.LocationStatus = LocationStatusEnum.Free.ObjToInt();
+                    _basicService.LocationInfoService.UpdateData(location);
+                }
+                #endregion
+
+                #region 浠诲姟璁板綍
+                Dt_SupplyTask supplyTask = new Dt_SupplyTask()
+                {
+                    WarehouseCode = cabinOrderDetail.Reservoirarea,
+                    BatchNo = inventoryInfo.BatchNo,
+                    MaterielName = inventoryInfo.MaterielName,
+                    MaterielCode = inventoryInfo.MaterielCode,
+                    MaterielSpec = inventoryInfo.MaterielSpec,
+                    TaskStatus = SupplyStatusEnum.OutFinish.ObjToInt(),
+                    TaskType = TaskTypeEnum.OutPick.ObjToInt(),
+                    CreateDate = DateTime.Now,
+                    Creater = App.User.UserName,
+                    LocationCode = location.LocationCode,
+                    OrderNo = cabinOrder.Out_no,
+                    StockQuantity = Inqty,
+                    SupplyQuantity = 0,
+                    Remark = "鍑哄簱"
+                };
+                _supplyTaskService.AddData(supplyTask);
+                #endregion
+
+                #region 搴撳瓨鎵规
+                Dt_Inventory_Batch inventory_Batch = _inventory_BatchServices.Repository.QueryFirst(x => x.BatchNo == inventoryInfo.BatchNo && x.MaterielCode == inventoryInfo.MaterielCode);
+                if (inventory_Batch != null)
+                {
+                    inventory_Batch.StockQuantity -= Inqty;
+                    if (inventory_Batch.StockQuantity <= 0)
+                        _inventory_BatchServices.DeleteData(inventory_Batch);
+                    else
+                        _inventory_BatchServices.UpdateData(inventory_Batch);
+                }
+                #endregion
+
+                _unitOfWorkManage.CommitTran();
+                #endregion
+
+                content.OK(cabinOrderDetail.Order_Outqty.ToString());
+            }
+            catch (Exception ex)
+            {
+                _unitOfWorkManage.RollbackTran();
+                content.Error(ex.Message);
+            }
+            return content;
+        }
     }
 }

--
Gitblit v1.9.3