yangle
2024-07-09 f74b68b80ed649fad002cc3910beefcdfa90e738
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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
using System;
using System.Data;
using System.IO;
using System.Windows.Forms;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel; // 对于.xlsx文件  
using NPOI.HSSF.UserModel; // 对于.xls文件  
using System.Collections.Generic;
 
namespace WFormReadData_SMR
{
    public partial class ReadCoordinateExcelFrom : Form
    {
        DataTable dataTable = new DataTable();
        private json objJsonResult = new json();
        public DBHelper oCN = new DBHelper();
        public ReadCoordinateExcelFrom()
        {
            InitializeComponent();
        }
 
        private void ReadCoordinateExcelFrom_Load(object sender, EventArgs e)
        {
            this.cmbJYSelect.SelectedIndex = 0;
            this.cmbJYSelect.DropDownStyle = ComboBoxStyle.DropDownList;
 
            //批次码只读
            this.txtPcm.ReadOnly = true;
 
            //路径只读
            this.txtLj.ReadOnly = true;
            this.txtPcm.ReadOnly = true;
 
           
        }
 
        private void btnLj_Click(object sender, EventArgs e)
        {
            string txtLzk = this.txtLzk.Text;
            //string txtLzk = "GXLX00002866";
            if (txtLzk == "")
            {
                MessageBox.Show("流转卡没有扫描!");
            }
            else {
               
                //路径赋值给文本
                using (FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog())
                {
                    DialogResult result = folderBrowserDialog.ShowDialog();
                    if (result == DialogResult.OK)
                    {
                        //保存按钮灰掉
                        this.btnSave.Enabled = true;
 
                        string selectedFolderPath = folderBrowserDialog.SelectedPath;
                        // 使用 selectedFolderPath 进行后续操作,例如显示在文本框中  
                        this.txtLj.Text = selectedFolderPath;
                        GetCSV_Read(selectedFolderPath);
                        //GetExcel_Read(selectedFolderPath);
                    }
                }
            }
        }
 
        //根据路径找到路径下面的所有文件进行数据读取 CSV
        public void GetCSV_Read(string FilePath)
        {
            DataSet ds = new DataSet();
            if (string.IsNullOrEmpty(FilePath) || !Directory.Exists(FilePath))
            {
                MessageBox.Show("文件夹内容为空!");
            }
 
            //FilePath = FilePath + "\\" + this.txtLzk.Text;
            string[] files = Directory.GetFiles(FilePath, "*.csv", SearchOption.TopDirectoryOnly);
 
            int num = 0;
            for (int i = 0; i < files.Length; i++)
            {
                bool flag= files[i].Contains(this.txtLzk.Text);
 
                if (flag)
                {
                    //根据文件路径找到对应的文件
                    flag = File.Exists(files[i]);
                    if (flag)
                    {
                        //获取Excel对应的数据
                        objJsonResult = Xt_CSVReadText(files[i], i);
                        if (objJsonResult.count == 1)
                        {
                            DataTable dt = objJsonResult.dataTable;
                            ds.Tables.Add(dt);
                        }
                        else
                        {
                            MessageBox.Show("错误:" + objJsonResult.Message);
                            break;
                        }
 
                    }
                    else
                    {
                        MessageBox.Show("路径:" + files[i] + ",文件不存在!");
                        break;
                    }
                }
                else {
                    num++;
                    if (num == files.Length) {
                        MessageBox.Show("选择文件夹不存在流转卡:" + this.txtLzk.Text + "!");
                        break;
                    }
                }
            }
 
            //显示数据
            ShowData(ds);
        }
 
