From a38b50675f2cf8e813bd337ca2f9d9456cc421d3 Mon Sep 17 00:00:00 2001
From: dengjunjie <dengjunjie@hnkhzn.com>
Date: 星期四, 17 十月 2024 09:50:14 +0800
Subject: [PATCH] WCS

---
 项目代码/WMS/WIDESEA_WMSServer/WIDESEA_BusinessServices/Dt_InboundOrderService.cs |  713 ++++++++++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 561 insertions(+), 152 deletions(-)

diff --git "a/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA_BusinessServices/Dt_InboundOrderService.cs" "b/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA_BusinessServices/Dt_InboundOrderService.cs"
index 69b55af..9f3e28e 100644
--- "a/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA_BusinessServices/Dt_InboundOrderService.cs"
+++ "b/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA_BusinessServices/Dt_InboundOrderService.cs"
@@ -1,7 +1,5 @@
-锘縰sing log4net.Layout;
-using SkiaSharp;
-using SqlSugar.SplitTableExtensions;
-using System.Data;
+锘縰sing System.Data;
+using System.Text;
 using WIDESEA_Core;
 using WIDESEA_Core.BaseRepository;
 using WIDESEA_Core.BaseServices;
@@ -9,14 +7,15 @@
 using WIDESEA_DTO;
 using WIDESEA_IBusinessesRepository;
 using WIDESEA_IBusinessServices;
+using WIDESEA_IStorageBasicRepository;
+using WIDESEA_IStorageTaskRepository;
 using WIDESEA_Model.Models;
-using WIDESEA_Repository;
 
 namespace WIDESEA_BusinessServices
 {
     public class Dt_InboundOrderService : ServiceBase<Dt_InboundOrder, IDt_InboundOrderRepository>, IDt_InboundOrderService
     {
-        #region 闇�娉ㄥ叆鎺ュ彛
+        #region 鎺ュ彛
         private readonly IUnitOfWorkManage _unitOfWorkManage;
         private readonly IDt_InboundOrderDetailRepository _dt_InboundOrderDetailRepository;
         private readonly IDt_InboundOrderProductionRepository _dt_InboundOrderProductionRepository;
@@ -30,9 +29,11 @@
         private readonly IDt_MaterielInfoRepository _dt_MaterielInfoRepository;
         private readonly IDt_WareAreaInfoRepository _dt_WareAreaInfoRepository;
         private readonly IDt_StrategyRepository _dt_StrategyRepository;
-        private readonly IDt_LocationInfoRepository _dt_LocationInfoRepository;
+        private readonly IDt_LocationRepository _dt_LocationInfoRepository;
         private readonly IDt_TaskRepository _dt_TaskRepository;
+        private readonly IDt_Task_HtyRepository _dt_Task_HtyRepository;
         private readonly IDt_TaskExecuteDetailRepository _dt_TaskExecuteDetailRepository;
+        private readonly IDt_TypeMappingRepository _dt_TypeMappingRepository;
 
         private readonly IDt_RoadWayInfoRepository _dt_RoadWayInfoRepository;
         #endregion
@@ -41,9 +42,9 @@
             IDt_InboundOrderProductionService dt_InboundOrderProductionService, IDt_InboundOrderProductionRepository dt_InboundOrderProductionRepository,
             IDt_InboundOrderPurchaseRepository dt_InboundOrderPurchaseRepository, IDt_InboundOrderPurchaseService dt_InboundOrderPurchaseService,
             IDt_BillGroupStockDetailRepository dt_BillGroupStockDetailRepository, IDt_MaterielInfoRepository dt_MaterielInfoRepository,
-            IDt_WareAreaInfoRepository dt_WareAreaInfoRepository, IDt_StrategyRepository dt_StrategyRepository, IDt_LocationInfoRepository dt_LocationInfoRepository,
-            IDt_BillGroupStockRepository dt_BillGroupStockRepository, IDt_TaskRepository dt_TaskRepository, IDt_TaskExecuteDetailRepository dt_TaskExecuteDetailRepository
-            , IDt_RoadWayInfoRepository dt_RoadWayInfoRepository) : base(BaseDal)
+            IDt_WareAreaInfoRepository dt_WareAreaInfoRepository, IDt_StrategyRepository dt_StrategyRepository, IDt_LocationRepository dt_LocationInfoRepository,
+            IDt_BillGroupStockRepository dt_BillGroupStockRepository, IDt_TaskRepository dt_TaskRepository, IDt_TaskExecuteDetailRepository dt_TaskExecuteDetailRepository, 
+            IDt_RoadWayInfoRepository dt_RoadWayInfoRepository, IDt_TypeMappingRepository dt_TypeMappingRepository, IDt_Task_HtyRepository dt_Task_HtyRepository) : base(BaseDal)
         {
             _unitOfWorkManage = unitOfWorkManage;
             _dt_InboundOrderDetailRepository = dt_InboundOrderDetailRepository;
@@ -60,6 +61,8 @@
             _dt_TaskRepository = dt_TaskRepository;
             _dt_TaskExecuteDetailRepository = dt_TaskExecuteDetailRepository;
             _dt_RoadWayInfoRepository = dt_RoadWayInfoRepository;
+            _dt_TypeMappingRepository = dt_TypeMappingRepository;
+            _dt_Task_HtyRepository = dt_Task_HtyRepository;
         }
         /// <summary>
         /// 鍚屾鐢熶骇鍏ュ簱鍘熷鍗曟嵁鍒版湰绯荤粺鍏ュ簱鍗曡〃锛屽苟鏇存柊鍘熷鍗曟嵁鐨勫悓姝ユ爣蹇�
@@ -68,7 +71,7 @@
         /// <returns></returns>
         public async Task<WebResponseContent> GetInboundOrderFromProductionOrigin()
         {
-            WebResponseContent content = new WebResponseContent();
+            WebResponseContent webResponseContent = new WebResponseContent();
             new LogLibrary.Log.LogFactory().GetLog("鎺ュ彛").InfoFormat(true, "鐢熶骇鍏ュ簱鍗曞悓姝�-GetInboundOrderFromProductionOrigin", "璇锋眰", "鏃犲弬鏁�");
 
             List<Dt_InboundOrderProduction> Dt_InboundOrderProductionList = await _dt_InboundOrderProductionService.GetDt_InboundOrderProduction((int)SynchronizationFlagEmun.鏈悓姝�);
@@ -83,8 +86,7 @@
                     #region 鍏ュ簱鍘熷鍗曟嵁涓昏〃鏍¢獙锛屽崟鎹浆鎹负鏈郴缁熷叆搴撳崟涓昏〃
                     //todo锛氭澶勫仛鍘熷鍗曟嵁涓昏〃瀛楁鏍¢獙
                     Dt_InboundOrder dt_InboundOrder = new Dt_InboundOrder();
-                    dt_InboundOrder.OrderId = Guid.NewGuid();
-                    dt_InboundOrder.InboundNo = dt_InboundOrder.OrderId.ToString();//todo:璋冪敤鑷姩鐢熸垚鍗曞彿鏂规硶
+                    dt_InboundOrder.InboundNo = GenerateOrderNumber("R",DateTime.Now);
                     dt_InboundOrder.InboundUpperNo = mainItem.ProductionNo;
                     dt_InboundOrder.BatchNo = mainItem.BatchNo;
                     dt_InboundOrder.InboundDate = mainItem.CreateDate;
@@ -99,7 +101,7 @@
                     {
                         //todo锛氭澶勫仛鍘熷鍗曟嵁鏄庣粏瀛楁鏍¢獙
                         Dt_InboundOrderDetail dt_InboundOrderDetail = new Dt_InboundOrderDetail();
-                        dt_InboundOrderDetail.OrderDetailId = Guid.NewGuid();
+                        //dt_InboundOrderDetail.OrderDetailId = Guid.NewGuid();
                         dt_InboundOrderDetail.OrderId = mainItem.OrderId;
                         dt_InboundOrderDetail.InboundNo = dt_InboundOrder.InboundNo;
                         dt_InboundOrderDetail.BatchNo = dt_InboundOrder.BatchNo;
@@ -123,6 +125,7 @@
                         //鎻愪氦浜嬪姟
                         _unitOfWorkManage.CommitTran();
                         new LogLibrary.Log.LogFactory().GetLog("鎺ュ彛").InfoFormat(true, "鐢熶骇鍏ュ簱鍗曞悓姝�-GetInboundOrderFromProductionOrigin", "鍝嶅簲", "鍚屾鎴愬姛");
+                        webResponseContent.Error($"鍚屾鎴愬姛");
                     }
                     catch (Exception)
                     {
@@ -136,11 +139,12 @@
                     mainItem.Remark = ex.Message;//璁板綍澶辫触鍘熷洜
                     _dt_InboundOrderProductionRepository.UpdateData(mainItem);
                     new LogLibrary.Log.LogFactory().GetLog("鎺ュ彛").InfoFormat(true, "鐢熶骇鍏ュ簱鍗曞悓姝�-GetInboundOrderFromProductionOrigin", "鍝嶅簲", $"鍚屾澶辫触{ex.ToString()}");
+                    webResponseContent.Error($"鍚屾澶辫触{ex.ToString()}");
                 }
 
             }
 
