using Newtonsoft.Json;
using ServiceCenter.Extensions;
using ServiceCenter.Logs;
using ServiceCenter.SqlSugars;
using System.ComponentModel;
using WCS.Core;
using WCS.Entity;
using WCS.Entity.Protocol.Station;
using WCS.Entity.Protocol.Truss;
using WCS.WorkEngineering.Extensions;
using WCS.WorkEngineering.WebApi.Controllers;
using WCS.WorkEngineering.Worlds;
using DeviceFlags = WCS.WorkEngineering.Extensions.DeviceFlags;
using WCS_CacheLine = WCS.Entity.WCS_CacheLine;
using WCS_CacheLineLoc = WCS.Entity.WCS_CacheLineLoc;

namespace WCS.WorkEngineering.Systems
{
    /// <summary>
    ///  桁架
    /// </summary>
    [BelongTo(typeof(TrussWorld))]
    [Description("桁架")]
    public class 桁架 : DeviceSystem<Truss>
    {
        protected override bool ParallelDo => true;

        /// <summary>
        ///  取货点设备集合
        /// </summary>
        private readonly Dictionary<Device<IStation524, IStation523>, List<Device<IStation524, IStation523>>> _pickUpDevices = new();

        public override void Do(Truss obj)
        {
            if (obj.Data.VoucherNo != obj.Data2.VoucherNo)
            {
                World.Log($"凭证号不一致,DB520:{obj.Data.VoucherNo}-DB521:{obj.Data2.VoucherNo}");
                return;
            }

            if (obj.Data2.CmdType == 1)
            {
                var isFinish = false;
                var isEnd = false; //是否强制结盘
                SqlSugarHelper.Do(_db =>
                {
                    var db = _db.Default;
                    List<int> ids = new List<int>() { obj.Data2.PalletizingRowId1, obj.Data2.PalletizingRowId2 };
                    var palletizingRow = db.Queryable<WCS_PalletizingRow>().Includes(x => x.CacheLine, c => c.Locations)
                                                                                                .Includes(x => x.Locs)
                                                                                                .Includes(x => x.Palletizing).Where(x => ids.Contains(x.Id)).ToList();
                    List<WCS.Entity.WCS_CacheLine> deleteCacheLine = new List<WCS.Entity.WCS_CacheLine>();
                    List<WCS_PalletizingRow> upDateableRow = new List<WCS_PalletizingRow>();
                    List<WCS_PalletizingLoc> upDateableLoc = new List<WCS_PalletizingLoc>();
                    List<WCS_CacheLineLoc> deleteLoc = new List<WCS_CacheLineLoc>();

                    //更新行数据
                    foreach (var row in palletizingRow)
                    {
                        var cacheLine = db.Queryable<WCS.Entity.WCS_CacheLine>().Includes(x => x.Locations).Single(x => x.Id == row.CacheLine.Id);
                        var outTime = cacheLine.Locations.MinBy(x => x.TimeOut).TimeOut;
                        //时间超六小时，且缓存线数量与码垛信息行数量不相等时强制结盘
                        if (cacheLine.AddTime < DateTime.Now.AddHours(-outTime) && cacheLine.Locations.Count(x => x.InStock) != row.QtyMaxCount)
                        {
                            isEnd = true;
                        }

                        //先更新对应的位信息
                        try
                        {
                            var index = 0;
                            foreach (var loc in row.Locs.Where(x => !x.IsEmpty).OrderBy(x => x.XYNo))
                            {
                                if (index < cacheLine.Locations.Count)
                                {
                                    var cacheLoc = cacheLine.Locations[index];
                                    cacheLoc = db.Queryable<WCS_CacheLineLoc>().Single(x => x.Id == cacheLoc.Id);
                                    loc.TaskId = cacheLoc.TaskId;
                                    loc.Finish = true;

                                    upDateableLoc.Add(loc);
                                    deleteLoc.Add(cacheLoc);
                                    index++;
                                }
                            }
                        }
                        catch (Exception a)
                        {
                            throw new KnownException(a.Message, LogLevelEnum.High);
                        }

                        row.Finish = true;
                        upDateableRow.Add(row);
                        deleteCacheLine.Add(cacheLine);
                    }
                    //更新层数据

                    db.UpdateableRowLock(upDateableLoc).UpdateColumns(x => new { x.TaskId, x.Finish }).ExecuteCommand();
                    db.DeleteableRowLock(deleteLoc).ExecuteCommand();
                    db.UpdateableRowLock(upDateableRow).UpdateColumns(x => new { x.Finish }).ExecuteCommand();
                    db.DeleteableRowLock(deleteCacheLine).ExecuteCommand();

                    var layerId = palletizingRow.FirstOrDefault().PalletizingLayerId;
                    var layer = db.Queryable<WCS_PalletizingLayer>().Includes(x => x.Rows).Single(x => x.Id == layerId);
                    if (layer.Rows.All(x => x.Finish || x.IsEmpty))
                    {
                        layer.Finish = true;
                        db.UpdateableRowLock(layer).UpdateColumns(x => new { x.Finish }).ExecuteCommand();
                    }

                    //码垛完成后开始绑定托盘
                    var palletizingId = upDateableRow.First().Palletizing.Id;
                    var palletizing = db.Queryable<WCS_Palletizing>().Single(x => x.Id == palletizingId);
                    palletizing.EditTime = DateTime.Now;
                    db.UpdateableRowLock(palletizing).UpdateColumns(x => new { x.EditTime }).ExecuteCommand();
                    var taskId = upDateableRow.SelectMany(x => x.Locs).Select(x => x.TaskId).ToList();
                    var taskList = db.Queryable<WCS_TaskInfo>().RowLock().Where(x => taskId.Contains(x.ID)).Select(x => x.BarCode).ToList();
                    WmsApi.BingPallet(palletizing.PalleCode, taskList, palletizing.WarehouseCode, palletizing.PalletizingStation);
                    isFinish = true;
                });
                if (!isFinish)
                {
                    World.Log("绑盘失败");
                    return;
                }

                if (isEnd)
                {
                    try
                    {
                        var dev = new Device<ITruss530>(Device.All.First(x => x.Code == obj.Data.TargetPallte1.ToString()), World);
                        dev.Data.CmdType = TrussCmdType.End1;
                    }
                    catch (Exception e)
                    {
                        World.Log($"超时托盘自动结盘失败:{e.Message}--{obj.Data.TargetPallte1}");
                    }
                }
                obj.Data.CmdType = 1;
                obj.Data.DestPosition_1 = 0;
                obj.Data.TargetPallte1 = 0;
                obj.Data.Task1_1 = 0;
                obj.Data.Dest1_1 = 0;
                obj.Data.Task1_2 = 0;
                obj.Data.Dest1_2 = 0;
                obj.Data.Task1_3 = 0;
                obj.Data.Dest1_3 = 0;
                obj.Data.Task1_4 = 0;
                obj.Data.Dest1_4 = 0;
                obj.Data.Task1_5 = 0;
                obj.Data.Dest1_5 = 0;
                obj.Data.TaskSum1 = 0;
                obj.Data.PalletizingRowId1 = 0;
                obj.Data.DestPosition_2 = 0;
                obj.Data.TargetPallte2 = 0;
                obj.Data.Task2_1 = 0;
                obj.Data.Dest2_1 = 0;
                obj.Data.Task2_2 = 0;
                obj.Data.Dest2_2 = 0;
                obj.Data.Task2_3 = 0;
                obj.Data.Dest2_3 = 0;
                obj.Data.Task2_4 = 0;
                obj.Data.Dest2_4 = 0;
                obj.Data.Task2_5 = 0;
                obj.Data.Dest2_5 = 0;
                obj.Data.TaskSum2 = 0;
                obj.Data.PalletizingRowId2 = 0;
            }
            if (obj.Data2.CmdType == 0 && obj.Data.CmdType != 0) obj.Data.CmdType = 0;
            if (obj.Data2.CmdType != 0 || obj.Data.CmdType != 0)
            {
                World.Log($"完成信号未清除:{obj.Data2.CmdType}--{obj.Data.CmdType}");
                return;
            }

            if (obj.Data2.Status != TrussStatus.Idle)
            {
                World.Log($"桁架处于{obj.Data2.Status.GetDescription()}");
                return;
            }

            //查找所有的可用开始搬运的线体
            var pickUpDevices = _pickUpDevices.Where(v => v.Value.All(x => !x.Data2.Status.HasFlag(StationStatus.Run) && x.Data2.Status.HasFlag(StationStatus.Auto)))
                .Select(x => new
                {
                    key = x.Key.Entity.Code,
                    Count = x.Value.Count(v => v.Data2.Status.HasFlag(StationStatus.PH_Status) && v.Data.TaskNumber > 0),
                    HPCount = x.Value.Count(v => v.Data2.Status.HasFlag(StationStatus.PH_Status)),
                    Dev = x
                }).Where(x => x.HPCount == x.Count).ToList();
            if (!pickUpDevices.Any())
            {
                World.Log($"没有可用取货点,请在电控触摸屏检查实际可取货点是否每个位置都有光电与任务号");
                return;
            }

            //判断本次周期应该优先取哪一侧的任务
            var warehouseCode = obj.Entity.GetFlag<string>("warehouseCode"); //获取对应的Key
            if (warehouseCode == default(string)) //如果获取结果为默认值表示系统初始化，系统初始化时默认优先北侧
            {
                warehouseCode = "N";
                obj.Entity.SetFlag<string>("warehouseCode", "N");
            }
            var quantity = obj.Entity.GetFlag<int>(warehouseCode); //获取当前侧执行的次数
            var maxQuantity = 3; //南北库的周期检查比为3:3
            if (quantity >= maxQuantity) //如果当前侧检查周期大于等于最大检查周期，切换到另一侧
            {
                obj.Entity.SetFlag(warehouseCode, 0);
                warehouseCode = warehouseCode == "N" ? "S" : "N"; //如果是北侧切换到南侧  如果是南侧切换到北侧
                quantity = obj.Entity.GetFlag<int>(warehouseCode); //获取南库的侧执行的次数
                obj.Entity.SetFlag<string>("warehouseCode", warehouseCode);
            }

            List<WCS_PalletizingRow> palletizingRowList = null;
            WCS_Palletizing palletizing = null;
            SqlSugarHelper.Do(_db =>
            {
                var db = _db.Default;
                //找到当前桁架所属所有还有层没结束的码垛记录信息,有对应托盘
                var palletizingLsit = db.Queryable<WCS_Palletizing>().Includes(x => x.Layers, r => r.Rows, l => l.Locs)
                    .Where(x => !x.Finish && x.DeviceCode == obj.Entity.Code && x.WarehouseCode.Contains(warehouseCode))
                    .Where(x => x.Layers.Any(l => !l.Finish) && x.PalletizingStation != null && x.PalleCode != null).ToList();
                //筛选出目标位置有光电的码垛记录信息
                var devCode = palletizingLsit.Select(x => x.PalletizingStation);
                var taskCode = palletizingLsit.Select(x => x.Id);
                var devList = Device.All.Where(x => devCode.Contains(x.Code)).Select(x => new Device<IStation521, IStation523, ITruss530, ITruss531>(x, World)).ToList();
                devList = devList.Where(x => x.Data2.Status.HasFlag(StationStatus.PH_Status) && taskCode.Contains(x.Data.TaskNumber) && !x.Data4.CmdType.HasFlag(TrussCmdType.End1)
                && !x.Data4.CmdType.HasFlag(TrussCmdType.End)).ToList();
                devCode = devList.Select(x => x.Entity.Code);
                if (!devCode.Any())
                {
                    World.Log($"无可用放货码垛位，请检查实际有货位置的光电是否正常");
                    obj.Entity.SetFlag(warehouseCode, maxQuantity + 1); //当前侧无满足抓取条件的码垛信息直接进行切侧
                    return;
                }

                World.Log($"可用放货位:{JsonConvert.SerializeObject(devCode)}");
                palletizingLsit = palletizingLsit.Where(x => devCode.Contains(x.PalletizingStation)).ToList(); //可以放货的目标托盘
                //可以取货的码垛信息
                var pickUpCode = _pickUpDevices.Select(x => x.Key.Entity.Code.ToShort());
                World.Log($"可用取货线体1:{JsonConvert.SerializeObject(pickUpCode)}");

                //从缓存线信息找到可以取货的缓存线
                var cacheLines = db.Queryable<WCS_CacheLine>().Includes(x => x.Locations).Where(x => pickUpCode.Contains(x.LocationNo)).ToList().Where(
                    x =>
                    {
                        var outTime = x.Locations.MinBy(l => l.TimeOut).TimeOut;
                        var isTime = x.AddTime < DateTime.Now.AddHours(-outTime);
                        var isPut = pickUpDevices.Any(p => p.key == x.LocationNo.ToString() && p.Count == x.Quantity);
                        var count = x.Locations.Count(l => l.InStock);
                        //满足取货条件，或者是超六小时
                        return isPut || (pickUpDevices.Any(p => p.key == x.LocationNo.ToString() && p.Count == count) && isTime);
                    });
                if (!cacheLines.Any())
                {
                    World.Log($"无可用取货线体");
                    obj.Entity.SetFlag(warehouseCode, maxQuantity + 1); //当前侧无满足抓取条件的码垛信息直接进行切侧
                    return;
                }
                World.Log($"物理可用取货线体:{JsonConvert.SerializeObject(pickUpDevices.Select(x => x.key).ToList())}");
                World.Log($"执行记录:可用取货线体:{JsonConvert.SerializeObject(cacheLines.Select(x => x.LocationNo).ToList())}");
                //找到可用缓存线可以去的码垛信息
                palletizingLsit = palletizingLsit.Where(x => x.Layers
                    .Where(w => !w.Finish)
                    .SelectMany(w => w.Rows)
                    .Where(v => !v.Finish).Any(w =>
                    {
                        return cacheLines.Any(a =>
                        {
                            var outTime = a.Locations.MinBy(l=>l.TimeOut).TimeOut;
                            return a.MatCodeList == w.MatCodeList && (a.Quantity == w.QtyMaxCount || (a.AddTime < DateTime.Now.AddHours(-outTime) && a.Quantity <= w.QtyMaxCount)) && a.WarehouseCode == w.WarehouseCode;
                        });
                    })).OrderBy(x => x.AddTime).ToList();
                if (!palletizingLsit.Any())
                {
                    World.Log($"{warehouseCode}没有可用取货点,码垛信息是否正常");
                    obj.Entity.SetFlag(warehouseCode, maxQuantity + 1); //当前侧无满足抓取条件的码垛信息直接进行切侧
                    return;
                }

                foreach (var palletizingInfo in palletizingLsit)
                {
                    //已分配完成，或无信息。进入下一次迭代
                    if (palletizingInfo == null || (palletizingRowList != null && palletizing != null)) continue;

                    //筛选出已经预锁的行
                    var palletizingRow = palletizingInfo.Layers
                        .Where(x => x is { Finish: false, IsEmpty: false }) //不空且未结束的层
                        .MinBy(x => x.LayerNo) //取最下层
                        .Rows
                        .Where(x => x is { Finish: false, IsEmpty: false, LineCode: null }) //不空且未结束的行
                        .Where(x => cacheLines.Any(a =>
                        {
                            var outTime = a.Locations.MinBy(l => l.TimeOut).TimeOut;
                            return a.MatCodeList == x.MatCodeList && (a.Quantity == x.QtyMaxCount || (a.AddTime < DateTime.Now.AddHours(-outTime) && a.Quantity <= x.QtyMaxCount)) && a.WarehouseCode == x.WarehouseCode;
                        })); //获取可以取货的码垛信息行

                    //无可用行，进入下一次迭代
                    if (!palletizingRow.Any())
                    {
                        World.Log($"{palletizingInfo.Id}");
                        continue;
                    }

                    var rowIds = new List<int>();

                    //记录当前放货点可用规格包含的取货信息
                    cacheLines = cacheLines.Where(x => x.WarehouseCode == palletizingInfo.WarehouseCode && palletizingInfo.MatCodeList.Contains(x.MatCodeList)).ToList();
                    World.Log($"执行记录:码垛位{palletizingInfo.PalletizingStation}--{JsonConvert.SerializeObject(cacheLines.Select(x => x.LocationNo).ToList())}");

                    //跟据缓存信息找到对应的可用取货行
                    var rowList = cacheLines.Select(x =>
                    {
                        var row = palletizingRow.Where(w =>
                        {
                            var outTime = w.Locs.MinBy(l => l.TimeOut).TimeOut;
                            return !rowIds.Contains(w.Id)
                                   && x.MatCodeList == w.MatCodeList
                                   && (x.Quantity == w.QtyMaxCount || (x.AddTime < DateTime.Now.AddHours(-outTime) &&
                                                                       x.Quantity <= w.QtyMaxCount))
                                   && w.WarehouseCode == x.WarehouseCode;
                        }).MinBy(o => o.RowNo);

                        if (row != null)
                        {
                            rowIds.Add(row.Id);
                            return new
                            {
                                CacheLineId = x.Id,
                                RowId = row.Id,
                                x.LocationNo,
                                row.RowNo,
                                x.AddTime
                            };
                        }
                        return new
                        {
                            CacheLineId = 0,
                            RowId = 0,
                            x.LocationNo,
                            RowNo = 0,
                            x.AddTime
                        };
                    }).Where(x => x.CacheLineId != 0).ToList();

                    rowList = rowList.OrderBy(x => x.RowNo).Take(2).ToList();

                    //两个任务取货线体号相同时，只执行一个任务
                    if (rowList.Count == 2 && rowList[0].LocationNo == rowList[1].LocationNo)
                    {
                        World.Log($"单抓拆分:取货线体号相同{rowList[0].LocationNo}-{rowList[1].LocationNo}");
                        rowList = rowList!.Take(1).ToList();
                    }

                    //两个行号，差值大于一时，只执行一个任务
                    if (rowList.Count == 2 && rowList[0].RowNo + 1 != rowList[1].RowNo)
                    {
                        World.Log($"单抓拆分:行号拆分{rowList[0].RowNo}-{rowList[1].RowNo}");
                        rowList = rowList!.Take(1).ToList();
                    }

                    //如果取货行号是在18取货区域，只执行一个任务
                    if (rowList.Count == 2)
                    {
                        var locationNos = rowList.Select(x => x.LocationNo);
                        var devCodes = Device.All.Where(x => x.HasFlag(DeviceFlags.桁架18取货点)).Select(x => x.Code.ToShort());
                        if (devCodes.Any(x => locationNos.Contains(x)))
                        {
                            World.Log($"单抓拆分:货物为18");
                            rowList = rowList!.Take(1).ToList();
                        }
                    }

                    // 分拣一强制全部单抓，线体存在干涉
                    if (obj.Entity.Code == "Truss1") rowList = rowList!.Take(1).ToList();

                    var palletizingRows = new List<WCS_PalletizingRow>();

                    foreach (var row in rowList)
                    {
                        //锁定缓存线
                        var cacheLine = db.Queryable<WCS_CacheLine>().Includes(x => x.Locations).Single(x => x.Id == row.CacheLineId);
                        cacheLine.IsTruss = true;
                        cacheLine.PalletizingRowId = row.RowId;
                        db.UpdateableRowLock(cacheLine).UpdateColumns(x => new { x.IsTruss, x.PalletizingRowId }).ExecuteCommand();
                        //解锁原有码垛信息
                        var oldRow = db.Queryable<WCS_PalletizingRow>().First(x => x.CacheLineId == row.CacheLineId);
                        if (oldRow != null)
                        {
                            oldRow.CacheLineId = 0;
                            oldRow.LineCode = null;
                            db.UpdateableRowLock(oldRow).UpdateColumns(x => new { x.CacheLineId, x.LineCode }).ExecuteCommand();
                        }

                        //锁定现有码垛信息
                        var wcsRow = db.Queryable<WCS_PalletizingRow>().Includes(x => x.Locs).First(x => x.Id == row.RowId);
                        wcsRow.CacheLineId = row.CacheLineId;
                        wcsRow.LineCode = row.LocationNo.ToString();
                        db.UpdateableRowLock(wcsRow).UpdateColumns(x => new { x.CacheLineId, x.LineCode }).ExecuteCommand();
                        palletizingRows.Add(wcsRow);
                    }

                    palletizingRowList = palletizingRows.OrderBy(x => x.RowNo).ToList();
                    palletizing = palletizingInfo;
                }
            });

            if (palletizingRowList == null || !palletizingRowList.Any())
            {
                World.Log($"没有可用任务");
                obj.Entity.SetFlag(warehouseCode, maxQuantity + 1); //当前侧无满足抓取条件的码垛信息直接进行切侧
                return;
            }
            obj.Entity.SetFlag(warehouseCode, quantity + 1); //满足抓取条件，当前侧的执行周期加一
            for (var i = 0; i < palletizingRowList.Count; i++)
            {
                var palletizingRow = palletizingRowList[i];
                var locs = palletizingRow.Locs.OrderBy(x => x.XYNo.ToInt()).ToArray();
                var devs = pickUpDevices.FirstOrDefault(x => x.key == palletizingRow.LineCode).Dev.Value.Where(x => x.Data2.Status.HasFlag(StationStatus.PH_Status))
                    .OrderBy(x => x.Entity.Code.ToShort()).ToArray();

                switch (i)
                {
                    case 0:
                        obj.Data.DestPosition_1 = palletizingRow.LineCode!.ToShort();
                        obj.Data.TargetPallte1 = palletizing.PalletizingStation!.ToShort();
                        if (devs.Length >= 1)
                        {
                            obj.Data.Task1_1 = devs[0].Data.TaskNumber;
                            obj.Data.Dest1_1 = locs[0].XYNo.ToShort();
                        }

                        if (devs.Length >= 2)
                        {
                            obj.Data.Task1_2 = devs[1].Data.TaskNumber;
                            obj.Data.Dest1_2 = locs[1].XYNo.ToShort();
                        }
                        if (devs.Length >= 3)
                        {
                            obj.Data.Task1_3 = devs[2].Data.TaskNumber;
                            obj.Data.Dest1_3 = locs[2].XYNo.ToShort();
                        }
                        if (devs.Length >= 4)
                        {
                            obj.Data.Task1_4 = devs[3].Data.TaskNumber;
                            obj.Data.Dest1_4 = locs[3].XYNo.ToShort();
                        }
                        if (devs.Length == 5)
                        {
                            obj.Data.Task1_5 = devs[4].Data.TaskNumber;
                            obj.Data.Dest1_5 = locs[4].XYNo.ToShort();
                        }

                        obj.Data.TaskSum1 = devs.Length.ToShort();
                        obj.Data.PalletizingRowId1 = palletizingRow.Id;
                        break;

                    case 1:
                        obj.Data.DestPosition_2 = palletizingRow.LineCode!.ToShort();
                        obj.Data.TargetPallte2 = palletizing.PalletizingStation!.ToShort();

                        if (devs.Length >= 1)
                        {
                            obj.Data.Task2_1 = devs[0].Data.TaskNumber;
                            obj.Data.Dest2_1 = locs[0].XYNo.ToShort();
                        }
                        if (devs.Length >= 2)
                        {
                            obj.Data.Task2_2 = devs[1].Data.TaskNumber;
                            obj.Data.Dest2_2 = locs[1].XYNo.ToShort();
                        }
                        if (devs.Length >= 3)
                        {
                            obj.Data.Task2_3 = devs[2].Data.TaskNumber;
                            obj.Data.Dest2_3 = locs[2].XYNo.ToShort();
                        }
                        if (devs.Length >= 4)
                        {
                            obj.Data.Task2_4 = devs[3].Data.TaskNumber;
                            obj.Data.Dest2_4 = locs[3].XYNo.ToShort();
                        }
                        if (devs.Length == 5)
                        {
                            obj.Data.Task2_5 = devs[4].Data.TaskNumber;
                            obj.Data.Dest2_5 = locs[4].XYNo.ToShort();
                        }

                        obj.Data.TaskSum2 = devs.Length.ToShort();
                        obj.Data.PalletizingRowId2 = palletizingRow.Id;
                        break;
                }
            }

            obj.Data.VoucherNo++;
        }

