Browse Source

优化楼层选择逻辑并添加异常处理日志

在 `SXService.cs` 中添加了对新命名空间的引用,并引入常量 `MinAvailableCells` 来定义最小有效货位数量。通过 `Enumerable.Range` 方法重构楼层信息结构,计算可用货位数和库存比率,同时移除旧的计算逻辑。增加了对禁用楼层的处理,确保计算时考虑这些楼层。修改了任务选择策略,优先选择可用货位数大于最小有效货位数量的楼层,并添加次选策略。最后,在异常处理部分添加了日志记录,以便于追踪错误,并注释掉了一些不再使用的逻辑以提高代码可读性。
林豪 左 4 months ago
parent
commit
fe5f8c718e
1 changed files with 318 additions and 173 deletions
  1. 318 173
      wms.service/Service/SXService.cs

+ 318 - 173
wms.service/Service/SXService.cs

@@ -1,6 +1,7 @@
 using AutoMapper;
 using Microsoft.Extensions.Logging;
 using Newtonsoft.Json;
+using NPOI.SS.Formula.Functions;
 using SqlSugar;
 using SqlSugar.Extensions;
 using System;
@@ -1559,104 +1560,176 @@ namespace wms.service.Service
 
             #endregion 检测库存占比是否符合要求
 
-            var cellfloor1count = isTobeTors == true ? cell.Where(p => p.Floor == 1 && p.Tunnel <= tun).Count() : cell.Where(p => p.Floor == 1 && p.Tunnel > tun).Count();  //1楼层可用的货位数
-            var cellfloor2count = isTobeTors == true ? cell.Where(p => p.Floor == 2 && p.Tunnel <= tun).Count() : cell.Where(p => p.Floor == 2 && p.Tunnel > tun).Count();  //2楼层可用的货位数
-            var cellfloor3count = isTobeTors == true ? cell.Where(p => p.Floor == 3 && p.Tunnel <= tun).Count() : cell.Where(p => p.Floor == 3 && p.Tunnel > tun).Count();  //3楼层可用的货位数
+            // 最小有效货位数量,用于保留移库
+            const int MinAvailableCells = 30;
+            var forbidLayer = wms.util.AppSettings.GetConfig("ForbidLayer");
+            // 定义楼层信息结构
+            var floors = Enumerable.Range(1, 3).Select(floorNumber =>
+            {
+                // 获取货位数
+                var cellQuery = cell.Where(p => p.Floor == floorNumber);
+                var cellCount = isTobeTors ? cellQuery.Count(p => p.Tunnel <= tun) : cellQuery.Count(p => p.Tunnel > tun);
+                if (!string.IsNullOrEmpty(forbidLayer) && forbidLayer.Contains(floorNumber.ToString())) cellCount = 0;
+                // 获取同规格库存数
+                var (minLayer, maxLayer) = floorNumber switch
+                {
+                    1 => (1, 4),
+                    2 => (5, 8),
+                    3 => (9, 12),
+                    _ => (0, 0)
+                };
 
-            var cellfloor1stockcount = isTobeTors == true ? existstock.Where(p => p.PutLayer >= 1 && p.PutLayer <= 4 && p.PutRow <= row).Count() : existstock.Where(p => p.PutLayer >= 1 && p.PutLayer <= 4 && p.PutRow > row).Count();  //1楼层同规格数量
-            var cellfloor2stockcount = isTobeTors == true ? existstock.Where(p => p.PutLayer >= 5 && p.PutLayer <= 8 && p.PutRow <= row).Count() : existstock.Where(p => p.PutLayer >= 5 && p.PutLayer <= 8 && p.PutRow > row).Count();  //2楼层同规格数量
-            var cellfloor3stockcount = isTobeTors == true ? existstock.Where(p => p.PutLayer >= 9 && p.PutLayer <= 12 && p.PutRow <= row).Count() : existstock.Where(p => p.PutLayer >= 9 && p.PutLayer <= 12 && p.PutRow > row).Count();  //3楼层同规格数量
+                var stockQuery = existstock.Where(p => p.PutLayer >= minLayer && p.PutLayer <= maxLayer && (isTobeTors ? p.PutRow <= row : p.PutRow > row));
 
-            var floorTaskCount = int.Parse(_sysconfigrepository.GetFirst(p => p.Code == "FloorTaskCount").SContent);
-            var forbidlayer = wms.util.AppSettings.GetConfig("ForbidLayer");//禁用的楼层
-            if (!string.IsNullOrEmpty(forbidlayer))
-            {
-                if (forbidlayer.Contains("1")) cellfloor1count = 0;
-                if (forbidlayer.Contains("2")) cellfloor2count = 0;
-                if (forbidlayer.Contains("3")) cellfloor3count = 0;
-            }
-            //cellfloor2count = 0;//临时修改,限制楼层分配
-            var tempcells = new List<Tuple<int, int, double>>() {
-             new Tuple<int, int,double>(1,cellfloor1count,cellfloor1stockcount / (cellfloor1count*1.0)),
-             new Tuple<int, int,double>(2,cellfloor2count,cellfloor2stockcount / (cellfloor2count*1.0)),
-             new Tuple<int, int,double>(3,cellfloor3count,cellfloor3stockcount / (cellfloor3count*1.0)),
-            };
-            //tempcells = tempcells.OrderByDescending(p => p.Item2).ToList();//按照空位数排序
-            tempcells = tempcells.OrderBy(p => p.Item3).ToList();//按照同规则物料少的排序
+                // 计算库存比率
+                var stockRatio = cellCount > 0 ? stockQuery.Count() / (double)cellCount : 0;
+
+                // 获取任务限制数
+                var taskLimit = int.Parse(_sysconfigrepository.GetFirst(p => p.Code == $"FloorTaskCount{floorNumber}").SContent);
+
+                return new
+                {
+                    Floor = floorNumber,
+                    CellCount = cellCount,
+                    StockRatio = stockRatio,
+                    TaskLimit = taskLimit
+                };
+            })
+            // 排序:库存比例低优先 > 货位数高优先
+            .OrderBy(f => f.StockRatio)
+            .ThenByDescending(f => f.CellCount)
+            .ToList();
+
+            // 获取各楼层当前任务数
+            var floorTasks = _taskrepository.AsQueryable()
+                .Where(p => p.Type == TaskType.EnterDepot && p.Status < TaskStatus.Finish&&p.BusType== "帘线工字轮入库")
+                .ToList()
+                .GroupBy(p => p.Floor).ToDictionary(x => x.Key, x => x.Count());
 
