bug
lu
2 天以前 45b0abd8850da515734792f01addd8aa5f18dd62
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
using DevExpress.Office.Utils;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraLayout.Utils;
using Gs.DevApp.Entity;
using Gs.DevApp.ToolBox;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NModbus;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Dynamic;
using System.IO;
using System.Net.Sockets;
using System.Text;
using System.Windows.Forms;
 
namespace Gs.DevApp.DevFrm.Work
{
    public partial class Frm_Work01 : DevExpress.XtraEditors.XtraForm
    {
        string hostName = "192.168.60.51";
        int hostNamePoint = 502;
        string strConn = "Data Source=192.168.1.146;Initial Catalog=GS_MES;User ID=mesUser;Password =qixi1qaz@WSXmes";
        bool isCleanDianJian = false;
        string _webServiceName = "WorkCollect/";
        string dianJianPath = "";//电检取值路径
        string goodTag = "√";
        string badTag = "×";
        string fGxTouRu = "G001:投入";
        string fGxDianJian = "G002:电检";
        string fGxShouJian = "G003:水检";
        string fGxZhongJian = "G004:终检";
 
        /// <summary>
        /// 
        /// </summary>
        /// <param name="_fGx">工序</param>
        /// <param name="_fBc">班次</param>
        /// <param name="_strinOther">目前为电检路径</param>
        public Frm_Work01(string _fGx, string _fBc, string _strinOther)
        {
            InitializeComponent();
            gvMx1.IndicatorWidth = 60;
            gvMx1.CustomDrawRowIndicator += (s, e) =>
            {
                if (e.Info.IsRowIndicator && e.RowHandle >= 0)
                    e.Info.DisplayText = (e.RowHandle + 1).ToString();
            };
            txt_gx.Text = _fGx;
            txt_bc.Text = _fBc;
            txt_dianJianPath.Text = _strinOther;
            dianJianPath = _strinOther;
            this.Text = "工序采集【" + _fGx + " - " + _fBc + "】";
            label8.Text = _fGx + "数量";
            addColumn();
            txt_barCode.KeyDown += (s, e) =>
            {
                if (e.KeyCode == Keys.Enter)
                {
                    //如果是水检,还要扫工位
                    if (txt_gx.Text.Trim() == fGxShouJian)
                    {
                        txt_gongWei.Focus();
                    }
                    else
                        addModel(goodTag);
                }
            };
            txt_gongWei.KeyDown += (s, e) =>
            {
                if (e.KeyCode == Keys.Enter)
                {
                    //如果是水检,可以提交了
                    if (txt_gx.Text.Trim() == fGxShouJian)
                    {
                        addModel(goodTag);
                    }
                }
            };
            btnGood.Click += (s, e) =>
            {
                addModel(goodTag);
            };
            btnBad.Click += (s, e) =>
            {
                string _barCode = txt_barCode.Text.Trim();
                if (string.IsNullOrEmpty(_barCode))
                {
                    Toast vm = new Toast(-1, "~请先扫追溯码~");
                    vm.Show();
                    return;
                }
                UcDictionarySelect frm = new UcDictionarySelect("工序不良描述", " a.s_type='工序不良描述' and pid<>'00000000-0000-0000-0000-000000000000'");
                frm.UpdateParent += (ss, ee) =>
                {
                    System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
                    var lst = ee.DynamicList;
                    foreach (dynamic dym in lst)
                    {
                        if (stringBuilder.Length > 0)
                            stringBuilder.Append("|");
                        stringBuilder.Append(dym.dicTxt);
                    }
                    string _txt = stringBuilder.ToString();
                    if (string.IsNullOrEmpty(_txt))
                    {
                        Toast vm = new Toast(-1, "~当为不良时,请选择不良项目~");
                        vm.Show();
                    }
                    else
                        addModel(badTag, _txt);
                };
                frm.ShowDialog();
            };
            this.ActiveControl = this.txt_barCode;
            txt_barCode.Focus();
        }
 