        //根据文件路径找到对应文件 并获取对应的数据
        public json Xt_CSVReadText(string FilePath, int num)
        {
            try
            {
                DataTable dt = new DataTable(num.ToString());
                dt.Columns.Add("Characteristic");
                dt.Columns.Add("Actual");
                DataTable dt2 = new DataTable();
                List<string[]> records = new List<string[]>();
 
                using (StreamReader sr = new StreamReader(FilePath))
                {
                    string[] headers = sr.ReadLine()?.Split(',');
                    if (headers != null)
                    {
                        foreach (var header in headers)
                        {
                            dt2.Columns.Add(header.Trim());
                        }
                    }
 
                    string line;
                    while ((line = sr.ReadLine()) != null)
                    {
                        var values = line.Split(',');
                        DataRow row = dt2.NewRow();
 
                        for (int i = 0; i < values.Length; i++)
                        {
                            // 这里可能需要添加类型转换和错误处理  
                            row[i] = values[i].Trim();
                        }
 
                        dt2.Rows.Add(row);
                    }
                }
 
                for (int i = 0; i < dt2.Rows.Count; i++)
                {
                    int nums = 0;
                    for (int j = 0; j < dt2.Columns.Count; j++)
                    {
                        if (dt2.Columns[j].ColumnName.Contains("VALUE"))
                        {
                            DataRow dr = dt.NewRow();
 
                            if (nums == 1)
                            {
                                dr["Characteristic"] = "FAI 3-16-3-" + (i + 1);
                                dr["Actual"] = dt2.Rows[0][dt2.Columns[j]].ToString();
                            }
                            else
                            {
                                dr["Characteristic"] = "FAI 3-16-1-" + (i + 1);
                                dr["Actual"] = dt2.Rows[0][dt2.Columns[j]].ToString();
                            }
 
                            dt.Rows.Add(dr);
                            nums++;
 
                            if (nums == 2)
                            {
                                break;
                            }
                        }
                    }
                }
 
                objJsonResult.count = 1;
                objJsonResult.Message = "";
                objJsonResult.dataTable = dt;
                return objJsonResult;
            }
            catch (Exception e)
            {
                objJsonResult.count = 0;
                objJsonResult.Message = e.Message;
                objJsonResult.dataTable = null;
                return objJsonResult;
            }
        }
 
        //根据路径找到路径下面的所有文件进行数据读取 EXCEL
        public void GetExcel_Read(string FilePath)
        {
            DataSet ds = new DataSet();
            if (string.IsNullOrEmpty(FilePath) || !Directory.Exists(FilePath))
            {
                MessageBox.Show("文件夹内容为空!");
            }
 
            string[] files = Directory.GetFiles(FilePath, "*.xls*", SearchOption.TopDirectoryOnly);
 
            int num = 0;
            for (int i = 0; i < files.Length; i++)
            {
                bool flag = files[i].Contains(this.txtLzk.Text);
 
                if (flag)
                {
                    //根据文件路径找到对应的文件
                    flag = File.Exists(files[i]);
                    if (flag)
                    {
                        //获取Excel对应的数据
                        objJsonResult = Xt_ExcelReadText(files[i], i);
                        if (objJsonResult.count == 1)
                        {
                            DataTable dt = objJsonResult.dataTable;
                            ds.Tables.Add(dt);
                        }
                        else
                        {
                            MessageBox.Show("错误:" + objJsonResult.Message);
                            break;
                        }
 
                    }
                    else
                    {
                        MessageBox.Show("路径:" + files[i] + ",文件不存在!");
                        break;
                    }
                }
                else
                {
                    num++;
                    if (num == files.Length)
                    {
                        MessageBox.Show("选择文件夹不存在流转卡:" + this.txtLzk.Text + "!");
                        break;
                    }
                }
            }
            //显示数据
            ShowData(ds);
        }
 
        //根据文件路径找到对应文件 并获取对应的数据
        public json Xt_ExcelReadText(string FilePath,int num)
        {
            try
            {
                DataTable dt = new DataTable(num.ToString());
                IWorkbook workbook;
                using (FileStream file = new FileStream(FilePath, FileMode.Open, FileAccess.Read))
                {
                    // 创建一个WorkbookFactory对象来读取文件  
                    workbook = WorkbookFactory.Create(file);
 
                    ISheet sheet = workbook.GetSheetAt(0); // 替换为实际的工作表名称
 
                    if (sheet != null)
                    {
 
                        // 迭代工作表中的行  
                        for (int rowNum = 12; rowNum <= sheet.LastRowNum; rowNum++)
                        {
                            DataRow dr = dt.NewRow();
                            IRow row = sheet.GetRow(rowNum);
                            if (row != null) // 空行可能为null  
                            {
                                // 迭代每一行的单元格  
                                for (int colNum = 0; colNum < row.LastCellNum; colNum++)
                                {
                                    ICell cell = row.GetCell(colNum);
                                    if (cell != null) // 单元格可能为null  
                                    {
                                        string cellValue = cell.ToString();
                                        if (rowNum == 12)
                                        {
                                            dt.Columns.Add(cellValue);
                                        }
                                        else
                                        {
                                            dr[dt.Columns[colNum]] = cellValue;
                                        }
                                    }
                                }
                                if (rowNum != 12)
                                {
                                    dt.Rows.Add(dr);
                                }
                            }
                        }
                    }
                }
                objJsonResult.count = 1;
                objJsonResult.Message = "";
                objJsonResult.dataTable = dt;
                return objJsonResult;
            }
            catch (Exception e)
            {
                objJsonResult.count = 0;
                objJsonResult.Message = e.Message;
                objJsonResult.dataTable = null;
                return objJsonResult;
            }
        }
 
