patrick.xu
2021-06-01 aefe9f2572eac7c61f6d2952593ec18a700dfcf0
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
 
namespace M423project
{
    public partial class FormDetectionResult : Form
    {
        private ConfigStruct opcConfig;
 
        public FormDetectionResult(ConfigStruct _opcConfig)
        {
            InitializeComponent();
            opcConfig = _opcConfig;
            gridPlate.AutoGenerateColumns = false;
        }
 
        private void btnFind_Click(object sender, EventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            System.Data.DataTable dt;
            switch (tcResult.SelectedIndex)
            { 
                case 0:
                    
                    sb.Append(string.Format(
                        " SELECT DetectID = AutoID "
                    + "      ,DetectTime "
                    + "      ,ProductNo "
                    + "      ,PlateID "
                    + "      ,Height = ProductHeight"
                    //+ "      ,CellHeight = ProductCellHeight"
                    + "      ,HeightResult = ProductHeightStatus"
                    + "      ,Length = ProductLength "
                    + "      ,Width = ProductWidth "
                    //+ "      ,CellWidth = ProductCellWidth "
                    + "      ,SizeResult = ProductSizeStatus "
                    + "      ,ProductResult = ProductStatus"
                    + "       ,ImageFileName,case when ISNULL(ProductType,0)=0 then N'产品' when ProductType=1 then N'标准块' end ProductTypeTxt"
                    + " FROM ProductDetectionDetail "
                    + " WHERE  DetectTime Between '{0}' AND '{1}' ", dtpBegin.Value.ToString("yyyy-MM-dd HH:mm:ss"), dtpEnd.Value.ToString("yyyy-MM-dd HH:mm:ss")));
                    if (!cbIncludeInvalidProductNo.Checked)
                    {
                        sb.Append(string.Format(" AND ProductNo <> 'N/A' "));
                    }
 
                    if (tbProductNo.Text.Trim() != string.Empty)
                    { 
                        sb.Append(string.Format(" AND ProductNo LIKE '%{0}%'", tbProductNo.Text));
                    }
 
                    dt = CommonUtil.mainForm.DetectData.Query(sb.ToString());
                    dgvResultDetail.DataSource = dt;
                    break;
                case 1:
 
 
                    sb.Append(string.Format(
                        " SELECT ProductNo "
                    +  "      ,HeightResult = ProductHeightStatus"
                    +  "      ,SizeResult = ProductSizeStatus "
                    +  "      ,DetectTimes = COUNT(ProductNo) "
                    + " FROM ProductDetectionDetail "
                    + " WHERE ProductNo <> 'N/A' AND ProductSizeStatus <> 'UNKNOWN' AND ProductHeightStatus <> 'UNKNOWN' "
                    + "   AND DetectTime Between '{0}' AND '{1}' ", dtpBegin.Value.ToString("yyyy-MM-dd HH:mm:ss"), dtpEnd.Value.ToString("yyyy-MM-dd HH:mm:ss")));
 
                    if (tbProductNo.Text.Trim() != string.Empty)
                    { 
                        sb.Append(string.Format(" AND ProductNo LIKE '%{0}%'", tbProductNo.Text));
                    }
 
                    if (!cbIncludeInvalidProductNo.Checked)
                    {
                        sb.Append(string.Format(" AND ProductNo <> 'N/A' "));
                    }
 
                    sb.Append(" GROUP BY ProductNo, ProductHeightStatus, ProductSizeStatus ");
                    dt = CommonUtil.mainForm.DetectData.Query(sb.ToString());
                    dgvTotal.DataSource = dt;
 
                    break;
                case 2:
                    sb.AppendFormat(@"select *,TotalQty-ProductOK ProductNG from
                                    (select T1.PlateID,ISNULL(T2.TotalQty,0) TotalQty,ISNULL(T3.ProductNoNG,0) ProductNoNG,
                                    ISNULL(T4.ProductHeightNG,0) ProductHeightNG,ISNULL(T5.ProductLengthNG,0) ProductLengthNG,
                                    ISNULL(T6.ProductWidthNG,0) ProductWidthNG,ISNULL(T7.ProductOK,0) ProductOK from 
                                    (select 1 PlateID union select 2 union select 3 union select 4 union select 5) T1
                                    left join
                                    (select PlateID,isnull(convert(decimal(10,2),COUNT(AutoID)),0) TotalQty from ProductDetectionDetail 
                                    where isnull(ProductType,0)=0
                                    and PlateID is not null
                                    and DetectTime Between '{0}' and '{1}'
                                    group by PlateID) T2
                                    on T1.PlateID=T2.PlateID
                                    left join 
                                    (select PlateID,isnull(convert(decimal(10,2),COUNT(AutoID)),0) ProductNoNG from ProductDetectionDetail 
                                    where isnull(ProductType,0)=0
                                    and PlateID is not null
                                    and DetectTime Between '{0}' and '{1}'
                                    and ProductNoStatus='NG'
                                    group by PlateID) T3
                                    on T1.PlateID=T3.PlateID
                                    left join 
                                    (select PlateID,isnull(convert(decimal(10,2),COUNT(AutoID)),0) ProductHeightNG from ProductDetectionDetail 
                                    where isnull(ProductType,0)=0
                                    and PlateID is not null
                                    and DetectTime Between '{0}' and '{1}'
                                    and ProductHeightStatus='NG'
                                    group by PlateID) T4
                                    on T1.PlateID=T4.PlateID
                                    left join 
                                    (select PlateID,isnull(convert(decimal(10,2),COUNT(AutoID)),0) ProductLengthNG from ProductDetectionDetail 
                                    where isnull(ProductType,0)=0
                                    and PlateID is not null
                                    and DetectTime Between '{0}' and '{1}'
                                    and ProductLengthStatus='NG'
                                    group by PlateID) T5
                                    on T1.PlateID=T5.PlateID
                                    left join 
                                    (select PlateID,isnull(convert(decimal(10,2),COUNT(AutoID)),0) ProductWidthNG from ProductDetectionDetail 
                                    where isnull(ProductType,0)=0
                                    and PlateID is not null
                                    and DetectTime Between '{0}' and '{1}'
                                    and ProductWidthStatus='NG'
                                    group by PlateID) T6
                                    on T1.PlateID=T6.PlateID
                                    left join 
                                    (select PlateID,isnull(convert(decimal(10,2),COUNT(AutoID)),0) ProductOK from ProductDetectionDetail 
                                    where isnull(ProductType,0)=0
                                    and PlateID is not null
                                    and DetectTime Between '{0}' and '{1}'
                                    and ProductStatus='OK'
                                    group by PlateID) T7
                                    on T1.PlateID=T7.PlateID
                                    ) T order by PlateID", dtpBegin.Value.ToString("yyyy-MM-dd HH:mm:ss"), dtpEnd.Value.ToString("yyyy-MM-dd HH:mm:ss"));
 
                    dt = CommonUtil.mainForm.DetectData.Query(sb.ToString());
                    gridPlate.DataSource = dt;
                    break;
            }
        }
 
        private void FormDetectionResult_Load(object sender, EventArgs e)
        {
            dtpBegin.Value = DateTime.Now.AddHours(-3);
            dtpEnd.Value = DateTime.Now; 
 
        }
 
        private void btnExport_Click(object sender, EventArgs e)
        {
            switch (tcResult.SelectedIndex)
            {
                case 0:
                    DataGridViewToExcel(dgvResultDetail);
                    break;
                case 1:
                    DataGridViewToExcel(dgvTotal);
                    break;
                case 2:
                    DataGridViewToExcel(gridPlate);
                    break;
            }
        }
        private void DataGridViewToExcel(DataGridView dgv)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Filter = "Execl files (*.csv)|*.csv";
            dlg.FilterIndex = 0;
            dlg.RestoreDirectory = true;
            dlg.CreatePrompt = true;
            dlg.Title = "导出到Excel文件";
 
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                Stream myStream;
                myStream = dlg.OpenFile();
                StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.UTF8);
                string columnTitle = "";
                try
                {
                     
                    for (int i = 0; i < dgv.ColumnCount; i++)
                    {
                        if (i > 0)
                        {
                            columnTitle += ",";
                        }
                        columnTitle += dgv.Columns[i].HeaderText;
                    }
                    sw.WriteLine(columnTitle);
 
 
                    for (int j = 0; j < dgv.Rows.Count; j++)
                    {
                        string columnValue = "";
                        for (int k = 0; k < dgv.Columns.Count; k++)
                        {
                            if (k > 0)
                            {
                                columnValue += ",";
                            }
                            if (dgv.Rows[j].Cells[k].Value == null)
                                columnValue += "";
                            else
                                columnValue += dgv.Rows[j].Cells[k].Value.ToString().Trim();
                        }
                        sw.WriteLine(columnValue);
                    }
                    sw.Close();
                    myStream.Close();
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.ToString());
                }
                finally
                {
                    sw.Close();
                    myStream.Close();
                }
            }
        }
 
        private void dgvResultDetail_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
 
 
            /*
           //if ( e.RowIndex >= 0)
           {
               Color backColor = e.CellStyle.BackColor;
               if (e.Value != null)
               {
                   if (dgvResultDetail.Columns["Height"].Index == e.ColumnIndex)
                   {
                       double val;
                       try 
                         {      
      
                           val  = (double)e.Value;
        
                         }
                         catch (Exception)
                         {
        
                             val = 0.0;
                         }
 
                       backColor = val >= opcConfig.batteryBMULimit.Min && val <= opcConfig.batteryBMULimit.Max ? backColor : Color.Red;
                   }
 
 
                   if (dgvResultDetail.Columns["Length"].Index == e.ColumnIndex)
                   {
                       double val;
                       try
                       {
 
                           val = (double)e.Value;
 
                       }
                       catch (Exception)
                       {
 
                           val = 0.0;
                       }
                       backColor = val >= opcConfig.batteryLengthLimit.Min && val <= opcConfig.batteryLengthLimit.Max ? backColor : Color.Red;
                   }
 
                   if (dgvResultDetail.Columns["Width"].Index == e.ColumnIndex)
                   {
                       double val;
                       try
                       {
 
                           val = (double)e.Value;
 
                       }
                       catch (Exception)
                       {
 
                           val = 0.0;
                       }
                       backColor = val >= opcConfig.batteryWidthLimit.Min && val <= opcConfig.batteryWidthLimit.Max ? backColor : Color.Red;
                   }
            
               }
 
               Rectangle newRect = new Rectangle(e.CellBounds.X + 1, e.CellBounds.Y + 1, e.CellBounds.Width - 1,
                 e.CellBounds.Height - 1);
               e.CellStyle.BackColor = backColor;
 
               using (Brush backColorBrush = new SolidBrush(dgvResultDetail.BackgroundColor))
               {
                   e.Graphics.FillRectangle(backColorBrush, e.CellBounds);
                   if (e.Value != null)
                   {
                       e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font,
                           backColorBrush, e.CellBounds.X + 2,
                           e.CellBounds.Y + 2, StringFormat.GenericDefault);
                   }
                   e.Handled = true;
               }
             
             
           } */
 
        }
 
        private void dgvResultDetail_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            /*
            if (e.RowIndex >= 0)
            {
                Color backColor = dgvResultDetail.BackgroundColor;
                if (dgvResultDetail.Columns["Height"].Index == e.ColumnIndex)
                {
                    double val = (double)e.Value;
                    backColor = val >= opcConfig.batteryBMULimit.Min && val <= opcConfig.batteryBMULimit.Max ? backColor : Color.Red;
                }
 
 
 
                if (dgvResultDetail.Columns["Length"].Index == e.ColumnIndex)
                {
                    double val = (double)e.Value;
                    backColor = val >= opcConfig.batteryLengthLimit.Min && val <= opcConfig.batteryLengthLimit.Max ? backColor : Color.Red;
                }
 
                if (dgvResultDetail.Columns["Width"].Index == e.ColumnIndex)
                {
                    double val = (double)e.Value;
                    backColor = val >= opcConfig.batteryWidthLimit.Min && val <= opcConfig.batteryWidthLimit.Max ? backColor : Color.Red;
                }
                e.CellStyle.BackColor = backColor;
 
 
            }
             * */
        }     
    }
}