From 7ca9651f81d7b84f054194d3d46fdbd1d9c8b922 Mon Sep 17 00:00:00 2001
From: dengjunjie <dengjunjie@hnkhzn.com>
Date: 星期三, 09 七月 2025 22:55:27 +0800
Subject: [PATCH] 增加质检出入库逻辑

---
 项目代码/WMS/WIDESEA_WMSServer/WIDESEA_TaskInfoService/TaskService.cs |  675 ++++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 610 insertions(+), 65 deletions(-)

diff --git "a/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA_TaskInfoService/TaskService.cs" "b/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA_TaskInfoService/TaskService.cs"
index 8ebdac4..29caa7a 100644
--- "a/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA_TaskInfoService/TaskService.cs"
+++ "b/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA_TaskInfoService/TaskService.cs"
@@ -17,6 +17,7 @@
 
 using AutoMapper;
 using MailKit.Search;
+using Newtonsoft.Json;
 using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;
 using SqlSugar;
 using System;
@@ -29,6 +30,7 @@
 using System.Text;
 using System.Threading.Tasks;
 using System.Transactions;
+using WIDESEA_Common.StockEnum;
 using WIDESEA_Common.TaskEnum;
 using WIDESEA_Core;
 using WIDESEA_Core.BaseRepository;
@@ -37,6 +39,7 @@
 using WIDESEA_Core.Helper;
 using WIDESEA_DTO.Inbound;
 using WIDESEA_DTO.Stock;
+using WIDESEA_DTO.WCSInfo;
 using WIDESEA_IBasicRepository;
 using WIDESEA_IBasicService;
 using WIDESEA_IInboundService;
@@ -48,6 +51,8 @@
 using WIDESEA_ITaskInfoRepository;
 using WIDESEA_ITaskInfoService;
 using WIDESEA_Model.Models;
+using WIDESEA_OutboundRepository;
+using WIDESEA_StockRepository;
 using WIDESEA_TaskInfoRepository;
 using WIDESEAWCS_DTO.WCSInfo;
 
@@ -63,11 +68,13 @@
         private readonly IInboundService _inboundService;
         private readonly IRecordService _recordService;
         private readonly IStockService _stockService;
+        private readonly IInventoryInfoRepository _inventoryInfoRepository;
+        private readonly IInventoryBatchRepository _inventoryBatchRepository;
         private readonly IBasicRepository _basicRepository;
 
         public ITaskRepository Repository => BaseDal;
 
-        public TaskService(ITaskRepository BaseDal, IMapper mapper, IUnitOfWorkManage unitOfWorkManage, IBasicService basicService, IOutboundService outboundService, IInboundService inboundService, IRecordService recordService, IStockService stockService, IBasicRepository basicRepository) : base(BaseDal)
+        public TaskService(ITaskRepository BaseDal, IMapper mapper, IUnitOfWorkManage unitOfWorkManage, IBasicService basicService, IOutboundService outboundService, IInboundService inboundService, IRecordService recordService, IStockService stockService, IInventoryInfoRepository inventoryInfoRepository, IInventoryBatchRepository inventoryBatchRepository, IBasicRepository basicRepository) : base(BaseDal)
         {
             _mapper = mapper;
             _unitOfWorkManage = unitOfWorkManage;
@@ -75,6 +82,8 @@
             _outboundService = outboundService;
             _inboundService = inboundService;
             _recordService = recordService;
+            _inventoryInfoRepository = inventoryInfoRepository;
+            _inventoryBatchRepository = inventoryBatchRepository;
             _stockService = stockService;
             _basicRepository = basicRepository;
         }
@@ -247,6 +256,235 @@
 
             return WebResponseContent.Instance.OK();
         }
