huanghongfeng
3 天以前 5ffc36a1db18d3112a9b50a9cf3953d7fcf21bae
ÏîÄ¿´úÂë/WMS/WIDESEA_WMSServer/WIDESEA_StorageBasicServices/Location/LocationInfoService.cs
@@ -21,6 +21,7 @@
    private readonly ITaskExecuteDetailRepository _taskExecuteDetailRepository;
    private readonly ILocationStatusChangeRecordRepository _locationStatusChangeRecordRepository;
    private readonly IDt_WheelsStockRepository _WheelsStockRepository;
    private readonly IDt_ZdpStockRepository _zdpStockRepository;
    private readonly IMapper _mapper;
    public LocationInfoService(ILocationInfoRepository BaseDal,
@@ -33,7 +34,8 @@
                                    IStockInfoDetailRepository stockInfoDetailRepository,
                                    IMapper mapper,
                                    IDt_TaskService taskService,
                                    IDt_WheelsStockRepository wheelsStockRepository) : base(BaseDal)
                                    IDt_WheelsStockRepository wheelsStockRepository,
                                    IDt_ZdpStockRepository zdpStockRepository) : base(BaseDal)
    {
        _unitOfWorkManage = unitOfWorkManage;
        _taskRepository = taskRepository;
@@ -45,6 +47,7 @@
        _mapper = mapper;
        _taskService = taskService;
        _WheelsStockRepository = wheelsStockRepository;
        _zdpStockRepository = zdpStockRepository;
    }
    /// <summary>
@@ -81,6 +84,8 @@
                // èŽ·å–æ–°çš„åº“ä½çš„ä»»åŠ¡
                var internalTransferTask = await _taskRepository.QueryFirstAsync(x => x.SourceAddress == newLocationID && x.Roadway == outboundTask.Roadway && x.TaskState == (int)TaskOutStatusEnum.OutNew);
                LogFactory.GetLog("检查是否需要移库").InfoFormat(true, "需要移库,获取新库位的任务", $"新库位任务:{internalTransferTask}");
                // å¦‚果新的库位没有找到对应的任务
                if (internalTransferTask == null)