-            return WebResponseContent.Instance.OK();
+            return webResponseContent;
         }
 
 
@@ -152,7 +156,7 @@
         /// <returns></returns>
         public async Task<WebResponseContent> GetInboundOrderFromPurchaseOrigin()
         {
-            WebResponseContent content = new WebResponseContent();
+            WebResponseContent webResponseContent = new WebResponseContent();
             new LogLibrary.Log.LogFactory().GetLog("鎺ュ彛").InfoFormat(true, "閲囪喘鍏ュ簱鍗曞悓姝�-GetInboundOrderFromProductionOrigin", "璇锋眰", "鏃犲弬鏁�");
             List<Dt_InboundOrderPurchase> Dt_InboundOrderPurchaseList = await _dt_InboundOrderPurchaseService.GetDt_InboundOrderPurchase((int)SynchronizationFlagEmun.鏈悓姝�);
 
@@ -166,8 +170,8 @@
                     #region 鍏ュ簱鍘熷鍗曟嵁涓昏〃鏍¢獙锛屽崟鎹浆鎹负鏈郴缁熷叆搴撳崟涓昏〃
                     //todo锛氭澶勫仛鍘熷鍗曟嵁涓昏〃瀛楁鏍¢獙
                     Dt_InboundOrder dt_InboundOrder = new Dt_InboundOrder();
-                    dt_InboundOrder.OrderId = Guid.NewGuid();
-                    dt_InboundOrder.InboundNo = dt_InboundOrder.OrderId.ToString();//todo:璋冪敤鑷姩鐢熸垚鍗曞彿鏂规硶
+                    //dt_InboundOrder.OrderId = Guid.NewGuid();
+                    dt_InboundOrder.InboundNo = GenerateOrderNumber("R", DateTime.Now);
                     dt_InboundOrder.InboundUpperNo = mainItem.PurchaseNo;
                     dt_InboundOrder.BatchNo = mainItem.BatchNo;
                     dt_InboundOrder.InboundDate = mainItem.CreateDate;
@@ -182,15 +186,14 @@
                     {
                         //todo锛氭澶勫仛鍘熷鍗曟嵁鏄庣粏瀛楁鏍¢獙
                         Dt_InboundOrderDetail dt_InboundOrderDetail = new Dt_InboundOrderDetail();
-                        dt_InboundOrderDetail.OrderDetailId = Guid.NewGuid();
                         dt_InboundOrderDetail.OrderId = mainItem.OrderId;
                         dt_InboundOrderDetail.InboundNo = dt_InboundOrder.InboundNo;
                         dt_InboundOrderDetail.BatchNo = dt_InboundOrder.BatchNo;
                         dt_InboundOrderDetail.InboundType = dt_InboundOrder.InboundType;
                         dt_InboundOrderDetail.InboundState = dt_InboundOrder.InboundState;
-                        dt_InboundOrderDetail.MaterialNo = detailItem.ProductionMatcode;
-                        dt_InboundOrderDetail.MaterialName = detailItem.ProductionName;
-                        dt_InboundOrderDetail.PreInboundQuantity = detailItem.ReceivableQuantity;
+                        dt_InboundOrderDetail.MaterialNo = detailItem.PurchaseMatcode;
+                        dt_InboundOrderDetail.MaterialName = detailItem.PurchaseName;
+                        dt_InboundOrderDetail.PreInboundQuantity = detailItem.PurchaseQty;
                         Dt_InboundOrderDetailList.Add(dt_InboundOrderDetail);
                     }
                     #endregion
@@ -205,6 +208,8 @@
                         _dt_InboundOrderPurchaseRepository.UpdateData(mainItem);
                         //鎻愪氦浜嬪姟
                         _unitOfWorkManage.CommitTran();
+                        new LogLibrary.Log.LogFactory().GetLog("鎺ュ彛").InfoFormat(true, "閲囪喘鍏ュ簱鍗曞悓姝�-GetInboundOrderFromPurchaseOrigin", "鍝嶅簲", "鍚屾鎴愬姛");
+                        webResponseContent.Error($"鍚屾鎴愬姛");
                     }
                     catch (Exception)
                     {
@@ -218,17 +223,16 @@
                     mainItem.Remark = ex.Message;//璁板綍澶辫触鍘熷洜
                     _dt_InboundOrderPurchaseRepository.UpdateData(mainItem);
                     new LogLibrary.Log.LogFactory().GetLog("鎺ュ彛").InfoFormat(true, "閲囪喘鍏ュ簱鍗曞悓姝�-GetInboundOrderFromProductionOrigin", "鍝嶅簲", $"鍚屾澶辫触{ex.ToString()}");
+                    webResponseContent.Error($"鍚屾澶辫触{ex.ToString()}");
                 }
 
             }
 
-            return WebResponseContent.Instance.OK();
+            return webResponseContent;
         }
 
-
-        /// 浜х敓鍏ュ簱浠诲姟 鍒嗛厤宸烽亾
-        /// 1,鏍规嵁鐗╂枡灞炴�ф寚瀹氬簱鍖�
-        /// 2,鏌ヨ婊¤冻鏉′欢鐨勭┖闂茶揣浣嶇殑宸烽亾锛氱┖闂茶揣浣嶅崰姣� 鐗╂枡搴撳瓨鍗犳瘮
+        /// <summary>
+        /// 鐢熸垚鍏ュ簱浠诲姟 鍒嗛厤宸烽亾
         /// </summary>
         /// <param name="plateCode">鎵樼洏鏉$爜</param>
         /// <param name="SourceAddress">璧峰鍦板潃</param>