-            //每个扫码固定楼层,如果固定楼层设备坏了或者库存满了,再换楼层
-            var taskcount = _taskrepository.AsQueryable().With(SqlWith.NoLock).Where(p => p.Type == TaskType.EnterDepot && p.Status < TaskStatus.Finish && p.Floor != 0).ToList();
             var time10 = new Stopwatch();
             time10.Restart();
-            if (tempcells[0].Item2 > 30)
+            // 选择楼层的策略
+            var selectedFloor = floors.FirstOrDefault(f =>
+                f.CellCount > MinAvailableCells &&
+                floorTasks.GetValueOrDefault(f.Floor, 0) < f.TaskLimit
+            );
+
+            // 次选策略:允许货位较少但未超限的楼层
+            selectedFloor ??= floors.FirstOrDefault(f =>
+                f.CellCount > 0 &&
+                floorTasks.GetValueOrDefault(f.Floor, 0) < f.TaskLimit
+            );
+
+            // 返回结果处理
+            if (selectedFloor != null)
             {
-                if (taskcount.Where(p => p.Floor == tempcells[0].Item1).Count() > floorTaskCount || tempcells[0].Item2 == 0)
-                {
-                    if (taskcount.Where(p => p.Floor == tempcells[1].Item1).Count() > floorTaskCount || tempcells[1].Item2 == 0)
-                    {
-                        if (taskcount.Where(p => p.Floor == tempcells[2].Item1).Count() > floorTaskCount || tempcells[2].Item2 == 0)
-                        {
-                            var it1 = taskcount.Where(p => p.Floor == tempcells[0].Item1).Count();
-                            var it2 = taskcount.Where(p => p.Floor == tempcells[1].Item1).Count();
-                            var it3 = taskcount.Where(p => p.Floor == tempcells[2].Item1).Count();
-                            result.ResData.TaskNo = 0;
-                            result.ResMsg = reqDto.Code + $"当前环线三个楼层执行中的任务数已满,不允许分配楼层1-{tempcells[0].Item1}:{it1}:{tempcells[0].Item2}--{tempcells[1].Item1}:{it2}:{tempcells[1].Item2}--{tempcells[2].Item1}:{it3}:{tempcells[2].Item2}";
-                            result.ResData.Floor = 0;
-                            return result;
-                        }
-                        else
-                        {
-                            result.ResData.Floor = tempcells[2].Item1;
-                        }
-                    }
-                    else
-                    {
-                        result.ResData.Floor = tempcells[1].Item1;
-                    }
-                }
-                else
-                {
-                    result.ResData.Floor = tempcells[0].Item1;
-                }
+                result.ResData.Floor = selectedFloor.Floor;
             }
             else
             {
-                if (tempcells[1].Item2 > 30)
-                {
-                    if (taskcount.Where(p => p.Floor == tempcells[1].Item1).Count() > floorTaskCount || tempcells[1].Item2 == 0)
-                    {
-                        if (taskcount.Where(p => p.Floor == tempcells[2].Item1).Count() > floorTaskCount || tempcells[2].Item2 == 0)
-                        {
-                            result.ResData.TaskNo = 0;
-                            result.ResMsg = reqDto.Code + "当前环线三个楼层执行中的任务数已满,不允许分配楼层2";
-                            result.ResData.Floor = 0;
-                            return result;
-                        }
-                        else
-                        {
-                            result.ResData.Floor = tempcells[2].Item1;
-                        }
-                    }
-                    else
-                    {
-                        result.ResData.Floor = tempcells[1].Item1;
-                    }
-                }
-                else
-                {
-                    if (taskcount.Where(p => p.Floor == tempcells[2].Item1).Count() < floorTaskCount && tempcells[2].Item2 > 30)
-                    {
-                        result.ResData.Floor = tempcells[2].Item1;
-                    }
-                    else
-                    {
-                        result.ResData.TaskNo = 0;
-                        result.ResMsg = reqDto.Code + "当前环线三个楼层执行中的任务数已满,不允许分配楼层3";
-                        result.ResData.Floor = 0;
-                        return result;
-                    }
-                }
+                result.ResData.TaskNo = 0;
+                result.ResMsg = $"{reqDto.Code} 所有楼层任务配额已满,当前各楼层任务数:" + string.Join("", floors.Select(f =>
+                 $"{f.Floor}层({floorTasks.GetValueOrDefault(f.Floor)}/{f.TaskLimit}) "));
             }