+        public WebResponseContent OutQC(string batchNo, int qty)
+        {
+            WebResponseContent webResponse = new WebResponseContent().OK();
+            try
+            {
+                Dt_Inventory_Batch inventory_Batch = _inventoryBatchRepository.QueryFirst(x => x.BatchNo == batchNo);
+                if (inventory_Batch == null) return webResponse.Error($"鏈壘鍒版壒鍙枫�恵batchNo}銆戠殑搴撳瓨鎵规淇℃伅");
+                if (inventory_Batch.ProductStatus != ProductStatusEmun.寰呮1.ObjToInt())
+                    return webResponse.Error($"鎵瑰彿銆恵batchNo}銆戝凡璐ㄦ鍏ュ簱");
+                List<Dt_InventoryInfo> _InventoryInfos = _inventoryInfoRepository.QueryData(x => x.BatchNo == batchNo && x.StockStatus == StockStatusEmun.宸插叆搴�.ObjToInt()).OrderByDescending(x => x.InDate).Take(qty).ToList();
+                if (_InventoryInfos.Count < 1) return webResponse.Error($"鏈壘鍒版壒鍙枫�恵batchNo}銆戠殑搴撳瓨");
+                Dt_LocationInfo locationInfo = _basicRepository.LocationInfoRepository.QueryFirst(x => x.LocationCode == _InventoryInfos.First().LocationCode);
+                List<Dt_Task> tasks = new List<Dt_Task>();
+                List<Dt_OutStockLockInfo> OutStockLockInfos = new List<Dt_OutStockLockInfo>();
+                #region 鐢熸垚璐ㄦ鍑哄簱鍗�
+                Dt_OutboundOrderDetail orderDetail = new Dt_OutboundOrderDetail()
+                {
+                    BatchNo = batchNo,
+                    Creater = "System",
+                    OrderDetailStatus = OrderDetailStatusEnum.Outbound.ObjToInt()
+                };
+                Dt_OutboundOrder outboundOrder = new Dt_OutboundOrder()
+                {
+                    CreateType = CreateType.CreateInSystem.ObjToInt(),
+                    OrderStatus = OutboundStatusEnum.鍑哄簱涓�.ObjToInt(),
+                    OrderNo = batchNo,
+                    OrderType = OutOrderTypeEnum.Quality.ObjToInt(),
+                    Creater = "System",
+                    Details = new List<Dt_OutboundOrderDetail>() { orderDetail }
+                };
+                #endregion
+                foreach (var item in _InventoryInfos)
+                {
+                    orderDetail.MaterielCode = item.MaterielCode;
+                    orderDetail.MaterielName = item.MaterielName;
+                    orderDetail.OrderQuantity += item.StockQuantity;
+                    orderDetail.LockQuantity += item.StockQuantity;
+                    item.OutboundQuantity = item.StockQuantity;
+                    item.StockStatus = StockStatusEmun.鍑哄簱涓�.ObjToInt();
+                    Dt_Task task = new Dt_Task()
+                    {
+                        CurrentAddress = item.LocationCode,
+                        Grade = 0,
+                        PalletCode = item.PalletCode,
+                        SourceIsPickPlace = item.SerialNumber == locationInfo.MaxQty,
+                        NextAddress = "1005",
+                        Roadway = "SC01",
+                        SourceAddress = item.LocationCode,
+                        TargetAddress = "1005",
+                        TaskState = OutTaskStatusEnum.OutNew.ObjToInt(),
+                        TaskType = TaskTypeEnum.OutQuality.ObjToInt(),
+                        TaskNum = BaseDal.GetTaskNum(nameof(SequenceEnum.SeqTaskNum))
+                    };
+                    tasks.Add(task);
+                    #region 娣诲姞鍑哄簱璇︽儏
+                    Dt_OutStockLockInfo outStockLockInfo = new Dt_OutStockLockInfo()
+                    {
+                        PalletCode = item.PalletCode,
+                        AssignQuantity = item.OutboundQuantity,
+                        MaterielCode = item.MaterielCode,
+                        BatchNo = item.BatchNo,
+                        LocationCode = item.LocationCode,
+                        MaterielName = item.MaterielName,
+                        OrderDetailId = orderDetail.Id,
+                        OrderNo = outboundOrder.OrderNo,
+                        OrderType = outboundOrder.OrderType,
+                        OriginalQuantity = item.StockQuantity,
+                        Status = OutLockStockStatusEnum.鍑哄簱涓�.ObjToInt(),
+                        StockId = item.Id,
+                        TaskNum = task.TaskNum,
+                        OrderQuantity = orderDetail.OrderQuantity
+                    };
+                    OutStockLockInfos.Add(outStockLockInfo);
+                    #endregion
+                }
+
+                #region 鐢熸垚璐ㄦ鍏ュ簱鍗�
+                Dt_InboundOrderDetail inboundOrderDetail = new Dt_InboundOrderDetail()
+                {
+                    BatchNo = batchNo,
+                    Creater = "System",
+                    OrderDetailStatus = OrderDetailStatusEnum.New.ObjToInt(),
+                    MaterielName = orderDetail.MaterielName,
+                    MaterielCode = orderDetail.MaterielCode,
+                    OrderQuantity = orderDetail.OrderQuantity
+                };
+                Dt_InboundOrder inboundOrder = new Dt_InboundOrder()
+                {
+                    CreateType = CreateType.CreateInSystem.ObjToInt(),
+                    OrderStatus = InboundStatusEnum.鏈紑濮�.ObjToInt(),
+                    OrderNo = batchNo,
+                    OrderType = InOrderTypeEnum.Quality.ObjToInt(),
+                    Creater = "System",
+                    Details = new List<Dt_InboundOrderDetail>() { inboundOrderDetail }
+                };
+                #endregion
+                #region 澶勭悊搴撳瓨鍜屽簱瀛樻壒娆′俊鎭�
+                inventory_Batch.StockQuantity = Convert.ToInt32(inventory_Batch.StockQuantity - orderDetail.OrderQuantity);
+
+                inventory_Batch.BoxQuantity = inventory_Batch.StockQuantity / 8;
+                if (inventory_Batch.StockQuantity % 8 > 0) inventory_Batch.BoxQuantity++;
+                inventory_Batch.OutboundQuantity = Convert.ToInt32(inventory_Batch.OutboundQuantity + orderDetail.OrderQuantity);
+                #endregion
+                #region 浜嬪姟澶勭悊
+                Db.Ado.BeginTran();
+                BaseDal.AddData(tasks);
+                _outboundService.OutboundOrderService.Repository.Db.InsertNav(outboundOrder).Include(x => x.Details).ExecuteCommand();
+                _outboundService.OutboundStockLockInfoService.Repository.AddData(OutStockLockInfos);
+                _inboundService.InbounOrderService.Repository.Db.InsertNav(inboundOrder).Include(x => x.Details).ExecuteCommand();
+                _inventoryInfoRepository.UpdateData(_InventoryInfos);
+                _inventoryBatchRepository.UpdateData(inventory_Batch);
+                #region 涓嬪彂浠诲姟缁橶CS
+                List<WMSTaskDTO> wMSTaskDTOs = _mapper.Map<List<WMSTaskDTO>>(tasks);
+                var ResultData = HttpHelper.PostAsync(WCSInterfaceAddress.ReceiveTask, wMSTaskDTOs.ToJson(), headers: new Dictionary<string, string>());
+                if (ResultData.Result == null) throw new Exception($"鍚慦CS涓嬪彂鍑哄簱浠诲姟瓒呮椂");
+                WebResponseContent content = JsonConvert.DeserializeObject<WebResponseContent>(ResultData.Result);
+                if (content == null) throw new Exception($"涓嬪彂鍑哄簱浠诲姟WCS鏃犲搷搴�");
+                if (!content.Status) throw new Exception(content.Message);
+                #endregion
+                Db.Ado.CommitTran();
+                #endregion
+            }
+            catch (Exception ex)
+            {
+                Db.Ado.RollbackTran();
+                webResponse.Error(ex.Message);
+            }
+            return webResponse;
+        }
+        public WebResponseContent ModifyQty(string palletCode, int qty)
+        {
+            WebResponseContent webResponseContent = new WebResponseContent();
+            try
+            {
+                Dt_InventoryInfo inventoryInfo = _inventoryInfoRepository.QueryFirst(x => x.PalletCode == palletCode);
+                if (inventoryInfo == null) throw new Exception("鏈壘鍒版墭鐩樹俊鎭�");
+                if (inventoryInfo.StockStatus != StockStatusEmun.缁勭洏鏆傚瓨.ObjToInt() && inventoryInfo.StockStatus != StockStatusEmun.鍏ュ簱纭.ObjToInt() && inventoryInfo.StockStatus != StockStatusEmun.鍏ュ簱涓�.ObjToInt())
+                    throw new Exception($"鎵樼洏闈炲叆搴撲腑,涓嶅彲淇敼鏁伴噺");
+                Dt_InboundOrderDetail orderDetail = _inboundService.InboundOrderDetailService.Repository.QueryFirst(x => x.BatchNo == inventoryInfo.BatchNo);
+                var StockQty = inventoryInfo.StockQuantity;
+                inventoryInfo.StockQuantity = qty;
+                inventoryInfo.BoxQuantity = qty % 8 > 0 ? qty / 8 + 1 : qty / 8;
+                if (orderDetail != null)
+                {
+                    orderDetail.ReceiptQuantity -= StockQty - qty;
+                    _inboundService.InboundOrderDetailService.Repository.UpdateData(orderDetail);
+                }
+                _inventoryInfoRepository.UpdateData(inventoryInfo);
+                return webResponseContent.OK();
+            }
+            catch (Exception ex)
+            {
+                return webResponseContent.Error(ex.Message);
+            }
+        }
+
+        public WebResponseContent ModifyBarcode(string barcode, string newBarcode)
+        {
+            WebResponseContent webResponseContent = new WebResponseContent();
+            try
+            {
+                Dt_Task? dt_Task = BaseDal.QueryFirst(x => x.PalletCode == barcode);
+                if (dt_Task == null) throw new Exception($"鏈壘鍒版墭鐩樺彿涓恒�恵barcode}銆戠殑浠诲姟锛�");
+                if (TaskEnumHelper.GetTaskTypeGroup(dt_Task.TaskType) != TaskTypeGroup.InboundGroup)
+                    throw new Exception($"鍙彲淇敼鍏ュ簱浠诲姟,濡傞渶淇敼鍏朵粬浠诲姟绫诲瀷璇疯仈绯荤鐞嗗憳锛�");
+                Dt_InventoryInfo inventoryInfo = _inventoryInfoRepository.QueryFirst(x => x.PalletCode == newBarcode);
+                if (inventoryInfo != null) throw new Exception($"鎵樼洏鍙枫�恵newBarcode}銆戝凡瀛樺湪搴撳瓨淇℃伅锛�");
+                inventoryInfo = _inventoryInfoRepository.QueryFirst(x => x.PalletCode == barcode);
+                if (inventoryInfo == null) throw new Exception($"鎵樼洏鍙枫�恵barcode}銆戞湭鎵惧埌搴撳瓨淇℃伅锛�");
+                inventoryInfo.PalletCode = newBarcode;
+
+                //var stock = _stockService.StockInfoService.Repository.QueryFirst(x => x.PalletCode == newBarcode);
+                //if (stock != null) throw new Exception($"鎵樼洏鍙枫�恵newBarcode}銆戝凡瀛樺湪搴撳瓨淇℃伅锛�");
+                //stock = _stockService.StockInfoService.Repository.QueryFirst(x => x.PalletCode == barcode);
+                //if (stock == null) throw new Exception($"鎵樼洏鍙枫�恵barcode}銆戞湭鎵惧埌搴撳瓨淇℃伅锛�");
+                //stock.PalletCode = newBarcode;
+                dt_Task.PalletCode = newBarcode;
+                Db.Ado.BeginTran();
+                WMSTaskDTO wMSTaskDTOs = _mapper.Map<WMSTaskDTO>(dt_Task);
+                var ResultData = HttpHelper.PostAsync(WCSInterfaceAddress.ModifyBarcode, wMSTaskDTOs.ToJson(), headers: new Dictionary<string, string>());
+                if (ResultData.Result == null) throw new Exception($"WCS鍝嶅簲瓒呮椂");
+                WebResponseContent content = JsonConvert.DeserializeObject<WebResponseContent>(ResultData.Result);
+                if (content == null) throw new Exception($"WCS鏃犲搷搴�");
+                if (!content.Status) throw new Exception(content.Message);
+                BaseDal.UpdateData(dt_Task);
+                //_stockService.StockInfoService.Repository.UpdateData(stock);
+                _inventoryInfoRepository.UpdateData(inventoryInfo);
+                Db.Ado.CommitTran();
+                webResponseContent.OK();
+            }
+            catch (Exception ex)
+            {
+                Db.Ado.RollbackTran();
+                webResponseContent.Error(ex.Message);
+            }
+            return webResponseContent;
+        }
+        public WebResponseContent CancelTask(WMSTaskDTO WMStask)
+        {
+            WebResponseContent webResponseContent = new WebResponseContent();
+            try
+            {
+                Dt_Task task = BaseDal.QueryFirst(x => x.TaskNum == WMStask.TaskNum && x.TaskType == WMStask.TaskType);
+                if (task == null) return WebResponseContent.Instance.Error("鏈壘鍒颁换鍔′俊鎭�");
+                Dt_StockInfo? _StockInfo = _stockService.StockInfoService.GetStockByPalletCode(task.PalletCode);
+                if (_StockInfo != null)
+                {
+                    Dt_StockInfoDetail? detail = _StockInfo.Details.FirstOrDefault();
+                    if (detail != null)
+                    {
+                        Dt_InboundOrder dt_InboundOrder = _inboundService.InbounOrderService.GetInboundOrder(detail.OrderNo);
+                        Dt_InboundOrderDetail? inboundOrderDetail = dt_InboundOrder.Details.FirstOrDefault(x => x.BatchNo == detail.BatchNo);
+                        if (inboundOrderDetail != null)
+                        {
+                            inboundOrderDetail.ReceiptQuantity--;
+                            _inboundService.InboundOrderDetailService.Repository.UpdateData(inboundOrderDetail);
+                        }
+                        _stockService.StockInfoDetailService.Repository.DeleteAndMoveIntoHty(_StockInfo.Details, OperateType.AGV鍒犻櫎);
+                    }
+                    _stockService.StockInfoService.Repository.DeleteAndMoveIntoHty(_StockInfo, OperateType.AGV鍒犻櫎);
+                }
+                BaseDal.DeleteAndMoveIntoHty(task, /*App.User.UserId == 0 ? OperateType.鑷姩瀹屾垚 :*/ OperateType.AGV鍒犻櫎);
+            }
+            catch (Exception ex)
+            {
+                webResponseContent.Error(ex.Message);
+            }
+            return webResponseContent;
+        }
         /// <summary>
         /// 鍚屾浠诲姟鐘舵��
         /// </summary>
