using ServiceCenter.Extensions;
using ServiceCenter.Logs;
using ServiceCenter.SqlSugars;
using System.ComponentModel;
using WCS.Core;
using WCS.Entity;
using WCS.WorkEngineering.Extensions;
using WCS.WorkEngineering.Protocol.Station;
using WCS.WorkEngineering.Protocol.Truss;
using WCS.WorkEngineering.Worlds;
using DeviceFlags = WCS.WorkEngineering.Extensions.DeviceFlags;
namespace WCS.WorkEngineering.Systems
{
///
/// 桁架
///
[BelongTo(typeof(MainWorld))]
[Description("桁架")]
public class 桁架 : DeviceSystem
{
protected override bool ParallelDo => true;
protected override bool SaveLogsToFile => true;
///
/// 取货点设备集合
///
private readonly Dictionary, List>> _pickUpDevices = new();
public override void Do(Truss obj)
{
if (obj.Data.VoucherNo != obj.Data2.VoucherNo) throw new KnownException($"凭证号不一致,DB520:{obj.Data.VoucherNo}-DB521:{obj.Data2.VoucherNo}", LogLevelEnum.High);
if (obj.Data2.CmdType == 1)
{
SqlSugarHelper.Do(_db =>
{
var db = _db.Default;
List ids = new List() { obj.Data2.PalletizingRowId1, obj.Data2.PalletizingRowId2 };
var palletizingRow = db.Queryable().Includes(x => x.CacheLine, c => c.Locations).Includes(x => x.Locs).Where(x => ids.Contains(x.Id)).ToList();
//更新行数据
foreach (var pr in palletizingRow)
{
var cacheLine = db.Queryable().Includes(x => x.Locations).Single(x => x.Id == pr.CacheLine.Id);
//先更新对应的位信息
try
{
foreach (var loc in pr.Locs)
{
//var cacheLoc = cacheLine.Locations.Single(x => x.XYNo == loc.XYNo);
//cacheLoc = db.Queryable().Single(x => x.Id == cacheLoc.Id);
//loc.TaskId = cacheLoc.TaskId;
loc.Finish = true;
db.Updateable(loc).ExecuteCommand();
//db.Deleteable(cacheLoc);s
}
}
catch (Exception a)
{
Console.WriteLine(a);
throw;
}
pr.Finish = true;
db.Updateable(pr).ExecuteCommand();
db.Deleteable(cacheLine).ExecuteCommand();
}
//更新层数据
var layerId = palletizingRow.FirstOrDefault().PalletizingLayerId;
var layer = db.Queryable().Includes(x => x.Rows).Single(x => x.Id == layerId);
if (layer.Rows.All(x => x.Finish))
{
layer.Finish = true;
db.Updateable(layer).ExecuteCommand();
}
});
obj.Data.CmdType = 1;
}
if (obj.Data2.CmdType == 0) obj.Data.CmdType = 0;
if (obj.Data2.CmdType != 0 || obj.Data2.CmdType != 0) return;
if (obj.Data2.Status != TrussStatus.Idle) return;
//查找所有的可用开始搬运的线体
var pickUpDevices = _pickUpDevices.Where(x => x.Key.Data2.Status1.HasFlag(StationStatus1.IsLock)).ToList();
List palletizingRowList = null;
WCS_Palletizing palletizing = null;
SqlSugarHelper.Do(_db =>
{
var db = _db.Default;
//找到有还有层没结束的码垛记录信息,有对应托盘
var palletizingLsit = db.Queryable().Includes(x => x.Layers, r => r.Rows, l => l.Locs)
.Where(x => x.Layers.Any(l => !l.Finish) && x.PalletizingStation != null).ToList();
//可用码垛信息记录中对应的托盘任务已经结束了//TODO:暂时默认托盘搬运任务已经结束
//筛选出目标位置有光电的码垛记录信息
var devCode = palletizingLsit.Select(x => x.PalletizingStation);
var devList = Device.All.Where(x => devCode.Contains(x.Code)).Select(x => new Device(x, World)).ToList();
devList = devList.Where(x => x.Data3.Status.HasFlag(StationStatus.PH_Status)).ToList();
devCode = devList.Select(x => x.Entity.Code);
palletizingLsit = palletizingLsit.Where(x => devCode.Contains(x.PalletizingStation)).ToList(); //可以放货的目标托盘
//可以取货的码垛信息
var pickUpCode = _pickUpDevices.Select(x => x.Key.Entity.Code);
palletizingLsit = palletizingLsit.Where(x => x.Layers
.Where(w => !w.Finish)
.SelectMany(w => w.Rows)
.Where(v => !v.Finish).Any(w => pickUpCode.Contains(w.LineCode))).ToList();
var palletizingInfo = palletizingLsit.FirstOrDefault();
if (palletizingInfo == null) return;
//筛选出已经预锁的行
var palletizingRow = palletizingInfo.Layers.SelectMany(x => x.Rows).Where(x => x is { Finish: false, IsEmpty: false, LineCode: not null }).Where(x => pickUpDevices.Select(p => p.Key.Entity.Code).Contains(x.LineCode)).OrderBy(x => x.RowNo).Take(2).ToList();
if (palletizingRow.Count == 2 && palletizingRow[0].LineCode == palletizingRow[1].LineCode) palletizingRow = palletizingRow!.Take(1).ToList();
if (palletizingRow.Count == 2 && palletizingRow[0].RowNo + 1 != palletizingRow[1].RowNo) palletizingRow = palletizingRow!.Take(1).ToList();
palletizingRowList = palletizingRow;
palletizing = palletizingInfo;
});
if (palletizingRowList == null) return;
if (!palletizingRowList.Any()) return;
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.Entity.Code == palletizingRow.LineCode).Value
.OrderBy(x => x.Entity.Code).ToArray();
switch (i)
{
case 0:
obj.Data.DestPosition_1 = palletizingRow.LineCode!.ToShort();
obj.Data.TargetPallte1 = palletizing.PalletizingStation!.ToShort();
obj.Data.Task1_1 = devs[0].Data.TaskNumber;
obj.Data.Dest1_1 = locs[0].XYNo.ToShort();
obj.Data.Task1_2 = devs[1].Data.TaskNumber;
obj.Data.Dest1_2 = locs[1].XYNo.ToShort();
obj.Data.Task1_3 = devs[2].Data.TaskNumber;
obj.Data.Dest1_3 = locs[2].XYNo.ToShort();
obj.Data.Task1_4 = devs[3].Data.TaskNumber;
obj.Data.Dest1_4 = locs[3].XYNo.ToShort();
obj.Data.Task1_5 = devs[4].Data.TaskNumber;
obj.Data.Dest1_5 = locs[4].XYNo.ToShort();
obj.Data.TaskSum1 = palletizingRow.QtyMaxCount.ToShort();
obj.Data.PalletizingRowId1 = palletizingRow.Id;
break;
case 1:
obj.Data.DestPosition_2 = palletizingRow.LineCode!.ToShort();
obj.Data.TargetPallte2 = palletizing.PalletizingStation!.ToShort();
obj.Data.Task2_1 = devs[0].Data.TaskNumber;
obj.Data.Dest2_1 = locs[0].XYNo.ToShort();
obj.Data.Task2_2 = devs[1].Data.TaskNumber;
obj.Data.Dest2_2 = locs[1].XYNo.ToShort();
obj.Data.Task2_3 = devs[2].Data.TaskNumber;
obj.Data.Dest2_3 = locs[2].XYNo.ToShort();
obj.Data.Task2_4 = devs[3].Data.TaskNumber;
obj.Data.Dest2_4 = locs[3].XYNo.ToShort();
obj.Data.Task2_5 = devs[4].Data.TaskNumber;
obj.Data.Dest2_5 = locs[4].XYNo.ToShort();
obj.Data.TaskSum2 = palletizingRow.QtyMaxCount.ToShort();
obj.Data.PalletizingRowId2 = palletizingRow.Id;
break;
}
}
if (palletizingRowList.Count == 1)
{
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;
}
obj.Data.VoucherNo++;
}
public override bool Select(Device dev)
{
return dev.HasFlag(Extensions.DeviceFlags.桁架);
}
public 桁架()
{
var trussList = Device.All.Where(x => x.HasFlag(DeviceFlags.桁架));
foreach (var truss in trussList)
{
switch (truss.Code)
{
case "Truss1":
_pickUpDevices.Add(new Device(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(x, World)).ToList());
_pickUpDevices.Add(new Device(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(x, World)).ToList());
_pickUpDevices.Add(new Device(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(x, World)).ToList());
_pickUpDevices.Add(new Device(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(x, World)).ToList());
_pickUpDevices.Add(new Device(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(x, World)).ToList());
_pickUpDevices.Add(new Device(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(x, World)).ToList());
_pickUpDevices.Add(new Device(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(x, World)).ToList());
_pickUpDevices.Add(new Device(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(x, World)).ToList());
_pickUpDevices.Add(new Device(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(x, World)).ToList());
_pickUpDevices.Add(new Device(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(x, World)).ToList());
//_pickUpDevices.Add(Device.All.First(x => x.Code == ""), Device.All.Where(x => x.Code is "" or "" or "" or "" or "").Select(x => new Device(x, World)).ToList());
//_pickUpDevices.Add(Device.All.First(x => x.Code == ""), Device.All.Where(x => x.Code is "" or "" or "" or "" or "").Select(x => new Device(x, World)).ToList());
//_pickUpDevices.Add(Device.All.First(x => x.Code == ""), Device.All.Where(x => x.Code is "" or "" or "" or "" or "").Select(x => new Device(x, World)).ToList());
//_pickUpDevices.Add(Device.All.First(x => x.Code == ""), Device.All.Where(x => x.Code is "" or "" or "" or "" or "").Select(x => new Device(x, World)).ToList());
//_pickUpDevices.Add(Device.All.First(x => x.Code == ""), Device.All.Where(x => x.Code is "" or "" or "" or "" or "").Select(x => new Device(x, World)).ToList());
//_pickUpDevices.Add(Device.All.First(x => x.Code == ""), Device.All.Where(x => x.Code is "" or "" or "" or "" or "").Select(x => new Device(x, World)).ToList());
//_pickUpDevices.Add(Device.All.First(x => x.Code == ""), Device.All.Where(x => x.Code is "" or "" or "" or "" or "").Select(x => new Device(x, World)).ToList());
//_pickUpDevices.Add(Device.All.First(x => x.Code == ""), Device.All.Where(x => x.Code is "" or "" or "" or "" or "").Select(x => new Device(x, World)).ToList());
//_pickUpDevices.Add(Device.All.First(x => x.Code == ""), Device.All.Where(x => x.Code is "" or "" or "" or "" or "").Select(x => new Device(x, World)).ToList());
//_pickUpDevices.Add(Device.All.First(x => x.Code == ""), Device.All.Where(x => x.Code is "" or "" or "" or "" or "").Select(x => new Device(x, World)).ToList());
break;
case "Truss2":
//_pickUpDevices.Add(Device.All.First(x => x.Code == ""), Device.All.Where(x => x.Code is "" or "" or "" or "" or "").Select(x => new Device(x, World)).ToList());
//_pickUpDevices.Add(Device.All.First(x => x.Code == ""), Device.All.Where(x => x.Code is "" or "" or "" or "" or "").Select(x => new Device(x, World)).ToList());
//_pickUpDevices.Add(Device.All.First(x => x.Code == ""), Device.All.Where(x => x.Code is "" or "" or "" or "" or "").Select(x => new Device(x, World)).ToList());
//_pickUpDevices.Add(Device.All.First(x => x.Code == ""), Device.All.Where(x => x.Code is "" or "" or "" or "" or "").Select(x => new Device(x, World)).ToList());
//_pickUpDevices.Add(Device.All.First(x => x.Code == ""), Device.All.Where(x => x.Code is "" or "" or "" or "" or "").Select(x => new Device(x, World)).ToList());
//_pickUpDevices.Add(Device.All.First(x => x.Code == ""), Device.All.Where(x => x.Code is "" or "" or "" or "" or "").Select(x => new Device(x, World)).ToList());
//_pickUpDevices.Add(Device.All.First(x => x.Code == ""), Device.All.Where(x => x.Code is "" or "" or "" or "" or "").Select(x => new Device(x, World)).ToList());
//_pickUpDevices.Add(Device.All.First(x => x.Code == ""), Device.All.Where(x => x.Code is "" or "" or "" or "" or "").Select(x => new Device(x, World)).ToList());
//_pickUpDevices.Add(Device.All.First(x => x.Code == ""), Device.All.Where(x => x.Code is "" or "" or "" or "" or "").Select(x => new Device(x, World)).ToList());
//_pickUpDevices.Add(Device.All.First(x => x.Code == ""), Device.All.Where(x => x.Code is "" or "" or "" or "" or "").Select(x => new Device(x, World)).ToList());
//_pickUpDevices.Add(Device.All.First(x => x.Code == ""), Device.All.Where(x => x.Code is "" or "" or "" or "" or "").Select(x => new Device(x, World)).ToList());
//_pickUpDevices.Add(Device.All.First(x => x.Code == ""), Device.All.Where(x => x.Code is "" or "" or "" or "" or "").Select(x => new Device(x, World)).ToList());
//_pickUpDevices.Add(Device.All.First(x => x.Code == ""), Device.All.Where(x => x.Code is "" or "" or "" or "" or "").Select(x => new Device(x, World)).ToList());
//_pickUpDevices.Add(Device.All.First(x => x.Code == ""), Device.All.Where(x => x.Code is "" or "" or "" or "" or "").Select(x => new Device(x, World)).ToList());
//_pickUpDevices.Add(Device.All.First(x => x.Code == ""), Device.All.Where(x => x.Code is "" or "" or "" or "" or "").Select(x => new Device(x, World)).ToList());
//_pickUpDevices.Add(Device.All.First(x => x.Code == ""), Device.All.Where(x => x.Code is "" or "" or "" or "" or "").Select(x => new Device(x, World)).ToList());
//_pickUpDevices.Add(Device.All.First(x => x.Code == ""), Device.All.Where(x => x.Code is "" or "" or "" or "" or "").Select(x => new Device(x, World)).ToList());
//_pickUpDevices.Add(Device.All.First(x => x.Code == ""), Device.All.Where(x => x.Code is "" or "" or "" or "" or "").Select(x => new Device(x, World)).ToList());
//_pickUpDevices.Add(Device.All.First(x => x.Code == ""), Device.All.Where(x => x.Code is "" or "" or "" or "" or "").Select(x => new Device(x, World)).ToList());
//_pickUpDevices.Add(Device.All.First(x => x.Code == ""), Device.All.Where(x => x.Code is "" or "" or "" or "" or "").Select(x => new Device(x, World)).ToList());
break;
case "Truss3":
//_pickUpDevices.Add(Device.All.First(x => x.Code == ""), Device.All.Where(x => x.Code is "" or "" or "" or "" or "").Select(x => new Device(x, World)).ToList());
//_pickUpDevices.Add(Device.All.First(x => x.Code == ""), Device.All.Where(x => x.Code is "" or "" or "" or "" or "").Select(x => new Device(x, World)).ToList());
//_pickUpDevices.Add(Device.All.First(x => x.Code == ""), Device.All.Where(x => x.Code is "" or "" or "" or "" or "").Select(x => new Device(x, World)).ToList());
//_pickUpDevices.Add(Device.All.First(x => x.Code == ""), Device.All.Where(x => x.Code is "" or "" or "" or "" or "").Select(x => new Device(x, World)).ToList());
//_pickUpDevices.Add(Device.All.First(x => x.Code == ""), Device.All.Where(x => x.Code is "" or "" or "" or "" or "").Select(x => new Device(x, World)).ToList());
//_pickUpDevices.Add(Device.All.First(x => x.Code == ""), Device.All.Where(x => x.Code is "" or "" or "" or "" or "").Select(x => new Device(x, World)).ToList());
//_pickUpDevices.Add(Device.All.First(x => x.Code == ""), Device.All.Where(x => x.Code is "" or "" or "" or "" or "").Select(x => new Device(x, World)).ToList());
//_pickUpDevices.Add(Device.All.First(x => x.Code == ""), Device.All.Where(x => x.Code is "" or "" or "" or "" or "").Select(x => new Device(x, World)).ToList());
//_pickUpDevices.Add(Device.All.First(x => x.Code == ""), Device.All.Where(x => x.Code is "" or "" or "" or "" or "").Select(x => new Device(x, World)).ToList());
//_pickUpDevices.Add(Device.All.First(x => x.Code == ""), Device.All.Where(x => x.Code is "" or "" or "" or "" or "").Select(x => new Device(x, World)).ToList());
//_pickUpDevices.Add(Device.All.First(x => x.Code == ""), Device.All.Where(x => x.Code is "" or "" or "" or "" or "").Select(x => new Device(x, World)).ToList());
//_pickUpDevices.Add(Device.All.First(x => x.Code == ""), Device.All.Where(x => x.Code is "" or "" or "" or "" or "").Select(x => new Device(x, World)).ToList());
//_pickUpDevices.Add(Device.All.First(x => x.Code == ""), Device.All.Where(x => x.Code is "" or "" or "" or "" or "").Select(x => new Device(x, World)).ToList());
//_pickUpDevices.Add(Device.All.First(x => x.Code == ""), Device.All.Where(x => x.Code is "" or "" or "" or "" or "").Select(x => new Device(x, World)).ToList());
//_pickUpDevices.Add(Device.All.First(x => x.Code == ""), Device.All.Where(x => x.Code is "" or "" or "" or "" or "").Select(x => new Device(x, World)).ToList());
//_pickUpDevices.Add(Device.All.First(x => x.Code == ""), Device.All.Where(x => x.Code is "" or "" or "" or "" or "").Select(x => new Device(x, World)).ToList());
//_pickUpDevices.Add(Device.All.First(x => x.Code == ""), Device.All.Where(x => x.Code is "" or "" or "" or "" or "").Select(x => new Device(x, World)).ToList());
//_pickUpDevices.Add(Device.All.First(x => x.Code == ""), Device.All.Where(x => x.Code is "" or "" or "" or "" or "").Select(x => new Device(x, World)).ToList());
//_pickUpDevices.Add(Device.All.First(x => x.Code == ""), Device.All.Where(x => x.Code is "" or "" or "" or "" or "").Select(x => new Device(x, World)).ToList());
//_pickUpDevices.Add(Device.All.First(x => x.Code == ""), Device.All.Where(x => x.Code is "" or "" or "" or "" or "").Select(x => new Device(x, World)).ToList());
break;
}
}
}
}
}