using Newtonsoft.Json;
using PlcSiemens.Core.Extension;
using ServiceCenter.Extensions;
using ServiceCenter.Logs;
using ServiceCenter.SqlSugars;
using SqlSugar;
using System.ComponentModel;
using WCS.Core;
using WCS.Entity;
using WCS.Entity.Protocol.BCR;
using WCS.Entity.Protocol.Station;
using WCS.WorkEngineering.Extensions;
using WCS.WorkEngineering.Model.WMS;
using WCS.WorkEngineering.Worlds;
using TaskStatus = WCS.Entity.TaskStatus;
namespace WCS.WorkEngineering.Systems
{
///
/// 满轮主线预写入目标地址
///
[BelongTo(typeof(SortingMainLineWorld))]
[Description("满轮主线预写入目标地址")]
public class 满轮主线预写入目标地址 : DeviceSystem>
{
protected override bool ParallelDo => true;
public override void Do(Device obj)
{
try
{
var devCode = obj.Entity.Code switch
{
"18" => "22",
"38" => "41",
"58" => "61",
"118" => "122",
"138" => "141",
"158" => "161",
_ => throw new ArgumentOutOfRangeException()
};
var dev = new Device(Device.All.First(x => x.Code == devCode), World);
dev.Data.Mode = StationMode.Automatic;
//从DB83中获取待处理条码组
var pendingBcrList = obj.Data2.GetBcrCodeList().Where(x => !x.IsNullOrEmpty());
//从DB525获取已处理条码组
var cacheBcrList = obj.Data3.GetBcrCodeList();
//筛选出未处理条码组
var codes = pendingBcrList.Where(x => !cacheBcrList.Contains(x));
World.Log($"扫码器:{JsonConvert.SerializeObject(pendingBcrList)}");
World.Log($"缓存信息:{JsonConvert.SerializeObject(cacheBcrList)}");
//循环处理所有缓存条码组中没有的条码
foreach (var bcrCode in codes)
{
WCS_TaskInfo taskInfo = null;
fjSysConfig config = new fjSysConfig();
SqlSugarHelper.Do(_db =>
{
var db = _db.Default;
var task = db.Queryable().NoLock().First(x => x.BarCode == bcrCode && x.Status == TaskStatus.NewBuild);
if (task == null)
{
World.Log($"{bcrCode}:找不到匹配的任务", LogLevelEnum.High);
var errorInfo = new BaseErrorInfo()
{
BusName = "主线分流",
BarCode = bcrCode,
Message = "找不到匹配的任务",
Count = 1,
AddTime = DateTime.Now,
Memo = obj.Entity.Code
};
errorInfo.AddBaseErrorInfo(db);
return;
}
var config1 = db.Queryable().NoLock().First(x => x.Code == $"{task.WarehouseCode}-Flow");
if (config1 == null)
{
config.Code = "4";
}
config = config1;
taskInfo = task;
});
if (taskInfo == null)
{
continue;
}
var srmCode = taskInfo.WarehouseCode.WarehouseToSrm();
var path = DevicePath.GetPath(obj.Entity.Code, srmCode);
if (path == null || path is { Points.Count: < 2 })
{
var msg = $"{bcrCode}:路径错误,当前位置{obj.Entity.Code},目标位置:{srmCode}";
World.Log(msg, LogLevelEnum.High);
var errorInfo = new BaseErrorInfo()
{
BusName = "主线分流",
BarCode = bcrCode,
Message = msg,
Count = 1,
AddTime = DateTime.Now,
Memo = obj.Entity.Code
};
errorInfo.AddBaseErrorInfo();
continue;
}
var next = path.Points[1].Code;
#region 计算应该去哪个分拣库
//应该要去的分拣库
if (taskInfo.WarehouseCode.Contains("N"))
{
next = config.SContent switch
{
"1" => "418",
"2" => "818",
"3" => "1218",
_ => next
};
}
else if (taskInfo.WarehouseCode.Contains("S"))
{
next = config.SContent switch
{
"1" => "618",
"2" => "1018",
"3" => "1418",
_ => next
};
}
#endregion 计算应该去哪个分拣库
if (taskInfo == null) continue;
//开始赋值
obj.Data3.GetType().GetProperty($"TaskNumber{obj.Data3.NextIndex}")
.SetValue(obj.Data3, taskInfo.ID);
obj.Data3.GetType().GetProperty($"TaskNumber{obj.Data3.NextIndex}")
.SetValue(obj.Data3, taskInfo.ID);
obj.Data3.GetType().GetProperty($"GoodsEnd{obj.Data3.NextIndex}")
.SetValue(obj.Data3, next.ToShort());
obj.Data3.GetType().GetProperty($"GoodsEnd{obj.Data3.NextIndex}")
.SetValue(obj.Data3, next.ToShort());
obj.Data3.GetType().GetProperty($"BcrCode{obj.Data3.NextIndex}").SetValue(obj.Data3, bcrCode);
if (obj.Data3.NextIndex >= 49)
{
obj.Data3.NextIndex = 0;
}
else
{
obj.Data3.NextIndex++;
}
}
dev.Data.Mode = StationMode.Automatic;
}
catch (Exception e)
{
World.Log($"{e.Message}:---{e.StackTrace}", LogLevelEnum.High);
}
}
public override bool Select(Device dev)
{
var codes = new List();
if (WorkStart.WareHouses.Contains("FJ1")) codes.AddRange(new List() { "18", "118" });
if (WorkStart.WareHouses.Contains("FJ2")) codes.AddRange(new List() { "38", "58" });
if (WorkStart.WareHouses.Contains("FJ3")) codes.AddRange(new List() { "138", "158" });
return codes.Contains(dev.Code);
return false;
}
}
///
/// 主线分流
///
public class MainlineDiversion
{
///
/// 任务号
///
public int TaskId { get; set; }
///
/// 仓库号
///
public string WarehouseCode { get; set; }
}
}