@@ -260,35 +498,48 @@
             {
                 Dt_Task task = BaseDal.QueryFirst(x => x.TaskNum == WMStask.TaskNum && x.TaskType == WMStask.TaskType);
                 if (task == null) return WebResponseContent.Instance.Error("鏈壘鍒颁换鍔′俊鎭�");
-                if (task.TaskType == (int)TaskTypeEnum.Inbound)
+                if (TaskEnumHelper.GetTaskTypeGroup(task.TaskType) == TaskTypeGroup.InboundGroup)
                 {
                     int nextStatus = task.TaskState.GetNextNotCompletedStatus<TaskInStatusEnum>();
-                    if (WMStask.TaskState != nextStatus) return WebResponseContent.Instance.Error($"璇ヤ换鍔$姸鎬佹湁璇�,浠诲姟鍙�:銆恵task.TaskNum}銆�,浠诲姟鐘舵��:銆恵task.TaskState}銆�");
-                    task.TaskState = nextStatus;
-                    switch (nextStatus)
+                    //if (WMStask.TaskState != nextStatus) return WebResponseContent.Instance.Error($"璇ヤ换鍔$姸鎬佹湁璇�,浠诲姟鍙�:銆恵task.TaskNum}銆�,浠诲姟鐘舵��:銆恵task.TaskState}銆�");
+                    //task.TaskState = nextStatus;
+                    task.TaskState = TaskInStatusEnum.InFinish.ObjToInt();
+                    content = NewUpdateTaskStatusInFinish(task);
+                }
+                else if (TaskEnumHelper.GetTaskTypeGroup(task.TaskType) == TaskTypeGroup.OutbondGroup)
+                {
+                    //int nextStatus = task.TaskState.GetNextNotCompletedStatus<TaskOutStatusEnum>();
+                    //if (WMStask.TaskState != nextStatus) return WebResponseContent.Instance.Error($"璇ヤ换鍔$姸鎬佹湁璇�,浠诲姟鍙�:銆恵task.TaskNum}銆�,浠诲姟鐘舵��:銆恵task.TaskState}銆�");
+                    //task.TaskState = nextStatus;
+                    task.TaskState = WMStask.TaskState;
+                    switch (task.TaskState)
                     {
-                        case (int)TaskInStatusEnum.SC_InFinish:
-                            content = UpdateTaskStatusInFinish(task);
+                        case (int)TaskOutStatusEnum.SC_OutFinish://鏇存柊璐т綅搴撳瓨淇℃伅
+                            content = NewUpdateTaskStatusSC_OutFinish(task);
                             break;
-                        case (int)TaskInStatusEnum.Car_InFinish:
-                            content = InboundTaskCompleted(task);
+                        case (int)TaskOutStatusEnum.AGV_OutFinish:
+                            {
+                                task.CurrentAddress = WMStask.CurrentAddress;
+                                task.TargetAddress = WMStask.TargetAddress;
+                                task.NextAddress = string.Empty;
+                                content = UpdateData(task);
+                            }
+                            break;
+                        case (int)TaskOutStatusEnum.OutFinish:
+                            content = UpdateTaskStatusOutFinish(task);
                             break;
                         default:
+                            content = UpdateData(task);
                             break;
                     }
                 }
-                else if (task.TaskType == (int)TaskTypeEnum.Outbound)
+                else if (task.TaskType == (int)TaskTypeEnum.Relocation)
                 {
-                    int nextStatus = task.TaskState.GetNextNotCompletedStatus<TaskOutStatusEnum>();
-                    if (WMStask.TaskState != nextStatus) return WebResponseContent.Instance.Error($"璇ヤ换鍔$姸鎬佹湁璇�,浠诲姟鍙�:銆恵task.TaskNum}銆�,浠诲姟鐘舵��:銆恵task.TaskState}銆�");
-                    task.TaskState = nextStatus;
-                    switch (nextStatus)
-                    {
-                        case (int)TaskOutStatusEnum.SC_OutFinish://鏇存柊璐т綅淇℃伅
-                            break;
-                        default:
-                            break;
-                    }
+                    task.TaskState = WMStask.TaskState;
+                    if (task.TaskState == (int)WIDESEA_Core.Enums.TaskRelocationStatusEnum.SC_RelocationFinish)
+                        content = UpdateTaskStatusRelocationCarFinish(task);
+                    else
+                        content = UpdateData(task);
                 }
             }
             catch (Exception ex)