+
+            //var cellfloor1count = isTobeTors == true ? cell.Where(p => p.Floor == 1 && p.Tunnel <= tun).Count() : cell.Where(p => p.Floor == 1 && p.Tunnel > tun).Count();  //1楼层可用的货位数
+            //var cellfloor2count = isTobeTors == true ? cell.Where(p => p.Floor == 2 && p.Tunnel <= tun).Count() : cell.Where(p => p.Floor == 2 && p.Tunnel > tun).Count();  //2楼层可用的货位数
+            //var cellfloor3count = isTobeTors == true ? cell.Where(p => p.Floor == 3 && p.Tunnel <= tun).Count() : cell.Where(p => p.Floor == 3 && p.Tunnel > tun).Count();  //3楼层可用的货位数
+
+            //var cellfloor1stockcount = isTobeTors == true ? existstock.Where(p => p.PutLayer >= 1 && p.PutLayer <= 4 && p.PutRow <= row).Count() : existstock.Where(p => p.PutLayer >= 1 && p.PutLayer <= 4 && p.PutRow > row).Count();  //1楼层同规格数量
+            //var cellfloor2stockcount = isTobeTors == true ? existstock.Where(p => p.PutLayer >= 5 && p.PutLayer <= 8 && p.PutRow <= row).Count() : existstock.Where(p => p.PutLayer >= 5 && p.PutLayer <= 8 && p.PutRow > row).Count();  //2楼层同规格数量
+            //var cellfloor3stockcount = isTobeTors == true ? existstock.Where(p => p.PutLayer >= 9 && p.PutLayer <= 12 && p.PutRow <= row).Count() : existstock.Where(p => p.PutLayer >= 9 && p.PutLayer <= 12 && p.PutRow > row).Count();  //3楼层同规格数量
+
+            //var floorTaskCount = int.Parse(_sysconfigrepository.GetFirst(p => p.Code == "FloorTaskCount").SContent);
+            //var forbidlayer = wms.util.AppSettings.GetConfig("ForbidLayer");//禁用的楼层
+            //if (!string.IsNullOrEmpty(forbidlayer))
+            //{
+            //    if (forbidlayer.Contains("1")) cellfloor1count = 0;
+            //    if (forbidlayer.Contains("2")) cellfloor2count = 0;
+            //    if (forbidlayer.Contains("3")) cellfloor3count = 0;
+            //}
+            ////cellfloor2count = 0;//临时修改,限制楼层分配
+            //var tempcells = new List<Tuple<int, int, double, int>>() {
+            // new Tuple<int, int,double,int>(1,cellfloor1count,cellfloor1stockcount / (cellfloor1count*1.0),int.Parse(_sysconfigrepository.GetFirst(p => p.Code == "FloorTaskCount1").SContent)),
+            // new Tuple<int, int,double,int>(2,cellfloor2count,cellfloor2stockcount / (cellfloor2count*1.0),int.Parse(_sysconfigrepository.GetFirst(p => p.Code == "FloorTaskCount2").SContent)),
+            // new Tuple<int, int,double,int>(3,cellfloor3count,cellfloor3stockcount / (cellfloor3count*1.0),int.Parse(_sysconfigrepository.GetFirst(p => p.Code == "FloorTaskCount3").SContent)),
+            //};
+            ////tempcells = tempcells.OrderByDescending(p => p.Item2).ToList();//按照空位数排序
+            //tempcells = tempcells.OrderBy(p => p.Item3).ToList();//按照同规则物料少的排序
+
+            ////每个扫码固定楼层,如果固定楼层设备坏了或者库存满了,再换楼层
+            //var taskcount = _taskrepository.AsQueryable().With(SqlWith.NoLock).Where(p => p.Type == TaskType.EnterDepot && p.Status < TaskStatus.Finish && p.Floor != 0).ToList();
+            //var time10 = new Stopwatch();
+            //time10.Restart();
+            //if (tempcells[0].Item2 > 30)
+            //{
+            //    if (taskcount.Where(p => p.Floor == tempcells[0].Item1).Count() > tempcells[0].Item4 || tempcells[0].Item2 == 0)
+            //    {
+            //        if (taskcount.Where(p => p.Floor == tempcells[1].Item1).Count() > tempcells[1].Item4 || tempcells[1].Item2 == 0)
+            //        {
+            //            if (taskcount.Where(p => p.Floor == tempcells[2].Item1).Count() > tempcells[2].Item4 || tempcells[2].Item2 == 0)
+            //            {
+            //                var it1 = taskcount.Where(p => p.Floor == tempcells[0].Item1).Count();
+            //                var it2 = taskcount.Where(p => p.Floor == tempcells[1].Item1).Count();
+            //                var it3 = taskcount.Where(p => p.Floor == tempcells[2].Item1).Count();
+            //                result.ResData.TaskNo = 0;
+            //                result.ResMsg = reqDto.Code + $"当前环线三个楼层执行中的任务数已满,不允许分配楼层1-{tempcells[0].Item1}:{it1}:{tempcells[0].Item2}--{tempcells[1].Item1}:{it2}:{tempcells[1].Item2}--{tempcells[2].Item1}:{it3}:{tempcells[2].Item2}";
+            //                result.ResData.Floor = 0;
+            //                return result;
+            //            }
+            //            else
+            //            {
+            //                result.ResData.Floor = tempcells[2].Item1;
+            //            }
+            //        }
+            //        else
+            //        {
+            //            result.ResData.Floor = tempcells[1].Item1;
+            //        }
+            //    }
+            //    else
+            //    {
+            //        result.ResData.Floor = tempcells[0].Item1;
+            //    }
+            //}
+            //else
+            //{
+            //    if (tempcells[1].Item2 > 30)
+            //    {
+            //        if (taskcount.Where(p => p.Floor == tempcells[1].Item1).Count() > floorTaskCount || tempcells[1].Item2 == 0)
+            //        {
+            //            if (taskcount.Where(p => p.Floor == tempcells[2].Item1).Count() > floorTaskCount || tempcells[2].Item2 == 0)
+            //            {
+            //                result.ResData.TaskNo = 0;
+            //                result.ResMsg = reqDto.Code + "当前环线三个楼层执行中的任务数已满,不允许分配楼层2";
+            //                result.ResData.Floor = 0;
+            //                return result;
+            //            }
+            //            else
+            //            {
+            //                result.ResData.Floor = tempcells[2].Item1;
+            //            }
+            //        }
+            //        else
+            //        {
+            //            result.ResData.Floor = tempcells[1].Item1;
+            //        }
+            //    }
+            //    else
+            //    {
+            //        if (taskcount.Where(p => p.Floor == tempcells[2].Item1).Count() < floorTaskCount && tempcells[2].Item2 > 30)
+            //        {
+            //            result.ResData.Floor = tempcells[2].Item1;
+            //        }
+            //        else
+            //        {
+            //            result.ResData.TaskNo = 0;
+            //            result.ResMsg = reqDto.Code + "当前环线三个楼层执行中的任务数已满,不允许分配楼层3";
+            //            result.ResData.Floor = 0;
+            //            return result;
+            //        }
+            //    }
+            //}
             time10.Stop();
             time1.Stop();
             time.Stop();