        /// <summary>
        /// 根据工序,构造不同类型的实体,读取不同的数据
        /// </summary>
        /// <param name="_barCode">条码</param>
        /// <param name="_checkResult">结果</param>
        /// <param name="_badDescription">不良描述</param>
        /// <returns></returns>
        private dynamic getObj(string _barCode, string _checkResult, string _badDescription = "")
        {
            dynamic _obj = new ExpandoObject();
            _obj.barCode = _barCode;
            _obj.checkResult = _checkResult;//检测结果
            _obj.processNo = txt_gx.Text.Trim();//工序
            _obj.banCi = txt_bc.Text.Trim();//班次
            _obj.gongWei = txt_gongWei.Text.Trim();//工位
            _obj.badDescription = _badDescription.Trim();//不良描述
            if (txt_gx.Text.Trim() == fGxTouRu)
            {
                return _obj;
            }
            //电检
            if (txt_gx.Text.Trim() == fGxDianJian)
            {
                string TM = "";
                string JD = "";
                string JY = "";
                string JN = "";
                string XL = "";
                string GV = "";
                string JDV = "";
                string JYV = "";
                string JNV = "";
                string XLV = "";
                string GVV = "";
                string myStr = File.ReadAllText(dianJianPath, Encoding.Default);
                if (myStr == "")
                {
                    return _obj;
                }
                JArray dsTemp = JArray.Parse(myStr);
                foreach (var ss in dsTemp)  //查找某个字段与值
                {
                    TM = (((JObject)ss)["TiaoMa"] == null) ? "" : ((JObject)ss)["TiaoMa"].ToString();
                    JD = (((JObject)ss)["JieDi"] == null) ? "" : ((JObject)ss)["JieDi"]["state"].ToString();
                    JY = (((JObject)ss)["JueYuan"] == null) ? "" : ((JObject)ss)["JueYuan"]["state"].ToString();
                    JN = (((JObject)ss)["JiaoNai"] == null) ? "" : ((JObject)ss)["JiaoNai"]["state"].ToString();
                    XL = (((JObject)ss)["XieLou"] == null) ? "" : ((JObject)ss)["XieLou"]["state"].ToString();
                    GV = (((JObject)ss)["GongLv"] == null) ? "" : ((JObject)ss)["GongLv"]["state"].ToString();
                    JDV = (((JObject)ss)["JieDi"] == null) ? "" : ((JObject)ss)["JieDi"]["value"].ToString().Replace(" ", ",");
                    JYV = (((JObject)ss)["JueYuan"] == null) ? "" : ((JObject)ss)["JueYuan"]["value"].ToString().Replace(" ", ", ");
                    JNV = (((JObject)ss)["JiaoNai"] == null) ? "" : ((JObject)ss)["JiaoNai"]["value"].ToString().Replace(" ", ", ");
                    XLV = (((JObject)ss)["XieLou"] == null) ? "" : ((JObject)ss)["XieLou"]["value"].ToString().Replace(" ", ", ");
                    GVV = (((JObject)ss)["GongLv"] == null) ? "" : ((JObject)ss)["GongLv"]["value"].ToString().Replace(" ", ", ");
                }
                _obj.t001 = TM;
                _obj.t002 = JD;
                _obj.t003 = JY;
                _obj.t004 = JN;
                _obj.t005 = XL;
                _obj.t006 = GV;
                _obj.t007 = JDV;
                _obj.t008 = JYV;
                _obj.t009 = JNV;
                _obj.t010 = XLV;
                _obj.t011 = GVV;
                if (isCleanDianJian == true)
                    File.WriteAllText(dianJianPath, "");
                return _obj;
            }
            //水检
            if (txt_gx.Text.Trim() == fGxShouJian)
            {
                string strCsl1 = "";//模式1出水量
                string strSw1 = "";//模式1水温
                string strCsl2 = "";
                string strSw2 = "";
                string strCsl3 = "";
                string strSw3 = "";
                string strCsl4 = "";
                string strSw4 = "";
                string strCsl5 = "";
                string strSw5 = "";
                string SJJG = "";//水检查结果ok、ng
                string GWZT = "";//工位状态
                string SJJGZ = "";//水检查结果1
                (strCsl1, strSw1, SJJG, GWZT, SJJGZ) = getShuiJian();
                _obj.t002 = strCsl1;
                _obj.t003 = strSw1;
                _obj.t004 = strCsl2;
                _obj.t005 = strSw2;
                _obj.t006 = strCsl3;
                _obj.t007 = strSw3;
                _obj.t008 = strCsl4;
                _obj.t009 = strSw4;
                _obj.t010 = strCsl5;
                _obj.t011 = strSw5;
                _obj.t012 = SJJGZ;
                _obj.t013 = GWZT;
                return _obj;
            }
            //终检
            if (txt_gx.Text.Trim() == fGxZhongJian)
            {
                return _obj;
            }
            return null;
        }
 