@@ -297,9 +548,296 @@
             }
             return content;
         }
+        public WebResponseContent NewUpdateTaskStatusSC_OutFinish(Dt_Task task)
+        {
+            try
+            {
+                Dt_Inventory_Batch? inventory_Batch = null;
+                Dt_OutboundOrderDetail? orderDetail = null;
+                Dt_OutboundOrder? outboundOrder = null;
+                Dt_InventoryInfo inventoryInfo = _inventoryInfoRepository.QueryFirst(x => x.PalletCode == task.PalletCode);
+                if (inventoryInfo != null)
+                {
+                    inventory_Batch = _inventoryBatchRepository.QueryFirst(x => x.BatchNo == inventoryInfo.BatchNo);
+                    orderDetail = _outboundService.OutboundOrderDetailService.Repository.QueryFirst(x => x.BatchNo == inventoryInfo.BatchNo);
+                }
+                Dt_LocationInfo Sourcelocation = _basicService.LocationInfoService.Repository.QueryFirst(x => x.LocationCode == task.SourceAddress);
 
+                Dt_OutStockLockInfo outStockLockInfo = _outboundService.OutboundStockLockInfoService.Repository.QueryFirst(x => x.TaskNum == task.TaskNum && task.PalletCode == x.PalletCode);
+                Db.Ado.BeginTran();
+                if (inventoryInfo != null) _inventoryInfoRepository.DeleteData(inventoryInfo);
+
+                if (Sourcelocation != null)
+                {
+                    if (BaseDal.QueryFirst(x => x.SourceAddress == task.SourceAddress && x.TaskState < TaskOutStatusEnum.SC_OutFinish.ObjToInt()) == null)
+                        Sourcelocation.LocationStatus = LocationStatusEnum.InStock.ObjToInt();
+                    Sourcelocation.CurrentQty--;
+                    if (Sourcelocation.CurrentQty <= 0)
+                    {
+                        Sourcelocation.CurrentQty = 0;
+                        Sourcelocation.LocationStatus = LocationStatusEnum.Free.ObjToInt();
+                        Sourcelocation.EnableStatus = EnableStatusEnum.Normal.ObjToInt();
+                    }
+                    _basicService.LocationInfoService.Repository.UpdateData(Sourcelocation);
+                }
+                if (inventory_Batch != null)
+                {
+                    //inventory_Batch.StockQuantity -= inventoryInfo.StockQuantity;
+                    inventory_Batch.OutboundQuantity -= inventoryInfo.StockQuantity;
+                    if (inventory_Batch.StockQuantity == 0 && inventory_Batch.OutboundQuantity == 0)
+                        _inventoryBatchRepository.DeleteData(inventory_Batch);
+                    else _inventoryBatchRepository.UpdateData(inventory_Batch);
+                }
+                if (orderDetail != null)
+                {
+                    if (outStockLockInfo != null)
+                    {
+                        outStockLockInfo.OrderDetailId = orderDetail.OrderId;
+                        _outboundService.OutboundStockLockInfoService.Repository.UpdateData(outStockLockInfo);
+                    }
+                    orderDetail.OverOutQuantity += inventoryInfo.StockQuantity;
+                    if (orderDetail.OverOutQuantity == orderDetail.OrderQuantity)
+                    {
+                        outboundOrder = _outboundService.OutboundOrderService.Repository.QueryFirst(x => x.Id == orderDetail.OrderId);
+                        if (outboundOrder != null)
+                        {
+                            outboundOrder.OrderStatus = OutboundStatusEnum.鍑哄簱瀹屾垚.ObjToInt();
+                            _outboundService.OutboundOrderService.Repository.DeleteAndMoveIntoHty(outboundOrder, OperateType.鑷姩瀹屾垚);
+                        }
+                        orderDetail.OrderDetailStatus = OrderDetailStatusEnum.Over.ObjToInt();
+                        _outboundService.OutboundOrderDetailService.Repository.DeleteAndMoveIntoHty(orderDetail, OperateType.鑷姩瀹屾垚);
+                    }
+                    else
+                        _outboundService.OutboundOrderDetailService.Repository.UpdateData(orderDetail);
+                }
+                task.CurrentAddress = "1005";
+                task.TargetAddress = "1008";
+                task.NextAddress = "1008";
+                UpdateData(task);
+                Db.Ado.CommitTran();
+                return WebResponseContent.Instance.OK();
+            }
+            catch (Exception ex)
+            {
+                Db.Ado.RollbackTran();
+                return WebResponseContent.Instance.Error(ex.Message);
+            }
+        }
         /// <summary>
