kingno
2025-04-03 a97ac998301461e6284595b1cf2c7b40ce5b2459
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
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;
            }
        }
    }
}