@@ -238,117 +242,95 @@
             WebResponseContent webResponseContent = new WebResponseContent();
             try
             {
+               //浠诲姟鍙蜂笉鑳介噸澶�
+               List<Dt_Task> dttasks  =_dt_TaskRepository.QueryData(x => x.PalletCode == plateCode && x.TaskState != (int)InTaskStatusEnum.InFinish && x.TaskType >= 200 && x.TaskType <= 203);
+                if(dttasks!=null&& dttasks.Count > 0)
+                {
+                    webResponseContent.OK("鎵樼洏鍙峰瓨鍦ㄩ噸澶嶇殑鍏ュ簱浠诲姟锛�");
+                }
+                    
                 new LogLibrary.Log.LogFactory().GetLog("鎺ュ彛").InfoFormat(true, "浜х敓鍏ュ簱浠诲姟骞跺垎閰嶅贩閬�-GenerateInboundTask", "璇锋眰", $"plateCode:{plateCode}-SourceAddress:{SourceAddress}");
                 List<RoadWayDTO> roadWays = new List<RoadWayDTO>();
-                //鏍规嵁鎵樼洏鐮佹煡璇㈢墿鏂欑紪鐮�
-                List<Dt_BillGroupStock> billGroup = _dt_BillGroupStockRepository.QueryData(x => x.PalletCode == plateCode && x.State == StockStateEmun.缁勭洏鏆傚瓨.ToString());
                 List<Dt_InboundOrder> inboundOrders = new List<Dt_InboundOrder>();
-                if (billGroup != null && billGroup.Count == 1)
+                Dt_BillGroupStock billGroupStock = new Dt_BillGroupStock();
+                //1,鏍规嵁鎵樼洏鍙锋煡璇㈢粍鐩樹俊鎭�
+                List<Dt_BillGroupStock> billGroup = Db.Queryable<Dt_BillGroupStock>().Includes(x => x.Dt_BillGroupStockDetailList).Where(x => x.PalletCode == plateCode && x.State == (int)StockStateEmun.缁勭洏鏆傚瓨).ToList();
+
+                if (billGroup != null && billGroup.Count > 0)
                 {
-
-                    //鏍规嵁鐗╂枡缂栫爜锛屾煡璇㈢墿鏂欏睘鎬�
-                    List<Dt_MaterielInfo> masterielInfoList = _dt_MaterielInfoRepository.QueryData(x => x.MaterielCode == billGroup[0].MaterialNo);
-                    if (masterielInfoList != null && masterielInfoList.Count > 0)
+                    billGroupStock = billGroup[0];
+                    //鏍规嵁鐗╂枡缂栫爜锛屾煡璇㈢墿鏂欎俊鎭紙鎵�灞炲簱鍖猴級
+                    List<Dt_MaterielInfo> masterielInfos = _dt_MaterielInfoRepository.QueryData(x => x.MaterielCode == billGroupStock.MaterialNo);
+                    if (masterielInfos != null && masterielInfos.Count > 0)
                     {
-                        //鏌ヨ宸烽亾鍒嗛厤绛栫暐                                                                                                          
-                        List<Dt_Strategy> strategyList = _dt_StrategyRepository.QueryData(x => x.StrategyType == "InboundRoadway" && x.IsPreset == true);
-                        if (strategyList != null && strategyList.Count > 0)
+                        //鑾峰彇绌洪棽璐т綅鍒楄〃 todo:闇�鑰冭檻璐т綅绂佺敤鐨勬儏鍐�
+                        var roadWayList = _dt_LocationInfoRepository.QueryData(x => x.Status == (int)LocationEnum.Free && x.WareAreaCode == masterielInfos[0].WareAreaCode.ToString()).GroupBy(x => x.Roadway).OrderByDescending(group => group.Count()).ToList(); 
+                        foreach (var item in roadWayList)
                         {
-                            string strategyName = strategyList[0].StrategyName;
-                            if (strategyName == "绌洪棽璐т綅鍗犳瘮")//todo锛氱瓥鐣ュ悕绉帮紝杩欓噷鍙互鍦ㄦ暟鎹簱涓厤缃瓥鐣ュ悗锛屽湪杩欓噷鏍规嵁涓嶅悓鐨勭瓥鐣ユ潵澶勭悊
+                            //鍘绘帀绂佺敤鐨勫贩閬� 
+                            Dt_RoadWayInfo roadway = _dt_RoadWayInfoRepository.QueryData(x => x.RoadWayNO == item.Key).FirstOrDefault();
+                            if (roadway != null && roadway.IsEnable == false)
                             {
-                                //鑾峰彇绌洪棽璐т綅闄嶅簭鍒楄〃
-
-                                var roadWayList = _dt_LocationInfoRepository.QueryData(x => x.Status == (int)LocationStatus.绌洪棽 && x.WareAreaCode == masterielInfoList[0].WareAreaCode.ToString()).GroupBy(x => x.Roadway).OrderByDescending(group => group.Count()).ToList(); // 璁$畻姣忎釜鍒嗙粍鐨勬暟閲�
-                                foreach (var item in roadWayList)
-                                {
-                                    //鍘绘帀绂佺敤鐨勫贩閬�
-                                   Dt_RoadWayInfo roadway=  _dt_RoadWayInfoRepository.QueryData(x => x.RoadWayNO == item.Key).FirstOrDefault();
-                                    if (roadway != null&&roadway.IsEnable==false)
-                                    {
-                                        continue;
-                                    }
-
-                                    //鏌ヨ宸烽亾浠诲姟鏁�
-                                    int taskCount = _dt_TaskRepository.QueryData(x => x.Roadway == item.Key && (x.TaskState == TaskStatusEmun.鏂板缓鍏ュ簱浠诲姟.ToString() || x.TaskState == TaskStatusEmun.AGV鍏ュ簱鎵ц涓�.ToString() || x.TaskState == TaskStatusEmun.杈撻�佺嚎鍏ュ簱鎵ц涓�.ToString())).Count();
-                                    RoadWayDTO roadWayDTO = new RoadWayDTO();
-                                    roadWayDTO.RoadWayNO = item.Key;
-                                    roadWayDTO.FreeLocationCount = item.Count();
-                                    roadWayDTO.TaskCount = taskCount;
-                                    roadWays.Add(roadWayDTO);
-                                }
-                                //鎸夌┖闂茶揣澶у埌灏忔帓搴忥紝浼樺厛鍙栦换鍔℃暟涓�0鐨勶紝鍐嶉�変换鍔℃暟灏忎簬骞冲潎鏁扮殑鐨勫贩閬擄紝鏈�鍚庨粯璁ゅ彇绗竴鏉$┖闂茶揣浣嶆渶澶氱殑
-                                decimal taskSum = roadWays.Sum(x => x.TaskCount);
-                                decimal avgQTY = roadWays.Count() == 0 ? 0 : taskSum / roadWays.Count();
-                                string roadWay = "";
-                                //todo:鏍规嵁宸烽亾浠诲姟鏁板拰绌洪棽鏁拌繑鍥炲贩閬撳彿
-                                foreach (var item in roadWays)
-                                {
-                                    if (item.TaskCount == 0)
-                                    {
-                                        roadWay = item.RoadWayNO;
-                                        break;
-                                    }
-                                    else if (item.TaskCount <= avgQTY)
-                                    {
-                                        roadWay = item.RoadWayNO;
-                                        break;
-                                    }
-                                    else
-                                    {
-                                        roadWay = roadWays[0].RoadWayNO;
-                                        break;
-                                    }
-
-                                }
-                                //鏌ヨ瀵瑰簲鐨勫叆搴撳崟,鐢ㄤ簬鍙栧�煎叆搴撳崟绫诲瀷
-                                inboundOrders = BaseDal.QueryData(x => x.InboundNo == billGroup[0].InboundNo);
-                                try
-                                {
-                                    //寮�鍚簨鐗�
-                                    _unitOfWorkManage.BeginTran();
-                                    //鐢熸垚浠诲姟锛屽苟杩斿洖浠诲姟
-                                    Dt_Task dt_Task = new Dt_Task();
-                                    dt_Task.TaskId = Guid.NewGuid();
-                                    dt_Task.TaskNum = GetTaskNo();
-                                    dt_Task.PalletCode = plateCode;
-                                    dt_Task.InboundNo = billGroup[0].InboundNo;
-                                    dt_Task.Roadway = roadWay;
-                                    dt_Task.TaskType = inboundOrders[0].InboundType;
-                                    dt_Task.TaskState = TaskStatusEmun.鏂板缓鍏ュ簱浠诲姟.ToString();
-                                    dt_Task.MaterialNo = billGroup[0].MaterialNo;
-                                    dt_Task.SourceAddress = SourceAddress;
-                                    dt_Task.Dispatchertime = DateTime.Now;
-                                    _dt_TaskRepository.AddData(dt_Task);
-                                    //鍒濆鍖栦换鍔℃墽琛屾槑缁�
-                                    List<Dt_TaskExecuteDetail> dt_TaskExecuteDetails = new List<Dt_TaskExecuteDetail>();
-                                    Dt_TaskExecuteDetail dt_TaskExecuteDetail = new Dt_TaskExecuteDetail();
-                                    dt_TaskExecuteDetail.TaskDetailId = Guid.NewGuid();
-                                    dt_TaskExecuteDetail.TaskId = dt_Task.TaskId;
-                                    dt_TaskExecuteDetail.TaskNum = dt_Task.TaskNum.ToString();
-                                    dt_TaskExecuteDetail.TaskState = dt_Task.TaskState;
-                                    dt_TaskExecuteDetail.IsManual = false;
-                                    dt_TaskExecuteDetails.Add(dt_TaskExecuteDetail);
-                                    _dt_TaskExecuteDetailRepository.AddData(dt_TaskExecuteDetail);
-                                    dt_Task.Dt_TaskExecuteDetailList = dt_TaskExecuteDetails;
-
-                                    //鎻愪氦浜嬬墿
-                                    _unitOfWorkManage.CommitTran();
-                                    webResponseContent.OK("鑾峰彇宸烽亾鍙锋垚鍔燂紒", dt_Task);
-
-                                }
-                                catch (Exception)
-                                {
-                                    _unitOfWorkManage.RollbackTran();
-                                    throw;
-                                }
-
+                                continue;
                             }
+                            //鏌ヨ宸烽亾鏂板缓鐨勫拰姝e湪鎵ц涓殑浠诲姟鏁�
+                            int taskCount = _dt_TaskRepository.QueryData(x => x.Roadway == item.Key && (x.TaskState == (int)InTaskStatusEnum.InNew || x.TaskState == (int)InTaskStatusEnum.AGV_InExecuting || x.TaskState == (int)InTaskStatusEnum.Line_InExecuting || x.TaskState == (int)InTaskStatusEnum.SC_InExecuting)).Count();
+                            RoadWayDTO roadWayDTO = new RoadWayDTO();
+                            roadWayDTO.RoadWayNO = item.Key;
+                            roadWayDTO.FreeLocationCount = item.Count();
+                            roadWayDTO.TaskCount = taskCount;
+                            roadWayDTO.PreFreeLocationCount = roadWayDTO.FreeLocationCount - roadWayDTO.TaskCount;
+                            roadWays.Add(roadWayDTO);
                         }
-                        else
+                        if(roadWays.Count == 0) 
                         {
-                            new LogLibrary.Log.LogFactory().GetLog("鎺ュ彛").InfoFormat(true, "浜х敓鍏ュ簱浠诲姟骞跺垎閰嶅贩閬�-GenerateInboundTask", "鍝嶅簲", $"plateCode:{plateCode}-鏃犳鐗╂枡鍩烘湰淇℃伅锛岃閰嶇疆鍒嗛厤宸烽亾绛栫暐锛�");
-                            webResponseContent.Error($"plateCode:{plateCode}-鏃犳鐗╂枡鍩烘湰淇℃伅锛岃閰嶇疆鍒嗛厤宸烽亾绛栫暐锛�");
+                            webResponseContent.OK("鏈幏鍙栧埌宸烽亾鍙峰垪琛紒");
+                        }
+                        //鏍规嵁棰勮璐т綅绌洪棽鏁帮紙璐т綅绌洪棽鏁�-宸烽亾褰撳墠浠诲姟鏁�),鑾峰彇椤归亾鍙�
+                        string roadWay = roadWays.OrderByDescending(roadway => roadway.PreFreeLocationCount).FirstOrDefault().RoadWayNO;
+                        //鏌ヨ瀵瑰簲鐨勫叆搴撳崟,鐢ㄤ簬鍙栧�煎叆搴撳崟绫诲瀷
+                        inboundOrders = BaseDal.QueryData(x => x.InboundNo == billGroupStock.OrderNo);
+                        if (inboundOrders.Count == 0)
+                        {
+                            return webResponseContent.Error($"鏈煡璇㈠埌褰撳墠鎷栫洏鍙蜂负{plateCode} 瀵瑰簲鐨勫叆搴撳崟锛�");
+                        }
+                        //閫氳繃鍗曟嵁绫诲瀷鑾峰彇浠诲姟绫诲瀷
+                        List<Dt_TypeMapping> typeMappings = _dt_TypeMappingRepository.QueryData(x => x.OrderType == inboundOrders[0].InboundType).ToList();
+                        if (typeMappings.Count == 0)
+                        {
+                            return webResponseContent.Error($"鏈厤缃崟鎹被鍨嬪搴旂殑浠诲姟绫诲瀷锛�");
+                        }
+                        try
+                        {
+                            //鐢熸垚浠诲姟
+                            Dt_Task dt_Task = new Dt_Task();
+                            #region 鐢熸垚浠诲姟
+                            dt_Task.TaskNum = GetTaskNo();
+                            dt_Task.PalletCode = plateCode;
+                            dt_Task.GroupID = billGroupStock.GroupId;
+                            dt_Task.InboundNo = billGroupStock.OrderNo;
+                            dt_Task.Roadway = roadWay;
+                            dt_Task.TaskType = typeMappings[0].OrderType; 
+                            dt_Task.TaskState = (int)InTaskStatusEnum.InNew;
+                            dt_Task.MaterialNo = billGroupStock.MaterialNo;
+                            dt_Task.SourceAddress = SourceAddress;
+                            dt_Task.Dispatchertime = DateTime.Now;
+                            #endregion
+                            _dt_TaskRepository.AddData(dt_Task);
+                            //寮�鍚簨鐗�
+                            _unitOfWorkManage.BeginTran();                        
+                            _dt_TaskRepository.AddData(dt_Task);
+                            //搴撳瓨鐘舵�� 鏀逛负 鍏ュ簱纭鐘舵��
+                            billGroupStock.State = (int)StockStateEmun.鍏ュ簱纭;
+                            _dt_BillGroupStockRepository.UpdateData(billGroupStock);
+                            //鎻愪氦浜嬬墿
+                            _unitOfWorkManage.CommitTran();
+                            webResponseContent.OK("鑾峰彇宸烽亾鍙锋垚鍔燂紒", dt_Task);
+                        }
+                        catch (Exception)
+                        {
+                            _unitOfWorkManage.RollbackTran();
+                            throw;
                         }
                     }
                     else
@@ -358,19 +340,188 @@
                     }
                 }
             }