-        /// 鏇存柊浠诲姟鐘舵�佸畬鎴�
+        /// 淇敼浠诲姟鐘舵�佷负鍫嗗灈鏈哄嚭搴撳畬鎴�
+        /// </summary>
+        /// <param name="task"></param>
+        /// <returns></returns>
+        public WebResponseContent UpdateTaskStatusSC_OutFinish(Dt_Task task)
+        {
+            try
+            {
+                Dt_StockInfo stockInfo = _stockService.StockInfoService.Repository.GetStockInfo(task.PalletCode);
+                Dt_LocationInfo Sourcelocation = _basicService.LocationInfoService.Repository.QueryFirst(x => x.LocationCode == task.SourceAddress);
+                Dt_OutStockLockInfo outStockLockInfo = _outboundService.OutboundStockLockInfoService.Repository.QueryFirst(x => x.TaskNum == task.TaskNum);
+                Db.Ado.BeginTran();
+                if (stockInfo != null)
+                {
+                    _stockService.StockInfoService.Repository.DeleteAndMoveIntoHty(stockInfo, App.User.UserId == 0 ? OperateType.鑷姩瀹屾垚 : OperateType.浜哄伐瀹屾垚);
+                    _stockService.StockInfoDetailService.Repository.DeleteAndMoveIntoHty(stockInfo.Details, App.User.UserId == 0 ? OperateType.鑷姩瀹屾垚 : OperateType.浜哄伐瀹屾垚);
+                }
+                if (Sourcelocation != null)
+                {
+                    /*if (task.TaskType == TaskOutboundTypeEnum.Outbound.ObjToInt())*/
+                    if (BaseDal.QueryFirst(x => x.SourceAddress == task.SourceAddress && x.TaskState < TaskOutStatusEnum.SC_OutFinish.ObjToInt()) == null)
+                        Sourcelocation.LocationStatus = LocationStatusEnum.InStock.ObjToInt();
+                    Sourcelocation.CurrentQty--;
+                    if (Sourcelocation.CurrentQty <= 0)
+                    {
+                        Sourcelocation.CurrentQty = 0;
+                        Sourcelocation.LocationStatus = LocationStatusEnum.Free.ObjToInt();
+                        Sourcelocation.EnableStatus = EnableStatusEnum.Normal.ObjToInt();
+                    }
+                    _basicService.LocationInfoService.Repository.UpdateData(Sourcelocation);
+                }
+                if (outStockLockInfo != null)
+                {
+                    outStockLockInfo.Status = OutLockStockStatusEnum.鍑哄簱涓�.ObjToInt();
+                    _outboundService.OutboundStockLockInfoService.Repository.UpdateData(outStockLockInfo);
+                }
+                UpdateData(task);
+                Db.Ado.CommitTran();
+                return WebResponseContent.Instance.OK();
+            }
+            catch (Exception ex)
+            {
+                Db.Ado.RollbackTran();
+                return WebResponseContent.Instance.Error(ex.Message);
+            }
+        }
+        /// <summary>
+        /// 鏇存柊鍑哄簱浠诲姟鐘舵�佸畬鎴�
+        /// </summary>
+        /// <param name="task"></param>
+        /// <returns></returns>
+        public WebResponseContent UpdateTaskStatusOutFinish(Dt_Task task)
+        {
+            try
+            {
+                Db.Ado.BeginTran();
+                Dt_OutStockLockInfo? outStockLockInfo = _outboundService.OutboundStockLockInfoService.Repository.QueryFirst(x => x.TaskNum == task.TaskNum && x.PalletCode == task.PalletCode);
+                Dt_OutboundOrder? outboundOrder = null;
+                Dt_OutboundOrderDetail? outboundOrderDetail = null;
+                int overCount = 0;
+                if (outStockLockInfo != null)
+                {
+                    outboundOrderDetail = _outboundService.OutboundOrderDetailService.Repository.QueryFirst(x => x.Id == outStockLockInfo.OrderDetailId);
+                    if (outboundOrderDetail != null)
+                    {
+                        outboundOrder = Db.Queryable<Dt_OutboundOrder>().Where(x => x.Id == outboundOrderDetail.OrderId).Includes(x => x.Details).First();
+                        if (outboundOrder != null)
+                        {
+                            outboundOrderDetail.OverOutQuantity++;
+                            overCount = outboundOrder.Details.Where(x => x.OrderDetailStatus == OrderDetailStatusEnum.Over.ObjToInt()).Count();
+                            if (outboundOrderDetail.OverOutQuantity == outboundOrderDetail.OrderQuantity)
+                            {
+                                var outboundOrderdetail = outboundOrder.Details.First(x => x.Id == outboundOrderDetail.Id);
+                                outboundOrderDetail.OrderDetailStatus = OrderDetailStatusEnum.Over.ObjToInt();
+                                outboundOrderdetail.OverOutQuantity = outboundOrderDetail.OverOutQuantity;
+                                outboundOrderdetail.OrderDetailStatus = outboundOrderDetail.OrderDetailStatus;
+                                overCount++;
+                            }
+                        }
+                    }
+                    outStockLockInfo.Status = OutLockStockStatusEnum.鍑哄簱瀹屾垚.ObjToInt();
+                }
+                if (outboundOrder != null && outboundOrder.Details.Count == overCount)
+                {
+                    outboundOrder.OrderStatus = OutboundStatusEnum.鍑哄簱瀹屾垚.ObjToInt();
+                    _outboundService.OutboundOrderDetailService.Repository.DeleteAndMoveIntoHty(outboundOrder.Details, App.User.UserId == 0 ? OperateType.鑷姩瀹屾垚 : OperateType.浜哄伐瀹屾垚);
+                    outboundOrder.Details = null;
+                    _outboundService.OutboundOrderService.Repository.DeleteAndMoveIntoHty(outboundOrder, App.User.UserId == 0 ? OperateType.鑷姩瀹屾垚 : OperateType.浜哄伐瀹屾垚);
+                }
+                else
+                {
+                    if (outboundOrderDetail != null)
+                        _outboundService.OutboundOrderDetailService.Repository.UpdateData(outboundOrderDetail);
+                }
+                BaseDal.DeleteAndMoveIntoHty(task, App.User.UserId == 0 ? OperateType.鑷姩瀹屾垚 : OperateType.浜哄伐瀹屾垚);
+                if (outStockLockInfo != null) _outboundService.OutboundStockLockInfoService.Repository.UpdateData(outStockLockInfo);
+                Db.Ado.CommitTran();
+                return WebResponseContent.Instance.OK();
+            }
+            catch (Exception ex)
+            {
+                Db.Ado.RollbackTran();
+                return WebResponseContent.Instance.Error(ex.Message);
+            }
+        }
+        /// <summary>
+        /// 鏇存柊绉诲簱浠诲姟鐘舵�佸畬鎴�
+        /// </summary>
+        /// <param name="task"></param>
+        /// <returns></returns>
+        public WebResponseContent UpdateTaskStatusRelocationCarFinish(Dt_Task task)
+        {
+            try
+            {
+                Dt_LocationInfo Sourcelocation = _basicService.LocationInfoService.Repository.QueryFirst(x => x.LocationCode == task.SourceAddress);//璧峰璐т綅
+                Dt_LocationInfo Targetlocation = _basicService.LocationInfoService.Repository.QueryFirst(x => x.LocationCode == task.TargetAddress);//鐩爣璐т綅
+                Dt_StockInfo stockInfo = _stockService.StockInfoService.Repository.GetStockInfo(task.PalletCode);//鎵樼洏搴撳瓨
+                Sourcelocation.CurrentQty--;
+                //Targetlocation.CurrentQty++;
+                stockInfo.LocationCode = Targetlocation.LocationCode;
+                //stockInfo.SerialNumber = Targetlocation.CurrentQty;
+                stockInfo.StockStatus = StockStatusEmun.宸插叆搴�.ObjToInt();
+                if (Sourcelocation.CurrentQty == 0) Sourcelocation.LocationStatus = LocationStatusEnum.Free.ObjToInt();
+                Dt_Task _Task = BaseDal.QueryFirst(x => x.TaskNum != task.TaskNum && x.TaskType == task.TaskType && x.SourceAddress == Sourcelocation.LocationCode && x.TargetAddress == Targetlocation.LocationCode);
+                if (_Task == null)
+                {
+                    if (Targetlocation.CurrentQty == Targetlocation.MaxQty) Targetlocation.LocationStatus = LocationStatusEnum.Fullload.ObjToInt();
+                    else Targetlocation.LocationStatus = LocationStatusEnum.InStock.ObjToInt();
+                    if (Sourcelocation.CurrentQty == 0) Sourcelocation.LocationStatus = LocationStatusEnum.Free.ObjToInt();
+                }
+                task.CurrentAddress = task.NextAddress;
+                task.NextAddress = string.Empty;
+                task.ModifyDate = DateTime.Now;
+                task.Modifier = "System";
+                Db.Ado.BeginTran();
+                _basicService.LocationInfoService.Repository.UpdateData(Sourcelocation);
+                _basicService.LocationInfoService.Repository.UpdateData(Targetlocation);
+                _stockService.StockInfoService.Repository.UpdateData(stockInfo);
+                //BaseDal.DeleteAndMoveIntoHty(task, OperateType.鑷姩瀹屾垚);
+                BaseDal.DeleteAndMoveIntoHty(task, App.User.UserId == 0 ? OperateType.鑷姩瀹屾垚 : OperateType.浜哄伐瀹屾垚);
+                Db.Ado.CommitTran();
+                return WebResponseContent.Instance.OK();
+            }
+            catch (Exception ex)
+            {
+                Db.Ado.RollbackTran();
+                return WebResponseContent.Instance.Error(ex.Message);
+            }
+        }
+        /// <summary>
+        /// 鏇存柊鍏ュ簱浠诲姟鐘舵�佸畬鎴�
+        /// </summary>
+        /// <param name="task"></param>
+        /// <returns></returns>
+        public WebResponseContent NewUpdateTaskStatusInFinish(Dt_Task task)
+        {
+            WebResponseContent content = new WebResponseContent().OK();
+            try
+            {
+                Dt_InventoryInfo inventoryInfo = _inventoryInfoRepository.QueryFirst(x => x.PalletCode == task.PalletCode);
+                if (inventoryInfo == null) return content.Error("鏈壘鍒版墭鐩樹俊鎭�");
+                Dt_LocationInfo locationInfo = _basicService.LocationInfoService.Repository.QueryFirst(x => x.LocationCode == task.TargetAddress);//璐т綅
+                if (locationInfo == null) return content.Error("鏈壘鍒拌揣浣嶄俊鎭�");
+                if (task.TaskType == TaskTypeEnum.PalletInbound.ObjToInt())
+                {
+                    return content;
+                }
+                inventoryInfo.InDate = DateTime.Now;
+                inventoryInfo.StockStatus = StockStatusEmun.宸插叆搴�.ObjToInt();
+                int beforeStatus = locationInfo.LocationStatus;
+                if (BaseDal.QueryFirst(x => x.TargetAddress == locationInfo.LocationCode && x.TaskNum != task.TaskNum) == null)
+                    locationInfo.LocationStatus = LocationStatusEnum.InStock.ObjToInt();
+                task.CurrentAddress = task.NextAddress;
+                task.NextAddress = string.Empty;
+
+                Dt_Inventory_Batch inventory_Batch = _inventoryBatchRepository.QueryFirst(x => x.BatchNo == inventoryInfo.BatchNo);
+                Db.Ado.BeginTran();
+                if (inventory_Batch == null)
+                {
+                    inventory_Batch = new Dt_Inventory_Batch()
+                    {
+                        BatchNo = inventoryInfo.BatchNo,
+                        BoxQuantity = inventoryInfo.BoxQuantity,
+                        MaterielCode = inventoryInfo.MaterielCode,
+                        MaterielName = inventoryInfo.MaterielName,
+                        OutboundQuantity = inventoryInfo.OutboundQuantity,
+                        StockQuantity = inventoryInfo.StockQuantity,
+                        ProductionDate = inventoryInfo.ProductionDate,
+                        ProductStatus = inventoryInfo.ProductStatus,
+                        ShelfLife = inventoryInfo.ShelfLife,
+                        ValidityPeriod = inventoryInfo.ValidityPeriod,
+                        Creater = "System"
+                    };
+                    _inventoryBatchRepository.AddData(inventory_Batch);
+                }
+                else
+                {
+                    inventory_Batch.BoxQuantity = inventory_Batch.BoxQuantity + inventoryInfo.BoxQuantity;
+                    inventory_Batch.StockQuantity = inventory_Batch.StockQuantity + inventoryInfo.StockQuantity;
+                    _inventoryBatchRepository.UpdateData(inventory_Batch);
+                }
+                BaseDal.DeleteAndMoveIntoHty(task, App.User.UserId == 0 ? OperateType.鑷姩瀹屾垚 : OperateType.浜哄伐瀹屾垚);
+                _basicService.LocationInfoService.Repository.UpdateData(locationInfo);
+                _stockService.InventoryInfoService.Repository.UpdateData(inventoryInfo);
+                Db.Ado.CommitTran();
+            }
+            catch (Exception ex)
+            {
+                Db.Ado.RollbackTran();
+            }
+            return content;
+        }
+        /// <summary>
+        /// 鏇存柊鍏ュ簱浠诲姟鐘舵�佸畬鎴�
         /// </summary>
         /// <param name="WMStask"></param>
         /// <returns></returns>