        /// <summary>
        /// 上报实体
        /// </summary>
        /// <param name="_checkResult">结果:√</param>
        /// <param name="_badDescription">不良描述</param>
        private void addModel(string _checkResult, string _badDescription = "")
        {
            string _barCode = txt_barCode.Text.Trim();
            if (string.IsNullOrEmpty(_barCode))
            {
                Toast vm = new Toast(-1, "~请先扫追溯码~");
                vm.Show();
                return;
            }
            dynamic _obj = getObj(_barCode, _checkResult, _badDescription);
            try
            {
                string strJson = UtilityHelper.HttpPost("", _webServiceName + "GetModel", JsonConvert.SerializeObject(_obj));
                ReturnModel<dynamic> _rtn = ToolBox.UtilityHelper.ReturnToDynamic(strJson);
                if (_rtn.rtnCode > 0)
                {
                    dynamic dy = _rtn.rtnData;
                    //表头和汇总
                    UtilityHelper.SetValueByObj(this.layoutMx1.Controls, dy, false);
                    lbSum1.Text = dy.lbSum1;
                    lbSum2.Text = dy.lbSum2;
                    lbSum3.Text = dy.lbSum3;
                    lbSum4.Text = dy.lbSum4;
                    prog1.Position = dy.jd;
                    //良品列表
                    txt_barCode.ReadOnly = false;
                    JObject _job = JObject.Parse(strJson);
                    JArray array = new JArray();
                    foreach (var a in _job["rtnData"]["list"])
                    {
                        array.Add(a);
                    }
                    DataTable dt = JsonConvert.DeserializeObject<DataTable>(array.ToString());
                    if (dt.Rows.Count > 0)
                    {
                        gcMx1.BindingContext = new BindingContext();
                        gcMx1.DataSource = dt;
                        gcMx1.ForceInitialize();
                        gvMx1.BestFitColumns();
                        Gs.DevApp.ToolBox.UtilityHelper.SetGridLayout(gvMx1);
                    }
                    else
                    {
                        Gs.DevApp.ToolBox.UtilityHelper.SetDefaultTable(gcMx1, gvMx1);
                    }
                    //不良品列表
                    JArray array2 = new JArray();
                    foreach (var a in _job["rtnData"]["list2"])
                    {
                        array2.Add(a);
                    }
                    DataTable dt2 = JsonConvert.DeserializeObject<DataTable>(array2.ToString());
                    if (dt2.Rows.Count > 0)
                    {
                        gcMx2.BindingContext = new BindingContext();
                        gcMx2.DataSource = dt2;
                        gcMx2.ForceInitialize();
                        gvMx2.BestFitColumns();
                        Gs.DevApp.ToolBox.UtilityHelper.SetGridLayout(gvMx2);
                    }
                    else
                    {
                        Gs.DevApp.ToolBox.UtilityHelper.SetDefaultTable(gcMx2, gvMx2);
                    }
                    string _msg = _rtn.rtnMsg;
                    if (!string.IsNullOrEmpty(_msg))
                    {
                        Toast vm = new Toast((goodTag == "√" ? 1 : -1), _msg);
                        vm.Show();
                    }
                }
                else
                {
                    Toast vm = new Toast(-1, _rtn.rtnMsg);
                    vm.Show();
                }
            }
            catch (Exception ex)
            {
                Toast vm = new Toast(-1, ex.Message);
                vm.Show();
            }
            txt_barCode.Text = "";
            txt_gongWei.Text = "";
            this.ActiveControl = this.txt_barCode;
            txt_barCode.Focus();
        }
 