-            catch(Exception ex)
+            catch (Exception ex)
             {
                 new LogLibrary.Log.LogFactory().GetLog("鎺ュ彛").InfoFormat(true, "浜х敓鍏ュ簱浠诲姟骞跺垎閰嶅贩閬�-GenerateInboundTask", "鍝嶅簲", $"褰撳墠鎷栫洏鍙蜂负{plateCode}鑾峰彇澶辫触{ex.ToString()}");
                 webResponseContent.Error($"褰撳墠鎷栫洏鍙蜂负{plateCode}鑾峰彇澶辫触{ex.ToString()}");
 
             }
-        
-            return webResponseContent.OK();
+
+            return webResponseContent;
+
+        }
+
+
+        /// <summary>
+        /// 鐢熸垚鍏ュ簱浠诲姟(鍏ュ簱纭)
+        /// </summary>
+        /// <param name="plateCode">鎵樼洏鏉$爜</param>
+        /// <param name="SourceAddress">璧峰鍦板潃</param>
+        /// <returns></returns>
+        public WebResponseContent GenerateInboundTask(string plateCode)
+        {
+            WebResponseContent webResponseContent = new WebResponseContent();
+            try
+            {
+                new LogLibrary.Log.LogFactory().GetLog("鎺ュ彛").InfoFormat(true, "浜х敓鍏ュ簱浠诲姟-GenerateInboundTask", "璇锋眰", $"plateCode:{plateCode}");
+                List<RoadWayDTO> roadWays = new List<RoadWayDTO>();
+                List<Dt_InboundOrder> inboundOrders = new List<Dt_InboundOrder>();
+                Dt_BillGroupStock billGroupStock = new Dt_BillGroupStock();
+                //1,鏍规嵁鎵樼洏鍙锋煡璇㈢粍鐩樹俊鎭�
+                List<Dt_BillGroupStock> billGroup = Db.Queryable<Dt_BillGroupStock>().Includes(x => x.Dt_BillGroupStockDetailList).Where(x => x.PalletCode == plateCode && x.State == (int)StockStateEmun.缁勭洏鏆傚瓨).ToList();
+                if (billGroup != null && billGroup.Count > 0)
+                {
+                    //鏌ヨ瀵瑰簲鐨勫叆搴撳崟,鐢ㄤ簬鍙栧�煎叆搴撳崟绫诲瀷
+                    inboundOrders = BaseDal.QueryData(x => x.InboundNo == billGroupStock.OrderNo);
+                    if(inboundOrders.Count == 0) 
+                    {
+                       return  webResponseContent.Error($"鏈煡璇㈠埌褰撳墠鎷栫洏鍙蜂负{plateCode} 瀵瑰簲鐨勫叆搴撳崟锛�");
+                    }
+                    //閫氳繃鍗曟嵁绫诲瀷鑾峰彇浠诲姟绫诲瀷
+                    List<Dt_TypeMapping> typeMappings= _dt_TypeMappingRepository.QueryData(x => x.OrderType == inboundOrders[0].InboundType).ToList();
+                    if (typeMappings.Count == 0)
+                    {
+                       return  webResponseContent.Error($"鏈厤缃崟鎹被鍨嬪搴旂殑浠诲姟绫诲瀷锛�");
+                    }
+                    try
+                    {
+                        //鐢熸垚浠诲姟
+                        Dt_Task dt_Task = new Dt_Task();
+                        dt_Task.TaskNum = GetTaskNo();
+                        dt_Task.PalletCode = plateCode;
+                        dt_Task.GroupID = billGroupStock.GroupId;
+                        dt_Task.InboundNo = billGroupStock.OrderNo;
+                        dt_Task.TaskType = typeMappings[0].OrderType;
+                        dt_Task.TaskState = (int)InTaskStatusEnum.InNew;
+                        dt_Task.MaterialNo = billGroupStock.MaterialNo;
+                        dt_Task.Dispatchertime = DateTime.Now;
+                        _dt_TaskRepository.AddData(dt_Task);
+                        //寮�鍚簨鐗�
+                        _unitOfWorkManage.BeginTran();
+                        _dt_TaskRepository.AddData(dt_Task);
+                        //搴撳瓨鐘舵�� 鏀逛负 鍏ュ簱纭鐘舵��
+                        billGroupStock.State = (int)StockStateEmun.鍏ュ簱纭;
+                        _dt_BillGroupStockRepository.UpdateData(billGroupStock);
+                        //鎻愪氦浜嬬墿
+                        _unitOfWorkManage.CommitTran();
+                        webResponseContent.OK("鐢熸垚浠诲姟鎴愬姛锛�", dt_Task);
+                    }
+                    catch (Exception)
+                    {
+                        _unitOfWorkManage.RollbackTran();
+                        throw;
+                    }
+                }
+            }
+            catch (Exception ex)
+            {
+                new LogLibrary.Log.LogFactory().GetLog("鎺ュ彛").InfoFormat(true, "浜х敓鍏ュ簱浠诲姟-GenerateInboundTask", "鍝嶅簲", $"褰撳墠鎷栫洏鍙蜂负{plateCode}鑾峰彇澶辫触{ex.ToString()}");
+                webResponseContent.Error($"褰撳墠鎷栫洏鍙蜂负{plateCode} 鐢熸垚浠诲姟澶辫触锛歿ex.ToString()}");
+
+            }
+
+            return webResponseContent;
+
+        }
+
+
+        /// <summary>
+        /// 鐢宠鍒嗛厤椤归亾
+        /// </summary>
+        /// <param name="plateCode">鎵樼洏鏉$爜</param>
+        /// <param name="SourceAddress">璧峰鍦板潃</param>
+        /// <returns></returns>
+        public WebResponseContent GetInboundRoadway(string plateCode, string SourceAddress)
+        {
+            WebResponseContent webResponseContent = new WebResponseContent();
+            try
+            {
+                new LogLibrary.Log.LogFactory().GetLog("鎺ュ彛").InfoFormat(true, "鐢宠鍒嗛厤椤归亾-GetInboundRoadway", "璇锋眰", $"plateCode:{plateCode}-SourceAddress:{SourceAddress}");
+                List<RoadWayDTO> roadWays = new List<RoadWayDTO>();
+                List<Dt_InboundOrder> inboundOrders = new List<Dt_InboundOrder>();
+                Dt_BillGroupStock billGroupStock = new Dt_BillGroupStock();
+                Dt_Task dt_Task = new Dt_Task();
+                List<Dt_Task> dt_TaskList = _dt_TaskRepository.QueryData(x => x.PalletCode == plateCode&&x.TaskState== (int)InTaskStatusEnum.InNew);
+                if (dt_TaskList != null && dt_TaskList.Count == 0)
+                {
+                   return  webResponseContent.OK("鏈煡璇㈠埌鐩稿叧浠诲姟锛�");
+                }
+                else
+                {
+                    dt_Task = dt_TaskList.FirstOrDefault();
+                }
+                  //1,鏍规嵁鎵樼洏鍙锋煡璇㈢粍鐩樹俊鎭�
+                List<Dt_BillGroupStock> billGroup = Db.Queryable<Dt_BillGroupStock>().Includes(x => x.Dt_BillGroupStockDetailList).Where(x => x.PalletCode == plateCode && x.State == (int)StockStateEmun.缁勭洏鏆傚瓨).ToList();
+
+                if (billGroup != null && billGroup.Count > 0)
+                {
+                    billGroupStock = billGroup[0];
+                    //鏍规嵁鐗╂枡缂栫爜锛屾煡璇㈢墿鏂欎俊鎭紙鎵�灞炲簱鍖猴級
+                    List<Dt_MaterielInfo> masterielInfos = _dt_MaterielInfoRepository.QueryData(x => x.MaterielCode == billGroupStock.MaterialNo);
+                    if (masterielInfos != null && masterielInfos.Count > 0)
+                    {
+                        //鑾峰彇绌洪棽璐т綅鍒楄〃
+                        var roadWayList = _dt_LocationInfoRepository.QueryData(x => x.Status == (int)LocationEnum.Free && x.WareAreaCode == masterielInfos[0].WareAreaCode.ToString()).GroupBy(x => x.Roadway).OrderByDescending(group => group.Count()).ToList();
+                        foreach (var item in roadWayList)
+                        {
+                            //鍘绘帀绂佺敤鐨勫贩閬�
+                            Dt_RoadWayInfo roadway = _dt_RoadWayInfoRepository.QueryData(x => x.RoadWayNO == item.Key).FirstOrDefault();
+                            if (roadway != null && roadway.IsEnable == false)
+                            {
+                                continue;
+                            }
+                            //鏌ヨ宸烽亾鏂板缓鐨勫拰姝e湪鎵ц涓殑浠诲姟鏁�
+                            int taskCount = _dt_TaskRepository.QueryData(x => x.Roadway == item.Key && (x.TaskState == (int)InTaskStatusEnum.InNew || x.TaskState == (int)InTaskStatusEnum.AGV_InExecuting || x.TaskState == (int)InTaskStatusEnum.Line_InExecuting || x.TaskState == (int)InTaskStatusEnum.SC_InExecuting)).Count();
+                            RoadWayDTO roadWayDTO = new RoadWayDTO();
+                            roadWayDTO.RoadWayNO = item.Key;
+                            roadWayDTO.FreeLocationCount = item.Count();
+                            roadWayDTO.TaskCount = taskCount;
+                            roadWayDTO.PreFreeLocationCount = roadWayDTO.FreeLocationCount - roadWayDTO.TaskCount;
+                            roadWays.Add(roadWayDTO);
+                        }
+                        if (roadWays.Count == 0)
+                        {
+                           return webResponseContent.OK("鏈幏鍙栧埌婊¤冻鏉′欢鐨勫贩閬撳彿鍒楄〃锛�");
+                        }
+                        //鏍规嵁棰勮璐т綅绌洪棽鏁帮紙璐т綅绌洪棽鏁�-宸烽亾褰撳墠浠诲姟鏁�),鑾峰彇椤归亾鍙�
+                        string roadWay = roadWays.OrderByDescending(roadway => roadway.PreFreeLocationCount).FirstOrDefault().RoadWayNO;
+                        //鏌ヨ瀵瑰簲鐨勫叆搴撳崟,鐢ㄤ簬鍙栧�煎叆搴撳崟绫诲瀷
+                        inboundOrders = BaseDal.QueryData(x => x.InboundNo == billGroupStock.OrderNo);
+                        try
+                        {
+                            //鏇存柊浠诲姟鐘舵�併�佸贩閬撳彿
+                            dt_Task.TaskState = (int)InTaskStatusEnum.AGV_InExecuting;
+                            dt_Task.SourceAddress = SourceAddress;
+                            dt_Task.Roadway = roadWay;
+                            _dt_TaskRepository.Update(dt_Task);
+                            webResponseContent.OK("鑾峰彇宸烽亾鍙锋垚鍔燂紒", dt_Task);
+                        }
+                        catch (Exception)
+                        {
+                            _unitOfWorkManage.RollbackTran();
+                            throw;
+                        }
+                    }
+                    else
+                    {
+                        new LogLibrary.Log.LogFactory().GetLog("鎺ュ彛").InfoFormat(true, "鐢宠鍒嗛厤椤归亾-GetInboundRoadway", "鍝嶅簲", $"plateCode:{plateCode}-鏃犳鐗╂枡鍩烘湰淇℃伅锛岀墿鏂欑紪鍙凤細{billGroup[0].MaterialNo}");
+                        webResponseContent.Error($"plateCode:{plateCode}-鏃犳鐗╂枡鍩烘湰淇℃伅锛岀墿鏂欑紪鍙凤細{billGroup[0].MaterialNo}");
+                    }
+                }
+            }
+            catch (Exception ex)
+            {
+                new LogLibrary.Log.LogFactory().GetLog("鎺ュ彛").InfoFormat(true, "浜х敓鍏ュ簱浠诲姟骞跺垎閰嶅贩閬�-GenerateInboundTask", "鍝嶅簲", $"褰撳墠鎷栫洏鍙蜂负{plateCode}鑾峰彇澶辫触{ex.ToString()}");
+                webResponseContent.Error($"褰撳墠鎷栫洏鍙蜂负{plateCode}鑾峰彇宸烽亾鍙峰け璐ex.ToString()}");
+
+            }
+
+            return webResponseContent;
 
         }
 
         /// <summary>