@@ -317,68 +855,75 @@
                 if (stockInfo.StockStatus != StockStatusEmun.鍏ュ簱涓�.ObjToInt()) throw new Exception($"鎵樼洏[{task.PalletCode}],璇ョ粍鐩樼姸鎬佷笉鍙叆搴�");
                 Dt_StockInfoDetail stockInfoDetail = stockInfo.Details.FirstOrDefault(x => x.StockId == stockInfo.Id);
 
+                List<Dt_StockInfo> stockInfos = _stockService.StockInfoService.Repository.LocationCodesGetStockInfos(new List<string> { stockInfo.LocationCode }).Where(x => x.StockStatus == StockStatusEmun.宸插叆搴�.ObjToInt()).ToList();
+                if (task.TaskType == TaskTypeEnum.PalletInbound.ObjToInt())
+                {
+                    //stockInfo.SerialNumber = stockInfos.Count + 1;
+                    stockInfo.InDate = DateTime.Now;
+                    stockInfo.StockStatus = StockStatusEmun.宸插叆搴�.ObjToInt();
+                    stockInfoDetail.Status = StockStatusEmun.宸插叆搴�.ObjToInt();
+                    locationInfo.LocationStatus = LocationStatusEnum.Pallet.ObjToInt();
+                    task.CurrentAddress = task.NextAddress;
+                    task.NextAddress = string.Empty;
+                    Db.Ado.BeginTran();
+                    BaseDal.DeleteAndMoveIntoHty(task, App.User.UserId == 0 ? OperateType.鑷姩瀹屾垚 : OperateType.浜哄伐瀹屾垚);
+                    _basicService.LocationInfoService.Repository.UpdateData(locationInfo);
+                    _stockService.StockInfoService.Repository.UpdateData(stockInfo);
+                    _stockService.StockInfoDetailService.Repository.UpdateData(stockInfoDetail);
+                    Db.Ado.CommitTran();
+                    return content;
+                }
+
+                Db.Ado.BeginTran();
                 #region 鍏ュ簱鍗�
                 Dt_InboundOrder inboundOrder = _inboundService.InbounOrderService.GetInboundOrder(stockInfoDetail.OrderNo);
