//using Admin.NET.Core.BasicInformation.Enum;
//using Admin.NET.Core.TaskModule.Enum;
//using iWareModel.Entity.MES;
//using iWareModel.Entity.WCS;
//using iWareModel.Struct;
//using iWareSql.DataAccess;
//using iWareSql.MyDbContext;
//using System;
//using System.Collections.Generic;
//using System.ComponentModel;
//using System.Data.Entity;
//using System.Linq;
//namespace iWareCC.Common.Helper
//{
// ///
// /// 寻找空库位帮助类 【EditBy shaocx,2022-02-05】
// ///
// public class FindEmptyLocationHelper
// {
// #region 为入库寻找空库位
// ///
// /// 入库,1、2巷道寻找空库位
// ///
// /// 是否是移库任务
// /// 是否需要计算 移库预留空闲数
// ///
// ///
// /// 需要移动的托盘号
// ///
// ///
// /// 指定巷道
// ///
// public static bool FindEmptyLocatonListForLane12(bool isMoveTask, bool isNeedCalcReservedMoveLocationNum, MyDbContext mycontext, string WcsIp, string containerCode,
// ref int PalletType, ref CriterionContainerOutput output, int lane = 0)
// {
// int srmNo = 0;
// List useSrmList = new List();
// MyExtendHelper.GetDevicesInfo(mycontext, WcsIp, ref useSrmList);//判断一二巷道堆垛机是否故障
// if (useSrmList.Count == 0)
// {
// return false;
// }
// //获取 目标空库位列表
// var criterionRetionOutputs1 = _FindEmptyLocatonListForLane12(isNeedCalcReservedMoveLocationNum, mycontext, containerCode, ref PalletType, lane);
// if (criterionRetionOutputs1 == null || criterionRetionOutputs1.Count == 0)
// {
// return false;
// }
// if (isMoveTask == false)
// {//如果不是移库任务
// if (useSrmList.Count == 1)
// {
// srmNo = useSrmList.First();
// }
// else
// {
// var lane_1_list = criterionRetionOutputs1.Where(x => x.Lane == 1).Count();
// var lane_2_list = criterionRetionOutputs1.Where(x => x.Lane == 2).Count();
// srmNo = lane_1_list > lane_2_list ? 1 : 2;//谁的空库位多就优先入哪个巷道
// }
// //筛选 目标空库位列表
// output = FilterLocationForPutFor12(mycontext, srmNo, criterionRetionOutputs1);
// if (output == null)
// {
// if (srmNo == 1)
// {
// srmNo++;
// }
// else if (srmNo == 2)
// {
// srmNo--;
// }
// //筛选 目标空库位列表
// output = FilterLocationForPutFor12(mycontext, srmNo, criterionRetionOutputs1);
// if (output == null)
// {
// return false;
// }
// }
// }
// else
// {//是移库任务
// if (lane != 1 && lane != 2)
// {
// SystemWarningMsg._lbl_Alert_OutPlanTaskHandler = "堆垛机状态查询模拟";
// return false;
// }
// srmNo = lane;
// //筛选 目标空库位列表
// output = FilterLocationForPutFor12(mycontext, srmNo, criterionRetionOutputs1);
// if (output == null)
// {
// return false;
// }
// }
// return true;
// }
// ///
// /// 入库,寻找空库位列表 (1,2巷道专用)
// ///
// ///
// ///
// ///
// ///
// private static List _FindEmptyLocatonListForLane12(bool isNeedCalcReservedMoveLocationNum, MyDbContext mycontext, string containerCode, ref int PalletType, int lane = 0)
// {
// //var containers = GetWareContainerType(mycontext, containerCode);//查询容器类型
// var criterionRetionOutputs = GetLocationDetailsForEnableFor12(isNeedCalcReservedMoveLocationNum, mycontext, lane);
// if (criterionRetionOutputs == null || criterionRetionOutputs.Count == 0)
// {
// return criterionRetionOutputs;
// }
// //匹配容器规格合适的库位
// PalletType = 0;
// bool isLane3 = false;
// PalletType = MyExtendHelper.GetPalletType(mycontext, containerCode, ref isLane3);
// if (isLane3)//判断是否为托盘
// {
// throw new Exception("此方法不支持3巷道寻找空库位");
// }
// var criterionRetionOutputs1 = new List();
// if (PalletType == 2)
// {
// //criterionRetionOutputs1 = criterionRetionOutputs.Where(x => x.LocTypeId.ToString() == "321122000007237").ToList();
// criterionRetionOutputs1 = criterionRetionOutputs.Where(x => x.LocTypeId.ToString() == WmsStorageType.StorageType_小料箱.ToString()).ToList();
// if (criterionRetionOutputs1 == null || criterionRetionOutputs1.Count == 0)
// {
// //criterionRetionOutputs1 = criterionRetionOutputs.Where(x => x.LocTypeId.ToString() == "321121948225605").ToList();
// criterionRetionOutputs1 = criterionRetionOutputs.Where(x => x.LocTypeId.ToString() == WmsStorageType.StorageType_大料箱.ToString()).ToList();
// }
// }
// if (PalletType == 1)
// {
// //criterionRetionOutputs1 = criterionRetionOutputs.Where(x => x.LocTypeId.ToString() == "321121948225605").ToList();
// criterionRetionOutputs1 = criterionRetionOutputs.Where(x => x.LocTypeId.ToString() == WmsStorageType.StorageType_大料箱.ToString()).ToList();
// }
// return criterionRetionOutputs1;
// }
// ///
// /// 1,2巷道 入库任务,筛选目标空库位
// ///
// ///
// ///
// /// 要筛选的目标
// ///
// private static CriterionContainerOutput FilterLocationForPutFor12(MyDbContext mycontext, int Lane, List criterionRetionOutputs1)
// {
// var criterionRetionOutputs2 = new List();
// criterionRetionOutputs2 = criterionRetionOutputs1.Where(x => x.Lane == Lane && (x.Row == 1 || x.Row == 4)).ToList();//先放1,4列
// if (criterionRetionOutputs2 == null || criterionRetionOutputs2.Count <= 0)
// {
// criterionRetionOutputs2 = criterionRetionOutputs1.Where(x => x.Lane == Lane && (x.Row == 2 || x.Row == 3)).ToList();
// }
// else
// {
// criterionRetionOutputs2.AddRange(criterionRetionOutputs1.Where(x => x.Lane == Lane && (x.Row == 2 || x.Row == 3)).ToList());
// }
// List linShiList = new List();
// //匹配容器规格合适的库位
// CriterionContainerOutput output = null;
// //入库时判断内侧库位是否有未完成任务,如果有就找下一个库位
// if (criterionRetionOutputs2 != null && criterionRetionOutputs2.Count > 0)
// {
// var tasks = mycontext.wms_task.Where(x => (x.IsDeleted == null || x.IsDeleted == false))
// .Where(TaskHandler.CommonFilterExpressionForNoFinishAndNoCancel())
// .ToList();
// foreach (var item in criterionRetionOutputs2)
// {
// var arr = item.Code.Split('-');
// if (arr[2] == "2" || arr[2] == "3")
// {//如果找到的空库位是 外侧库位2和3
// if (arr[2] == "2")
// {
// var locationCode = arr[0] + "-" + arr[1] + "-1-" + arr[3];
// if (tasks == null || tasks.Count <= 0)
// {
// output = item;//空库位找到
// break;
// }
// var findTask = tasks.Where(x => x.ToLocationCode == locationCode || x.FromLocationCode == locationCode).FirstOrDefault();
// if (findTask == null)
// {
// output = item;//空库位找到
// break;
// }
// else
// {
// //任务状态 0-未下发WCS ,1-已下发WCS
// if (findTask.State != 1)
// {
// linShiList.Add(item);
// }
// }
// }
// if (arr[2] == "3")
// {
// var locationCode = arr[0] + "-" + arr[1] + "-4-" + arr[3];
// if (tasks == null || tasks.Count <= 0)
// {
// output = item;//空库位找到
// break;
// }
// var findTask = tasks.Where(x => x.ToLocationCode == locationCode || x.FromLocationCode == locationCode).FirstOrDefault();
// if (findTask == null)
// {
// output = item;//空库位找到
// break;
// }
// else
// {
// //任务状态 0-未下发WCS ,1-已下发WCS
// if (findTask.State != 1)
// {
// linShiList.Add(item);
// }
// }
// }
// }
// else
// {//如果找到的空库位是 内侧库位1和4
// var locationCode = "";
// if (arr[2] == "1")
// {
// locationCode = arr[0] + "-" + arr[1] + "-2-" + arr[3];
// }
// if (arr[2] == "4")
// {
// locationCode = arr[0] + "-" + arr[1] + "-3-" + arr[3];
// }
// var wareLocation = mycontext.ware_location.Where(x => x.IsLocked == 1 && (x.IsDeleted == null || x.IsDeleted == false) && x.Code == locationCode).FirstOrDefault();
// var wareLocationVsContainer = mycontext.ware_location_vs_container.Where(x => (x.IsDeleted == null || x.IsDeleted == false) && x.WareLocationCode == locationCode).FirstOrDefault();
// var noFinishTask = tasks.Where(x => x.ToLocationCode == locationCode || x.FromLocationCode == locationCode).FirstOrDefault();
// if (wareLocation == null && wareLocationVsContainer == null && noFinishTask == null)
// {//如果找到的空库位 外侧库位2和3 没有被锁定,并且没有库存,并且没有未结束的任务
// output = item;
// break;
// }
// }
// }
// }
// if (output == null)
// {
// //没办法了,移库只能进入 外侧库位2和3位置了 【Editby shaocx,2023-12-28】
// output = linShiList.FirstOrDefault();
// }
// return output;
// }
// ///
// /// 查找可用库位(仅支持1,2巷道)
// ///
// ///
// ///
// public static List GetLocationDetailsForEnableFor12(bool isNeedCalcReservedMoveLocationNum, MyDbContext mycontext, int lane)
// {
// ////查询库位未删除无货未锁定的库位
// string sql = @"SELECT A.Code,A.Name,A.Long,A.Width,A.High AS Height,A.Lane,A.[Column],A.Row,A.Layer,A.LocTypeId,A.IsDeleted,A.IsVirtual,A.IsLocked,
// (select top(1) WareLocationCode from ware_location_vs_container B where B.WareLocationCode=A.Code and B.IsDeleted =0 ) WareLocationCode
// FROM dbo.ware_location A WHERE A.IsDeleted = 0 AND A.IsVirtual = 0 and A.IsLocked=0 ";
// if (lane > 0)
// {
// sql += $" and A.Lane={lane} ";
// }
// var bb = mycontext.Database.SqlQuery(sql).ToList();
// //筛选出 WareLocationCode为NULL或者是空的库位,就表示 他是空库位
// bb = bb.Where(x => string.IsNullOrWhiteSpace(x.WareLocationCode)).ToList();
// //还需要筛选掉 当前有未结束任务占用的情况 【Editby shaocx,2023-03-08】
// List new_locationList = new List();
// var locationList = TaskHandler.GetLocationListByNoFinishedTask(mycontext);
// //判断这些库位,如果存在外侧库位,那么 里侧的库位也不能允许使用 【Editby shaocx,2023-03-08】
// foreach (var item in locationList)
// {
// var insideLocation = GetInsideLocation(item);
// if (!string.IsNullOrEmpty(insideLocation))
// {
// new_locationList.Add(insideLocation);
// }
// }
// new_locationList.AddRange(locationList);
// bb = bb.Where(x => !new_locationList.Contains(x.Code)).ToList();
// //如果是需要预留空闲库位,就预留出 【Editby shaocx,2023-04-08】
// if (isNeedCalcReservedMoveLocationNum)
// {
// // 移库预留空闲数计算 【Editby shaocx,2023-02-28】
// bb = MyExtendHelper.RemoveEmptyLocatonForReservedMoveLocationNumByLane12(mycontext, bb);
// }
// return bb;
// }
// ///
// /// 根据库位寻找 他的内测库位
// ///
// ///
// public static string GetInsideLocation(string locationCode)
// {
// var retCode = string.Empty;
// var arr = locationCode.Split('-');//这里只是立体库,不考虑平库 【Edity shaocx,2022-12-7】
// if (arr.Length != 4)
// {
// return retCode;
// }
// if (arr[2] == "2" || arr[2] == "3")
// {
// if (arr[2] == "2")
// {
// retCode = arr[0] + "-" + arr[1] + "-1-" + arr[3];
// }
// if (arr[2] == "3")
// {
// retCode = arr[0] + "-" + arr[1] + "-4-" + arr[3];
// }
// }
// return retCode;
// }
// ///
// /// 根据库位寻找 他的外测库位
// ///
// ///
// public static string GetWidesideLocation(string locationCode)
// {
// var retCode = string.Empty;
// var arr = locationCode.Split('-');//这里只是立体库,不考虑平库 【Edity shaocx,2022-12-7】
// if (arr.Length != 4)
// {
// return retCode;
// }
// if (arr[2] == "1")
// {
// retCode = arr[0] + "-" + arr[1] + "-2-" + arr[3];
// }
// if (arr[2] == "4")
// {
// retCode = arr[0] + "-" + arr[1] + "-3-" + arr[3];
// }
// return retCode;
// }
// ///
// /// 根据库位判断 他是否是内测库位
// ///
// ///
// /// true:是内侧库位,即1 4行;false:是外侧库位,即2 3 行
// public static bool IsInsideLocation(string locationCode)
// {
// var retCode = string.Empty;
// var arr = locationCode.Split('-');//这里只是立体库,不考虑平库 【Edity shaocx,2022-12-7】
// if (arr.Length != 4)
// {
// return false;
// }
// if (arr[2] == "1" || arr[2] == "4")
// {
// return true;
// }
// return false;
// }
// #endregion
// }
//}