        public override bool Select(Device dev)
        {
            return dev.Code is "Truss1" or "Truss2" or "Truss3";
        }

        public 桁架()
        {
            var trussList = Device.All.Where(x => x.HasFlag(DeviceFlags.桁架));
            foreach (var truss in trussList)
            {
                switch (truss.Code)
                {
                    case "Truss1":
                        _pickUpDevices.Add(new Device<IStation524, IStation523>(Device.All.First(x => x.Code == "461"), World), Device.All.Where(x => x.Code is "461" or "462" or "463" or "464" or "465").Select(x => new Device<IStation524, IStation523>(x, World)).ToList());
                        _pickUpDevices.Add(new Device<IStation524, IStation523>(Device.All.First(x => x.Code == "475"), World), Device.All.Where(x => x.Code is "475" or "476" or "477" or "478" or "479").Select(x => new Device<IStation524, IStation523>(x, World)).ToList());
                        _pickUpDevices.Add(new Device<IStation524, IStation523>(Device.All.First(x => x.Code == "489"), World), Device.All.Where(x => x.Code is "490" or "491" or "492" or "493" or "489").Select(x => new Device<IStation524, IStation523>(x, World)).ToList());
                        _pickUpDevices.Add(new Device<IStation524, IStation523>(Device.All.First(x => x.Code == "503"), World), Device.All.Where(x => x.Code is "503" or "504" or "505" or "506" or "507").Select(x => new Device<IStation524, IStation523>(x, World)).ToList());
                        _pickUpDevices.Add(new Device<IStation524, IStation523>(Device.All.First(x => x.Code == "517"), World), Device.All.Where(x => x.Code is "517" or "518" or "519" or "520" or "521").Select(x => new Device<IStation524, IStation523>(x, World)).ToList());
                        _pickUpDevices.Add(new Device<IStation524, IStation523>(Device.All.First(x => x.Code == "531"), World), Device.All.Where(x => x.Code is "532" or "533" or "534" or "535" or "531").Select(x => new Device<IStation524, IStation523>(x, World)).ToList());
                        _pickUpDevices.Add(new Device<IStation524, IStation523>(Device.All.First(x => x.Code == "545"), World), Device.All.Where(x => x.Code is "546" or "547" or "548" or "549" or "545").Select(x => new Device<IStation524, IStation523>(x, World)).ToList());
                        _pickUpDevices.Add(new Device<IStation524, IStation523>(Device.All.First(x => x.Code == "559"), World), Device.All.Where(x => x.Code is "559" or "560" or "561" or "562" or "563").Select(x => new Device<IStation524, IStation523>(x, World)).ToList());
                        _pickUpDevices.Add(new Device<IStation524, IStation523>(Device.All.First(x => x.Code == "573"), World), Device.All.Where(x => x.Code is "573" or "574" or "575" or "576" or "577").Select(x => new Device<IStation524, IStation523>(x, World)).ToList());
                        _pickUpDevices.Add(new Device<IStation524, IStation523>(Device.All.First(x => x.Code == "586"), World), Device.All.Where(x => x.Code is "586" or "587" or "588" or "589" or "590").Select(x => new Device<IStation524, IStation523>(x, World)).ToList());
                        _pickUpDevices.Add(new Device<IStation524, IStation523>(Device.All.First(x => x.Code == "661"), World), Device.All.Where(x => x.Code is "661" or "662" or "663" or "664" or "665").Select(x => new Device<IStation524, IStation523>(x, World)).ToList());
                        _pickUpDevices.Add(new Device<IStation524, IStation523>(Device.All.First(x => x.Code == "675"), World), Device.All.Where(x => x.Code is "675" or "676" or "677" or "678" or "679").Select(x => new Device<IStation524, IStation523>(x, World)).ToList());
                        _pickUpDevices.Add(new Device<IStation524, IStation523>(Device.All.First(x => x.Code == "689"), World), Device.All.Where(x => x.Code is "690" or "691" or "692" or "693" or "689").Select(x => new Device<IStation524, IStation523>(x, World)).ToList());
                        _pickUpDevices.Add(new Device<IStation524, IStation523>(Device.All.First(x => x.Code == "703"), World), Device.All.Where(x => x.Code is "703" or "704" or "705" or "706" or "707").Select(x => new Device<IStation524, IStation523>(x, World)).ToList());
                        _pickUpDevices.Add(new Device<IStation524, IStation523>(Device.All.First(x => x.Code == "717"), World), Device.All.Where(x => x.Code is "717" or "718" or "719" or "720" or "721").Select(x => new Device<IStation524, IStation523>(x, World)).ToList());
                        _pickUpDevices.Add(new Device<IStation524, IStation523>(Device.All.First(x => x.Code == "731"), World), Device.All.Where(x => x.Code is "732" or "733" or "734" or "735" or "731").Select(x => new Device<IStation524, IStation523>(x, World)).ToList());
                        _pickUpDevices.Add(new Device<IStation524, IStation523>(Device.All.First(x => x.Code == "745"), World), Device.All.Where(x => x.Code is "746" or "747" or "748" or "749" or "745").Select(x => new Device<IStation524, IStation523>(x, World)).ToList());
                        _pickUpDevices.Add(new Device<IStation524, IStation523>(Device.All.First(x => x.Code == "759"), World), Device.All.Where(x => x.Code is "759" or "760" or "761" or "762" or "763").Select(x => new Device<IStation524, IStation523>(x, World)).ToList());
                        _pickUpDevices.Add(new Device<IStation524, IStation523>(Device.All.First(x => x.Code == "773"), World), Device.All.Where(x => x.Code is "773" or "774" or "775" or "776" or "777").Select(x => new Device<IStation524, IStation523>(x, World)).ToList());
                        _pickUpDevices.Add(new Device<IStation524, IStation523>(Device.All.First(x => x.Code == "786"), World), Device.All.Where(x => x.Code is "786" or "787" or "788" or "789" or "790").Select(x => new Device<IStation524, IStation523>(x, World)).ToList());
                        break;

                    case "Truss2":
                        _pickUpDevices.Add(new Device<IStation524, IStation523>(Device.All.First(x => x.Code == "861"), World), Device.All.Where(x => x.Code is "861" or "862" or "863" or "864" or "865").Select(x => new Device<IStation524, IStation523>(x, World)).ToList());
                        _pickUpDevices.Add(new Device<IStation524, IStation523>(Device.All.First(x => x.Code == "875"), World), Device.All.Where(x => x.Code is "875" or "876" or "877" or "878" or "879").Select(x => new Device<IStation524, IStation523>(x, World)).ToList());
                        _pickUpDevices.Add(new Device<IStation524, IStation523>(Device.All.First(x => x.Code == "889"), World), Device.All.Where(x => x.Code is "890" or "891" or "892" or "893" or "889").Select(x => new Device<IStation524, IStation523>(x, World)).ToList());
                        _pickUpDevices.Add(new Device<IStation524, IStation523>(Device.All.First(x => x.Code == "903"), World), Device.All.Where(x => x.Code is "903" or "904" or "905" or "906" or "907").Select(x => new Device<IStation524, IStation523>(x, World)).ToList());
                        _pickUpDevices.Add(new Device<IStation524, IStation523>(Device.All.First(x => x.Code == "917"), World), Device.All.Where(x => x.Code is "917" or "918" or "919" or "920" or "921").Select(x => new Device<IStation524, IStation523>(x, World)).ToList());
                        _pickUpDevices.Add(new Device<IStation524, IStation523>(Device.All.First(x => x.Code == "931"), World), Device.All.Where(x => x.Code is "932" or "933" or "934" or "935" or "931").Select(x => new Device<IStation524, IStation523>(x, World)).ToList());
                        _pickUpDevices.Add(new Device<IStation524, IStation523>(Device.All.First(x => x.Code == "945"), World), Device.All.Where(x => x.Code is "946" or "947" or "948" or "949" or "945").Select(x => new Device<IStation524, IStation523>(x, World)).ToList());
                        _pickUpDevices.Add(new Device<IStation524, IStation523>(Device.All.First(x => x.Code == "959"), World), Device.All.Where(x => x.Code is "959" or "960" or "961" or "962" or "963").Select(x => new Device<IStation524, IStation523>(x, World)).ToList());
                        _pickUpDevices.Add(new Device<IStation524, IStation523>(Device.All.First(x => x.Code == "973"), World), Device.All.Where(x => x.Code is "973" or "974" or "975" or "976" or "977").Select(x => new Device<IStation524, IStation523>(x, World)).ToList());
                        _pickUpDevices.Add(new Device<IStation524, IStation523>(Device.All.First(x => x.Code == "986"), World), Device.All.Where(x => x.Code is "986" or "987" or "988" or "989" or "990").Select(x => new Device<IStation524, IStation523>(x, World)).ToList());
                        _pickUpDevices.Add(new Device<IStation524, IStation523>(Device.All.First(x => x.Code == "1061"), World), Device.All.Where(x => x.Code is "1061" or "1062" or "1063" or "1064" or "1065").Select(x => new Device<IStation524, IStation523>(x, World)).ToList());
                        _pickUpDevices.Add(new Device<IStation524, IStation523>(Device.All.First(x => x.Code == "1075"), World), Device.All.Where(x => x.Code is "1075" or "1076" or "1077" or "1078" or "1079").Select(x => new Device<IStation524, IStation523>(x, World)).ToList());
                        _pickUpDevices.Add(new Device<IStation524, IStation523>(Device.All.First(x => x.Code == "1089"), World), Device.All.Where(x => x.Code is "1090" or "1091" or "1092" or "1093" or "1089").Select(x => new Device<IStation524, IStation523>(x, World)).ToList());
                        _pickUpDevices.Add(new Device<IStation524, IStation523>(Device.All.First(x => x.Code == "1103"), World), Device.All.Where(x => x.Code is "1103" or "1104" or "1105" or "1106" or "1107").Select(x => new Device<IStation524, IStation523>(x, World)).ToList());
                        _pickUpDevices.Add(new Device<IStation524, IStation523>(Device.All.First(x => x.Code == "1117"), World), Device.All.Where(x => x.Code is "1117" or "1118" or "1119" or "1120" or "1121").Select(x => new Device<IStation524, IStation523>(x, World)).ToList());
                        _pickUpDevices.Add(new Device<IStation524, IStation523>(Device.All.First(x => x.Code == "1131"), World), Device.All.Where(x => x.Code is "1132" or "1133" or "1134" or "1135" or "1131").Select(x => new Device<IStation524, IStation523>(x, World)).ToList());
                        _pickUpDevices.Add(new Device<IStation524, IStation523>(Device.All.First(x => x.Code == "1145"), World), Device.All.Where(x => x.Code is "1146" or "1147" or "1148" or "1149" or "1145").Select(x => new Device<IStation524, IStation523>(x, World)).ToList());
                        _pickUpDevices.Add(new Device<IStation524, IStation523>(Device.All.First(x => x.Code == "1159"), World), Device.All.Where(x => x.Code is "1159" or "1160" or "1161" or "1162" or "1163").Select(x => new Device<IStation524, IStation523>(x, World)).ToList());
                        _pickUpDevices.Add(new Device<IStation524, IStation523>(Device.All.First(x => x.Code == "1173"), World), Device.All.Where(x => x.Code is "1173" or "1174" or "1175" or "1176" or "1177").Select(x => new Device<IStation524, IStation523>(x, World)).ToList());
                        _pickUpDevices.Add(new Device<IStation524, IStation523>(Device.All.First(x => x.Code == "1186"), World), Device.All.Where(x => x.Code is "1186" or "1187" or "1188" or "1189" or "1190").Select(x => new Device<IStation524, IStation523>(x, World)).ToList());
                        break;

                    case "Truss3":
                        //北
                        _pickUpDevices.Add(new Device<IStation524, IStation523>(Device.All.First(x => x.Code == "1261"), World), Device.All.Where(x => x.Code is "1261" or "1262" or "1263" or "1264" or "1265").Select(x => new Device<IStation524, IStation523>(x, World)).ToList());
                        _pickUpDevices.Add(new Device<IStation524, IStation523>(Device.All.First(x => x.Code == "1275"), World), Device.All.Where(x => x.Code is "1275" or "1276" or "1277" or "1278" or "1279").Select(x => new Device<IStation524, IStation523>(x, World)).ToList());
                        _pickUpDevices.Add(new Device<IStation524, IStation523>(Device.All.First(x => x.Code == "1289"), World), Device.All.Where(x => x.Code is "1290" or "1291" or "1292" or "1293" or "1289").Select(x => new Device<IStation524, IStation523>(x, World)).ToList());
                        _pickUpDevices.Add(new Device<IStation524, IStation523>(Device.All.First(x => x.Code == "1303"), World), Device.All.Where(x => x.Code is "1303" or "1304" or "1305" or "1306" or "1307").Select(x => new Device<IStation524, IStation523>(x, World)).ToList());
                        _pickUpDevices.Add(new Device<IStation524, IStation523>(Device.All.First(x => x.Code == "1317"), World), Device.All.Where(x => x.Code is "1317" or "1318" or "1319" or "1320" or "1321").Select(x => new Device<IStation524, IStation523>(x, World)).ToList());
                        _pickUpDevices.Add(new Device<IStation524, IStation523>(Device.All.First(x => x.Code == "1331"), World), Device.All.Where(x => x.Code is "1332" or "1333" or "1334" or "1335" or "1331").Select(x => new Device<IStation524, IStation523>(x, World)).ToList());
                        _pickUpDevices.Add(new Device<IStation524, IStation523>(Device.All.First(x => x.Code == "1345"), World), Device.All.Where(x => x.Code is "1346" or "1347" or "1348" or "1349" or "1345").Select(x => new Device<IStation524, IStation523>(x, World)).ToList());
                        _pickUpDevices.Add(new Device<IStation524, IStation523>(Device.All.First(x => x.Code == "1359"), World), Device.All.Where(x => x.Code is "1359" or "1360" or "1361" or "1362" or "1363").Select(x => new Device<IStation524, IStation523>(x, World)).ToList());
                        _pickUpDevices.Add(new Device<IStation524, IStation523>(Device.All.First(x => x.Code == "1373"), World), Device.All.Where(x => x.Code is "1373" or "1374" or "1375" or "1376" or "1377").Select(x => new Device<IStation524, IStation523>(x, World)).ToList());
                        _pickUpDevices.Add(new Device<IStation524, IStation523>(Device.All.First(x => x.Code == "1386"), World), Device.All.Where(x => x.Code is "1386" or "1387" or "1388" or "1389" or "1390").Select(x => new Device<IStation524, IStation523>(x, World)).ToList());
                        //南
                        _pickUpDevices.Add(new Device<IStation524, IStation523>(Device.All.First(x => x.Code == "1461"), World), Device.All.Where(x => x.Code is "1461" or "1462" or "1463" or "1464" or "1465").Select(x => new Device<IStation524, IStation523>(x, World)).ToList());
                        _pickUpDevices.Add(new Device<IStation524, IStation523>(Device.All.First(x => x.Code == "1475"), World), Device.All.Where(x => x.Code is "1475" or "1476" or "1477" or "1478" or "1479").Select(x => new Device<IStation524, IStation523>(x, World)).ToList());
                        _pickUpDevices.Add(new Device<IStation524, IStation523>(Device.All.First(x => x.Code == "1489"), World), Device.All.Where(x => x.Code is "1490" or "1491" or "1492" or "1493" or "1489").Select(x => new Device<IStation524, IStation523>(x, World)).ToList());
                        _pickUpDevices.Add(new Device<IStation524, IStation523>(Device.All.First(x => x.Code == "1503"), World), Device.All.Where(x => x.Code is "1503" or "1504" or "1505" or "1506" or "1507").Select(x => new Device<IStation524, IStation523>(x, World)).ToList());
                        _pickUpDevices.Add(new Device<IStation524, IStation523>(Device.All.First(x => x.Code == "1517"), World), Device.All.Where(x => x.Code is "1517" or "1518" or "1519" or "1520" or "1521").Select(x => new Device<IStation524, IStation523>(x, World)).ToList());
                        _pickUpDevices.Add(new Device<IStation524, IStation523>(Device.All.First(x => x.Code == "1531"), World), Device.All.Where(x => x.Code is "1532" or "1533" or "1534" or "1535" or "1531").Select(x => new Device<IStation524, IStation523>(x, World)).ToList());
                        _pickUpDevices.Add(new Device<IStation524, IStation523>(Device.All.First(x => x.Code == "1545"), World), Device.All.Where(x => x.Code is "1546" or "1547" or "1548" or "1549" or "1545").Select(x => new Device<IStation524, IStation523>(x, World)).ToList());
                        _pickUpDevices.Add(new Device<IStation524, IStation523>(Device.All.First(x => x.Code == "1559"), World), Device.All.Where(x => x.Code is "1559" or "1560" or "1561" or "1562" or "1563").Select(x => new Device<IStation524, IStation523>(x, World)).ToList());
                        _pickUpDevices.Add(new Device<IStation524, IStation523>(Device.All.First(x => x.Code == "1573"), World), Device.All.Where(x => x.Code is "1573" or "1574" or "1575" or "1576" or "1577").Select(x => new Device<IStation524, IStation523>(x, World)).ToList());
                        _pickUpDevices.Add(new Device<IStation524, IStation523>(Device.All.First(x => x.Code == "1586"), World), Device.All.Where(x => x.Code is "1586" or "1587" or "1588" or "1589" or "1590").Select(x => new Device<IStation524, IStation523>(x, World)).ToList());
                        break;
                }
            }
        }
    }
}