-        /// 鏍规嵁浠诲姟鍙疯幏鍙栬揣浣嶅彿
+        /// 鐢宠鍒嗛厤璐т綅
         /// </summary>
         /// <param name="taskNo">浠诲姟鍙�</param>
         /// <returns></returns>
@@ -384,15 +535,15 @@
                 if (dt_TaskList != null && dt_TaskList.Count > 0)
                 {
                     //鏍规嵁宸烽亾鏌ヨ璐т綅鍙�
-                    List<Dt_LocationInfo> dt_LocationInfoList = _dt_LocationInfoRepository.QueryData(x => x.Roadway == dt_TaskList[0].Roadway && x.Status == (int)LocationStatus.绌洪棽).ToList();
-                    //鏌ヨ绗﹀悎瑕佹眰鐨勮揣浣嶅彿
+                    #region 鏌ヨ鍙敤璐т綅
+                    List<Dt_LocationInfo> dt_LocationInfoList = _dt_LocationInfoRepository.QueryData(x => x.Roadway == dt_TaskList[0].Roadway && x.Status == (int)LocationEnum.Free).ToList();
                     List<Dt_LocationInfo> dt_LocationInfoResult = new List<Dt_LocationInfo>();
                     foreach (Dt_LocationInfo dt_LocationInfo in dt_LocationInfoList)
                     {
                         //濡傛灉娣卞害涓�2锛屽垯闇�鍒ゆ柇瀵瑰簲鐨�1娣变綅(鍒楋紝灞備竴鏍凤紝鎺掍负濂囨暟锛屽綋鍓嶆帓鏁�-1),濡傛灉1娣变綅绌洪棽锛屽垯绗﹀悎瑕佹眰
                         if (dt_LocationInfo.Depth == "2")
                         {
-                            Dt_LocationInfo locationItem = _dt_LocationInfoRepository.QueryData(x => x.Layer == dt_LocationInfo.Layer && x.Column == dt_LocationInfo.Column && x.Line == (int.Parse(dt_LocationInfo.Line.ToString()) - 1).ToString() && x.Status == (int)LocationStatus.绌洪棽).FirstOrDefault();
+                            Dt_LocationInfo locationItem = _dt_LocationInfoRepository.QueryData(x => x.Layer == dt_LocationInfo.Layer && x.Column == dt_LocationInfo.Column && x.Line == (int.Parse(dt_LocationInfo.Line.ToString()) - 1).ToString() && x.Status == (int)(int)LocationEnum.Free).FirstOrDefault();
                             if (locationItem != null)
                             {
                                 dt_LocationInfoResult.Add(dt_LocationInfo);
@@ -400,14 +551,20 @@
                         }
                         else//濡傛灉娣卞害涓�1锛屽垯闇�鍒ゆ柇瀵瑰簲鐨�2娣变綅(鍒楋紝灞備竴鏍凤紝鎺掍负鍋舵暟锛屽綋鍓嶆帓鏁�+1),濡傛灉2娣变綅鏈夎揣锛屽垯绗﹀悎瑕佹眰
                         {
-                            Dt_LocationInfo locationItem = _dt_LocationInfoRepository.QueryData(x => x.Layer == dt_LocationInfo.Layer && x.Column == dt_LocationInfo.Column && x.Line == (int.Parse(dt_LocationInfo.Line.ToString()) + 1).ToString() && x.Status == (int)LocationStatus.鏈夎揣).FirstOrDefault();
+                            Dt_LocationInfo locationItem = _dt_LocationInfoRepository.QueryData(x => x.Layer == dt_LocationInfo.Layer && x.Column == dt_LocationInfo.Column && x.Line == (int.Parse(dt_LocationInfo.Line.ToString()) + 1).ToString() && x.Status == (int)LocationEnum.InStock).FirstOrDefault();
                             if (locationItem != null)
                             {
                                 dt_LocationInfoResult.Add(dt_LocationInfo);
                             }
                         }
                     }
-                    //todo锛氬绗﹀悎瑕佹眰鐨勮揣浣嶏紝杩涜绛涢��,鍏堟寜娣卞害闄嶅簭锛堟繁搴︿紭鍏堬級锛屽啀鎸夋帓鍗囧簭锛堝氨杩戝師鍒欙級锛屽啀鎸夊眰鏁帮紙鍏堜笅鍚庝笂锛�
+                    #endregion
+                    //瀵圭鍚堣姹傜殑璐т綅锛岃繘琛岀瓫閫�,鍏堟寜娣卞害闄嶅簭锛堟繁搴︿紭鍏堬級锛屽啀鎸夊垪鍗囧簭锛堝氨杩戝師鍒欙級锛屽啀鎸夊眰鏁帮紙鍏堜笅鍚庝笂锛�
+                    //todo: 鍔犲垽鏂� dt_LocationInfoResult>=2 灏忎簬鐩存帴鎻愮ず褰撳墠绌洪棽璐т綅涓嶈冻
+                    if(dt_LocationInfoResult.Count<2)
+                    {
+                        return webResponseContent.OK("褰撳墠绌洪棽璐т綅涓嶈冻!");
+                    }
                     dt_LocationInfoResult.OrderByDescending(x => x.Depth).ThenBy(x => x.Column).ThenBy(x => x.Layer).ToList();
                     List<Dt_LocationInfo> dt_Locations = new List<Dt_LocationInfo>();
                     Dt_Task dt_task = dt_TaskList[0];
@@ -420,17 +577,16 @@
                             if (locationResult.Depth == "2")
                             {
                                 locationResult.IsLocked = true;
-                                locationResult.Status = (int)LocationStatus.閿佸畾;
+                                locationResult.Status = (int)LocationEnum.Lock;
                                 //鑾峰彇瀵瑰簲鐨�1娣变綅璐т綅淇℃伅
-                                Dt_LocationInfo location = _dt_LocationInfoRepository.QueryData(x => x.Layer == locationResult.Layer && x.Column == locationResult.Column && x.Line == (int.Parse(locationResult.Line.ToString()) - 1).ToString() && x.Status == (int)LocationStatus.绌洪棽).FirstOrDefault();
+                                Dt_LocationInfo location = dt_LocationInfoResult.Where(x => x.Layer == locationResult.Layer && x.Column == locationResult.Column && x.Line == (int.Parse(locationResult.Line.ToString()) - 1).ToString() && x.Status == (int)LocationEnum.Free).FirstOrDefault();
                                 if (location != null)
                                 {
                                     location.IsLocked = true;
-                                    location.Status = (int)LocationStatus.閿佸畾;
+                                    location.Status = (int)LocationEnum.Lock;
                                     dt_Locations.Add(locationResult);//1娣变綅
                                     dt_Locations.Add(location);//2娣变綅
-
-                                    dt_task.LocationCode = locationResult.LocationCode;
+                                    dt_task.LocationCode = locationResult.LocationCode;//鍒嗛厤瀵瑰簲鐨勮揣浣�
                                     break;
                                 }
                                 else
@@ -442,9 +598,10 @@
                             else
                             {
                                 locationResult.IsLocked = true;
-                                locationResult.Status = (int)LocationStatus.閿佸畾;
+                                locationResult.Status = (int)LocationEnum.Lock;
                                 dt_Locations.Add(locationResult);
-                                dt_task.LocationCode = locationResult.LocationCode;
+                                dt_task.LocationCode = locationResult.LocationCode;// 鍒嗛厤瀵瑰簲鐨勮揣浣�
+
                                 break;
                             }
 
@@ -463,6 +620,7 @@
                         }
                         catch (Exception)
                         {
+                            _unitOfWorkManage.RollbackTran();
                             throw;
                         }
 
@@ -493,7 +651,7 @@
 
 
         /// <summary>
-        /// 鍏ュ簱浠诲姟瀹屾垚澶勭悊
+        /// 鍏ュ簱浠诲姟瀹屾垚
         /// </summary>
         /// <returns></returns>
         public WebResponseContent FinishInboundTask(int taskNum)
@@ -501,9 +659,10 @@
             WebResponseContent webResponseContent = new WebResponseContent();
             try
             {
-                //1锛屼慨鏀硅揣浣嶇姸鎬�
+                //淇敼璐т綅鐘舵��
                 new LogLibrary.Log.LogFactory().GetLog("鎺ュ彛").InfoFormat(true, "鍏ュ簱浠诲姟瀹屾垚澶勭悊-FinishInboundTask", "璇锋眰", $"taskNum-{taskNum}");
                 List<Dt_LocationInfo> dt_Locations = new List<Dt_LocationInfo>();
+                List<Dt_InboundOrderDetail> dt_InboundOrderDetails = new List<Dt_InboundOrderDetail>();
                 List<Dt_Task> dt_TaskList = _dt_TaskRepository.QueryData(x => x.TaskNum == taskNum);
                 if (dt_TaskList != null && dt_TaskList.Count > 0)
                 {
@@ -515,12 +674,12 @@
                         if (locationinfo != null && locationinfo.Depth == "2")
                         {
                             //鑾峰彇瀵瑰簲鐨�1娣变綅璐т綅淇℃伅
-                            Dt_LocationInfo location = _dt_LocationInfoRepository.QueryData(x => x.Layer == locationinfo.Layer && x.Column == locationinfo.Column && x.Line == (int.Parse(locationinfo.Line.ToString()) - 1).ToString() && x.Status == (int)LocationStatus.閿佸畾).FirstOrDefault();
+                            Dt_LocationInfo location = _dt_LocationInfoRepository.QueryData(x => x.Layer == locationinfo.Layer && x.Column == locationinfo.Column && x.Line == (int.Parse(locationinfo.Line.ToString()) - 1).ToString() && x.Status == (int)LocationEnum.Lock).FirstOrDefault();
                             if (location != null)
                             {
-                                locationinfo.Status = (int)LocationStatus.绌洪棽;
+                                locationinfo.Status = (int)LocationEnum.Free;
                                 locationinfo.IsLocked = false;
-                                location.Status = (int)LocationStatus.绌洪棽;
+                                location.Status = (int)LocationEnum.Free;
                                 location.IsLocked = false;
                                 locationinfos.Add(locationinfo);
                                 locationinfos.Add(location);
@@ -528,13 +687,72 @@
                         }
                         else
                         {
-                            locationinfo.Status = (int)LocationStatus.绌洪棽;
+                            locationinfo.Status = (int)LocationEnum.Free;
                             locationinfo.IsLocked = false;
                             locationinfos.Add(locationinfo);
                         }
                     }
-                    dt_TaskList[0].TaskState = TaskStatusEmun.鍏ュ簱浠诲姟瀹屾垚.ToString();
+                    //鏇存柊浠诲姟鐘舵��
+                    dt_TaskList[0].TaskState =(int) InTaskStatusEnum.InFinish;
                 }
+                else
+                {
+                   return  webResponseContent.Error($"鏈煡璇㈠埌璇ュ叆搴撲换鍔¤褰曪紒浠诲姟鍙�:{taskNum}");
+                }
+
+                //鏇存柊搴撳瓨淇℃伅锛堣揣浣嶄笌鎵樼洏鍙风粦瀹氾級,鐘舵�佹敼涓哄叆搴撳畬鎴�
+                Dt_BillGroupStock billGroupStock = _dt_BillGroupStockRepository.QueryData(x => x.GroupId == dt_TaskList[0].GroupID).FirstOrDefault();
+                decimal? palletQuantity = null;
+                if (billGroupStock != null)
+                {
+                    billGroupStock.LocationCode = dt_TaskList[0].LocationCode;
+                    billGroupStock.State = (int)StockStateEmun.鍏ュ簱瀹屾垚;
+                }
+
+                Dt_InboundOrder inboundOrder = BaseDal.QueryData(x => x.InboundNo == billGroupStock.OrderNo).FirstOrDefault();
+                
+                if (inboundOrder != null)
+                {
+                    if (billGroupStock.Dt_BillGroupStockDetailList != null && billGroupStock.Dt_BillGroupStockDetailList.Count > 0)
+                    {
+                        //褰撳墠浠诲姟瀹屾垚鍏ュ簱鏁伴噺锛堝綋鍓嶆墭鐩樻槑缁嗘暟閲忥級 
+                        //澶氫釜鏄庣粏鍏ュ簱鍗曟嵁鍒嗙粍缁熻
+                        var billStockGroups = billGroupStock.Dt_BillGroupStockDetailList.GroupBy(x => x.OrderDetailId).Select(g => new { OrderDetailId = g.Key, PalletQuantity = g.Sum(x => x.PalletQuantity) }).ToList();
+
+                        foreach (var kvp in billStockGroups)
+                        {
+                            //鏌ヨ鍏ュ簱鍗曟槑缁�
+                            Dt_InboundOrderDetail inboundOrderDetail = new Dt_InboundOrderDetail();
+                            inboundOrderDetail = _dt_InboundOrderDetailRepository.QueryFirst(x => x.OrderDetailId == kvp.OrderDetailId);
+                            if (inboundOrderDetail != null)
+                            {
+                                palletQuantity = kvp.PalletQuantity;
+                                //鏇存柊鍏ュ簱鍗曟槑缁嗗叆搴撴暟閲�(绱姞)
+                                inboundOrderDetail.ActualInboundQuantity = string.IsNullOrEmpty(inboundOrderDetail.ActualInboundQuantity) ? palletQuantity.ToString() : (Convert.ToDecimal(inboundOrderDetail.ActualInboundQuantity) + palletQuantity).ToString();
+                                //鏇存柊鍏ュ簱鍗曟槑缁嗙姸鎬佷负宸插叆搴�
+
+                                if (Convert.ToDecimal(inboundOrderDetail.ActualInboundQuantity) == Convert.ToDecimal(inboundOrderDetail.PreInboundQuantity))
+                                {
+                                    inboundOrderDetail.InboundState = (int)InboundStateEmun.鍏ュ簱瀹屾垚;
+                                }
+                                else
+                                {
+                                    inboundOrderDetail.InboundState = (int)InboundStateEmun.鍏ュ簱涓�;//涓�鏉″叆搴撳崟鎹槑缁嗗彲鑳戒細瀵瑰簲澶氫釜鎵樼洏锛屾墍鏈夌殑浠诲姟瀹屾垚鍚庯紝鍐嶆敼鐘舵�佷负鍏ュ簱涓��
+                                }
+
+                                dt_InboundOrderDetails.Add(inboundOrderDetail);
+                            }
+                            else
+                            {
+                                return webResponseContent.Error($"鏈煡璇㈠埌瀵瑰簲鐨勫叆搴撴槑缁嗭紒鍏ュ簱鏄庣粏ID:{kvp.OrderDetailId}");
+                            }
+
+                        }
+
+                    }
+ 
+                }
+
 
                 if (dt_Locations.Count > 0)
                 {
@@ -544,13 +762,26 @@
                         _unitOfWorkManage.BeginTran();
                         //1,鏇存柊璐т綅鐘舵��   
                         _dt_LocationInfoRepository.UpdateData(dt_Locations);
-                        //2锛屾洿鏂颁换鍔$姸鎬�
+                        //2锛屾洿鏂颁换鍔$姸鎬侊紙todo:鐩存帴鎻掑叆鍘嗗彶琛ㄤ腑锛屼笉鐢ㄥ啀鏇存柊浠诲姟琛ㄤ腑鐨勭姸鎬侊級
                         _dt_TaskRepository.UpdateData(dt_TaskList[0]);
+                        //3,鏇存柊搴撳瓨淇℃伅锛堣揣浣嶄笌鎵樼洏鍙风粦瀹氾級,鐘舵�佹敼涓哄叆搴撳畬鎴�
+                        _dt_BillGroupStockRepository.UpdateData(billGroupStock);
+                        //4,鏇存柊鍏ュ簱鏄庣粏鍗曟嵁淇℃伅锛屽凡鍏ュ簱鏁伴噺鍜岀姸鎬�  Dt_InboundOrderDetail  涓殑ActualInboundQuantity锛堝疄闄呭叆搴撴暟閲忥級
+                        _dt_InboundOrderDetailRepository.UpdateData(dt_InboundOrderDetails);
+                        //5,鏇存柊鍏ュ簱鍗曟嵁涓昏〃鐘舵�� Dt_InboundOrder 鎵�鏈夌殑鍏ュ簱鏄庣粏瀹屾垚鍏ュ簱鍚庯紝鏁翠釜鍗曟嵁涓昏〃鏀逛负鍏ュ簱瀹屾垚銆�
+                        List<Dt_InboundOrderDetail> inboundOrders = inboundOrder.Dt_InboundOrderDetailList.Where(x => x.InboundState != (int)InboundStateEmun.鍏ュ簱瀹屾垚).ToList();
+                        if(inboundOrders.Count == 0)
+                        {
+                            inboundOrder.InboundState =(int)InboundStateEmun.鍏ュ簱瀹屾垚;
+                        }
+                        BaseDal.UpdateData(inboundOrder);
+
                         _unitOfWorkManage.CommitTran();//鎻愪氦浜嬬墿
                         webResponseContent.OK("鑾峰彇鎴愬姛", dt_TaskList[0]);
                     }
                     catch (Exception)
                     {
+                        _unitOfWorkManage.RollbackTran();
                         throw;
                     }
 
@@ -571,6 +802,156 @@
         }
 
         /// <summary>
+        /// 鍏ュ簱浠诲姟鍙栨秷
+        /// </summary>
+        /// <param name="taskNum"></param>
+        /// <returns></returns>
+        public WebResponseContent CancelInboundTask(int taskNum)
+        {
+            WebResponseContent webResponseContent = new WebResponseContent();
+            try
+            {
+                new LogLibrary.Log.LogFactory().GetLog("鎺ュ彛").InfoFormat(true, "鍏ュ簱浠诲姟鍙栨秷-CancelInboundTask", "璇锋眰", $"taskNum-{taskNum}");
+                Dt_BillGroupStock dt_billGroupStock = new Dt_BillGroupStock();
+                Dt_Task_Hty dt_Task_Hty = new Dt_Task_Hty();
+                Dt_LocationInfo dt_locationInfo = new Dt_LocationInfo();
+                //1,鏌ヨ鏄惁瀛樺湪璇ヤ换鍔★紝骞朵笖浠诲姟鐘舵�佹槸锛�200 鏂板缓鍏ュ簱浠诲姟
+                Dt_Task dt_Task = _dt_TaskRepository.QueryFirst(x => x.TaskNum == taskNum&&x.TaskState==(int) InTaskStatusEnum.InNew);
+                if (dt_Task != null)
+                {
+                    //2,杩樺師缁勭洏鐘舵�佷负锛�1 缁勭洏鏆傚瓨   
+                    dt_billGroupStock = _dt_BillGroupStockRepository.QueryFirst(x => x.GroupId == dt_Task.GroupID);
+                    if (dt_billGroupStock != null)
+                    {
+                        dt_billGroupStock.State = (int)StockStateEmun.缁勭洏鏆傚瓨;
+                        dt_billGroupStock.LocationCode = "";
+                    }
+                    else
+                    {
+                        new LogLibrary.Log.LogFactory().GetLog("鎺ュ彛").InfoFormat(true, "鍏ュ簱浠诲姟鍙栨秷-CancelInboundTask", "鍝嶅簲", $"鏈煡璇㈠埌璇ヤ换鍔″彿鐨勭粍鐩樹俊鎭細{taskNum}");
+                        return webResponseContent.Error($"鏈煡璇㈠埌璇ヤ换鍔″彿鐨勭粍鐩樹俊鎭細{taskNum}");
+                    }
+                    //3,绉诲埌鍘嗗彶浠诲姟琛ㄤ腑 
+                    #region 鐢熸垚鍘嗗彶浠诲姟璁板綍
+                    dt_Task_Hty.TaskNum = taskNum;
+                    dt_Task_Hty.TaskId = dt_Task.TaskId;
+                    dt_Task_Hty.PalletCode = dt_Task.PalletCode;
+                    dt_Task_Hty.InboundNo = dt_Task.InboundNo;
+                    dt_Task_Hty.GroupID = dt_Task.GroupID;
+                    dt_Task_Hty.Roadway = dt_Task.Roadway;
+                    dt_Task_Hty.LocationCode = dt_Task.LocationCode;
+                    dt_Task_Hty.TaskType = dt_Task.TaskType;
+                    dt_Task_Hty.TaskState = (int)InTaskStatusEnum.InCancel;
+                    dt_Task_Hty.MaterialNo = dt_Task.MaterialNo;
+                    dt_Task_Hty.SourceAddress = dt_Task.SourceAddress;
+                    dt_Task_Hty.TargetAddress = dt_Task.TargetAddress;
+                    dt_Task_Hty.CurrentAddress = dt_Task.CurrentAddress;
+                    dt_Task_Hty.NextAddress = dt_Task.NextAddress;
+                    dt_Task_Hty.Grade = dt_Task.Grade;
+                    dt_Task_Hty.Dispatchertime = dt_Task.Dispatchertime;
+                    dt_Task_Hty.Remark = dt_Task.Remark;
+                    dt_Task_Hty.CreateID = dt_Task.CreateID;
+                    dt_Task_Hty.Creater = dt_Task.Creater;
+                    dt_Task_Hty.CreateDate = dt_Task.CreateDate;
+                    dt_Task_Hty.ModifyID = dt_Task.ModifyID;
+                    dt_Task_Hty.Modifier = dt_Task.Modifier;
+                    dt_Task_Hty.ModifyDate = dt_Task.ModifyDate;
+                    #endregion
+                    //4,浠诲姟琛ㄤ腑鍒犻櫎浠诲姟
+
+                    //5,閲婃斁璐т綅
+                    dt_locationInfo = _dt_LocationInfoRepository.QueryFirst(x => x.LocationCode == dt_Task.LocationCode);
+                    dt_locationInfo.Status = (int)LocationEnum.Free;
+                }
+                else
+                {
+                    new LogLibrary.Log.LogFactory().GetLog("鎺ュ彛").InfoFormat(true, "鍏ュ簱浠诲姟鍙栨秷-CancelInboundTask", "鍝嶅簲", $"璇ヤ换鍔″彿涓嶅瓨鍦細{taskNum}");
+                    return webResponseContent.Error($"璇ヤ换鍔″彿涓嶅瓨鍦細{taskNum}");
+                }
+
+                try
+                {
+                    //寮�鍚簨鐗�
+                    _unitOfWorkManage.BeginTran();
+                    //2,鏇存柊浠诲姟鐘舵�佷负锛�270 鍏ュ簱浠诲姟鍙栨秷
+                    _dt_Task_HtyRepository.AddData(dt_Task_Hty);
+                    //3,杩樺師缁勭洏鐘舵�佷负锛�1 缁勭洏鏆傚瓨 
+                    _dt_BillGroupStockRepository.UpdateData(dt_billGroupStock);
+                    //4,鍒犻櫎浠诲姟
+                    _dt_TaskRepository.DeleteDataById(dt_Task.TaskId);
+                    //5,璐т綅琛ㄩ噴鏀捐揣浣�
+                    _dt_LocationInfoRepository.UpdateData(dt_locationInfo);
+                    //鎻愪氦浜嬬墿
+                    _unitOfWorkManage.CommitTran();
+                }
+                catch (Exception)
+                {
+                    _unitOfWorkManage.RollbackTran();
+                    throw;
+                }
+            }
+            catch (Exception ex)
+            {
+                new LogLibrary.Log.LogFactory().GetLog("鎺ュ彛").InfoFormat(true, "鍏ュ簱浠诲姟鍙栨秷-CancelInboundTask", "鍝嶅簲", $"浠诲姟鍙凤細{taskNum}浠诲姟鍙栨秷澶辫触锛屽紓甯竰ex.ToString()}");
+                return webResponseContent.Error($"浠诲姟鍙凤細{taskNum}浠诲姟鍙栨秷澶辫触锛屽紓甯竰ex.ToString()}");
+            }
+            return webResponseContent;
+
+
+        }
+
+
+        /// <summary>
+        /// 鍏ュ簱浠诲姟鏇存柊
+        /// </summary>
+        /// <param name="taskNum"></param>
+        /// <returns></returns>
+        public WebResponseContent UpdateInboundTask(int taskNum, string currentAddress, string nextAddress, string ErrorMessage)
+        {
+            WebResponseContent webResponseContent = new WebResponseContent();
+            new LogLibrary.Log.LogFactory().GetLog("鎺ュ彛").InfoFormat(true, "鍏ュ簱浠诲姟鏇存柊-UpdateInboundTask", "璇锋眰", $"taskNum-{taskNum} currentAddress-{currentAddress} nextAddress-{nextAddress} ErrorMessage-{ErrorMessage}");
+            //1,鏌ヨ鏄惁瀛樺湪璇ヤ换鍔�
+            try
+            {
+                Dt_Task dt_Task = _dt_TaskRepository.QueryFirst(x => x.TaskNum == taskNum);
+                if (dt_Task != null)
+                {
+                    if (string.IsNullOrEmpty(currentAddress))
+                    {
+                        dt_Task.CurrentAddress = currentAddress;
+                    }
+                    if (string.IsNullOrEmpty(nextAddress))
+                    {
+                        dt_Task.NextAddress = nextAddress;
+                    }
+                    if (string.IsNullOrEmpty(ErrorMessage))
+                    {
+                        dt_Task.ErrorMessage = ErrorMessage;
+                    }
+                    dt_Task.ModifyDate = DateTime.Now;
+                    dt_Task.Modifier = App.User.UserId.ToString();
+                    //2,鏇存柊浠诲姟淇℃伅
+                    _dt_TaskRepository.UpdateData(dt_Task);
+                }
+                else
+                {
+                    new LogLibrary.Log.LogFactory().GetLog("鎺ュ彛").InfoFormat(true, "鍏ュ簱浠诲姟鏇存柊-UpdateInboundTask", "鍝嶅簲", $"浠诲姟鍙凤細{taskNum} 浠诲姟涓嶅瓨鍦�");
+                    return webResponseContent.Error($"浠诲姟鍙凤細{taskNum} 浠诲姟涓嶅瓨鍦�");
+                }
+            }
+            catch (Exception ex)
+            {
+                new LogLibrary.Log.LogFactory().GetLog("鎺ュ彛").InfoFormat(true, "鍏ュ簱浠诲姟鏇存柊-UpdateInboundTask", "鍝嶅簲", $"浠诲姟鍙凤細{taskNum}浠诲姟鏇存柊澶辫触锛屽紓甯竰ex.ToString()}");
+                return webResponseContent.Error($"浠诲姟鍙凤細{taskNum}浠诲姟鏇存柊澶辫触锛屽紓甯竰ex.ToString()}");
+
+            }
+             return webResponseContent;
+        }
+
+
+
+
+        /// <summary>
         /// 鑾峰彇浠诲姟缂栧彿
         /// </summary>
         /// <returns></returns>
@@ -580,6 +961,34 @@
             return int.Parse(dt.Rows[0][0].ToString());
         }
 
+        /// <summary>
+        /// 鑷姩鐢熸垚鍏ュ簱鍗曞彿
+        /// </summary>
+        /// <param name="prefix">R</param>
+        /// <param name="date"></param>
+        /// <returns></returns>
+        public  string GenerateOrderNumber(string prefix, DateTime date)
+        {
+            // 鏍煎紡鍖栨棩鏈熶负 "YYYYMMDD"
+            string dateString = date.ToString("yyyyMMdd");
+            int sn = 1;//娴佹按鍙蜂粠1寮�濮�
+            //閫掑娴佹按鍙�
+            List<Dt_InboundOrder> inboundOrders = BaseDal.QueryData().OrderByDescending(x => x.InboundNo.Substring(x.InboundNo.Length - 5)).ToList();
+            if(inboundOrders.Count > 0 )
+            {
+                Dt_InboundOrder dt_InboundOrder = new Dt_InboundOrder();
+                sn = int.Parse(dt_InboundOrder.InboundNo.Substring(dt_InboundOrder.InboundNo.Length - 5)) + 1;
+            }
+
+            // 缁勫悎鍓嶇紑銆佹棩鏈熷拰娴佹按鍙�
+            StringBuilder orderNumber = new StringBuilder();
+            orderNumber.Append(prefix);
+            orderNumber.Append(dateString);
+            orderNumber.Append(sn.ToString().PadLeft(5, '0'));
+
+            return orderNumber.ToString();
+        }
+
 
     }
 }

--
Gitblit v1.9.3