using PlcSiemens.Core.Extension;
using ServiceCenter.Extensions;
using ServiceCenter.Logs;
using ServiceCenter.SqlSugars;
using System.ComponentModel;
using System.Threading.Tasks;
using WCS.Core;
using WCS.Entity;
using WCS.Entity.Protocol.SRM;
using WCS.Entity.Protocol.Station;
using WCS.WorkEngineering.Extensions;
using WCS.WorkEngineering.WebApi.Controllers;
using WCS.WorkEngineering.WebApi.Models.AGV.Response;
using WCS.WorkEngineering.Worlds;
using static System.Runtime.InteropServices.JavaScript.JSType;
using DeviceFlags = WCS.WorkEngineering.Extensions.DeviceFlags;
using KnownException = ServiceCenter.Logs.KnownException;
using TaskStatus = WCS.Entity.TaskStatus;

namespace WCS.WorkEngineering.Systems
{
    /// <summary>
    /// 堆垛机系统
    /// </summary>
    [BelongTo(typeof(MainWorld))]
    [Description("堆垛机系统")]
    public class SrmSystems : DeviceSystem<SRM>
    {
        /// <summary>
        /// 取货点设备集合
        /// </summary>
        private Dictionary<string, List<Station>> PickUpDevices = new Dictionary<string, List<Station>>();

        /// <summary>
        /// 放货设备
        /// </summary>
        private Dictionary<string, List<Station>> PutDevices = new Dictionary<string, List<Station>>();

        public SrmSystems()
        {
            //获取所有的巷道集合
            var devices = Device.All.Where(v => v.HasFlag(DeviceFlags.巷道));

            //开始分配
            foreach (var item in devices)
            {
                //取货设备
                var srm = item.Targets.Where(v => v.HasFlag(DeviceFlags.堆垛机)).FirstOrDefault();
                PickUpDevices.Add(srm.Code, item.Targets.Where(v => v.HasFlag(DeviceFlags.输送机)).Select(v => new Station(v, this.World)).ToList());
                //放货设备
                srm = item.Sources.Where(v => v.HasFlag(DeviceFlags.堆垛机)).FirstOrDefault();
                PutDevices.Add(srm.Code, item.Sources.Where(v => v.HasFlag(DeviceFlags.输送机)).Select(v => new Station(v, this.World)).ToList());
            }
        }

        protected override bool ParallelDo => true;

        protected override bool SaveLogsToFile => true;

