647556386
2 天以前 696edbff3c8812e4b820f624d66a02ae6ddb1a06
ÏîÄ¿´úÂë/WMSÎÞ²Ö´¢°æ/WIDESEA_WMSServer/WIDESEA_TaskInfoService/TaskService_Outbound.cs
@@ -1181,7 +1181,7 @@
                }
                stockInfos.ForEach(x =>
                {
                    x.StockStatus = StockStatusEmun.出库锁定.ObjToInt();
                    x.StockStatus = StockStatusEmun.盘点出库锁定.ObjToInt();
                });
                tasks.ForEach(x =>
                {
@@ -1441,6 +1441,149 @@
                }
            }
        }
        /// <summary>
        /// é€‰å®šåº“存跨区域移库
        /// </summary>
        /// <param name="stockViews"></param>
        /// <param name="targetLocationType">目标货位类型</param>
        /// <returns></returns>
        public async Task<WebResponseContent> CrossAreaOutbound(List<StockViewDTO> stockViews, int targetLocationType)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                if(targetLocationType == (int)LocationTypeEnum.Electronic)
                {
                    return content.Error("电子仓不允许跨区域移库");
                }
                List<int> ids = stockViews.Select(x => x.StockId).ToList();
                List<Dt_StockInfo> stockInfos = _stockRepository.Db.Queryable<Dt_StockInfo>().Where(x => ids.Contains(x.Id)).Includes(x => x.Details).ToList();
                if (stockInfos.Count != stockViews.Count)
                {
                    StockViewDTO? stockViewDTO = stockViews.FirstOrDefault(x => !stockInfos.Select(x => x.PalletCode).Contains(x.PalletCode));
                    return content.Error($"未找到{stockViewDTO?.PalletCode}库存");
                }
                List<string> locStrs = stockInfos.Select(x => x.LocationCode).ToList();
                List<Dt_LocationInfo> locationInfos = _locationInfoService.Db.Queryable<Dt_LocationInfo>().Where(x => locStrs.Contains(x.LocationCode)).ToList();
                if (stockInfos.Count != locationInfos.Count)
                {
                    string? locStr = locStrs.FirstOrDefault(x => !locationInfos.Select(x => x.LocationCode).Contains(x));
                    return content.Error($"未找到{locStr}货位数据");
                }
                foreach (var item in stockInfos)
                {
                    if (item.PalletType != PalletTypeEnum.Empty.ObjToInt())
                    {
                        return content.Error($"托盘【{item.PalletCode}】非空箱,仅空箱允许跨区域移库!");
                    }
                    Dt_LocationInfo? locationInfo = locationInfos.FirstOrDefault(x => x.LocationCode == item.LocationCode);
                    if (locationInfo == null || locationInfo.EnableStatus != EnableStatusEnum.Normal.ObjToInt() || item.StockStatus != StockStatusEmun.入库完成.ObjToInt())
                    {
                        return content.Error($"{item.PalletCode}货位或库存状态不满足出库条件");
                    }
                }
                List<Dt_Task> tasks = CrossAreaGetTasks(stockInfos, targetLocationType, TaskTypeEnum.CrossAreaRelocation);
                if (tasks == null || tasks.Count <= 0)
                {
                    return content.Error("生成跨区域移库任务失败");
                }
                stockInfos.ForEach(x =>
                {
                    x.StockStatus = StockStatusEmun.出库锁定.ObjToInt();
                });
                tasks.ForEach(x =>
                {
                    x.OrderNo = "跨区域移库";
                });
                locationInfos.ForEach(x =>
                {
                    x.LocationStatus = LocationStatusEnum.Lock.ObjToInt();
                });
                _unitOfWorkManage.BeginTran();
                _stockRepository.UpdateData(stockInfos);
                BaseDal.AddData(tasks);
                _locationInfoService.UpdateData(locationInfos);
                _unitOfWorkManage.CommitTran();
                content.OK();
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                return await Task.FromResult(WebResponseContent.Instance.Error(ex.Message));
            }
            return content;
        }
        /// <summary>
        /// ç”Ÿæˆè·¨åŒºåŸŸç§»åº“任务
        /// </summary>
        /// <param name="stockInfos">库存列表</param>
        /// <param name="targetAreaCode">目标区域编码</param>
        /// <param name="taskType">任务类型</param>
        /// <returns></returns>
        public List<Dt_Task> CrossAreaGetTasks(List<Dt_StockInfo> stockInfos, int targetLocationType, TaskTypeEnum taskType)
        {
            using (var scope = new TransactionScope(TransactionScopeOption.Required,
                new TransactionOptions { IsolationLevel = IsolationLevel.ReadCommitted }))
            {
                try
                {
                    List<Dt_Task> tasks = new List<Dt_Task>();
                    foreach (var stockInfo in stockInfos)
                    {
                        Dt_LocationInfo newLocation = _locationInfoService.AssignLocation(targetLocationType);
                        if (newLocation == null)
                        {
                            throw new Exception($"{stockInfo.PalletCode} æ²¡æœ‰ç©ºé—²è´§ä½å¯è¿›è¡Œè·¨åŒºåŸŸç§»åº“");
                        }
                        if (!tasks.Exists(x => x.PalletCode == stockInfo.PalletCode))
                        {
                            Dt_Task task = new()
                            {
                                CurrentAddress = stockInfo.LocationCode,
                                Grade = 0,
                                PalletCode = stockInfo.PalletCode,
                                NextAddress = "",
                                Roadway = newLocation.RoadwayNo,
                                SourceAddress = stockInfo.LocationCode,
                                TargetAddress = newLocation.LocationCode,
                                TaskStatus = TaskStatusEnum.New.ObjToInt(),
                                TaskType = taskType.ObjToInt(),
                                PalletType = stockInfo.PalletType,
                                WarehouseId = stockInfo.WarehouseId,
                            };
                            tasks.Add(task);
                        }
                        newLocation.LocationStatus = LocationStatusEnum.Lock.ObjToInt();
                        _locationInfoService.UpdateData(newLocation);
                    }
                    scope.Complete();
                    return tasks;
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }
            }
        }
    }
}