        //把数据显示在页面上
        public void ShowData(DataSet ds) {
            //清空网格内容
            ListData.Items.Clear();
            //清空表格内容
            dataTable.Columns.Clear();
            dataTable.Rows.Clear();
 
            dataTable.Columns.Add("HQCSchemeID");
            dataTable.Columns.Add("CharacteristicID");
            dataTable.Columns.Add("HQCStd");
            dataTable.Columns.Add("Characteristic");
            dataTable.Columns.Add("Actual");
            dataTable.Columns.Add("HUpLimit");
            dataTable.Columns.Add("HDownLimit");
 
            for (int i = 0; i < ds.Tables.Count; i++)
            {
                //修改是在哪个设备上进行的数据采集 b.HInspectInstruMentID=1  三坐标1    轮廓仪2
                DataSet dataSet = oCN.RunProcReturn(@"select a.HInterID HQCSchemeID,b.HQCCheckItemID, ch.HNumber HQCCheckItemNumber,m.HNumber,HUpLimit,HDownLimit,HTargetVal 
from Sc_ProcessExchangeBillMain pr with(nolock)
inner join Gy_QCCheckProjectMain a with(nolock) on pr.HMaterID=a.HMaterID
inner join Gy_QCCheckProjectSub b with(nolock) on a.HInterID=b.HInterID and  b.HInspectInstruMentID=2
left join Gy_Material m with(nolock) on a.HMaterID=m.HItemID
left join Gy_QCCheckItem ch with(nolock) on b.HQCCheckItemID=ch.HItemID  
where pr.HBillNo='" + this.txtLzk.Text + "'", "Gy_QCCheckProjectMain");
 
                if (dataSet.Tables[0].Rows.Count == 0)
                {
                    MessageBox.Show("当前流转卡对应的物料没有对应的检验方案!");
                }
                else {
                    for (int j = 0; j < dataSet.Tables[0].Rows.Count; j++)
                    {
                        int num = 0;
                        DataRow dr = dataTable.NewRow();
                        for (int k = 0; k < ds.Tables[i].Rows.Count; k++)
                        {
                            if (dataSet.Tables[0].Rows[j]["HQCCheckItemNumber"].ToString() == ds.Tables[i].Rows[k]["Characteristic"].ToString())
                            {
                                dr["HQCSchemeID"] = dataSet.Tables[0].Rows[j]["HQCSchemeID"].ToString();
                                dr["CharacteristicID"] = dataSet.Tables[0].Rows[j]["HQCCheckItemID"].ToString();
                                dr["HQCStd"] = dataSet.Tables[0].Rows[j]["HTargetVal"].ToString();
                                dr["Characteristic"] = ds.Tables[i].Rows[k]["Characteristic"].ToString();
                                dr["Actual"] = ds.Tables[i].Rows[k]["Actual"].ToString();
                                dr["HUpLimit"] = dataSet.Tables[0].Rows[j]["HUpLimit"].ToString();
                                dr["HDownLimit"] = dataSet.Tables[0].Rows[j]["HDownLimit"].ToString();
                                ListData.Items.Add("检验项目:" + ds.Tables[i].Rows[k]["Characteristic"].ToString() + ",实测值:" + ds.Tables[i].Rows[k]["Actual"].ToString() + ",上公差:" + dataSet.Tables[0].Rows[j]["HUpLimit"].ToString() + ",下公差:" + dataSet.Tables[0].Rows[j]["HDownLimit"].ToString());
                                break;
                            }
                            else
                            {
                                num++;
                            }
                        }
                        if (num == ds.Tables[i].Rows.Count)
                        {
                            MessageBox.Show("检验项目:" + dataSet.Tables[0].Rows[j]["HQCCheckItemNumber"].ToString() + ",不存在文件里!");
                            break;
                        }
                        if (dr.ItemArray.Length > 0)
                        {
                            dataTable.Rows.Add(dr);
                        }
                    }
                }
            }
        }
 
        //保存数据
        private void btnSave_Click(object sender, EventArgs e)
        {
            string txtLzk = this.txtLzk.Text;
            string txtLj = this.txtLj.Text;
            if (txtLzk == "")
            {
                MessageBox.Show("流转卡没有扫描!");
            }
            else if (txtLj == "")
            {
                MessageBox.Show("请选择路径!");
            }
            else {
                //保存按钮灰掉
                this.btnSave.Enabled = false;
 
                string SelectTpye = this.cmbJYSelect.Text;
                switch (SelectTpye)
                {
                    case "首件检验单":
                        FistInspectionForm_Save();
                        break;
                    case "巡检检验单":
                        InspectionForm_Save();
                        break;
                    case "工序检验单":
                        FinalInspectionForm_Save();
                        break;
                    default:
                        MessageBox.Show("检验类型错误!");
                        break;
                }
            }
        }
 
        //首件检验单保存
        public void FistInspectionForm_Save()
        {
            try
            {
                string sExeReturnInfo = "";
                string BillType = "7505";
                long HInterID = oCN.CreateBillID_Prod(BillType, ref sExeReturnInfo);
                string HBillNo = oCN.CreateBillCode(BillType, ref sExeReturnInfo, true);
 
                DataSet ds = oCN.RunProcReturn(@"select    staus.HSourceID,a.HICMOInterID,a.HICMOEntryID,a.HICMOBillNo,a.HPlanQty,a.HInterID HProcExchInterID,
staus.HSourceEntryID HProcExchEntryID,a.HBillNo HProcExchBillNo,a.HQty,a.HMaterID
from Sc_ProcessExchangeBillMain a with(nolock)
inner join Sc_ICMOBillStatus_Tmp staus with(nolock) on a.HInterID=staus.HSourceInterID  
where a.HBillNo='" + this.txtLzk.Text + "' and staus.HProcID=186 ", "Sc_ProcessExchangeBillMain");
 
                if (ds.Tables[0].Rows.Count == 0)
                {
                    MessageBox.Show("当前工单没有开工数据!");
                }
                else {
                    string HSourceID = ds.Tables[0].Rows[0]["HSourceID"].ToString();
                    string HICMOInterID = ds.Tables[0].Rows[0]["HICMOInterID"].ToString();
                    string HICMOEntryID = ds.Tables[0].Rows[0]["HICMOEntryID"].ToString();
                    string HICMOBillNo = ds.Tables[0].Rows[0]["HICMOBillNo"].ToString();
                    string HPlanQty = ds.Tables[0].Rows[0]["HPlanQty"].ToString();
                    string HProcExchInterID = ds.Tables[0].Rows[0]["HProcExchInterID"].ToString();
                    string HProcExchEntryID = ds.Tables[0].Rows[0]["HProcExchEntryID"].ToString();
                    string HProcExchBillNo = ds.Tables[0].Rows[0]["HProcExchBillNo"].ToString();
                    string HQty = ds.Tables[0].Rows[0]["HQty"].ToString();
                    string HMaterID = ds.Tables[0].Rows[0]["HMaterID"].ToString();
                    string HQCSchemeID = dataTable.Rows[0]["HQCSchemeID"].ToString();
                    int HLastResults = 1;
 
                    oCN.BeginTran();
 
                    for (int i = 0; i < dataTable.Rows.Count; i++)
                    {
                        double HUpLimits = Double.Parse(dataTable.Rows[i]["HUpLimit"].ToString()) + Double.Parse(dataTable.Rows[i]["HQCStd"].ToString());
                        double HDownLimits = Double.Parse(dataTable.Rows[i]["HDownLimit"].ToString()) + Double.Parse(dataTable.Rows[i]["HQCStd"].ToString());
                        int HLastResult = (Double.Parse(dataTable.Rows[i]["Actual"].ToString()) > HUpLimits || HDownLimits < Double.Parse(dataTable.Rows[i]["HDownLimit"].ToString())) ? 0 : 1;
 
                        if (HLastResult == 1 && HLastResults != 0)
                        {
                            HLastResults = 1;
                        }
                        else
                        {
                            HLastResults = 0;
                        }
 
                        oCN.RunProc("Insert into QC_FirstPieceCheckBillSub " +
                        " (HInterID,HBillNo_bak,HEntryID,HCloseMan" +
                        ",HEntryCloseDate,HCloseType,HRemark,HSourceInterID" +
                        ",HSourceEntryID,HSourceBillNo,HSourceBillType,HRelationQty,HRelationMoney" +
                        ",HQCCheckItemID,HQCStd,HUnit,HQCNote,HResult" +
                        ",HMax,HMin,HAvg,HSampleSchemeID,HSampleQty,HSampleDamageQty,HAcceptQty" +
                        ",HRejectQty,HSampleUnRightQty,HStatus,HUnitID,HInspectVal,HTargetVal" +
                        ",HUpLimit,HDownLimit,HUpOffSet,HDownOffSet,HKeyInspect,HAnalysisMethod,HInspectInstruMentID,HInspectResult" +
                        ") values("
                        + HInterID + ",'" + HBillNo + "'," + (i + 1) + ",''" +
                        ",getdate(),0,''," + HProcExchInterID +
                        "," + HProcExchEntryID + ",'" + HProcExchBillNo + "','',0,0" +
                        "," + dataTable.Rows[i]["CharacteristicID"].ToString() + ",'" + dataTable.Rows[i]["HQCStd"].ToString() + "','','"+ dataTable.Rows[i]["HQCStd"].ToString() + "','" + HLastResult + "'" +
                        ",'" + dataTable.Rows[i]["HUpLimit"].ToString() + "','" + dataTable.Rows[i]["HDownLimit"].ToString() + "',''" + ",0,0,0,0,0,0,'',0,'" + dataTable.Rows[i]["Actual"].ToString() + "','" + dataTable.Rows[i]["HQCStd"].ToString() + "','" + dataTable.Rows[i]["HUpLimit"].ToString() + "','" + dataTable.Rows[i]["HDownLimit"].ToString() + "','','',0,'1',0,'"+ HLastResult + "'" + ") ");
                    }
 
                    //主表
                    oCN.RunProc("Insert Into QC_FirstPieceCheckBillMain " +
                        "(HBillType,HBillSubType,HInterID,HBillNo,HBillStatus,HDate,HMaker,HMakeDate" +
                        ",HYear,HPeriod,HRemark" +
                        ",HSourceID,HICMOInterID,HICMOBillNo,HICMOQty,HProcExchInterID,HProcExchEntryID" +
                        ",HProcExchBillNo,HProcExchQty,HMaterID,HFirstCheckEmp,HLastResult" +
                        ",HMainSourceInterID,HMainSourceEntryID,HMainSourceBillNo,HMainSourceBillType,HICMOEntryID,HQCSchemeID,HShiftsID,HErrTreatment" +
                        ") " +
                        " values('" + BillType + "','" + BillType + "'," + HInterID + ",'" + HBillNo + "',1,getdate(),'',getdate()" +
                        "," + DateTime.Now.Year + "," + DateTime.Now.Month + ",''" +
                        "," + HSourceID + "," + HICMOInterID + ",'" + HICMOBillNo + "'," + HPlanQty + "," + HProcExchInterID + "," + HProcExchEntryID +
                        ",'" + HProcExchBillNo + "'," + HQty + "," + HMaterID + ",'', " + HLastResults +
                        "," + HProcExchInterID + "," + HProcExchEntryID + ",'" + HProcExchBillNo + "',''," + HICMOEntryID + "," + HQCSchemeID + ",0,''" +
                        ") ");
 
                    oCN.Commit();
 
                    MessageBox.Show("保存成功!");
 
                }
            }
            catch (Exception e)
            {
                oCN.RollBack();
                MessageBox.Show("保存失败!" + e.Message);
            }
        }
 
        //巡检检验单保存
        public void InspectionForm_Save()
        {
            try
            {
                string sExeReturnInfo = "";
                string BillType = "7520";
                long HInterID = oCN.CreateBillID_Prod(BillType, ref sExeReturnInfo);
                string HBillNo = oCN.CreateBillCode(BillType, ref sExeReturnInfo, true);
 
                DataSet ds = oCN.RunProcReturn(@"select    staus.HSourceID,a.HICMOInterID,a.HICMOEntryID,a.HICMOBillNo,a.HPlanQty,a.HInterID HProcExchInterID,
staus.HSourceEntryID HProcExchEntryID,a.HBillNo HProcExchBillNo,a.HQty,a.HMaterID
from Sc_ProcessExchangeBillMain a with(nolock)
inner join Sc_ICMOBillStatus_Tmp staus with(nolock) on a.HInterID=staus.HSourceInterID  
where a.HBillNo='" + this.txtLzk.Text + "' and staus.HProcID=186 ", "Sc_ProcessExchangeBillMain");
 
                if (ds.Tables[0].Rows.Count == 0)
                {
                    MessageBox.Show("当前工单没有开工数据!");
                }
                else
                {
                    string HSourceID = ds.Tables[0].Rows[0]["HSourceID"].ToString();
                    string HICMOInterID = ds.Tables[0].Rows[0]["HICMOInterID"].ToString();
                    string HICMOEntryID = ds.Tables[0].Rows[0]["HICMOEntryID"].ToString();
                    string HICMOBillNo = ds.Tables[0].Rows[0]["HICMOBillNo"].ToString();
                    string HPlanQty = ds.Tables[0].Rows[0]["HPlanQty"].ToString();
                    string HProcExchInterID = ds.Tables[0].Rows[0]["HProcExchInterID"].ToString();
                    string HProcExchEntryID = ds.Tables[0].Rows[0]["HProcExchEntryID"].ToString();
                    string HProcExchBillNo = ds.Tables[0].Rows[0]["HProcExchBillNo"].ToString();
                    string HQty = ds.Tables[0].Rows[0]["HQty"].ToString();
                    string HMaterID = ds.Tables[0].Rows[0]["HMaterID"].ToString();
                    string HQCSchemeID = dataTable.Rows[0]["HQCSchemeID"].ToString();
                    int HLastResults = 1;
 
                    oCN.BeginTran();
 
                    for (int i = 0; i < dataTable.Rows.Count; i++)
                    {
                        double HUpLimits = Double.Parse(dataTable.Rows[i]["HUpLimit"].ToString()) + Double.Parse(dataTable.Rows[i]["HQCStd"].ToString());
                        double HDownLimits = Double.Parse(dataTable.Rows[i]["HDownLimit"].ToString()) + Double.Parse(dataTable.Rows[i]["HQCStd"].ToString());
                        int HLastResult = (Double.Parse(dataTable.Rows[i]["Actual"].ToString()) > HUpLimits || HDownLimits < Double.Parse(dataTable.Rows[i]["HDownLimit"].ToString())) ? 0 : 1;
 
                        if (HLastResult == 1 && HLastResults != 0)
                        {
                            HLastResults = 1;
                        }
                        else
                        {
                            HLastResults = 0;
                        }
 
                        oCN.RunProc("Insert into QC_PatrolProcCheckOtherBillSub " +
                        " (HInterID,HBillNo_bak,HEntryID,HCloseMan" +
                        ",HEntryCloseDate,HCloseType,HRemark,HSourceInterID" +
                        ",HSourceEntryID,HSourceBillNo,HSourceBillType,HRelationQty,HRelationMoney" +
                        ",HQCCheckItemID,HQCStd,HUnit,HQCNote,HResult" +
                        ",HMax,HMin,HAvg,HSampleSchemeID,HSampleQty,HSampleDamageQty,HAcceptQty" +
                        ",HRejectQty,HSampleUnRightQty,HStatus,HUnitID,HInspectVal,HTargetVal" +
                        ",HUpLimit,HDownLimit,HUpOffSet,HDownOffSet,HKeyInspect,HAnalysisMethod,HInspectInstruMentID,HInspectResult" +
                        ") values("
                        + HInterID + ",'" + HBillNo + "'," + (i + 1) + ",''" +
                        ",getdate(),0,''," + HProcExchInterID +
                        "," + HProcExchEntryID + ",'" + HProcExchBillNo + "','',0,0," + dataTable.Rows[i]["CharacteristicID"].ToString() + ",'" + dataTable.Rows[i]["HQCStd"].ToString() + "','','"+ dataTable.Rows[i]["HQCStd"].ToString() + "','" + HLastResult + "'" +
                        ",'" + dataTable.Rows[i]["HUpLimit"].ToString() + "','" + dataTable.Rows[i]["HDownLimit"].ToString() + "',''" + ",0,0,0,0,0,0,'',0,'" + dataTable.Rows[i]["Actual"].ToString() + "','" + dataTable.Rows[i]["HQCStd"].ToString() + "','" + dataTable.Rows[i]["HUpLimit"].ToString() + "','" + dataTable.Rows[i]["HDownLimit"].ToString() + "','','',0,'1',0,'"+ HLastResult + "'" + ") ");
                    }
 
                    //主表
                    oCN.RunProc("Insert Into QC_PatrolProcCheckOtherBillMain " +
                        "(HBillType,HBillSubType,HInterID,HBillNo,HBillStatus,HDate,HMaker,HMakeDate" +
                        ",HYear,HPeriod,HRemark" +
                        ",HSourceID,HICMOInterID,HICMOBillNo,HICMOQty,HProcExchInterID,HProcExchEntryID" +
                        ",HProcExchBillNo,HProcExchQty,HMaterID,HFirstCheckEmp,HLastResult" +
                        ",HMainSourceInterID,HMainSourceEntryID,HMainSourceBillNo,HMainSourceBillType,HICMOEntryID,HQCSchemeID,HShiftsID,HErrTreatment" +
                        ") " +
                        " values('" + BillType + "','" + BillType + "'," + HInterID + ",'" + HBillNo + "',1,getdate(),'',getdate()" +
                        "," + DateTime.Now.Year + "," + DateTime.Now.Month + ",''" +
                        "," + HSourceID + "," + HICMOInterID + ",'" + HICMOBillNo + "'," + HPlanQty + "," + HProcExchInterID + "," + HProcExchEntryID +
                        ",'" + HProcExchBillNo + "'," + HQty + "," + HMaterID + ",'', " + HLastResults +
                        "," + HProcExchInterID + "," + HProcExchEntryID + ",'" + HProcExchBillNo + "',''," + HICMOEntryID + "," + HQCSchemeID + ",0,''" +
                        ") ");
 
                    oCN.Commit();
 
                    MessageBox.Show("保存成功!");
                }
            }
            catch (Exception e)
            {
                oCN.RollBack();
                MessageBox.Show("保存失败!" + e.Message);
            }
        }
 
        //末检检验单
        public void FinalInspectionForm_Save()
        {
            try
            {
                string sExeReturnInfo = "";
                string BillType = "7507";
                long HInterID = oCN.CreateBillID_Prod(BillType, ref sExeReturnInfo);
                string HBillNo = oCN.CreateBillCode(BillType, ref sExeReturnInfo, true);
 
                DataSet ds = oCN.RunProcReturn(@"select    staus.HSourceID,a.HICMOInterID,a.HICMOEntryID,a.HICMOBillNo,a.HPlanQty,a.HInterID HProcExchInterID,
staus.HSourceEntryID HProcExchEntryID,a.HBillNo HProcExchBillNo,a.HQty,a.HMaterID,staus.HProcID
from Sc_ProcessExchangeBillMain a with(nolock)
inner join Sc_ICMOBillStatus_Tmp staus with(nolock) on a.HInterID=staus.HSourceInterID   
where a.HBillNo='" + this.txtLzk.Text + "' and staus.HProcID=186 ", "Sc_ProcessExchangeBillMain");
 
                if (ds.Tables[0].Rows.Count == 0)
                {
                    MessageBox.Show("当前工单没有开工数据!");
                }
                else {
                    string HSourceID = ds.Tables[0].Rows[0]["HSourceID"].ToString();
                    string HICMOInterID = ds.Tables[0].Rows[0]["HICMOInterID"].ToString();
                    string HICMOEntryID = ds.Tables[0].Rows[0]["HICMOEntryID"].ToString();
                    string HICMOBillNo = ds.Tables[0].Rows[0]["HICMOBillNo"].ToString();
                    string HPlanQty = ds.Tables[0].Rows[0]["HPlanQty"].ToString();
                    string HProcExchInterID = ds.Tables[0].Rows[0]["HProcExchInterID"].ToString();
                    string HProcExchEntryID = ds.Tables[0].Rows[0]["HProcExchEntryID"].ToString();
                    string HProcExchBillNo = ds.Tables[0].Rows[0]["HProcExchBillNo"].ToString();
                    string HQty = ds.Tables[0].Rows[0]["HQty"].ToString();
                    string HMaterID = ds.Tables[0].Rows[0]["HMaterID"].ToString();
                    string HProcID = ds.Tables[0].Rows[0]["HProcID"].ToString();
                    string HQCSchemeID = dataTable.Rows[0]["HQCSchemeID"].ToString();
                    int HLastResults = 1;
 
 
                    oCN.BeginTran();
 
                    for (int i = 0; i < dataTable.Rows.Count; i++)
                    {
                        double HUpLimits = Double.Parse(dataTable.Rows[i]["HUpLimit"].ToString()) + Double.Parse(dataTable.Rows[i]["HQCStd"].ToString());
                        double HDownLimits = Double.Parse(dataTable.Rows[i]["HDownLimit"].ToString()) + Double.Parse(dataTable.Rows[i]["HQCStd"].ToString());
                        int HLastResult = (Double.Parse(dataTable.Rows[i]["Actual"].ToString()) > HUpLimits || HDownLimits < Double.Parse(dataTable.Rows[i]["HDownLimit"].ToString())) ? 0 : 1;
 
                        if (HLastResult == 1 && HLastResults != 0)
                        {
                            HLastResults = 1;
                        }
                        else
                        {
                            HLastResults = 0;
                        }
 
                        oCN.RunProc("Insert into QC_ProcessCheckBillSub " +
                        " (HInterID,HBillNo_bak,HEntryID,HCloseMan" +
                        ",HEntryCloseDate,HCloseType,HRemark,HSourceInterID" +
                        ",HSourceEntryID,HSourceBillNo,HSourceBillType,HRelationQty,HRelationMoney" +
                         ",HQCCheckClassID,HQCCheckItemID,HQCStd,HQCRelValue,HResult" +
                        ",HProcCheckEmp,HProcCheckTime,HSampleSchemeID,HSampleQty,HSampleDamageQty,HAcceptQty" +
                        ",HRejectQty,HSampleUnRightQty,HStatus,HUnitID,HInspectVal,HTargetVal" +
                        ",HUpLimit,HDownLimit,HUpOffSet,HDownOffSet,HKeyInspect,HAnalysisMethod,HInspectInstruMentID,HInspectResult" +
                        ",HQCNote) values("
                        + HInterID + ",'" + HBillNo + "'," + (i + 1) + ",''" +
                        ",getdate(),0,''," + HProcExchInterID +
                        "," + HProcExchEntryID + ",'" + HProcExchBillNo + "','',0,0,0," + dataTable.Rows[i]["CharacteristicID"].ToString() + ",'" + dataTable.Rows[i]["HQCStd"].ToString() + "','','" + HLastResult + "'" +
                        ",0,'',0,0,0,0,0,0,'',0,'" + dataTable.Rows[i]["Actual"].ToString() + "','" + dataTable.Rows[i]["HQCStd"].ToString() + "','" + dataTable.Rows[i]["HUpLimit"].ToString() + "','" + dataTable.Rows[i]["HDownLimit"].ToString() +
                                "','','',0,'',0,'"+ HLastResult + "'" +
                                ",'"+ dataTable.Rows[i]["HQCStd"].ToString() + "') ");
                    }
 
                    //主表
                    oCN.RunProc("Insert Into QC_ProcessCheckBillMain " +
                    "(HBillType,HBillSubType,HInterID,HBillNo,HBillStatus,HDate,HMaker,HMakeDate" +
                    ",HYear,HPeriod,HRemark" +
                    ",HICMOInterID,HICMOBillNo,HProcExchInterID,HProcExchEntryID,HProcExchBillNo" +
                    ",HMaterID,HProcID,HSourceID,HEmpID,HInStockQty" +
                    ",HCheckQty,HRightQty,HBadQty,HFirstCheckEmp,HLastResult" +
                    ",HBarCode,HLBatchNo,HCusID,HSortBillNo,HContrctBatchNo" +
                    ",HProdAreaID,HProdTypeID,HProdStoveNo,HRecipeID,HDiameter1" +
                    ",HDiameter2,HRoutingInterID,HDrawingDireID,HPackTypeID" +
",HMainSourceInterID,HMainSourceEntryID,HMainSourceBillNo,HMainSourceBillType,HICMOEntryID,HQCSchemeID,HICMOQty,HProcExchQty,HShiftsID,HErrTreatment" +
                    ") " +
                    " values('" + BillType + "','" + BillType + "'," + HInterID + ",'" + HBillNo + "',1,getdate(),'',getdate()" +
                    "," + DateTime.Now.Year + "," + DateTime.Now.Month + ",''" +
                    "," + HICMOInterID + ",'" + HICMOBillNo + "'," + HProcExchInterID + "," + HProcExchEntryID + ",'" + HProcExchBillNo + "'" +
                    "," + HMaterID + "," + HProcID + "," + HSourceID + ",0,0,0,0,0,0,'" + HLastResults + "'" +
                    ",'','',0,'',''" +
                    ",0,0,'',0,0,0,0,'',0," + HProcExchInterID + "," + HProcExchEntryID + ",'" + HProcExchBillNo + "',''," + HICMOEntryID + "," + HQCSchemeID + "," + HPlanQty + "," + HQty + ",0,''" +
                    ") ");
 
 
                    oCN.Commit();
 
                    MessageBox.Show("保存成功!");
                }
            }
            catch (Exception e)
            {
                oCN.RollBack();
                MessageBox.Show("保存失败!" + e.Message);
            }
        }
 
        //流转卡回车
        private void txtLzk_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SelectHBardCode(this.txtLzk.Text);
            }
        }
 
        //查询条形码
        public void SelectHBardCode(string HBillNo)
        {
            try
            {
                DataSet ds = oCN.RunProcReturn("select  * from Sc_ProcessExchangeBillMain where HBillNo='" + HBillNo + "' ", "Sc_ProcessExchangeBillMain");
                if (ds.Tables[0].Rows.Count == 0)
                {
                    MessageBox.Show("查无数据!");
                }
                else
                {
                    string HProjectNum = ds.Tables[0].Rows[0]["HProjectNum"].ToString();
                    this.txtPcm.Text = HProjectNum.Split('-')[0];
                    this.txtLzk.ReadOnly = true;
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(this, e.Message, "提示");
            }
        }
 
        private void ReadCoordinateExcelFrom_Activated(object sender, EventArgs e)
        { 
            //光标选中流转卡
            this.txtLzk.Focus();
        }
 
        private void ReadCoordinateExcelFrom_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MessageBox.Show("确定要关闭吗?", "确认", MessageBoxButtons.YesNo) == DialogResult.No)
            {
                e.Cancel = true;
            }
        }
    }
}