using Prism.Mvvm;
using SKMC.Api.Common;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
namespace SKMC.Api.Product.Material.Tray
{
///
/// 基础物料盘, 可放m*n个物料
///
public class Tray : BindableBase
{
protected static ILogger log = LogFactory.Get();
///
/// index:
/// 0 1 2 3 4 5
/// 6 7 8 9 ...
///
public ObservableCollection Slots { get; set; } = new ObservableCollection();
public long Id { get; set; }
///
/// 类型编号
///
public string Code { get; set; }
///
/// 料盘类型 0:主盘 1:NG盘 2:OK盘
///
public byte TrayType { get; set; } = 0;
///
/// 料盘码
///
public string SerialNo { get; set; }
///
/// 产品码
///
public string ProductNo { get; set; }
///
/// 料盘参数对象
///
public TrayConfig TrayConfiger { get; set; }
// 开始进料时刻
public long OnLoadMillis { get; set; }
// 开始准备时刻
public long OnReadyMillis { get; set; }
// 完全离开时刻
public long OnExitMillis { get; set; }
///
/// X 方向的穴位坐标正负 1:+ -1:-
///
public short DirectX { get; set; } = 1;
///
/// X 方向的穴位坐标正负 1:+ -1:-
///
public short DirectY { get; set; } = 1;
///
/// 生成Matters的状态字符串
///
///
public string ToMap()
{
StringBuilder builder = new StringBuilder();
foreach (var mat in Slots)
{
builder.Append(Convert.ToString(mat.Result));
}
return builder.ToString();
}
///
/// 解析状态字符串并恢复到Matters集合中
///
///
public void FromMap(string map, int rows, int cols)
{
//Matters.Clear();
//for (int i = 0; i < map.Length; i++)
//{
// Matter matter = new Matter();
// matter.TrayCols = cols;
// matter.Set(i);
// string s = Convert.ToString(map[i]);
// matter.Result = Convert.ToByte(s);
// Matters.Add(matter);
//}
//Num = map.Length;
Counts();
}
public int Row { get; set; }
public int Col { get; set; }
private int _num;
// 总数
public int Num
{
get { return _num; }
set { _num = value; RaisePropertyChanged(); }
}
private int _numOK;
public int NumOK
{
get { return _numOK; }
set
{
_numOK = value; RaisePropertyChanged();
}
}
private int _numNG;
// NG数
public int NumNG
{
get { return _numNG; }
set
{
_numNG = value; RaisePropertyChanged();
}
}
private int _unLocated;
// 剩余空位(未定位)
public int Unlocated
{
get { return _unLocated; }
set { _unLocated = value; RaisePropertyChanged(); }
}
public Tray(TrayConfig trayConfiger, byte trayType = 0, byte siteStatus = (byte)MatterStatus.NULL)
{
Id = BitConverter.ToInt64(System.Guid.NewGuid().ToByteArray(), 0);
TrayType = trayType;
Num = trayConfiger.Number;
Row = trayConfiger.Row;
Col = trayConfiger.Col;
if (trayConfiger == null)
{
throw new ArgumentException("tray model is null");
}
for (int i = 0; i < trayConfiger.Row; i++)
{
for (int j = 0; j < trayConfiger.Col; j++)
{
TraySlot matter = new TraySlot
{
Index = j + i * trayConfiger.Col,
ColX = j,
RowY = i,
IntervalX = trayConfiger.XPitch * j,
IntervalY = trayConfiger.YPitch * i,
Result = siteStatus
};
if (trayConfiger.SiteSwitchs != null && trayConfiger.SiteSwitchs.Count > matter.Index)
{
matter.IsEnable = trayConfiger.SiteSwitchs[matter.Index];
}
Slots.Add(matter);
}
}
Counts();
}
// 注入一个Slot数据
public void Inject(TraySlot slot)
{
//Matter thisMatter = Matters.Where(m => m.ColX == matter.ColX & m.RowY == matter.RowY).FirstOrDefault();
//if (thisMatter != null)
//{
// if (matter.Code != null) thisMatter.Code = matter.Code;
// if (matter.Result > 0) thisMatter.Result = matter.Result;
// if (matter.ResultMsg != null) thisMatter.ResultMsg = matter.ResultMsg;
// if (matter.FixsetX > 0) thisMatter.FixsetX = matter.FixsetX;
// if (matter.FixsetY > 0) thisMatter.FixsetY = matter.FixsetY;
// if (matter.FixsetR > 0) thisMatter.FixsetR = matter.FixsetR;
//}
foreach (var thisMat in Slots)
{
if (thisMat != null && thisMat.ColX == slot.ColX && thisMat.RowY == slot.RowY)
{
thisMat.Code = slot.Code;
thisMat.Result = slot.Result;
thisMat.Located = slot.Located;
}
}
}
///
/// 注入一个Slot集合数据
///
///
public void Injects(List slots)
{
foreach (var matter in slots)
{
Inject(matter);
}
}
public int Count(MatterStatus status)
{
int counter = 0;
foreach (var matter in Slots)
{
if (matter.Result == (byte)status) counter++;
}
return counter;
}
// 统计当前OK, NG数, 空位数
public void Counts()
{
_numNG = 0;
_numOK = 0;
_unLocated = 0;
foreach (var matter in Slots)
{
if (matter.Result == (byte)MatterStatus.OK) _numOK++;
if (matter.Result == (byte)MatterStatus.NG) _numNG++;
if (matter.Located == (byte)LocateStatus.TODO) _unLocated++;
}
NumOK = _numOK;
NumNG = _numNG;
Unlocated = _unLocated;
}
public void UpdateResults(MatterStatus status)
{
foreach (var matter in Slots)
{
matter.Result = (byte)status;
}
Counts();
}
public void Updates(Func cnd, Action action)
{
foreach (var matter in Slots)
{
if (cnd.Invoke(matter)) action.Invoke(matter);
}
}
public List FindSlots(MatterStatus status)
{
List results = new List();
foreach (var matter in Slots)
{
if (matter.Result == (byte)status) results.Add(matter);
}
return results;
}
public TraySlot FindNext(MatterStatus status)
{
foreach (var matter in Slots)
{
if (matter.Result == (byte)status) return matter;
}
return null;
}
public TraySlot FindNext(Func cnd)
{
foreach (var matter in Slots)
{
if (cnd.Invoke(matter)) return matter;
}
return null;
}
public void FindNext(ref TraySlot matter)
{
if (matter == null) return;
if (matter.Index == Slots.Count() - 1)
{
matter = null;
return;
}
matter = Slots[matter.Index + 1];
}
public override string ToString()
{
return $"id: {Id}, code: {SerialNo}";
}
public void PrintMatters()
{
log.Debug($"tray: {this}");
foreach (var matter in Slots)
{
log.Debug(matter.ToString());
}
}
}
}