@@ -3391,7 +3464,8 @@ namespace wms.service.Service
             }
             catch (Exception ex)
             {
-                _db.RollbackTran();
+                
+                _db.RollbackTran(); 
                 _logger.LogInformation(ex.ToString());
                 res.ResCode = ResponseStatusCodeEnum.InnerServerErr.GetHashCode();
                 res.ResMsg = "完成码垛任务异常" + ex.Message;
@@ -13221,9 +13295,9 @@ namespace wms.service.Service
                 //今天这一批次的扭转入库的分配楼层数量
                 var existstock = _billInvnowrepository.AsQueryable().With(SqlWith.NoLock).Where(p => p.InvStateCode == InvState.InvEcecState_In.ToString() && p.IsTorsChk == true && p.TorsChkQty == 1 && p.ProductTime >= startTime && p.ProductTime <= endTime).ToList();
                 int row = 8;
-                var cellfloor1stockcount = existstock.Where(p => p.PutLayer >= 1 && p.PutLayer <= 4 && p.PutRow > row).Count();  //1楼层同规格数量
-                var cellfloor2stockcount = existstock.Where(p => p.PutLayer >= 5 && p.PutLayer <= 8 && p.PutRow > row).Count();  //2楼层同规格数量
-                var cellfloor3stockcount = existstock.Where(p => p.PutLayer >= 9 && p.PutLayer <= 12 && p.PutRow > row).Count();  //3楼层同规格数量
+                //var cellfloor1stockcount = existstock.Where(p => p.PutLayer >= 1 && p.PutLayer <= 4 && p.PutRow > row).Count();  //1楼层同规格数量
+                //var cellfloor2stockcount = existstock.Where(p => p.PutLayer >= 5 && p.PutLayer <= 8 && p.PutRow > row).Count();  //2楼层同规格数量
+                //var cellfloor3stockcount = existstock.Where(p => p.PutLayer >= 9 && p.PutLayer <= 12 && p.PutRow > row).Count();  //3楼层同规格数量
 
                 //bool isTobeTors = false;
                 int tun = 2;
@@ -13241,96 +13315,167 @@ namespace wms.service.Service
                         cell = cell.Where(p => !disids.Contains(p.Id)).ToList();
                     }
                 }