@@ -95,6 +100,62 @@
            // è¿”回当前库位的出库任务
            return outboundTask;
        }
        catch (Exception)
        {
            return null;
        }
    }
    /// <summary>
    /// æ£€æŸ¥å¹¶ç”Ÿæˆç§»åº“任务或返回入库任务
    /// </summary>
    /// <param name="locationID">任务号</param>
    /// <returns>任务对象</returns>
    public virtual async Task<Dt_Task> InTransferCheckAsync(RequestTaskDto input)
    {
        try
        {
            int taskNum = input.taskNum;
            // æ ¹æ®ä»»åŠ¡å·èŽ·å–å…¥åº“ä»»åŠ¡
            var inboundTask = await _taskRepository.QueryFirstAsync(x => x.TaskNum == taskNum);
            if (inboundTask == null)
                return null;
            LogFactory.GetLog("检查是否需要移库").InfoFormat(true, "根据任务号获取任务", $"任务号:{taskNum},任务数据:{inboundTask}");
            var location = await BaseDal.QueryFirstAsync(x => x.LocationCode == inboundTask.TargetAddress);
            LogFactory.GetLog("检查是否需要移库").InfoFormat(true, "获取指定库位的货位数据", $"货位号:{inboundTask.TargetAddress},货位数据:{location}");
            // æ£€æŸ¥æ˜¯å¦éœ€è¦è¿›è¡Œç§»åº“
            if (CheckForInternalTransfer(location))
            {
                LogFactory.GetLog("检查是否需要移库").InfoFormat(true, "需要移库", $"货位号:{inboundTask.TargetAddress},货位数据:{location}");
                // è®¡ç®—对应位置的相对库位 ï¼ˆå¥‡æ•°è¡Œçš„下一行或者偶数行的上一行)
                var newLocationID = GetRelativeLocationID(location);
                LogFactory.GetLog("检查是否需要移库").InfoFormat(true, "需要移库,组建需要移库货位号", $"新的库位ID:{newLocationID}");
                var relocationTask = await _taskRepository.QueryFirstAsync(x => x.TargetAddress == newLocationID && x.Roadway == inboundTask.Roadway && x.TaskType == (int)TaskTypeEnum.Relocation);
                if (relocationTask != null) return relocationTask;
                // èŽ·å–æ–°çš„åº“ä½çš„ä»»åŠ¡
                var internalTransferTask = await _taskRepository.QueryFirstAsync(x => x.TargetAddress == newLocationID && x.Roadway == inboundTask.Roadway && x.TaskState == (int)TaskOutStatusEnum.OutNew);
                LogFactory.GetLog("检查是否需要移库").InfoFormat(true, "需要移库,获取新库位的任务", $"新库位任务:{internalTransferTask}");
                // å¦‚果新的库位没有找到对应的任务
                if (internalTransferTask == null)
                {
                    LogFactory.GetLog("检查是否需要移库").InfoFormat(true, "需要移库,未获取到新库位任务", $"");
                    return await InHandleLocation(inboundTask.TargetAddress, newLocationID, inboundTask);
                }
                LogFactory.GetLog("检查是否需要移库").InfoFormat(true, "需要移库,获取到新库位任务直接返回", $"新库位任务:{internalTransferTask}");
                // ç›´æŽ¥è¿”回一深位出库任务
                return internalTransferTask;
            }
            // è¿”回当前库位的出库任务
            return inboundTask;
        }
        catch (Exception)
        {
@@ -253,6 +314,32 @@
    #endregion å¯ç”¨ç¦ç”¨è´§ä½
    #region å¤§å±åº“位状态获取
    public WebResponseContent LocationStatus()
    {
        WebResponseContent content = new WebResponseContent();
        // èŽ·å–æ‰€æœ‰åº“ä½æ•°æ®
        List<DtLocationInfo> locations = BaseDal.QueryData();
        // æŒ‰LocationStatus分组统计
        var statusGroups = locations
            .GroupBy(l => l.LocationStatus)
            .Select(g => new
            {
                Status = g.Key==0?"空货位": g.Key == 1 ? "锁定": g.Key == 2 ? "有货": "有货禁用",
                Count = g.Count(),
            })
            .ToList();
        // å°†ç»Ÿè®¡ç»“果放入响应内容
        content.Data = statusGroups;
        return content;
    }
    #endregion
    #region å†…部方法
    #region ç§»åº“方法
@@ -287,8 +374,12 @@
        // åˆ¤æ–­è¯¥ä½ç½®æ˜¯å¦æœ‰åº“å­˜
        var stockInfo = await _WheelsStockRepository.QueryFirstAsync(x => x.Wheels_Location == newLocationID);
        // åˆ¤æ–­è¯¥ä½ç½®æ˜¯å¦æœ‰åº“å­˜
        var zdpInfo = await _zdpStockRepository.QueryFirstAsync(x => x.Zdp_Location == newLocationID);
        LogFactory.GetLog("检查是否需要移库").InfoFormat(true, "需要移库,未获取到新库位任务", $"该位置是否有库存:{stockInfo}");
        if (stockInfo == null)
        if (stockInfo == null && zdpInfo==null)
        {
            LogFactory.GetLog("检查是否需要移库").InfoFormat(true, "需要移库,未获取到库存数据", $"直接返回原先出库任务:{outboundTask}");
            // å¦‚果没有库存,直接返回当前出库任务
@@ -306,14 +397,73 @@
            {
                CreateDate = DateTime.Now,
                Creater = App.User.UserName == null ? "admin" : App.User.UserName,
                CurrentAddress = stockInfo.Wheels_Location,
                CurrentAddress = stockInfo != null ?stockInfo.Wheels_Location: zdpInfo.Zdp_Location,
                Grade = 99,
                MaterialNo = stockInfo.Wheels_Type,
                MaterialNo = stockInfo != null ? stockInfo.Wheels_Type: "2",
                NextAddress = emptyLocation.LocationCode,
                PalletCode = stockInfo.Wheels_Num,
                PalletCode = stockInfo != null ? stockInfo.Wheels_Num : zdpInfo.Zdp_code,
                Remark = "移库",
                Roadway = "SC1",
                SourceAddress = stockInfo.Wheels_Location,
                SourceAddress = stockInfo != null ? stockInfo.Wheels_Location : zdpInfo.Zdp_Location,
                TaskNum = taskNo,
                TargetAddress = emptyLocation.LocationCode,
                TaskState = (int)TaskStatus.Created,
                TaskType = (int)TaskTypeEnum.Relocation,
                Dispatchertime = DateTime.Now,
            };
            LogFactory.GetLog("检查是否需要移库").InfoFormat(true, "需要移库,新建移库任务", $"移库任务数据:{newTransferTask}");
            //修改库位状态
            emptyLocation.LocationStatus = (int)LocationEnum.FreeDisable;
            BaseDal.UpdateData(emptyLocation);
            return await _taskRepository.Create(newTransferTask);
        }
    }
    /// <summary>
    /// å¤„理入库没有任务的库位情况
    /// </summary>
    /// <param name="originalLocationID">原始库位ID</param>
    /// <param name="newLocationID">新的库位ID</param>
    /// <param name="outboundTask">出库任务</param>
    /// <returns>生成的移库任务或原始出库任务</returns>
    private async Task<Dt_Task> InHandleLocation(string originalLocationID, string newLocationID, Dt_Task inboundTask)
    {
        // åˆ¤æ–­è¯¥ä½ç½®æ˜¯å¦æœ‰åº“å­˜
        var stockInfo = await _WheelsStockRepository.QueryFirstAsync(x => x.Wheels_Location == newLocationID);
        // åˆ¤æ–­è¯¥ä½ç½®æ˜¯å¦æœ‰åº“å­˜
        var zdpInfo = await _zdpStockRepository.QueryFirstAsync(x => x.Zdp_Location == newLocationID);
        LogFactory.GetLog("检查是否需要移库").InfoFormat(true, "需要移库,未获取到新库位任务", $"该位置是否有库存:{stockInfo}");
        if (stockInfo == null && zdpInfo == null)
        {
            LogFactory.GetLog("检查是否需要移库").InfoFormat(true, "需要移库,未获取到库存数据", $"直接返回原先出库任务:{inboundTask}");
            // å¦‚果没有库存,直接返回当前出库任务
            return inboundTask;
        }
        else
        {
            // å¦‚果有库存,生成移库任务
            var emptyLocation = RequestLocation("SC1");
            if (emptyLocation == null) throw new Exception("暂无空库");
            LogFactory.GetLog("检查是否需要移库").InfoFormat(true, "需要移库,查找能移库货位", $"货位数据:{emptyLocation}");
            var taskNo = await _taskRepository.GetTaskNo();
            Dt_Task newTransferTask = new Dt_Task()
            {
                CreateDate = DateTime.Now,
                Creater = App.User.UserName == null ? "admin" : App.User.UserName,
                CurrentAddress = stockInfo != null ? stockInfo.Wheels_Location : zdpInfo.Zdp_Location,
                Grade = 99,
                MaterialNo = stockInfo != null ? stockInfo.Wheels_Type : "2",
                NextAddress = emptyLocation.LocationCode,
                PalletCode = stockInfo != null ? stockInfo.Wheels_Num : zdpInfo.Zdp_code,
                Remark = "移库",
                Roadway = "SC1",
                SourceAddress = stockInfo != null ? stockInfo.Wheels_Location : zdpInfo.Zdp_Location,
                TaskNum = taskNo,
                TargetAddress = emptyLocation.LocationCode,
                TaskState = (int)TaskStatus.Created,
@@ -360,17 +510,20 @@
                //List<DtLocationInfo> locations = new List<DtLocationInfo>();
                if (locationInfos == null || locationInfos.Count == 0)
                {
                    locationInfos = BaseDal.QueryData(x => x.LocationStatus == (int)LocationEnum.Free && x.RoadwayNo == "SC1" && x.EnalbeStatus == 1 && x.LocationType == 1);
                    locationInfos = BaseDal.QueryData(x => x.LocationStatus == 0 && x.RoadwayNo == "SC1" && x.EnalbeStatus == 1 && x.LocationType == 1);
                }
                var location = GetEmptyLocation(locationInfos);
                var location = GetEmptyLocation(locationInfos); //找出货位
                if (location != null)
                {
                    if (location.Depth == 2)
                    {
                        int row = location.Row;
                        int relativeLine = row % 2 == 1 ? row + 1 : row - 1;
                        //int relativeLine = row % 2 == 1 ? row + 1 : row - 1;
                        int relativeLine = row == 1 ? row + 1 : row - 1;
                        var insideLocation = BaseDal.QueryFirst(x => x.Row == relativeLine && x.Layer == location.Layer && x.Column == location.Column);
@@ -378,7 +531,7 @@
                        {
                            locationInfos.Remove(location);
                            if (locationInfos.Count == 0) return null;
                            RequestLocation(RoadWayNo, locationInfos);
                            return RequestLocation(RoadWayNo, locationInfos);
                        }
                    }
                }
@@ -399,7 +552,7 @@
    }
    private DtLocationInfo GetEmptyLocation(List<DtLocationInfo> dtLocationInfos)
    {
        var locationinfo = dtLocationInfos.Where(x => x.LocationStatus == (int)LocationEnum.Free && x.RoadwayNo == "SC1" && x.EnalbeStatus == 1 && x.LocationType == 1).OrderBy(x => x.Layer).ThenByDescending(x => x.Depth).ThenBy(x => x.Row).ThenBy(x => x.Column).FirstOrDefault();
        var locationinfo = dtLocationInfos.Where(x => x.LocationStatus == 0 && x.RoadwayNo == "SC1" && x.EnalbeStatus == 1 && x.LocationType == 1).OrderBy(x => x.Layer).ThenByDescending(x => x.Depth).ThenBy(x => x.Row).ThenBy(x => x.Column).FirstOrDefault();
        return locationinfo;
    }