-                if (inboundOrder == null || inboundOrder.Details == null) throw new Exception($"鏈壘鍒版墭鐩榌{task.PalletCode}]鐨勫叆搴撳崟鏄庣粏淇℃伅");
-                Dt_InboundOrderDetail inboundOrderDetail = inboundOrder.Details.FirstOrDefault(x => x.BatchNo == stockInfoDetail.BatchNo && x.MaterielCode == stockInfoDetail.MaterielCode);
-                inboundOrderDetail.OverInQuantity++;
-                inboundOrderDetail.OrderDetailStatus = inboundOrderDetail.OverInQuantity == inboundOrderDetail.OrderQuantity ? OrderDetailStatusEnum.Over.ObjToInt() : OrderDetailStatusEnum.GroupAndInbound.ObjToInt();
+                if (inboundOrder != null)
+                {
+                    Dt_InboundOrderDetail inboundOrderDetail = inboundOrder.Details.FirstOrDefault(x => x.BatchNo == stockInfoDetail.BatchNo && x.MaterielCode == stockInfoDetail.MaterielCode);
+                    if (inboundOrderDetail != null)
+                        inboundOrderDetail.OrderDetailStatus = inboundOrderDetail.OverInQuantity == inboundOrderDetail.OrderQuantity ? OrderDetailStatusEnum.Over.ObjToInt() : OrderDetailStatusEnum.GroupAndInbound.ObjToInt();
 
-                if (inboundOrder.Details.FirstOrDefault(x => x.OrderDetailStatus != OrderDetailStatusEnum.Over.ObjToInt()) == null)
-                {
-                    inboundOrder.OrderStatus = InboundStatusEnum.鍏ュ簱瀹屾垚.ObjToInt();
+                    if (inboundOrder.Details.FirstOrDefault(x => x.OrderDetailStatus != OrderDetailStatusEnum.Over.ObjToInt()) == null)
+                    {
+                        inboundOrder.OrderStatus = InboundStatusEnum.鍏ュ簱瀹屾垚.ObjToInt();
+                    }
+                    else if (inboundOrder.OrderStatus == InboundStatusEnum.鏈紑濮�.ObjToInt())
+                    {
+                        inboundOrder.OrderStatus = InboundStatusEnum.鍏ュ簱涓�.ObjToInt();
+                    }
+                    if (inboundOrder.OrderStatus != InboundStatusEnum.鍏ュ簱瀹屾垚.ObjToInt())
+                    {
+                        _inboundService.InbounOrderService.Repository.UpdateData(inboundOrder);
+                        if (inboundOrderDetail != null)
+                            _inboundService.InboundOrderDetailService.Repository.UpdateData(inboundOrderDetail);
+                    }
+                    else
+                    {
+                        List<Dt_InboundOrderDetail> orderDetails = inboundOrder.Details;
+                        inboundOrder.Details = null;
+                        _inboundService.InbounOrderService.Repository.DeleteAndMoveIntoHty(inboundOrder, OperateType.鑷姩瀹屾垚);
+                        _inboundService.InboundOrderDetailService.Repository.DeleteAndMoveIntoHty(orderDetails, OperateType.鑷姩瀹屾垚);
+                    }
                 }
-                else if (inboundOrder.OrderStatus == InboundStatusEnum.鏈紑濮�.ObjToInt())
-                {
-                    inboundOrder.OrderStatus = InboundStatusEnum.鍏ュ簱涓�.ObjToInt();
-                }
+
                 #endregion
-
-                #region 鍒ゆ柇鏄惁涓哄爢鍨涙満鍙栨斁璐т綅
-                if (task.IsPickPlace)
-                {
-
-                }
-                #endregion
-                List<Dt_StockInfo> stockInfos = _stockService.StockInfoService.Repository.LocationCodesGetStockInfos(new List<string> { stockInfo.LocationCode }).Where(x => x.StockStatus == StockStatusEmun.宸插叆搴�.ObjToInt()).ToList();
-                stockInfo.SerialNumber = stockInfos.Count + 1;
+                //stockInfo.SerialNumber = stockInfos.Count + 1;
                 stockInfo.InDate = DateTime.Now;
                 stockInfo.StockStatus = StockStatusEmun.宸插叆搴�.ObjToInt();
                 stockInfoDetail.Status = StockStatusEmun.宸插叆搴�.ObjToInt();
                 int beforeStatus = locationInfo.LocationStatus;
                 locationInfo.LocationStatus = locationInfo.MaxQty - locationInfo.CurrentQty == 0 ? LocationStatusEnum.Fullload.ObjToInt() : LocationStatusEnum.InStock.ObjToInt();
-
-                Db.Ado.BeginTran();
-                BaseDal.UpdateData(task);
+                task.CurrentAddress = task.NextAddress;
+                task.NextAddress = string.Empty;
+                BaseDal.DeleteAndMoveIntoHty(task, App.User.UserId == 0 ? OperateType.鑷姩瀹屾垚 : OperateType.浜哄伐瀹屾垚);
                 _basicService.LocationInfoService.Repository.UpdateData(locationInfo);
                 _stockService.StockInfoService.Repository.UpdateData(stockInfo);
                 _stockService.StockInfoDetailService.Repository.UpdateData(stockInfoDetail);
-                _inboundService.InbounOrderService.Repository.UpdateData(inboundOrder);
-                _inboundService.InboundOrderDetailService.Repository.UpdateData(inboundOrderDetail);
-                _recordService.LocationStatusChangeRecordSetvice.AddLocationStatusChangeRecord(locationInfo, beforeStatus, StockChangeType.Inbound.ObjToInt(), stockInfo.Details.FirstOrDefault()?.OrderNo ?? "", task.TaskNum);
                 Db.Ado.CommitTran();
             }
             catch (Exception ex)
             {
                 Db.Ado.RollbackTran();
-                content.Error(ex.Message);
-            }
-            return content;
-        }
-        /// <summary>
-        /// 鏇存柊浠诲姟鐘舵�佹墽琛�
-        /// </summary>
-        /// <param name="WMStask"></param>
-        /// <returns></returns>
-        public WebResponseContent UpdateTaskStatusExecuting(Dt_Task task)
-        {
-            WebResponseContent content = new WebResponseContent().OK();
-            try
-            {
-
-            }
-            catch (Exception ex)
-            {
                 content.Error(ex.Message);
             }
             return content;

--
Gitblit v1.9.3