-
-                var cellfloor1count = cell.Where(p => p.Floor == 1 && p.Tunnel > tun).Count();  //1楼层可用的货位数
-                var cellfloor2count = cell.Where(p => p.Floor == 2 && p.Tunnel > tun).Count();  //2楼层可用的货位数
-                var cellfloor3count = cell.Where(p => p.Floor == 3 && p.Tunnel > tun).Count();  //3楼层可用的货位数
-
-                var forbidlayer = wms.util.AppSettings.GetConfig("ForbidLayer");//禁用的楼层
-                if (!string.IsNullOrEmpty(forbidlayer))
+                                                  
+                const int MinAvailableCells = 30;// 最小有效货位数量,用于保留移库
+                var forbidLayer = wms.util.AppSettings.GetConfig("ForbidLayer");
+                // 定义楼层信息结构
+                var floors = Enumerable.Range(1, 3).Select(floorNumber =>
                 {
-                    if (forbidlayer.Contains("1")) cellfloor1count = 0;
-                    if (forbidlayer.Contains("2")) cellfloor2count = 0;
-                    if (forbidlayer.Contains("3")) cellfloor3count = 0;
-                }
+                    // 获取货位数
+                    var cellQuery = cell.Where(p => p.Floor == floorNumber);
+                    var cellCount = cellQuery.Count(p => p.Tunnel > tun);
+                    if (!string.IsNullOrEmpty(forbidLayer) && forbidLayer.Contains(floorNumber.ToString())) cellCount = 0;
 
-                var tempcells = new List<Tuple<int, int, double>>() {
-             new Tuple<int, int,double>(1,cellfloor1count,cellfloor1stockcount / (cellfloor1count * 1.0)),
-             new Tuple<int, int,double>(2,cellfloor2count,cellfloor2stockcount / (cellfloor2count * 1.0)),
-             new Tuple<int, int,double>(3,cellfloor3count,cellfloor3stockcount / (cellfloor3count * 1.0)),
-            };
-                //tempcells = tempcells.OrderByDescending(p => p.Item2).ToList();
-                tempcells = tempcells.OrderBy(p => p.Item3).ToList();//按照同规则物料少的排序
+                    // 获取同规格库存数
+                    var (minLayer, maxLayer) = floorNumber switch
+                    {
+                        1 => (1, 4),
+                        2 => (5, 8),
+                        3 => (9, 12),
+                        _ => (0, 0)
+                    };
+                    var stockQuery = existstock.Where(p => p.PutLayer >= minLayer && p.PutLayer <= maxLayer && p.PutRow > row);
 
-                //每个扫码固定楼层,如果固定楼层设备坏了或者库存满了,再换楼层
-                var taskcount = _taskrepository.AsQueryable().With(SqlWith.NoLock).Where(p => p.Type == TaskType.EnterDepot && p.Status < TaskStatus.Finish && p.Floor != 0).ToList();
+                    // 计算库存比率
+                    var stockRatio = cellCount > 0 ? stockQuery.Count() / (double)cellCount : 0;
 
-                if (tempcells[0].Item2 > (reqDto.Count + 10))
-                {
-                    if (taskcount.Where(p => p.Floor == tempcells[0].Item1).Count() > floorTaskCount || tempcells[0].Item2 == 0)
-                    {
-                        if (taskcount.Where(p => p.Floor == tempcells[1].Item1).Count() > floorTaskCount || tempcells[1].Item2 == 0)
-                        {
-                            if (taskcount.Where(p => p.Floor == tempcells[2].Item1).Count() > floorTaskCount || tempcells[2].Item2 == 0)
-                            {
-                                res.ResCode = ResponseStatusCodeEnum.Fail.GetHashCode();
-                                res.ResMsg = reqDto.Count + "扭转回库申请楼层失败,当前环线三个楼层执行中的任务数已满,不允许分配楼层";
-                                res.ResData = 0;
-                                return res;
-                            }
-                            else
-                            {
-                                res.ResData = tempcells[2].Item1;
-                            }
-                        }
-                        else
-                        {
-                            res.ResData = tempcells[1].Item1;
-                        }
-                    }
-                    else
+                    // 获取任务限制数
+                    var taskLimit = int.Parse(_sysconfigrepository.GetFirst(p => p.Code == $"FloorTaskCount{floorNumber}").SType);
+
+                    return new
                     {
-                        res.ResData = tempcells[0].Item1;
-                    }
+                        Floor = floorNumber,
+                        CellCount = cellCount,
+                        StockRatio = stockRatio,
+                        TaskLimit = taskLimit
+                    };
+                })
+                // 排序:库存比例低优先 > 货位数高优先
+                .OrderBy(f => f.StockRatio)
+                .ThenByDescending(f => f.CellCount)
+                .ToList();
+
+                // 获取各楼层当前任务数
+                var floorTasks = _taskrepository.AsQueryable()
+                    .Where(p => p.Type == TaskType.EnterDepot && p.Status < TaskStatus.Finish && p.BusType == "扭转回库")
+                    .ToList()
+                    .GroupBy(p => p.Floor).ToDictionary(x => x.Key, x => x.Count());
+
+                var time10 = new Stopwatch();
+                time10.Restart();
+                // 选择楼层的策略
+                var selectedFloor = floors.FirstOrDefault(f =>
+                    f.CellCount > MinAvailableCells &&
+                    floorTasks.GetValueOrDefault(f.Floor, 0) < f.TaskLimit
+                );
+
+                // 次选策略:允许货位较少但未超限的楼层
+                selectedFloor ??= floors.FirstOrDefault(f =>
+                    f.CellCount > 0 &&
+                    floorTasks.GetValueOrDefault(f.Floor, 0) < f.TaskLimit
+                );
+
+                // 返回结果处理
+                if (selectedFloor != null)
+                {
+                    res.ResData = selectedFloor.Floor;
                 }
                 else
                 {
-                    if (tempcells[1].Item2 > (reqDto.Count + 10))
-                    {
-                        if (taskcount.Where(p => p.Floor == tempcells[1].Item1).Count() > floorTaskCount || tempcells[1].Item2 == 0)
-                        {
-                            if (taskcount.Where(p => p.Floor == tempcells[2].Item1).Count() > floorTaskCount || tempcells[2].Item2 == 0)
-                            {
-                                res.ResCode = ResponseStatusCodeEnum.Fail.GetHashCode();
-                                res.ResMsg = reqDto.Count + "扭转回库申请楼层失败,当前环线三个楼层执行中的任务数已满,不允许分配楼层";
-                                res.ResData = 0;
-                                return res;
-                            }
-                            else
-                            {
-                                res.ResData = tempcells[2].Item1;
-                            }
-                        }
-                        else
-                        {
-                            res.ResData = tempcells[1].Item1;
-                        }
-                    }
-                    else
-                    {
-                        if (taskcount.Where(p => p.Floor == tempcells[2].Item1).Count() < floorTaskCount && tempcells[2].Item2 > 30)
-                        {
-                            res.ResData = tempcells[2].Item1;
-                        }
-                        else
-                        {
-                            res.ResCode = ResponseStatusCodeEnum.Fail.GetHashCode();
-                            res.ResMsg = reqDto.Count + "扭转回库申请楼层失败,当前环线三个楼层执行中的任务数已满,不允许分配楼层";
-                            res.ResData = 0;
-                            return res;
-                        }
-                    }
+                    res.ResCode = ResponseStatusCodeEnum.Fail.GetHashCode();
+                    res.ResMsg = $"{reqDto.Count} 扭转回库申请楼层失败,当前环线三个楼层执行中的任务数已满,不允许分配楼层:" + string.Join("", floors.Select(f =>
+                     $"{f.Floor}层({floorTasks.GetValueOrDefault(f.Floor)}/{f.TaskLimit}) "));
                 }
