wangxinhui
2025-10-17 ce40df5daffae0d17b4e9fa7cb6d677afaa4d66f
ÏîÄ¿´úÂë/WMS/WMSServices/WIDESEA_TaskInfoService/TaskService_Outbound.cs
@@ -229,7 +229,7 @@
        /// æˆå“å‡ºåº“
        /// </summary>
        /// <returns></returns>
        public WebResponseContent RequestCPWMSTaskOut(string materialCode,int Count, int targetId)
        public WebResponseContent RequestCPWMSTaskOut(string materialCode,int Count, string targetId)
        {
            WebResponseContent content = new WebResponseContent();
            try
@@ -241,13 +241,19 @@
                }
                //获取货位
                List<Dt_LocationInfo> locationInfos = _basicRepository.LocationInfoRepository.QueryData(x => x.WarehouseId == WarehouseEnum.LLDCP.ObjToInt() && (x.EnableStatus == EnableStatusEnum.OnlyOut.ObjToInt() || x.EnableStatus == EnableStatusEnum.Normal.ObjToInt()) && x.LocationStatus == LocationStatusEnum.InStock.ObjToInt());
                //获取可用库存
                List<Dt_ProStockInfo> stockInfos = BaseDal.Db.Queryable<Dt_ProStockInfo>().Where(x => locationInfos.Select(x => x.LocationCode).Contains(x.LocationCode) && x.StockStatus == StockStatusEmun.入库完成.ObjToInt()).Includes(x => x.proStockInfoDetails)
                .Where(x => x.proStockInfoDetails
                .Any(v =>
                    v.ProductCode == materialCode)
                ).Take(Count).ToList();
                List<Dt_ProStockInfo> stockInfos = new List<Dt_ProStockInfo>();
                if (materialCode=="1")
                {
                    stockInfos = BaseDal.Db.Queryable<Dt_ProStockInfo>().Where(x => locationInfos.Select(x => x.LocationCode).Contains(x.LocationCode) && x.StockStatus == StockStatusEmun.入库完成.ObjToInt() && x.ProStockAttribute==ProStockAttributeEnum.空托.ObjToInt()).Take(Count).ToList();
                }
                else
                {
                    stockInfos = BaseDal.Db.Queryable<Dt_ProStockInfo>().Where(x => locationInfos.Select(x => x.LocationCode).Contains(x.LocationCode) && x.StockStatus == StockStatusEmun.入库完成.ObjToInt()).Includes(x => x.proStockInfoDetails)
                    .Where(x => x.proStockInfoDetails
                    .Any(v =>
                        v.ProductCode == materialCode)
                    ).Take(Count).ToList();
                }
                if (stockInfos.Count!=Count)
                {
@@ -265,10 +271,15 @@
                }
                TaskTypeEnum typeEnum = targetId switch
                {
                    (int)AGVStationAreaEnum.AreaA => TaskTypeEnum.OutProduct,
                    (int)AGVStationAreaEnum.AreaC => TaskTypeEnum.OutWFB,
                    nameof(AGVStationAreaEnum.一楼月台码头) => TaskTypeEnum.OutProduct,
                    nameof(AGVStationAreaEnum.一楼无纺织布) => TaskTypeEnum.OutWFB,
                    nameof(AGVStationAreaEnum.一楼无纺淋膜) => TaskTypeEnum.OutWFBLM,
                    _ => throw new Exception("未找到对应任务")
                };
                if (materialCode == "1")
                {
                    typeEnum = TaskTypeEnum.OutEmpty;
                }
                List<Dt_Task> tasks = GetTasks(stockInfos, typeEnum, AssignLocations);
                if (tasks == null || tasks.Count <= 0)
                {
@@ -525,5 +536,151 @@
            }
        }
        /// <summary>
        /// åˆ›å»ºåŽŸçº¸ç”Ÿç®¡æŽ’ç¨‹å‡ºåº“ä»»åŠ¡
        /// </summary>
        /// <param name="keys"></param>
        /// <returns></returns>
        public WebResponseContent CreateSGOutboundTasks(int[] keys)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                List<Dt_Task> tasks = new List<Dt_Task>();
                List<Dt_StockInfo> stockInfos = new List<Dt_StockInfo>();
                List<Dt_OutSGOrderDetail> outSGOrderDetails = new List<Dt_OutSGOrderDetail>();
                List<Dt_OutStockLockInfo> outStockLockInfos = new List<Dt_OutStockLockInfo>();
                List<Dt_LocationInfo> locationInfos = new List<Dt_LocationInfo>();
                //生成任务、库存等信息
                (List<Dt_Task>, List<Dt_StockInfo>?, List<Dt_OutSGOrderDetail>?, List<Dt_OutStockLockInfo>?, List<Dt_LocationInfo>?) result = OutSGTaskDataHandle(keys);
                if (result.Item2 != null && result.Item2.Count > 0)
                {
                    stockInfos.AddRange(result.Item2);
                }
                if (result.Item3 != null && result.Item3.Count > 0)
                {
                    outSGOrderDetails.AddRange(result.Item3);
                }
                if (result.Item4 != null && result.Item4.Count > 0)
                {
                    outStockLockInfos.AddRange(result.Item4);
                }
                if (result.Item5 != null && result.Item5.Count > 0)
                {
                    locationInfos.AddRange(result.Item5);
                }
                if (result.Item1 != null && result.Item1.Count > 0)
                {
                    tasks.AddRange(result.Item1);
                }
                //处理出库数据
                return GenerateOutboundTaskDataUpdate(tasks, stockInfos, outSGOrderDetails, outStockLockInfos, locationInfos);
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                content.Error(ex.Message);
            }
            return content;
        }
        /// <summary>
        /// å‡ºåº“任务数据处理
        /// </summary>
        /// <param name="orderDetailId"></param>
        /// <param name="stockSelectViews"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public (List<Dt_Task>, List<Dt_StockInfo>?, List<Dt_OutSGOrderDetail>?, List<Dt_OutStockLockInfo>?, List<Dt_LocationInfo>?) OutSGTaskDataHandle(int[] keys)
        {
            List<Dt_Task> tasks = new List<Dt_Task>();
            List<Dt_OutSGOrderDetail> outSGOrderDetails = _outboundRepository.OutSGOrderDetailRepository.QueryData(x => keys.Contains(x.Id));
            if (outSGOrderDetails == null || outSGOrderDetails.Count == 0)
            {
                throw new Exception("未找到出库单明细信息");
            }
            Dt_OutSGOrderDetail? outBSTOrderDetail = outSGOrderDetails.FirstOrDefault(x => x.OutSGOrderDetailStatus != OutOrderStatusEnum.未开始.ObjToInt());
            if (outBSTOrderDetail != null)
            {
                throw new Exception($"出库明细{nameof(Dt_OutSGOrderDetail.BoardMpsDetailId)}{outBSTOrderDetail.BoardMpsDetailId}物料{outBSTOrderDetail.MaterialNo},出库中或已完成");
            }
            List<Dt_StockInfo>? stockInfos = null;
            List<Dt_OutSGOrderDetail>? orderDetails = null;
            List<Dt_OutStockLockInfo>? outStockLockInfos = null;
            List<Dt_LocationInfo>? locationInfos = null;
            //分配库存
            (List<Dt_StockInfo>, List<Dt_OutSGOrderDetail>, List<Dt_OutStockLockInfo>, List<Dt_LocationInfo>) result = _outboundService.OutSGOrderDetailService.AssignStockOutbound(outSGOrderDetails);
            if (result.Item1 != null && result.Item1.Count > 0)
            {
                //获取任务
                tasks = GetTasks(result.Item1, TaskTypeEnum.OldYLOutbound);
                result.Item2.ForEach(x =>
                {
                    x.OutSGOrderDetailStatus = OutOrderStatusEnum.出库中.ObjToInt();
                });
                result.Item3.ForEach(x =>
                {
                    x.Status = OutLockStockStatusEnum.出库中.ObjToInt();
                });
                stockInfos = result.Item1;
                orderDetails = result.Item2;
                outStockLockInfos = result.Item3;
                locationInfos = result.Item4;
            }
            else
            {
                throw new Exception("无库存");
            }
            return (tasks, stockInfos, orderDetails, outStockLockInfos, locationInfos);
        }
        public WebResponseContent GenerateOutboundTaskDataUpdate(List<Dt_Task> tasks, List<Dt_StockInfo>? stockInfos = null, List<Dt_OutSGOrderDetail>? outboundOrderDetails = null, List<Dt_OutStockLockInfo>? outStockLockInfos = null, List<Dt_LocationInfo>? locationInfos = null)
        {
            try
            {
                _unitOfWorkManage.BeginTran();
                BaseDal.AddData(tasks);
                if (stockInfos != null && stockInfos.Count > 0 && outboundOrderDetails != null && outboundOrderDetails.Count > 0 && outStockLockInfos != null && outStockLockInfos.Count > 0 && locationInfos != null && locationInfos.Count > 0)
                {
                    stockInfos.ForEach(x =>
                    {
                        x.StockStatus = StockStatusEmun.出库锁定.ObjToInt();
                    });
                    outboundOrderDetails.ForEach(x =>
                    {
                        x.OutSGOrderDetailStatus = OutOrderStatusEnum.出库中.ObjToInt();
                    });
                    List<Dt_OutSGOrder> outSGOrders = _outboundRepository.OutSGOrderRepository.QueryData(x => outboundOrderDetails.Select(x=>x.OutSGOrderId).Distinct().Contains(x.Id));
                    if (outSGOrders.Count<=0)
                    {
                        throw new Exception("未找到单据信息");
                    }
                    outSGOrders.ForEach(x =>
                    {
                        x.OutSGOrderStatus = OutOrderStatusEnum.出库中.ObjToInt();
                    });
                    _outboundRepository.OutSGOrderRepository.UpdateData(outSGOrders);
                    WebResponseContent content = _outboundService.OutSGOrderDetailService.LockOutboundStockDataUpdate(stockInfos, outboundOrderDetails, outStockLockInfos, locationInfos, tasks: tasks);
                    if (!content.Status)
                    {
                        _unitOfWorkManage.RollbackTran();
                        return content;
                    }
                }
                _unitOfWorkManage.CommitTran();
                PushTasksToWCS(tasks);
                return WebResponseContent.Instance.OK();
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
    }
}