using Bro.Common.Helper;
|
using Bro.Common.Interface;
|
using Bro.Common.Model;
|
using Bro.UI.Model.Winform;
|
using Sunny.UI;
|
using System.ComponentModel;
|
using System.Data;
|
|
namespace Bro.M135.Common
|
{
|
//[MenuNode("ProductList", "产品列表", 2, EnumHelper.TopMenu.SystemInfo, MenuNodeType.Form)]
|
public partial class FrmProductList : MenuFormBase
|
{
|
DataGridView dgvProducts = new DataGridView();
|
bool _isFirstLoaded = true;
|
object _loadLock = new object();
|
|
|
//BindingList<dynamic> BindDatas = new BindingList<dynamic>();
|
|
BindingList<Dictionary<string, object>> BindDatas = new BindingList<Dictionary<string, object>>();
|
|
public FrmProductList()
|
{
|
InitializeComponent();
|
|
dgvProducts.AutoGenerateColumns = false;
|
dgvProducts.ReadOnly = true;
|
dgvProducts.AllowUserToAddRows = false;
|
dgvProducts.AllowUserToDeleteRows = false;
|
dgvProducts.Dock = DockStyle.Fill;
|
dgvProducts.RowsAdded += DgvProducts_RowsAdded;
|
this.Controls.Add(dgvProducts);
|
|
var head = new Dictionary<string, string>() { { "PID", "PID" }, { "条码", "SN" }, { "检测结果", "Result" } };
|
head.ForEach(u =>
|
{
|
DataGridViewColumn col = new DataGridViewTextBoxColumn();
|
col.HeaderText = u.Key;
|
col.DataPropertyName = u.Value;
|
col.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
|
|
dgvProducts.Columns.Add(col);
|
});
|
|
dgvProducts.DataSource = BindDatas;
|
}
|
|
private void DgvProducts_RowsAdded(object? sender, DataGridViewRowsAddedEventArgs e)
|
{
|
int rowIndex = e.RowIndex;
|
if (rowIndex < 0)
|
return;
|
|
try
|
{
|
if (dgvProducts.Rows[rowIndex].DataBoundItem is Dictionary<string, object> model)
|
{
|
for (int i = 0; i < dgvProducts.Columns.Count; i++)
|
{
|
string propName = dgvProducts.Columns[i].DataPropertyName;
|
var cell = dgvProducts.Rows[rowIndex].Cells[i];
|
cell.Value = model[propName];
|
|
if (i == 2)
|
{
|
if (cell.Value.ToString().ToUpper().StartsWith("OK"))
|
{
|
cell.Style.BackColor = Color.Lime;
|
}
|
else
|
{
|
cell.Style.BackColor = Color.Red;
|
}
|
}
|
else if (i > 2 && i < dgvProducts.Columns.Count - 1)
|
{
|
var measureResult = model[$"{propName}_MeasureResult"] as bool?;
|
if (measureResult == null)
|
{
|
cell.Style.BackColor = Color.Orange;
|
}
|
else if (measureResult.Value == false)
|
{
|
cell.Style.BackColor = Color.Red;
|
}
|
}
|
else if (i == dgvProducts.Columns.Count - 1)
|
{
|
if (!string.IsNullOrWhiteSpace(cell.Value.ToString()))
|
{
|
cell.Style.BackColor = Color.Red;
|
}
|
}
|
}
|
}
|
}
|
catch (Exception ex)
|
{
|
CommonLogger.LogAsync(DateTime.Now, EnumHelper.LogLevel.Error, $"界面添加新产品数据时异常,{ex.GetExceptionMessage()}");
|
}
|
}
|
|
public override void OnProcessUpdated()
|
{
|
base.OnProcessUpdated();
|
|
if (Process is IUpdateProductData upd)
|
{
|
upd.OnUpdateProductData -= Upd_OnUpdateProductData;
|
upd.OnUpdateProductData += Upd_OnUpdateProductData;
|
}
|
}
|
|
private async void Upd_OnUpdateProductData(ProductModel p)
|
{
|
await Task.Run(() =>
|
{
|
this.Invoke(() =>
|
{
|
var specList = p.Details.SelectMany(u => u.SpecList ?? new List<ISpec>()).ToList();
|
specList.AddRange(p.Details.SelectMany(u => u.ResultList.SelectMany(s => s.Specs.Select(m => (Spec)m))));
|
specList.Sort((a, b) => string.Compare(a.Code, b.Code));
|
|
if (_isFirstLoaded)
|
{
|
lock (_loadLock)
|
{
|
if (_isFirstLoaded)
|
{
|
_isFirstLoaded = false;
|
|
specList.ForEach(s =>
|
{
|
DataGridViewColumn col = new DataGridViewTextBoxColumn();
|
col.HeaderText = s.Code;
|
col.DataPropertyName = s.Code;
|
col.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
|
col.DefaultCellStyle.Format = "f4";
|
|
dgvProducts.Columns.Add(col);
|
});
|
|
DataGridViewColumn col = new DataGridViewTextBoxColumn();
|
col.HeaderText = "缺陷汇总";
|
col.DataPropertyName = "Defects";
|
col.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
|
|
dgvProducts.Columns.Add(col);
|
}
|
}
|
}
|
|
Dictionary<string, object> model = new Dictionary<string, object>();
|
model["PID"] = p.PID;
|
model["SN"] = p.SN;
|
model["Result"] = p.Result;
|
specList.ForEach(s =>
|
{
|
model[s.Code] = s.GetMeasureValueStr();
|
model[$"{s.Code}_MeasureResult"] = s.MeasureResult;
|
});
|
|
var defects = p.Details.SelectMany(u => u.ResultList.GetDefectDescList()).ToList();
|
defects.AddRange(p.Details.SelectMany(u => u.DefectList));
|
defects.AddRange(p.Details.SelectMany(u => u.SpecList.Where(s => s.MeasureResult != true).Select(s => s.Code)));
|
model["Defects"] = $"{string.Join(",", defects)}";
|
|
BindDatas.Insert(0, model);
|
|
while (BindDatas.Count > 100)
|
{
|
BindDatas.RemoveAt(BindDatas.Count - 1);
|
}
|
|
//dgvProducts.DataSource = null;
|
//dgvProducts.DataSource = BindDatas;
|
|
dgvProducts.Invalidate();
|
});
|
});
|
}
|
|
public override void OnCustomizedDispose()
|
{
|
base.OnCustomizedDispose();
|
|
if (Process is IUpdateProductData upd)
|
{
|
upd.OnUpdateProductData -= Upd_OnUpdateProductData;
|
}
|
}
|
}
|
}
|