+
+                //    var cellfloor1count = cell.Where(p => p.Floor == 1 && p.Tunnel > tun).Count();  //1楼层可用的货位数
+                //    var cellfloor2count = cell.Where(p => p.Floor == 2 && p.Tunnel > tun).Count();  //2楼层可用的货位数
+                //    var cellfloor3count = cell.Where(p => p.Floor == 3 && p.Tunnel > tun).Count();  //3楼层可用的货位数
+
+                //    var forbidlayer = wms.util.AppSettings.GetConfig("ForbidLayer");//禁用的楼层
+                //    if (!string.IsNullOrEmpty(forbidlayer))
+                //    {
+                //        if (forbidlayer.Contains("1")) cellfloor1count = 0;
+                //        if (forbidlayer.Contains("2")) cellfloor2count = 0;
+                //        if (forbidlayer.Contains("3")) cellfloor3count = 0;
+                //    }
+
+                //    var tempcells = new List<Tuple<int, int, double>>() {
+                // new Tuple<int, int,double>(1,cellfloor1count,cellfloor1stockcount / (cellfloor1count * 1.0)),
+                // new Tuple<int, int,double>(2,cellfloor2count,cellfloor2stockcount / (cellfloor2count * 1.0)),
+                // new Tuple<int, int,double>(3,cellfloor3count,cellfloor3stockcount / (cellfloor3count * 1.0)),
+                //};
+                //    //tempcells = tempcells.OrderByDescending(p => p.Item2).ToList();
+                //    tempcells = tempcells.OrderBy(p => p.Item3).ToList();//按照同规则物料少的排序
+
+                //    //每个扫码固定楼层,如果固定楼层设备坏了或者库存满了,再换楼层
+                //    var taskcount = _taskrepository.AsQueryable().With(SqlWith.NoLock).Where(p => p.Type == TaskType.EnterDepot && p.Status < TaskStatus.Finish && p.Floor != 0).ToList();
+
+                //    if (tempcells[0].Item2 > (reqDto.Count + 10))
+                //    {
+                //        if (taskcount.Where(p => p.Floor == tempcells[0].Item1).Count() > floorTaskCount || tempcells[0].Item2 == 0)
+                //        {
+                //            if (taskcount.Where(p => p.Floor == tempcells[1].Item1).Count() > floorTaskCount || tempcells[1].Item2 == 0)
+                //            {
+                //                if (taskcount.Where(p => p.Floor == tempcells[2].Item1).Count() > floorTaskCount || tempcells[2].Item2 == 0)
+                //                {
+                //                    res.ResCode = ResponseStatusCodeEnum.Fail.GetHashCode();
+                //                    res.ResMsg = reqDto.Count + "扭转回库申请楼层失败,当前环线三个楼层执行中的任务数已满,不允许分配楼层";
+                //                    res.ResData = 0;
+                //                    return res;
+                //                }
+                //                else
+                //                {
+                //                    res.ResData = tempcells[2].Item1;
+                //                }
+                //            }
+                //            else
+                //            {
+                //                res.ResData = tempcells[1].Item1;
+                //            }
+                //        }
+                //        else
+                //        {
+                //            res.ResData = tempcells[0].Item1;
+                //        }
+                //    }
+                //    else
+                //    {
+                //        if (tempcells[1].Item2 > (reqDto.Count + 10))
+                //        {
+                //            if (taskcount.Where(p => p.Floor == tempcells[1].Item1).Count() > floorTaskCount || tempcells[1].Item2 == 0)
+                //            {
+                //                if (taskcount.Where(p => p.Floor == tempcells[2].Item1).Count() > floorTaskCount || tempcells[2].Item2 == 0)
+                //                {
+                //                    res.ResCode = ResponseStatusCodeEnum.Fail.GetHashCode();
+                //                    res.ResMsg = reqDto.Count + "扭转回库申请楼层失败,当前环线三个楼层执行中的任务数已满,不允许分配楼层";
+                //                    res.ResData = 0;
+                //                    return res;
+                //                }
+                //                else
+                //                {
+                //                    res.ResData = tempcells[2].Item1;
+                //                }
+                //            }
+                //            else
+                //            {
+                //                res.ResData = tempcells[1].Item1;
+                //            }
+                //        }
+                //        else
+                //        {
+                //            if (taskcount.Where(p => p.Floor == tempcells[2].Item1).Count() < floorTaskCount && tempcells[2].Item2 > 30)
+                //            {
+                //                res.ResData = tempcells[2].Item1;
+                //            }
+                //            else
+                //            {
+                //                res.ResCode = ResponseStatusCodeEnum.Fail.GetHashCode();
+                //                res.ResMsg = reqDto.Count + "扭转回库申请楼层失败,当前环线三个楼层执行中的任务数已满,不允许分配楼层";
+                //                res.ResData = 0;
+                //                return res;
+                //            }
+                //        }
+                //    }
             }
             catch (Exception ex)
             {