        public override void Do(SRM obj)
        {
            #region 处理完成任务

            //判断DB520 完成任务确认清除信号 是否为1
            if (obj.Data.OkAck == 1) throw new KnownException($"WCS任务完成确认信号[DB520.OkAck]未清除,请检查堆垛机处理异常原因", LogLevelEnum.Mid);

            if (obj.Data2.TaskFinishiId > 0)
            {
                //处理完成的任务信息
                WCS_TaskInfo taskInfo = null;
                //开始处理
                SqlSugarHelper.Do(db =>
                {
                    World.Log($"堆垛机任务处理:开始--完成任务{obj.Data2.TaskFinishiId}", LogLevelEnum.Low);

                    #region 获取完成任务

                    //根据DB521任务号获取对应任务
                    var task = db.Default.Queryable<WCS_TaskInfo>().First(v => v.ID == obj.Data2.TaskFinishiId && v.Device == obj.Entity.Code);
                    if (task == null) throw new KnownException($"堆垛机完成任务号{obj.Data2.TaskFinishiId},在WCS当前任务信息中未找到对应任务。", LogLevelEnum.High);
                    if (task.Status != Entity.TaskStatus.StackerExecution) throw new KnownException($"任务{task.ID}状态是{task.Status.GetDescription()}.堆垛机完成任务需要对应任务状态处于堆垛机执行中", LogLevelEnum.High);
                    //{
                    //    if (task.Type == TaskType.EmptyInit && task.Status == TaskStatus.ConveyorExecution)
                    //    {
                    //        var taskDtl = db.Default.Queryable<WCS_TaskDtl>().Where(v => v.ParentTaskCode == task.ID && v.Desc == "任务下发堆垛机执行").SplitTable(v => v.Take(2)).ToList();
                    //        if (!taskDtl.Any()) throw new KnownException($"任务{task.ID}状态是{task.Status.GetDescription()}.堆垛机完成任务需要对应任务状态处于堆垛机执行中。", LogLevelEnum.High);
                    //        World.Log($"堆垛机任务处理:结束--异常完成任务{obj.Data2.TaskFinishiId}异常", LogLevelEnum.Mid);
                    //    }
                    //    else
                    //}

                    #endregion 获取完成任务

                    //根据任务类型做不同的处理
                    switch (task.Type)
                    {
                        case TaskType.EnterDepot:
                            if (task.TaskGroupKey == "1")
                            {
                                //完成任务
                                task.Status = Entity.TaskStatus.ConveyorExecution;
                                var dev = new Station(Device.All.FirstOrDefault(v => v.Code == task.AddrTo), this.World);
                                dev.Data.TaskNumber = task.ID;
                                task.EedTime = DateTime.Now;
                                db.Default.Updateable(task).ExecuteCommand();
                                task.AddWCS_TASK_DTL(db, task.AddrTo, "二楼搬运空轮至一楼任务到达放货站台,堆垛机完成");
                            }
                            else if (task.ProdLine == 1)
                            {
                                //完成任务
                                task.Status = Entity.TaskStatus.ConveyorExecution;
                                var dev = new Station(Device.All.FirstOrDefault(v => v.Code == task.AddrTo), this.World);
                                dev.Data.TaskNumber = task.ID;
                                task.EedTime = DateTime.Now;
                                db.Default.Updateable(task).ExecuteCommand();
                                task.AddWCS_TASK_DTL(db, task.AddrTo, "一楼搬运满轮至二楼任务到达放货站台,堆垛机完成");
                            }
                            else
                            {
                                //完成任务
                                task.Status = Entity.TaskStatus.Finish;
                                task.EedTime = DateTime.Now;
                                db.Default.Updateable(task).ExecuteCommand();
                                task.AddWCS_TASK_DTL(db, task.AddrTo, "入库任务结束,堆垛机完成");
                            }
                          
                            break;

                        case TaskType.OutDepot:
                            switch (task.OutType)
                            {
                                case OutTypeEnum.自动出库任务:
                                    task.Status = Entity.TaskStatus.ConveyorExecution;
                                    var dev = new Station(Device.All.FirstOrDefault(v => v.Code == task.SrmStation), this.World);
                                    dev.Data.TaskNumber = task.ID;
                                    db.Default.Updateable(task).ExecuteCommand();
                                    task.AddWCS_TASK_DTL(db, task.SrmStation, "出库任务到达放货站台,堆垛机完成");
                                    break;

                                case OutTypeEnum.全自动手动出库任务:

                                    break;

                                case OutTypeEnum.半自动手动出库任务:
                                    task.Status = Entity.TaskStatus.Finish;
                                    task.EditTime = DateTime.Now;
                                    db.Default.Updateable(task).ExecuteCommand();
                                    task.AddWCS_TASK_DTL(db, task.SrmStation, "半自动手动出库任务结束,堆垛机完成");
                                    break;

                                default:
                                    break;
                            }

                            break;

                        case TaskType.TransferDepot:
                            task.Status = Entity.TaskStatus.Finish;
                            task.EedTime = DateTime.Now;
                            db.Default.Updateable(task).ExecuteCommand();
                            task.AddWCS_TASK_DTL(db, task.AddrTo, "移库任务结束,堆垛机完成");
                            break;

                        case TaskType.EmptyInit:
                            task.Status = Entity.TaskStatus.Finish;
                            task.EedTime = DateTime.Now;
                            db.Default.Updateable(task).ExecuteCommand();
                            task.AddWCS_TASK_DTL(db, task.AddrTo, "空轮初始化任务结束,堆垛机完成");
                            break;
                    }

                    //通知WMS任务完成
                    if (task.Status == TaskStatus.Finish) WmsApi.CompleteTask(task.ID);
                    if (task.Type == TaskType.OutDepot && task.Status == TaskStatus.ConveyorExecution) WmsApi.SrmPickOutCompleted(task.ID);

                    if (task.Status >= TaskStatus.Finish) task.CompleteOrCancelTasks(db);

                    taskInfo = task;
                });
                if (taskInfo == null) throw new KnownException("数据库提交事务错误", LogLevelEnum.High);
                // 写入信号
                obj.Data.OkAck = 1;
                //通知WMS任务完成
                //if (taskInfo.Status == TaskStatus.Finish) WmsApi.CompleteTask(taskInfo.ID);
                //if (taskInfo.Type == TaskType.OutDepot && taskInfo.Status == TaskStatus.ConveyorExecution) WmsApi.SrmPickOutCompleted(taskInfo.ID);



            }

            #endregion 处理完成任务

            //堆垛机是否可以下发任务
            if (obj.Data2.VoucherNo != obj.Data.VoucherNo) throw new KnownException($"凭证号不一致,DB520:{obj.Data.VoucherNo},DB521:{obj.Data2.VoucherNo}", LogLevelEnum.Mid);
            if (obj.Data2.AutoStatus != SrmAutoStatus.Automatic) throw new KnownException($"堆垛机处于{obj.Data2.AutoStatus.GetDescription()}模式", LogLevelEnum.Low);
            if (obj.Data2.RunStatus != SrmRunStatus.Idle) throw new KnownException($"堆垛机处于{obj.Data2.RunStatus.GetDescription()}状态", LogLevelEnum.High);

            //默认没有移库任务
            bool isTransfer = false;

            //出入库优先级任务 1:无优先 2:入库 3:出库
            int enterOrOut = 1;

            //再检查是否有等待执行的货物
            SqlSugarHelper.Do(db =>
            {
                //获取当前堆垛机的所有未完成任务
                var tasks = db.Default.Queryable<WCS_TaskInfo>().Where(v => v.Status < Entity.TaskStatus.Finish && v.Device == obj.Entity.Code);
                //任务集合是否有处于堆垛机执行状态的任务
                if (tasks.Any(v => v.Status == Entity.TaskStatus.StackerExecution)) throw new KnownException($"有任务处于堆垛机执行状态", LogLevelEnum.High);

                ////判断是否存在调整优先级任务
                //if (!tasks.Any(v => v.Type != TaskType.TransferDepot && v.Status < Entity.TaskStatus.StackerExecution && v.Priority > 0))
                //{
                //不存在调整优先级任务,判断是否存在移库任务
                isTransfer = tasks.Any(v => v.Type == TaskType.TransferDepot && v.Status == Entity.TaskStatus.NewBuild);
                //}
                if (!isTransfer) //存在调整优先级任务
                {
                    //获取出库任务中新建状态最大优先级
                    var outPriorityNewBuild = tasks.Where(v => v.Type == TaskType.OutDepot && v.Status == Entity.TaskStatus.WaitingToExecute).Max(v => v.Priority);
                    //获取入库任务中最大优先级
                    var enterPriority = tasks.Where(v => v.Type == TaskType.EnterDepot && v.Status < Entity.TaskStatus.StackerExecution).Max(v => v.Priority);
                    //出入库最大优先级相加大于零
                    if (outPriorityNewBuild + enterPriority > 0)
                    {
                        //出入库优先级任务 1:无优先 2:入库 3:出库
                        enterOrOut = enterPriority > outPriorityNewBuild ? 2 : 3;
                    }
                }

                #region 移库

                if (isTransfer)
                {
                    WCS_TaskInfo taskInfo = null;
                    SqlSugarHelper.Do(db =>
                    {
                        //获取一条当前堆垛机优先级最高的新建移库任务
                        var task = db.Default.Queryable<WCS_TaskInfo>().Where(v => v.Device == obj.Entity.Code && v.Type == TaskType.TransferDepot && v.Status == Entity.TaskStatus.NewBuild)
                                                                       .OrderByDescending(v => v.Priority)
                                                                       .First() ?? throw new KnownException("未找到移库任务", LogLevelEnum.High);
                        //任务状态改为堆垛机执行中
                        task.Status = Entity.TaskStatus.StackerExecution;
                        task.StartTime = DateTime.Now;
                        db.Default.Updateable(task).ExecuteCommand();
                        task.AddWCS_TASK_DTL(db, task.AddrFrom, task.Device, $"堆垛机{obj.Entity.Code}开始执行任务");
                        taskInfo = task;
                    });
                    if (taskInfo == null) throw new KnownException("数据更新错误", LogLevelEnum.High);
                    var addrFrom = taskInfo.AddrFrom.Split("-");
                    var addrTo = taskInfo.AddrTo.Split("-");
                    World.Log($"堆垛机任务处理:开始--下发移库任务[{obj.Data.TaskNumber}][{obj.Data.SLine}][{obj.Data.SCol}][{obj.Data.SLayer}][{obj.Data.ELine}][{obj.Data.ECol}][{obj.Data.ELayer}][{obj.Data.TaskType}][{obj.Data.VoucherNo}]", LogLevelEnum.Mid);
                    //下发任务
                    obj.Data.TaskNumber = taskInfo.ID;
                    obj.Data.SLine = addrFrom[0].ToShort();
                    obj.Data.SCol = addrFrom[1].ToShort();
                    obj.Data.SLayer = addrFrom[2].ToShort();
                    obj.Data.ELine = addrTo[0].ToShort();
                    obj.Data.ECol = addrTo[1].ToShort();
                    obj.Data.ELayer = addrTo[2].ToShort();
                    obj.Data.TaskType = SrmTaskTypeEnum.MoveGoods;
                    obj.Data.VoucherNo++;
                    World.Log($"堆垛机任务处理:结束---下发移库任务[{obj.Data.TaskNumber}][{obj.Data.SLine}][{obj.Data.SCol}][{obj.Data.SLayer}][{obj.Data.ELine}][{obj.Data.ECol}][{obj.Data.ELayer}][{obj.Data.TaskType}][{obj.Data.VoucherNo}]", LogLevelEnum.Mid);
                }

                #endregion 移库

                #region 出入库

                //上一个周期是不是出库任务 第一次获取返回结果会是false
                var lastIsOut = obj.Entity.GetFlag<bool>("LastIsOut");
                obj.Entity.SetFlag("LastIsOut", !lastIsOut);

                //入库任务优先 或 上一个周期是出库任务并且出库任务无优先
                if (enterOrOut == 2 || (lastIsOut && enterOrOut == 1)) //入库任务
                {
                    //判断本次优先执行楼层,并设置下次执行时优先楼层
                    var floor = obj.Entity.GetFlag<int>("FloorIn");
                    floor = floor % 2 + 1;
                    obj.Entity.SetFlag("FloorIn", floor);

                    //获取当前堆垛机所有的取货站台
                    var arrIn = PickUpDevices.First(v => v.Key == obj.Entity.Code).Value;
                    if (!arrIn.Any()) throw new KnownException($"堆垛机{obj.Entity.Code}无取货路径点", LogLevelEnum.High);

                    //获取有货的设备
                    arrIn = arrIn.Where(v => v.Data.TaskNumber > 0 && v.Data3.Status.HasFlag(StatusEunm.PH_Status) && !v.Data3.Status.HasFlag(StatusEunm.Run)).ToList();
                    if (!arrIn.Any()) throw new KnownException($"[{obj.Entity.Code}]等待入库任务输送到位", LogLevelEnum.Low);
                    WCS_TaskInfo taskInfo = null;
                    Station station = null;
                    //判断1楼空轮位是否满足直接放下去的条件
                    #region 二楼空轮搬运直接去一楼

                    bool isDirToStation = true;
                    string tostation = "";
                    #endregion
                    SqlSugarHelper.Do(db =>
                    {
                        //根据有货设备的任务号获取所有类型为入库状态为输送机执行中的任务
                        var tasks = db.Default.Queryable<WCS_TaskInfo>().Where(v => (v.Type == TaskType.EnterDepot || v.Type == TaskType.EmptyInit)
                                                                                  && v.Status == TaskStatus.ConveyorExecution
                                                                                  && arrIn.Select(p => p.Data.TaskNumber).Contains(v.ID) && v.Device == obj.Entity.Code);
                        //按条件先后排序获取一条排序后第一条结果1.优先级2.所在楼层与本次优先执行楼层 TODO:待验证排序结果
                        var task = tasks.OrderByDescending(v => v.Priority).OrderByDescending(v => v.Floor == floor ? 1 : 0).First() ?? throw new KnownException($"{obj.Entity.Code}未找到入库任务", LogLevelEnum.High);
                        if (task.BusType == "二楼湿拉空轮回立库")
                        {
                            var arrOut = PutDevices.First(v => v.Key == obj.Entity.Code).Value;

                            if (!arrOut.Any()) throw new KnownException($"堆垛机{obj.Entity.Code}无放货路径点", LogLevelEnum.High);
                            var list = new List<string>() { "1012", "1014", "1016", "1118", "1131", "1132", "1133", "1134", "1135", "1136", "1137", "1139" };
                            //获取可以放货的设备集合
                            arrOut = arrOut.Where(v => !v.Data3.Status.HasFlag(StatusEunm.PH_Status) //无光电
                                                            && !v.Data3.Status.HasFlag(StatusEunm.Run) //未运行
                                                            && !v.Data3.Status.HasFlag(StatusEunm.OT_Status) //无任务
                                                            && !v.Data3.Status.HasFlag(StatusEunm.UnassignedTask) //未分配任务
                                                            && list.Contains(v.Entity.Code)
                                                            && v.Data3.Status.HasFlag(StatusEunm.Auto)).ToList(); //自动

                            if (!arrOut.Any()) { isDirToStation = false; };

                            if (isDirToStation == true)
                            {
                                var allOutCode = arrOut.Select(v => v.Entity.Code).ToList();//一楼的出库站台
                                tostation = allOutCode.First();

                                var taskout = db.Default.Queryable<WCS_TaskInfo>().Where(v => v.Type == TaskType.OutDepot && v.Status == TaskStatus.StackerExecution && v.Device == obj.Entity.Code)
                                                                                .Where(v => allOutCode.Contains(v.SrmStation))
                                                                                .First();
                                if (taskout != null) { isDirToStation = false; };

                                if (isDirToStation == true)
                                {
                                    if (db.Default.Queryable<WCS_TaskInfo>().Any(v => v.AddrTo == tostation && v.Status == TaskStatus.ConveyorExecution)) { isDirToStation = false; };
                                }
                                if (isDirToStation == true)
                                {
                                    //判断是否有小于agv小车离开取货位状态的agv任务，有的话则不允许下发
                                    if (db.Default.Queryable<WCS_AgvTaskInfo>().Where(v => v.Station == tostation && v.AgvStatus < AGVTaskStatus.Complete3 && v.AgvStatus >= AGVTaskStatus.RequestOrPermission2).SplitTable(tabs => tabs.Take(2)).Any())
                                    {
                                        isDirToStation = false;
                                    }
                                }
                            }

                            //获取任务所有设备
                            station = arrIn.First(v => v.Data.TaskNumber == task.ID);
                            if (isDirToStation)
                            {
                                task.Status = TaskStatus.StackerExecution;
                                task.AddrTo = tostation;
                                task.LastInteractionPoint = station.Entity.Code;
                                task.AddrNext = tostation;
                                task.EditWho = "WCS";
                                task.TaskGroupKey = "1";
                                db.Default.Updateable(task).ExecuteCommand();
                                task.AddWCS_TASK_DTL(db, station.Entity.Code, task.AddrTo, "任务下发堆垛机执行,去一楼出库点");
                                taskInfo = task;
                            }
                            else//空轮子入库
                            {
                                //获取任务所有设备
                                station = arrIn.First(v => v.Data.TaskNumber == task.ID);
                                //获取当前货物巷道
                                var res = WmsApi.GetLocalIn(task.ID, task.Device, station.Entity.Code);
                                var loc = res.ResData.CellNo.Split("-");
                                task.Status = TaskStatus.StackerExecution;
                                task.AddrTo = $"{loc[0]}-{loc[1]}-{loc[2]}";
                                task.LastInteractionPoint = station.Entity.Code;
                                task.EditWho = "WCS";
                                db.Default.Updateable(task).ExecuteCommand();
                                task.AddWCS_TASK_DTL(db, station.Entity.Code, task.AddrTo, "任务下发堆垛机执行");
                                taskInfo = task;
                            }
                        }
                        else if (task.BusType == "镀铜一楼取满" && task.ProdLine == 1)//满轮越库
                        {
                            var arrOut = PutDevices.First(v => v.Key == obj.Entity.Code).Value;

                            if (!arrOut.Any()) throw new KnownException($"堆垛机{obj.Entity.Code}无放货路径点", LogLevelEnum.High);
                            var list = new List<string>() { "1022", "1024", "1026", "1128"};
                            //获取可以放货的设备集合
                            arrOut = arrOut.Where(v => !v.Data3.Status.HasFlag(StatusEunm.PH_Status) //无光电
                                                            && !v.Data3.Status.HasFlag(StatusEunm.Run) //未运行
                                                            && !v.Data3.Status.HasFlag(StatusEunm.OT_Status) //无任务
                                                            && !v.Data3.Status.HasFlag(StatusEunm.UnassignedTask) //未分配任务
                                                            && list.Contains(v.Entity.Code)
                                                            && v.Data3.Status.HasFlag(StatusEunm.Auto)).ToList(); //自动

                            if (!arrOut.Any()) { isDirToStation = false; };

                            if (isDirToStation == true)
                            {
                                var allOutCode = arrOut.Select(v => v.Entity.Code).ToList();//二楼的出库站台
                                tostation = allOutCode.First();

                                var taskout = db.Default.Queryable<WCS_TaskInfo>().Where(v => v.Type == TaskType.OutDepot && v.Status == TaskStatus.StackerExecution && v.Device == obj.Entity.Code)
                                                                                .Where(v => allOutCode.Contains(v.SrmStation))
                                                                                .First();
                                if (taskout != null) { isDirToStation = false; };

                                if (isDirToStation == true)
                                {
                                    if (db.Default.Queryable<WCS_TaskInfo>().Any(v => v.AddrTo == tostation && v.Status == TaskStatus.ConveyorExecution)) { isDirToStation = false; };
                                }
                                if (isDirToStation == true)
                                {
                                    //判断是否有小于agv小车离开取货位状态的agv任务，有的话则不允许下发
                                    if (db.Default.Queryable<WCS_AgvTaskInfo>().Where(v => v.Station == tostation && v.AgvStatus < AGVTaskStatus.Complete3 && v.AgvStatus >= AGVTaskStatus.RequestOrPermission2).SplitTable(tabs => tabs.Take(2)).Any())
                                    {
                                        isDirToStation = false;
                                    }
                                }
                            }

                            //获取任务所有设备
                            station = arrIn.First(v => v.Data.TaskNumber == task.ID);
                            if (isDirToStation)
                            {
                                task.Status = TaskStatus.StackerExecution;
                                task.AddrTo = tostation;
                                task.LastInteractionPoint = station.Entity.Code;
                                task.AddrNext = tostation;
                                task.EditWho = "WCS";
                                //task.TaskGroupKey = "1";
                                db.Default.Updateable(task).ExecuteCommand();
                                task.AddWCS_TASK_DTL(db, station.Entity.Code, task.AddrTo, "任务下发堆垛机执行,去二楼出库点");
                                taskInfo = task;
                            }
                            else//满轮入库
                            {
                                //station = arrIn.First(v => v.Data.TaskNumber == task.ID);
                                //var res = WmsApi.GetLocalIn(task.ID, task.Device, station.Entity.Code);
                                //var loc = res.ResData.CellNo.Split("-");
                                //task.Status = TaskStatus.StackerExecution;
                                //task.AddrTo = $"{loc[0]}-{loc[1]}-{loc[2]}";
                                //task.LastInteractionPoint = station.Entity.Code;
                                //task.EditWho = "WCS";
                                //db.Default.Updateable(task).ExecuteCommand();
                                //task.AddWCS_TASK_DTL(db, station.Entity.Code, task.AddrTo, "任务下发堆垛机执行");
                                //taskInfo = task;
                                throw new KnownException($"{obj.Entity.Code}不满足越库条件", LogLevelEnum.High);
                            }
                        }
                        else
                        {
                            //获取任务所有设备
                            station = arrIn.First(v => v.Data.TaskNumber == task.ID);
                            //获取当前货物巷道
                            var res = WmsApi.GetLocalIn(task.ID, task.Device, station.Entity.Code);
                            var loc = res.ResData.CellNo.Split("-");
                            task.Status = TaskStatus.StackerExecution;
                            task.AddrTo = $"{loc[0]}-{loc[1]}-{loc[2]}";
                            task.LastInteractionPoint = station.Entity.Code;
                            task.EditWho = "WCS";
                            db.Default.Updateable(task).ExecuteCommand();
                            task.AddWCS_TASK_DTL(db, station.Entity.Code, task.AddrTo, "任务下发堆垛机执行");
                            taskInfo = task;
                        }
                     
                    });

                    if (taskInfo == null) throw new KnownException("数据更新错误", LogLevelEnum.High);
                    if (isDirToStation && taskInfo.BusType == "二楼湿拉空轮回立库")
                    {
                        World.Log($"堆垛机任务处理:开始--下发搬运二楼至一楼空轮任务[{obj.Data.TaskNumber}][{obj.Data.SLine}][{obj.Data.SCol}][{obj.Data.SLayer}][{obj.Data.ELine}][{obj.Data.ECol}][{obj.Data.ELayer}][{obj.Data.TaskType}][{obj.Data.VoucherNo}]", LogLevelEnum.Mid);
                        //下发任务
                        obj.Data.TaskNumber = taskInfo.ID;
                        obj.Data.SLine = station.Entity.Code.ToShort(); ;
                        obj.Data.SCol = 0;
                        obj.Data.SLayer = 0;
                        obj.Data.ELine = tostation.ToShort();
                        obj.Data.ECol = 0;
                        obj.Data.ELayer = 0;
                        obj.Data.TaskType = SrmTaskTypeEnum.MoveGoods;
                        obj.Data.VoucherNo++;
                        World.Log($"堆垛机任务处理:结束---下发搬运二楼至一楼空轮任务[{obj.Data.TaskNumber}][{obj.Data.SLine}][{obj.Data.SCol}][{obj.Data.SLayer}][{obj.Data.ELine}][{obj.Data.ECol}][{obj.Data.ELayer}][{obj.Data.TaskType}][{obj.Data.VoucherNo}]", LogLevelEnum.Mid);
                    }
                    else if (isDirToStation && taskInfo.BusType == "镀铜一楼取满" && taskInfo.ProdLine == 1)
                    {
                        World.Log($"堆垛机任务处理:开始--下发搬运一楼至二楼满轮任务[{obj.Data.TaskNumber}][{obj.Data.SLine}][{obj.Data.SCol}][{obj.Data.SLayer}][{obj.Data.ELine}][{obj.Data.ECol}][{obj.Data.ELayer}][{obj.Data.TaskType}][{obj.Data.VoucherNo}]", LogLevelEnum.Mid);
                        //下发任务
                        obj.Data.TaskNumber = taskInfo.ID;
                        obj.Data.SLine = station.Entity.Code.ToShort(); ;
                        obj.Data.SCol = 0;
                        obj.Data.SLayer = 0;
                        obj.Data.ELine = tostation.ToShort();
                        obj.Data.ECol = 0;
                        obj.Data.ELayer = 0;
                        obj.Data.TaskType = SrmTaskTypeEnum.MoveGoods;
                        obj.Data.VoucherNo++;
                        World.Log($"堆垛机任务处理:结束---下发搬运一楼至二楼满轮任务[{obj.Data.TaskNumber}][{obj.Data.SLine}][{obj.Data.SCol}][{obj.Data.SLayer}][{obj.Data.ELine}][{obj.Data.ECol}][{obj.Data.ELayer}][{obj.Data.TaskType}][{obj.Data.VoucherNo}]", LogLevelEnum.Mid);
                    }
                    else
                    {
                        var addrTo = taskInfo.AddrTo.Split("-");
                        World.Log($"堆垛机任务处理:开始--下发入库任务[{obj.Data.TaskNumber}][{obj.Data.SLine}][{obj.Data.SCol}][{obj.Data.SLayer}][{obj.Data.ELine}][{obj.Data.ECol}][{obj.Data.ELayer}][{obj.Data.TaskType}][{obj.Data.VoucherNo}]", LogLevelEnum.Mid);
                        //下发任务
                        obj.Data.TaskNumber = taskInfo.ID;
                        obj.Data.TaskType = SrmTaskTypeEnum.Default;
                        obj.Data.SLine = station.Entity.Code.ToShort();
                        obj.Data.SCol = 0;
                        obj.Data.SLayer = 0;
                        obj.Data.ELine = addrTo[0].ToShort();
                        obj.Data.ECol = addrTo[1].ToShort();
                        obj.Data.ELayer = addrTo[2].ToShort();
                        obj.Data.VoucherNo++;
                        World.Log($"堆垛机任务处理:结束---下发入库任务[{obj.Data.TaskNumber}][{obj.Data.SLine}][{obj.Data.SCol}][{obj.Data.SLayer}][{obj.Data.ELine}][{obj.Data.ECol}][{obj.Data.ELayer}][{obj.Data.TaskType}][{obj.Data.VoucherNo}]", LogLevelEnum.Mid);
                    }
                  
                }
                else if (enterOrOut == 3 || !lastIsOut) //出库任务
                {
                    //判断本次优先执行楼层,并设置下次执行时优先楼层

                    var floor = obj.Entity.GetFlag<int>("FloorOut");

                    floor = floor % 2 + 1;
                    obj.Entity.SetFlag("FloorOut", floor);

                    //获取当前堆垛机所有的取货站台
                    var arrOut = PutDevices.First(v => v.Key == obj.Entity.Code).Value;

                    if (!arrOut.Any()) throw new KnownException($"堆垛机{obj.Entity.Code}无放货路径点", LogLevelEnum.High);

                    //获取可以放货的设备集合
                    arrOut = arrOut.Where(v => !v.Data3.Status.HasFlag(StatusEunm.PH_Status) //无光电
                                                    && !v.Data3.Status.HasFlag(StatusEunm.Run) //未运行
                                                    && !v.Data3.Status.HasFlag(StatusEunm.OT_Status) //无任务
                                                    && !v.Data3.Status.HasFlag(StatusEunm.UnassignedTask) //未分配任务
                                                    && v.Data3.Status.HasFlag(StatusEunm.Auto)).ToList(); //自动

                    //var devs = Device.All.Where(v => v.HasFlag(DeviceFlags.出库, DeviceFlags.巷道口)).Select(v => new Station(v, this.World)).ToList();
                    //var dev = devs.Find(v => v.Entity.Code == "1026");




                    if (!arrOut.Any()) throw new KnownException($"[{obj.Entity.Code}]等待出库任务输送到位", LogLevelEnum.Low);

                    WCS_TaskInfo taskInfo = null;

                    SqlSugarHelper.Do(db =>
                    {
                        var allOutCode = arrOut.Select(v => v.Entity.Code).ToList();

                        //按条件先后排序获取一条排序后第一条结果1.优先级2.所在楼层与本次优先执行楼层
                        var task = db.Default.Queryable<WCS_TaskInfo>().Where(v => v.Type == TaskType.OutDepot && v.Status == TaskStatus.WaitingToExecute && v.Device == obj.Entity.Code)
                                                                       .Where(v => allOutCode.Contains(v.SrmStation))
                                                                       .OrderByDescending(v => v.Priority)
                                                                       .OrderByDescending(v => v.Floor == floor ? 1 : 0)
                                                                       .OrderBy(v => v.AddTime)
                                                                       .First();
                        if (task == null) return;//throw new KnownException($"{obj.Entity.Code}未找到出库任务", LogLevelEnum.High);

                        string addrTo = task.SrmStation;
                        if (db.Default.Queryable<WCS_TaskInfo>().Any(v => v.AddrTo == task.AddrTo && v.Status == TaskStatus.ConveyorExecution)) return;
                        //判断agv任务存在没有离开取货位的，则卡控
                        if (db.Default.Queryable<WCS_AgvTaskInfo>().Where(v =>  v.Station == task.AddrTo && v.AgvStatus < AGVTaskStatus.Complete3 && v.AgvStatus>= AGVTaskStatus.RequestOrPermission2 ).SplitTable(tabs => tabs.Take(2)).Any())
                        {
                            return;
                        }
                        var taskId = task.ID;

                        //判断是否是二深位任务
                        var addrFrom = task.AddrFrom.Split("-");
                        if (addrFrom[4] == "02")
                        {
                            var res = WmsApi.AddWcsMoveTask(task.ID);
                            switch (res.ResData.ResType)
                            {
                                case WmsApiMoveTask.允许2升位执行:
                                    break;

                                case WmsApiMoveTask.执行移库任务:
                                    throw new KnownException($"堆垛机{obj.Entity.Code}需要先执行移库任务", LogLevelEnum.Mid);

                                case WmsApiMoveTask.一深位有出库任务:
                                    task = db.Default.Queryable<WCS_TaskInfo>()
                                                     .Where(v => v.Type == TaskType.OutDepot && v.Status == TaskStatus.WaitingToExecute)
                                                     .Where(v => allOutCode.Contains(v.SrmStation) && v.AddrFrom == res.ResData.CellNo)
                                                     .First() ?? throw new KnownException($"请尽快搬离{obj.Entity.Code}二楼出库口满轮，当前堆垛机出库任务呗阻塞", LogLevelEnum.Mid);
                                    break;
                            };
                            if (res.ResData.ResType == WmsApiMoveTask.执行移库任务) return;
                            if (res.ResData.ResType == WmsApiMoveTask.一深位有出库任务 && res.ResData.CellNo != task.AddrFrom) return;
                        }

                        task.Status = TaskStatus.StackerExecution;
                        task.LastInteractionPoint = task.Device;
                        task.EditWho = "WCS";
                        db.Default.Updateable(task).ExecuteCommand();
                        task.AddWCS_TASK_DTL(db, task.Device, task.SrmStation, "任务下发堆垛机执行");
                        taskInfo = task;
                    });

                    if (taskInfo == null) return;//throw new KnownException("数据更新错误", LogLevelEnum.High);

                    var addrFrom = taskInfo.AddrFrom.Split("-");
                    World.Log($"堆垛机任务处理:开始--下发出库任务[{obj.Data.TaskNumber}][{obj.Data.SLine}][{obj.Data.SCol}][{obj.Data.SLayer}][{obj.Data.ELine}][{obj.Data.ECol}][{obj.Data.ELayer}][{obj.Data.TaskType}][{obj.Data.VoucherNo}]", LogLevelEnum.Mid);
                    obj.Data.TaskNumber = taskInfo.ID;
                    obj.Data.SLine = addrFrom[0].ToShort();
                    obj.Data.SCol = addrFrom[1].ToShort();
                    obj.Data.SLayer = addrFrom[2].ToShort();
                    obj.Data.ELine = taskInfo.SrmStation.ToShort();
                    obj.Data.ECol = 0;
                    obj.Data.ELayer = 0;
                    obj.Data.TaskType = SrmTaskTypeEnum.Default;
                    obj.Data.VoucherNo++;
                    World.Log($"堆垛机任务处理:结束---下发出库任务[{obj.Data.TaskNumber}][{obj.Data.SLine}][{obj.Data.SCol}][{obj.Data.SLayer}][{obj.Data.ELine}][{obj.Data.ECol}][{obj.Data.ELayer}][{obj.Data.TaskType}][{obj.Data.VoucherNo}]", LogLevelEnum.Mid);
                }

                #endregion 出入库

            });

            
        }

        public override bool Select(Device dev)
        {
            //return dev.Code == "SRM2";
            return dev.Code is "SRM1" or "SRM2" or "SRM3" or "SRM4";
           
        }
    }
}
