dengjunjie
6 天以前 4f39dcc195f28fa275fc2d065fbf1bf6a46c21b7
´úÂë¹ÜÀí/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;
@@ -24,6 +25,8 @@
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Linq.Expressions;
using System.Net.Http.Headers;
using System.Reflection;
using System.Reflection.Metadata;
using System.Text;
@@ -33,6 +36,7 @@
using WIDESEA_Core.BaseServices;
using WIDESEA_Core.Enums;
using WIDESEA_Core.Helper;
using WIDESEA_Core.Log;
using WIDESEA_DTO.Inbound;
using WIDESEA_DTO.Stock;
using WIDESEA_IBasicRepository;
@@ -85,6 +89,7 @@
        /// <returns>返回处理结果</returns>
        public WebResponseContent TaskCompleted(int taskNum)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                Dt_Task task = BaseDal.QueryFirst(x => x.TaskNum == taskNum);
@@ -92,53 +97,206 @@
                {
                    return WebResponseContent.Instance.Error("未找到任务信息");
                }
                MethodInfo? methodInfo = GetType().GetMethod(((TaskTypeEnum)task.TaskType) + "TaskCompleted");
                _unitOfWorkManage.BeginTran();
                #region MyRegion
                switch ((TaskTypeEnum)task.TaskType)
                {
                    case TaskTypeEnum.Inbound:
                        content = InboundTaskCompleted(task);
                        break;
                    case TaskTypeEnum.Outbound:
                        content = OutboundTaskCompleted(task);
                        break;
                    case TaskTypeEnum.PalletInbound:
                        content = PalletInboundTaskCompleted(task);
                        break;
                    case TaskTypeEnum.PalletOutbound:
                        content = PalletOutboundTaskCompleted(task);
                        break;
                    case TaskTypeEnum.Relocation:
                        content = RelocationTaskCompleted(task);
                        break;
                    case TaskTypeEnum.AGVCarry:
                        break;
                    default:
                        return content = WebResponseContent.Instance.Error("未找到任务类型对应业务处理逻辑");
                }
                if (content.Status)
                {
                    if (App.User.UserId > 0)
                    {
                        content = HttpHelper.Post<WebResponseContent>(url + "StackCraneTaskCompleted?taskNum=" + task.TaskNum, "", "任务完成");
                        if (!content.Status) throw new Exception(content.Message);
                    }
                    _unitOfWorkManage.CommitTran();
                }
                return content;
                #endregion
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                return content = WebResponseContent.Instance.Error(ex.Message);
            }
            finally
            {
                WriteLog.GetLog("任务完成").Write($"操作人:{(App.User.UserId > 0 ? App.User.UserName : "System")}{Environment.NewLine}任务号:{taskNum}{Environment.NewLine}{JsonConvert.SerializeObject(content)}", "任务完成");
            }
        }
        /// <summary>
        /// ä»»åŠ¡å–æ¶ˆ
        /// </summary>
        /// <param name="taskNum"></param>
        /// <returns></returns>
        public WebResponseContent TaskCancel(int taskNum)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                Dt_Task task = BaseDal.QueryFirst(x => x.TaskNum == taskNum);
                if (task == null)
                {
                    return content = WebResponseContent.Instance.Error("未找到任务信息");
                }
                _unitOfWorkManage.BeginTran();
                MethodInfo? methodInfo = GetType().GetMethod(((TaskTypeEnum)task.TaskType) + "TaskCancel");
                if (methodInfo != null)
                {
                    WebResponseContent? responseContent = (WebResponseContent?)methodInfo.Invoke(this, new object[] { task });
                    if (responseContent != null)
                    {
                        return responseContent;
                        if (responseContent != null)
                        {
                            if (App.User.UserId > 0)
                            {
                                var response = HttpHelper.Post<WebResponseContent>(url + "TaskCancel?taskNum=" + task.TaskNum, "", "任务取消");
                                if (!response.Status)
                                {
                                    _unitOfWorkManage.RollbackTran();
                                    return content = WebResponseContent.Instance.Error(response.Message);
                                }
                                _unitOfWorkManage.CommitTran();
                                return content = responseContent;
                            }
                            _unitOfWorkManage.CommitTran();
                            return content = responseContent;
                        }
                    }
                }
                return WebResponseContent.Instance.Error("未找到任务类型对应业务处理逻辑");
                return content = WebResponseContent.Instance.Error("未找到任务类型对应业务处理逻辑");
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                return content = WebResponseContent.Instance.Error(ex.Message);
            }
            finally
            {
                WriteLog.GetLog("任务取消").Write($"操作人:{(App.User.UserId > 0 ? App.User.UserName : "System")}{Environment.NewLine}任务号:{taskNum}{Environment.NewLine}{JsonConvert.SerializeObject(content)}", "任务取消");
            }
        }
        public WebResponseContent UpdateTaskStatus(int taskNum)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                Dt_Task task = BaseDal.QueryFirst(x => x.TaskNum == taskNum);
                if (task == null)
                {
                    return WebResponseContent.Instance.Error("未找到任务信息");
                }
                if (task.TaskType == TaskTypeEnum.Outbound.ObjToInt() || task.TaskType == TaskTypeEnum.PalletOutbound.ObjToInt())
                {
                    task.TaskStatus = OutTaskStatusEnum.SC_OutExecuting.ObjToInt();
                }
                else if (task.TaskType == TaskTypeEnum.Inbound.ObjToInt() || task.TaskType == TaskTypeEnum.PalletInbound.ObjToInt())
                {
                    task.TaskStatus = InTaskStatusEnum.SC_InExecuting.ObjToInt();
                }
                else if (task.TaskType == TaskTypeEnum.Relocation.ObjToInt())
                {
                    task.TaskStatus = RelocationTaskStatusEnum.RelocationExecuting.ObjToInt();
                }
                else
                {
                    throw new Exception($"任务类型错误,未找到该任务类型,任务号:【{task.TaskNum}】,任务类型:【{task.TaskType}】");
                }
                _unitOfWorkManage.BeginTran();
                task.Dispatchertime = DateTime.Now;
                task.ModifyDate = DateTime.Now;
                task.Modifier = App.User.UserId > 0 ? App.User.UserName : "System";
                BaseDal.UpdateData(task);
                _unitOfWorkManage.CommitTran();
                return content = WebResponseContent.Instance.OK();
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
        public WebResponseContent InboundTaskCompleted(Dt_Task task)
        {
            try
            {
                decimal beforeQuantity = 0;
                Dt_StockInfo stockInfo = _stockService.StockInfoService.Repository.GetStockInfo(task.PalletCode);
                Dt_StockInfoDetail stockInfoDetail = stockInfo.Details.FirstOrDefault();
                Dt_LocationInfo locationInfo = _basicService.LocationInfoService.Repository.QueryFirst(x => x.LocationCode == task.TargetAddress);
                CheckCompleted(stockInfo, locationInfo);
                Dt_InboundOrder inboundOrder = _inboundService.InbounOrderService.GetInboundOrder(stockInfoDetail.OrderNo);
                if (inboundOrder != null)
                {
                    Dt_InboundOrderDetail? inboundOrderDetail = inboundOrder.Details.Where(x => x.BatchNo == stockInfoDetail.BatchNo).FirstOrDefault();
                    if (inboundOrderDetail != null)
                    {
                        inboundOrderDetail.OverInQuantity++;
                        inboundOrderDetail.OrderDetailStatus = inboundOrderDetail.OverInQuantity == inboundOrderDetail.OrderQuantity ? OrderDetailStatusEnum.Over.ObjToInt() : OrderDetailStatusEnum.GroupAndInbound.ObjToInt();
                        if (inboundOrderDetail.OrderDetailStatus == OrderDetailStatusEnum.Over.ObjToInt())
                        {
                            if (inboundOrder.Details.FirstOrDefault(x => x.OrderDetailStatus != OrderDetailStatusEnum.Over.ObjToInt() && x.BatchNo != inboundOrderDetail.BatchNo) == null)
                            {
                                inboundOrder.OrderStatus = InboundStatusEnum.入库完成.ObjToInt();
                                _inboundService.InbounOrderService.Repository.DeleteAndMoveIntoHty(inboundOrder, App.User.UserId == 0 ? OperateType.自动完成 : OperateType.人工完成);
                                _inboundService.InboundOrderDetailService.Repository.DeleteAndMoveIntoHty(inboundOrder.Details, App.User.UserId == 0 ? OperateType.自动完成 : OperateType.人工完成);
                            }
                        }
                        _inboundService.InboundOrderDetailService.Repository.UpdateData(inboundOrderDetail);
                    }
                    _inboundService.InbounOrderService.Repository.UpdateData(inboundOrder);
                }
                stockInfo.LocationCode = locationInfo.LocationCode;
                stockInfo.StockStatus = StockStatusEmun.已入库.ObjToInt();
                _stockService.StockInfoService.Repository.UpdateData(stockInfo);
                beforeQuantity = stockInfo.Details.Where(x => x.Id != 0).Sum(x => x.StockQuantity);
                int beforeStatus = locationInfo.LocationStatus;
                locationInfo.LocationStatus = LocationStatusEnum.InStock.ObjToInt();
                _basicService.LocationInfoService.UpdateLocationFree(locationInfo, task.TaskNum, StockChangeType.Inbound.ObjToInt(), false);
                _basicService.LocationInfoService.Repository.UpdateData(locationInfo);
                task.TaskStatus = InTaskStatusEnum.InFinish.ObjToInt();
                BaseDal.DeleteAndMoveIntoHty(task, App.User.UserId == 0 ? OperateType.自动完成 : OperateType.人工完成);
                _recordService.LocationStatusChangeRecordSetvice.AddLocationStatusChangeRecord(locationInfo, beforeStatus, StockChangeType.Inbound.ObjToInt(), stockInfo.Details.FirstOrDefault()?.OrderNo ?? "", task.TaskNum);
                _recordService.StockQuantityChangeRecordService.AddStockChangeRecord(stockInfo, stockInfo.Details, beforeQuantity, stockInfo.Details.Sum(x => x.StockQuantity) + beforeQuantity, StockChangeType.Inbound, task.TaskNum);
                return WebResponseContent.Instance.OK();
            }
            catch (Exception ex)
            {
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
        public WebResponseContent InboundTaskCompleted(Dt_Task task)
        {
            decimal beforeQuantity = 0;
            Dt_StockInfo stockInfo = _stockService.StockInfoService.Repository.GetStockInfo(task.PalletCode);
            Dt_LocationInfo locationInfo = _basicService.LocationInfoService.Repository.QueryFirst(x => x.LocationCode == task.TargetAddress);
            CheckCompleted(stockInfo, locationInfo);
            stockInfo.LocationCode = locationInfo.LocationCode;
            stockInfo.StockStatus = StockStatusEmun.已入库.ObjToInt();
            _stockService.StockInfoService.Repository.UpdateData(stockInfo);
            beforeQuantity = stockInfo.Details.Where(x => x.Id != 0).Sum(x => x.StockQuantity);
            int beforeStatus = locationInfo.LocationStatus;
            locationInfo.LocationStatus = LocationStatusEnum.InStock.ObjToInt();
            _basicService.LocationInfoService.UpdateLocationFree(locationInfo, task.TaskNum, StockChangeType.Inbound.ObjToInt(),false);
            _basicService.LocationInfoService.Repository.UpdateData(locationInfo);
            task.TaskStatus = InTaskStatusEnum.InFinish.ObjToInt();
            BaseDal.DeleteAndMoveIntoHty(task, App.User.UserId == 0 ? OperateType.自动完成 : OperateType.人工完成);
            _recordService.LocationStatusChangeRecordSetvice.AddLocationStatusChangeRecord(locationInfo, beforeStatus, StockChangeType.Inbound.ObjToInt(), stockInfo.Details.FirstOrDefault()?.OrderNo ?? "", task.TaskNum);
            _recordService.StockQuantityChangeRecordService.AddStockChangeRecord(stockInfo, stockInfo.Details, beforeQuantity, stockInfo.Details.Sum(x => x.StockQuantity) + beforeQuantity, StockChangeType.Inbound);
            return WebResponseContent.Instance.OK();
        }
        /// <summary>
@@ -148,31 +306,42 @@
        /// <returns>返回处理结果</returns>
        public WebResponseContent PalletInboundTaskCompleted(Dt_Task task)
        {
            Dt_StockInfo stockInfo = new Dt_StockInfo()
            try
            {
                PalletCode = task.PalletCode,
                LocationCode = task.TargetAddress,
                StockStatus = StockStatusEmun.已入库.ObjToInt()
            };
            Dt_LocationInfo locationInfo = _basicService.LocationInfoService.Repository.QueryFirst(x => x.LocationCode == task.TargetAddress);
                _unitOfWorkManage.BeginTran();
            CheckCompleted(stockInfo, locationInfo);
                Dt_StockInfo stockInfo = new Dt_StockInfo()
                {
                    PalletCode = task.PalletCode,
                    LocationCode = task.TargetAddress,
                    StockStatus = StockStatusEmun.已入库.ObjToInt(),
                    Creater = App.User.UserId > 0 ? App.User.UserName : "System",
                    CreateDate = DateTime.Now,
                };
                Dt_LocationInfo locationInfo = _basicService.LocationInfoService.Repository.QueryFirst(x => x.LocationCode == task.TargetAddress);
            _stockService.StockInfoService.Repository.AddData(stockInfo);
                CheckCompleted(stockInfo, locationInfo);
            int beforeStatus = locationInfo.LocationStatus;
            locationInfo.LocationStatus = LocationStatusEnum.Pallet.ObjToInt();
            _basicService.LocationInfoService.UpdateLocationFree(locationInfo, task.TaskNum, StockChangeType.Inbound.ObjToInt(), false);
            _basicService.LocationInfoService.Repository.UpdateData(locationInfo);
                _stockService.StockInfoService.Repository.AddData(stockInfo);
            task.TaskStatus = InTaskStatusEnum.InFinish.ObjToInt();
            BaseDal.DeleteAndMoveIntoHty(task, App.User.UserId == 0 ? OperateType.自动完成 : OperateType.人工完成);
                int beforeStatus = locationInfo.LocationStatus;
                locationInfo.LocationStatus = LocationStatusEnum.Pallet.ObjToInt();
                _basicService.LocationInfoService.UpdateLocationFree(locationInfo, task.TaskNum, StockChangeType.Inbound.ObjToInt(), false);
                _basicService.LocationInfoService.Repository.UpdateData(locationInfo);
            _recordService.LocationStatusChangeRecordSetvice.AddLocationStatusChangeRecord(locationInfo, beforeStatus, StockChangeType.Inbound.ObjToInt(), "", task.TaskNum);
                task.TaskStatus = InTaskStatusEnum.InFinish.ObjToInt();
                BaseDal.DeleteAndMoveIntoHty(task, App.User.UserId == 0 ? OperateType.自动完成 : OperateType.人工完成);
            return WebResponseContent.Instance.OK();
                _recordService.LocationStatusChangeRecordSetvice.AddLocationStatusChangeRecord(locationInfo, beforeStatus, StockChangeType.Inbound.ObjToInt(), "", task.TaskNum);
                _unitOfWorkManage.CommitTran();
                return WebResponseContent.Instance.OK();
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
        /// <summary>
@@ -203,113 +372,360 @@
        public WebResponseContent OutboundTaskCompleted(Dt_Task task)
        {
            decimal beforeQuantity = 0;
            Dt_StockInfo stockInfo = _stockService.StockInfoService.Repository.GetStockInfo(task.PalletCode);
            Dt_LocationInfo locationInfo = _basicService.LocationInfoService.Repository.QueryFirst(x => x.LocationCode == task.SourceAddress);
            CheckCompleted(stockInfo, locationInfo);
            stockInfo.LocationCode = locationInfo.LocationCode;
            stockInfo.StockStatus = StockStatusEmun.出库完成.ObjToInt();
            _stockService.StockInfoService.Repository.UpdateData(stockInfo);
            int beforeStatus = locationInfo.LocationStatus;
            if (task.Depth == 1)
            try
            {
                if (DepthTask(task)) { }
                else
                decimal beforeQuantity = 0;
                Dt_StockInfo stockInfo = _stockService.StockInfoService.Repository.GetStockInfo(task.PalletCode);
                Dt_LocationInfo locationInfo = _basicService.LocationInfoService.Repository.QueryFirst(x => x.LocationCode == task.SourceAddress);
                CheckCompleted(stockInfo, locationInfo);
                stockInfo.LocationCode = locationInfo.LocationCode;
                stockInfo.StockStatus = StockStatusEmun.出库完成.ObjToInt();
                _stockService.StockInfoService.Repository.UpdateData(stockInfo);
                int beforeStatus = locationInfo.LocationStatus;
                if (locationInfo.Depth == 2)
                {
                    locationInfo.LocationStatus = LocationStatusEnum.Free.ObjToInt();
                    _basicService.LocationInfoService.UpdateLocationFree(locationInfo, task.TaskNum, StockChangeType.Outbound.ObjToInt(), false);
                    _basicService.LocationInfoService.Repository.UpdateData(locationInfo);
                    if (DepthTask(locationInfo) == false)
                    {
                        _basicService.LocationInfoService.UpdateLocationFree(locationInfo, task.TaskNum, StockChangeType.Outbound.ObjToInt(), false);
                    }
                }
            }
            else
            {
                locationInfo.LocationStatus = LocationStatusEnum.Free.ObjToInt();
                _basicService.LocationInfoService.UpdateLocationFree(locationInfo, task.TaskNum, StockChangeType.Outbound.ObjToInt(), false);
                _basicService.LocationInfoService.Repository.UpdateData(locationInfo);
                task.TaskStatus = OutTaskStatusEnum.OutFinish.ObjToInt();
                BaseDal.DeleteAndMoveIntoHty(task, App.User.UserId == 0 ? OperateType.自动完成 : OperateType.人工完成);
                _stockService.StockInfoService.Repository.DeleteAndMoveIntoHty(stockInfo, App.User.UserId == 0 ? OperateType.自动完成 : OperateType.人工完成);
                _stockService.StockInfoDetailService.Repository.DeleteAndMoveIntoHty(stockInfo.Details, App.User.UserId == 0 ? OperateType.自动完成 : OperateType.人工完成);
                beforeQuantity = stockInfo.Details.Where(x => x.Id != 0).Sum(x => x.StockQuantity);
                _recordService.LocationStatusChangeRecordSetvice.AddLocationStatusChangeRecord(locationInfo, beforeStatus, StockChangeType.Outbound.ObjToInt(), stockInfo.Details.FirstOrDefault()?.OrderNo ?? "", task.TaskNum);
                _recordService.StockQuantityChangeRecordService.AddStockChangeRecord(stockInfo, stockInfo.Details, beforeQuantity, stockInfo.Details.Sum(x => x.StockQuantity) - beforeQuantity, StockChangeType.Outbound, task.TaskNum);
                return OnOutboundTaskCompleted?.Invoke(task) ?? WebResponseContent.Instance.OK();
            }
            task.TaskStatus = OutTaskStatusEnum.OutFinish.ObjToInt();
            BaseDal.DeleteAndMoveIntoHty(task, App.User.UserId == 0 ? OperateType.自动完成 : OperateType.人工完成);
            _stockService.StockInfoService.Repository.DeleteAndMoveIntoHty(stockInfo, App.User.UserId == 0 ? OperateType.自动完成 : OperateType.人工完成);
            _stockService.StockInfoDetailService.Repository.DeleteAndMoveIntoHty(stockInfo.Details, App.User.UserId == 0 ? OperateType.自动完成 : OperateType.人工完成);
            beforeQuantity = stockInfo.Details.Where(x => x.Id != 0).Sum(x => x.StockQuantity);
            _recordService.LocationStatusChangeRecordSetvice.AddLocationStatusChangeRecord(locationInfo, beforeStatus, StockChangeType.Outbound.ObjToInt(), stockInfo.Details.FirstOrDefault()?.OrderNo ?? "", task.TaskNum);
            _recordService.StockQuantityChangeRecordService.AddStockChangeRecord(stockInfo, stockInfo.Details, beforeQuantity, stockInfo.Details.Sum(x => x.StockQuantity) - beforeQuantity, StockChangeType.Outbound);
            return OnOutboundTaskCompleted?.Invoke(task) ?? WebResponseContent.Instance.OK();
            catch (Exception ex)
            {
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
        public WebResponseContent PalletOutboundTaskCompleted(Dt_Task task)
        {
            Dt_StockInfo stockInfo = _stockService.StockInfoService.Repository.GetStockInfo(task.PalletCode);
            try
            {
                _unitOfWorkManage.BeginTran();
                Dt_StockInfo stockInfo = _stockService.StockInfoService.Repository.GetStockInfo(task.PalletCode);
            Dt_LocationInfo locationInfo = _basicService.LocationInfoService.Repository.QueryFirst(x => x.LocationCode == task.SourceAddress);
                Dt_LocationInfo locationInfo = _basicService.LocationInfoService.Repository.QueryFirst(x => x.LocationCode == task.SourceAddress);
            CheckCompleted(stockInfo, locationInfo);
                CheckCompleted(stockInfo, locationInfo);
            _stockService.StockInfoService.Repository.DeleteData(stockInfo);
                int beforeStatus = locationInfo.LocationStatus;
            int beforeStatus = locationInfo.LocationStatus;
                if (locationInfo.Depth == 2)
                {
                    if (DepthTask(locationInfo) == false)
                    {
                        _basicService.LocationInfoService.UpdateLocationFree(locationInfo, task.TaskNum, StockChangeType.Outbound.ObjToInt(), false);
                    }
                }
                locationInfo.LocationStatus = LocationStatusEnum.Free.ObjToInt();
                _basicService.LocationInfoService.Repository.UpdateData(locationInfo);
            locationInfo.LocationStatus = LocationStatusEnum.Free.ObjToInt();
            _basicService.LocationInfoService.UpdateLocationFree(locationInfo, task.TaskNum, StockChangeType.Outbound.ObjToInt(), false);
            _basicService.LocationInfoService.Repository.UpdateData(locationInfo);
                task.TaskStatus = OutTaskStatusEnum.OutFinish.ObjToInt();
                BaseDal.DeleteAndMoveIntoHty(task, App.User.UserId == 0 ? OperateType.自动完成 : OperateType.人工完成);
                _stockService.StockInfoService.Repository.DeleteAndMoveIntoHty(stockInfo, App.User.UserId == 0 ? OperateType.自动完成 : OperateType.人工完成);
                _recordService.LocationStatusChangeRecordSetvice.AddLocationStatusChangeRecord(locationInfo, beforeStatus, StockChangeType.Outbound.ObjToInt(), stockInfo.Details.FirstOrDefault()?.OrderNo ?? "", task.TaskNum);
            task.TaskStatus = OutTaskStatusEnum.OutFinish.ObjToInt();
            BaseDal.DeleteAndMoveIntoHty(task, App.User.UserId == 0 ? OperateType.自动完成 : OperateType.人工完成);
            _stockService.StockInfoService.Repository.DeleteAndMoveIntoHty(stockInfo, App.User.UserId == 0 ? OperateType.自动完成 : OperateType.人工完成);
            _recordService.LocationStatusChangeRecordSetvice.AddLocationStatusChangeRecord(locationInfo, beforeStatus, StockChangeType.Outbound.ObjToInt(), stockInfo.Details.FirstOrDefault()?.OrderNo ?? "", task.TaskNum);
            return WebResponseContent.Instance.OK();
                _unitOfWorkManage.CommitTran();
                return WebResponseContent.Instance.OK();
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
        public WebResponseContent RelocationTaskCompleted(Dt_Task task)
        {
            Dt_StockInfo stockInfo = _stockService.StockInfoService.Repository.GetStockInfo(task.PalletCode);
            Dt_LocationInfo locationEnd = _basicService.LocationInfoService.Repository.QueryFirst(x => x.LocationCode == task.TargetAddress);
            CheckCompleted(stockInfo, locationEnd);
            stockInfo.LocationCode = locationEnd.LocationCode;
            stockInfo.StockStatus = StockStatusEmun.已入库.ObjToInt();
            _stockService.StockInfoService.Repository.UpdateData(stockInfo);
            _basicService.LocationInfoService.RelocationFree(locationEnd, task.TaskNum);
            task.TaskStatus = InTaskStatusEnum.RelocationFinish.ObjToInt();
            BaseDal.DeleteAndMoveIntoHty(task, App.User.UserId == 0 ? OperateType.自动完成 : OperateType.人工完成);
            return WebResponseContent.Instance.OK();
        }
        public bool DepthTask(Dt_Task task)
        {
            Dt_LocationInfo location=_locationInfoService.Repository.QueryFirst(x=>x.LocationCode == task.SourceAddress);
            if(location != null)
            try
            {
                if(location.Row%2==0)
                _unitOfWorkManage.BeginTran();
                Dt_StockInfo stockInfo = _stockService.StockInfoService.Repository.GetStockInfo(task.PalletCode);
                Dt_LocationInfo locationEnd = _basicService.LocationInfoService.Repository.QueryFirst(x => x.LocationCode == task.TargetAddress);
                Dt_LocationInfo locationStart = _basicService.LocationInfoService.Repository.QueryFirst(x => x.LocationCode == task.SourceAddress);
                CheckCompleted(stockInfo, locationEnd);
                stockInfo.LocationCode = locationEnd.LocationCode;
                stockInfo.StockStatus = StockStatusEmun.已入库.ObjToInt();
                _stockService.StockInfoService.Repository.UpdateData(stockInfo);
                if (locationStart.Depth == 1)
                {
                    Dt_LocationInfo locations = _locationInfoService.Repository.QueryFirst(x => x.Row == location.Row + 1 && x.Layer == location.Layer && x.Column == location.Column && x.RoadwayNo == location.RoadwayNo);
                    Dt_Task tasks=BaseDal.QueryFirst(x=>x.SourceAddress == locations.LocationCode);
                    return tasks != null ? true : false;
                    if (DepthTask(locationStart) == false)
                    {
                        locationStart.LocationStatus = LocationStatusEnum.Free.ObjToInt();
                        _basicService.LocationInfoService.RelocationFree(locationEnd, task.TaskNum);
                        _basicService.LocationInfoService.Repository.UpdateData(locationStart);
                    }
                    else
                    {
                        locationStart.LocationStatus = LocationStatusEnum.Free.ObjToInt();
                        _basicService.LocationInfoService.Repository.UpdateData(locationStart);
                    }
                }
                else
                _basicService.LocationInfoService.RelocationFree(locationEnd, task.TaskNum);
                task.TaskStatus = RelocationTaskStatusEnum.RelocationFinish.ObjToInt();
                BaseDal.DeleteAndMoveIntoHty(task, App.User.UserId == 0 ? OperateType.自动完成 : OperateType.人工完成);
                _unitOfWorkManage.CommitTran();
                return WebResponseContent.Instance.OK();
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
        public WebResponseContent InboundTaskCancel(Dt_Task task)
        {
            try
            {
                _unitOfWorkManage.BeginTran();
                decimal beforeQuantity = 0;
                Dt_StockInfo stockInfo = _stockService.StockInfoService.Repository.GetStockInfo(task.PalletCode);
                Dt_LocationInfo locationInfo = _basicService.LocationInfoService.Repository.QueryFirst(x => x.LocationCode == task.TargetAddress);
                CheckCompleted(stockInfo, locationInfo);
                stockInfo.StockStatus = StockStatusEmun.组盘暂存.ObjToInt();
                _stockService.StockInfoService.Repository.UpdateData(stockInfo);
                beforeQuantity = stockInfo.Details.Where(x => x.Id != 0).Sum(x => x.StockQuantity);
                int beforeStatus = locationInfo.LocationStatus;
                locationInfo.LocationStatus = LocationStatusEnum.Free.ObjToInt();
                if (locationInfo.Depth == 2)
                {
                    Dt_LocationInfo locations = _locationInfoService.Repository.QueryFirst(x => x.Row == location.Row - 1 && x.Layer == location.Layer && x.Column == location.Column && x.RoadwayNo == location.RoadwayNo);
                    _basicService.LocationInfoService.UpdateLocationFree(locationInfo, task.TaskNum, StockChangeType.Outbound.ObjToInt(), false);
                }
                _basicService.LocationInfoService.Repository.UpdateData(locationInfo);
                task.TaskStatus = InTaskStatusEnum.InCancel.ObjToInt();
                BaseDal.DeleteAndMoveIntoHty(task, App.User.UserId == 0 ? OperateType.自动完成 : OperateType.人工完成);
                _recordService.LocationStatusChangeRecordSetvice.AddLocationStatusChangeRecord(locationInfo, beforeStatus, StockChangeType.Inbound.ObjToInt(), stockInfo.Details.FirstOrDefault()?.OrderNo ?? "", task.TaskNum);
                _unitOfWorkManage.CommitTran();
                return WebResponseContent.Instance.OK();
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
        public WebResponseContent PalletInboundTaskCancel(Dt_Task task)
        {
            try
            {
                _unitOfWorkManage.BeginTran();
                Dt_LocationInfo locationInfo = _basicService.LocationInfoService.Repository.QueryFirst(x => x.LocationCode == task.TargetAddress);
                int beforeStatus = locationInfo.LocationStatus;
                locationInfo.LocationStatus = LocationStatusEnum.Free.ObjToInt();
                if (locationInfo.Depth == 2)
                {
                    _basicService.LocationInfoService.UpdateLocationFree(locationInfo, task.TaskNum, StockChangeType.Outbound.ObjToInt(), false);
                }
                _basicService.LocationInfoService.Repository.UpdateData(locationInfo);
                task.TaskStatus = InTaskStatusEnum.InCancel.ObjToInt();
                BaseDal.DeleteAndMoveIntoHty(task, App.User.UserId == 0 ? OperateType.自动完成 : OperateType.人工完成);
                _recordService.LocationStatusChangeRecordSetvice.AddLocationStatusChangeRecord(locationInfo, beforeStatus, StockChangeType.Inbound.ObjToInt(), "", task.TaskNum);
                _unitOfWorkManage.CommitTran();
                return WebResponseContent.Instance.OK();
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
        public WebResponseContent OutboundTaskCancel(Dt_Task task)
        {
            try
            {
                _unitOfWorkManage.BeginTran();
                decimal beforeQuantity = 0;
                Dt_StockInfo stockInfo = _stockService.StockInfoService.Repository.GetStockInfo(task.PalletCode);
                Dt_StockInfoDetail stockInfoDetail = _stockService.StockInfoDetailService.Repository.QueryFirst(x => x.StockId == stockInfo.Id);
                Dt_LocationInfo locationInfo = _basicService.LocationInfoService.Repository.QueryFirst(x => x.LocationCode == task.SourceAddress);
                CheckCompleted(stockInfo, locationInfo);
                stockInfo.LocationCode = locationInfo.LocationCode;
                stockInfo.StockStatus = StockStatusEmun.已入库.ObjToInt();
                stockInfoDetail.OutboundQuantity = beforeQuantity;
                _stockService.StockInfoService.Repository.UpdateData(stockInfo);
                _stockService.StockInfoDetailService.Repository.UpdateData(stockInfoDetail);
                int beforeStatus = locationInfo.LocationStatus;
                if (locationInfo.Depth == 2)
                {
                    if (DepthTask(locationInfo) == false)
                    {
                        _basicService.LocationInfoService.UpdateLocationFree(locationInfo, task.TaskNum, StockChangeType.Outbound.ObjToInt(), true);
                    }
                }
                locationInfo.LocationStatus = LocationStatusEnum.InStock.ObjToInt();
                _basicService.LocationInfoService.Repository.UpdateData(locationInfo);
                task.TaskStatus = OutTaskStatusEnum.OutCancel.ObjToInt();
                BaseDal.DeleteAndMoveIntoHty(task, App.User.UserId == 0 ? OperateType.自动完成 : OperateType.人工完成);
                beforeQuantity = stockInfo.Details.Where(x => x.Id != 0).Sum(x => x.StockQuantity);
                _recordService.LocationStatusChangeRecordSetvice.AddLocationStatusChangeRecord(locationInfo, beforeStatus, StockChangeType.Outbound.ObjToInt(), stockInfo.Details.FirstOrDefault()?.OrderNo ?? "", task.TaskNum);
                _recordService.StockQuantityChangeRecordService.AddStockChangeRecord(stockInfo, stockInfo.Details, beforeQuantity, stockInfo.Details.Sum(x => x.StockQuantity) - beforeQuantity, StockChangeType.Outbound, task.TaskNum);
                _unitOfWorkManage.CommitTran();
                return WebResponseContent.Instance.OK();
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
        public WebResponseContent PalletOutboundTaskCancel(Dt_Task task)
        {
            try
            {
                _unitOfWorkManage.BeginTran();
                Dt_StockInfo stockInfo = _stockService.StockInfoService.Repository.GetStockInfo(task.PalletCode);
                Dt_LocationInfo locationInfo = _basicService.LocationInfoService.Repository.QueryFirst(x => x.LocationCode == task.SourceAddress);
                CheckCompleted(stockInfo, locationInfo);
                int beforeStatus = locationInfo.LocationStatus;
                if (locationInfo.Depth == 2)
                {
                    if (DepthTask(locationInfo) == false)
                    {
                        _basicService.LocationInfoService.UpdateLocationFree(locationInfo, task.TaskNum, StockChangeType.Outbound.ObjToInt(), true);
                    }
                }
                locationInfo.LocationStatus = LocationStatusEnum.Pallet.ObjToInt();
                _basicService.LocationInfoService.Repository.UpdateData(locationInfo);
                stockInfo.StockStatus = StockStatusEmun.已入库.ObjToInt();
                _stockService.StockInfoService.Repository.UpdateData(stockInfo);
                task.TaskStatus = OutTaskStatusEnum.OutCancel.ObjToInt();
                BaseDal.DeleteAndMoveIntoHty(task, App.User.UserId == 0 ? OperateType.自动完成 : OperateType.人工完成);
                _recordService.LocationStatusChangeRecordSetvice.AddLocationStatusChangeRecord(locationInfo, beforeStatus, StockChangeType.Outbound.ObjToInt(), stockInfo.Details.FirstOrDefault()?.OrderNo ?? "", task.TaskNum);
                _unitOfWorkManage.CommitTran();
                return WebResponseContent.Instance.OK();
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
        public WebResponseContent RelocationTaskCancel(Dt_Task task)
        {
            try
            {
                _unitOfWorkManage.BeginTran();
                Dt_StockInfo stockInfo = _stockService.StockInfoService.Repository.GetStockInfo(task.PalletCode);
                Dt_LocationInfo locationEnd = _basicService.LocationInfoService.Repository.QueryFirst(x => x.LocationCode == task.TargetAddress);
                Dt_LocationInfo locationStart = _basicService.LocationInfoService.Repository.QueryFirst(x => x.LocationCode == task.SourceAddress);
                stockInfo.StockStatus = StockStatusEmun.已入库.ObjToInt();
                _stockService.StockInfoService.Repository.UpdateData(stockInfo);
                locationEnd.LocationStatus = LocationStatusEnum.Free.ObjToInt();
                _basicService.LocationInfoService.RelocationFree(locationStart, task.TaskNum);
                _basicService.LocationInfoService.Repository.UpdateData(locationEnd);
                task.TaskStatus = RelocationTaskStatusEnum.RelocationCancel.ObjToInt();
                BaseDal.DeleteAndMoveIntoHty(task, App.User.UserId == 0 ? OperateType.自动完成 : OperateType.人工完成);
                _unitOfWorkManage.CommitTran();
                return WebResponseContent.Instance.OK();
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
        public bool DepthTask(Dt_LocationInfo location)
        {
            if (location != null)
            {
                Dt_LocationInfo locations = _locationInfoService.Repository.QueryFirst(x => x.RoadwayNo == location.RoadwayNo && x.Column == location.Column && x.Layer == location.Layer && x.Depth != location.Depth && x.Row != location.Row && (SqlFunc.Abs(x.Row - location.Row) == 1));
                if (locations != null)
                {
                    Dt_Task tasks = BaseDal.QueryFirst(x => x.SourceAddress == locations.LocationCode);
                    return tasks != null ? true : false;
                }
            }
            return false;
        }
        public WebResponseContent GetTaskInfo()
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                Expression<Func<Dt_Task, bool>> expression = x => true;
                if (!App.User.IsSuperAdmin)
                {
                    expression = x => x.Creater == App.User.UserName;
                }
                var task = BaseDal.Db.Queryable<Dt_Task>().OrderByDescending(x => x.CreateDate).Take(10).Select(x => new Dt_Task { TaskNum = x.TaskNum, PalletCode = x.PalletCode, TaskType = x.TaskType, SourceAddress = x.SourceAddress, TargetAddress = x.TargetAddress }).ToList();
                content = WebResponseContent.Instance.OK(data: task);
            }
            catch (Exception ex)
            {
                content = WebResponseContent.Instance.Error(ex.Message);
            }
            return content;
        }
    }
}