        /// <summary>
        /// 增加电检,水检的列
        /// </summary>
        public void addColumn()
        {
            //只有电检才显示
            if (txt_gx.Text.Trim() == fGxDianJian)
                layDianJian.Visibility = LayoutVisibility.Always;
            else
                layDianJian.Visibility = LayoutVisibility.Never;
            //只有水检才显示
            if (txt_gx.Text.Trim() == fGxShouJian)
                layShuiJian.Visibility = LayoutVisibility.Always;
            else
                layShuiJian.Visibility = LayoutVisibility.Never;
            List<dynamic> _lst = new List<dynamic>();
            if (txt_gx.Text.Trim() == fGxDianJian)
            {
                _lst.Add(new
                {
                    ziduan = "t002",
                    wenben = "接地",
                    kejian = true,
                    visibleIndex = 1,
                });
                _lst.Add(new
                {
                    ziduan = "t004",
                    wenben = "交耐",
                    kejian = true,
                    visibleIndex = 1,
                });
                _lst.Add(new
                {
                    ziduan = "t005",
                    wenben = "泄漏",
                    kejian = true,
                    visibleIndex = 1,
                });
                _lst.Add(new
                {
                    ziduan = "t006",
                    wenben = "功率",
                    kejian = true,
                    visibleIndex = 1,
                });
                _lst.Add(new
                {
                    ziduan = "t007",
                    wenben = "接地结果",
                    visibleIndex = -1,
                });
                _lst.Add(new
                {
                    ziduan = "t009",
                    wenben = "交耐结果",
                    visibleIndex = -1,
                });
                _lst.Add(new
                {
                    ziduan = "t010",
                    wenben = "泄漏结果",
                    visibleIndex = -1,
                });
                _lst.Add(new
                {
                    ziduan = "t011",
                    wenben = "功率结果",
                    visibleIndex = -1,
                });
            }
            if (txt_gx.Text.Trim() == fGxShouJian)
            {
                _lst.Add(new
                {
                    ziduan = "t002",
                    wenben = "出水量1",
                    kejian = true,
                    visibleIndex = 1,
                });
                _lst.Add(new
                {
                    ziduan = "t003",
                    wenben = "水温1",
                    kejian = true,
                    visibleIndex = 1,
                });
                _lst.Add(new
                {
                    ziduan = "t004",
                    wenben = "出水量2",
                    visibleIndex = -1,
                });
                _lst.Add(new
                {
                    ziduan = "t005",
                    wenben = "水温2",
                    visibleIndex = -1,
                });
                _lst.Add(new
                {
                    ziduan = "t006",
                    wenben = "出水量3",
                    visibleIndex = -1,
                });
                _lst.Add(new
                {
                    ziduan = "t007",
                    wenben = "水温3",
                    visibleIndex = -1,
                });
                _lst.Add(new
                {
                    ziduan = "t008",
                    wenben = "出水量4",
                    visibleIndex = -1,
                });
                _lst.Add(new
                {
                    ziduan = "t009",
                    wenben = "水温4",
                    visibleIndex = -1,
                });
                _lst.Add(new
                {
                    ziduan = "t010",
                    wenben = "出水量5",
                    visibleIndex = -1,
                });
                _lst.Add(new
                {
                    ziduan = "t011",
                    wenben = "水温5",
                    visibleIndex = -1,
                });
                _lst.Add(new
                {
                    ziduan = "t012",
                    wenben = "数据结果",
                    kejian = true,
                    visibleIndex = 1,
                });
            }
            foreach (var item in _lst)
            {
                GridColumn newColumn = new GridColumn();
                newColumn.FieldName = item.ziduan; // 设置与数据源对应的字段名
                newColumn.Caption = item.wenben; // 设置列的标题
                newColumn.VisibleIndex = item.visibleIndex; // 设置列的显示顺序
                gvMx1.Columns.Add(newColumn);
            }
        }
 
 
        #region  读取水检数据
        private (string, string, string, string, string) getShuiJian()
        {
            string strCsl1 = ""; //模式1出水量
            string strSw1 = "";//模式1水温
            string SJJG = "";//水检查结果ok、ng
            string GWZT = "";//工位状态
            string SJJGZ = "";//水检查结果1
            System.Text.StringBuilder sbSql1 = new StringBuilder();
            sbSql1.Append($"select MIG004,MIG005 from BASMIG(nolock) where MIG001='ShuiJianZhanWei01' and MIG003='{((BaseEdit)this.txt_gongWei).EditValue.ToString()}'");
            DataTable dataTable1 = this.GetDataTable(sbSql1.ToString(), "tmpBASMIG2");
            if (dataTable1 == null || dataTable1.Rows.Count <= 0)
            {
                ((BaseEdit)this.txt_gongWei).EditValue = (object)"";
                LogHelper.Debug(this.ToString(), txt_gongWei.Text + "该水检站位码不存在,请确认!");
                //Toast vm = new Toast(-1, "该水检站位码不存在,请确认!");
                //vm.Show();
            }
            else
            {
                ModbusFactory modbusFactory = new ModbusFactory();
                byte num1 = byte.Parse("1");
                ushort num2 = ushort.Parse("1");
                System.Text.StringBuilder sbSql2 = new StringBuilder();
                sbSql2.Append(string.Format($"select case when MIG006='D' then MIG003+{dataTable1.Rows[0][0]} else MIG003+{dataTable1.Rows[0][1]} end MIG003,MIG004 from BASMIG(nolock) where MIG001 = 'ShuiJianAddress01' order by MIG005"));
                DataTable dataTable2 = GetDataTable(sbSql2.ToString(), "tmpBASMIG3");
                if (dataTable2 == null || dataTable2.Rows.Count <= 0)
                {
                    ((BaseEdit)this.txt_gongWei).EditValue = (object)"";
                    LogHelper.Debug(this.ToString(), txt_gongWei.Text + "水检地址不存在,请确认!");
                    //Toast vm = new Toast(-1, "水检地址不存在,请确认!");
                    //vm.Show();
                }
                else
                {
                    try
                    {
                        IModbusMaster master = modbusFactory.CreateMaster(new TcpClient(this.hostName, this.hostNamePoint));
                        master.Transport.ReadTimeout = 10000;
                        master.Transport.Retries = 10000;
                        strCsl1 = master.ReadHoldingRegisters(num1, ushort.Parse(dataTable2.Rows[0]["MIG003"].ToString()), num2)[0].ToString();
                        strSw1 = (float.Parse(master.ReadHoldingRegisters(num1, ushort.Parse(dataTable2.Rows[1]["MIG003"].ToString()), num2)[0].ToString()) / 10f).ToString();
                        ushort[] numArray = master.ReadHoldingRegisters(num1, ushort.Parse(dataTable2.Rows[2]["MIG003"].ToString()), num2);
                        SJJGZ = numArray[0].ToString();
                        SJJG = numArray[0].ToString();
                        SJJG = (SJJG == "1" ? "OK" : (SJJG == "2" ? "NG" : SJJG));
                        GWZT = master.ReadCoils(num1, ushort.Parse(dataTable2.Rows[3]["MIG003"].ToString()), num2)[0].ToString();
                    }
                    catch (Exception ex)
                    {
                        ((BaseEdit)this.txt_gongWei).EditValue = (object)"";
                        LogHelper.Debug(this.ToString(), txt_gongWei.Text + ex.Message);
                        //Toast vm = new Toast(-1, ex.Message);
                        //vm.Show();
                    }
                }
            }
            return (strCsl1, strSw1, SJJG, GWZT, SJJGZ);
        }
        private DataTable GetDataTable(string SQLString, string tmpBASMIG2)
        {
            using (var connection = new SqlConnection(strConn))
            {
                var ds = new DataSet();
                try
                {
                    connection.Open();
                    var command = new SqlDataAdapter(SQLString, connection);
                    command.Fill(ds, tmpBASMIG2);
                }
                catch (SqlException ex)
                {
                    LogHelper.Debug(this.ToString(), ex.Message);
                }
                return ds.Tables[0];
            }
        }
        #endregion
 
    }
}