1
yangle
2023-12-29 2d0c00e1ec3e52de61f2039d3fee3ec03665a354
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
using Microsoft.VisualBasic.FileIO;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
 
namespace WFormReadData_SMR
{
    public partial class ReadCSV : Form
    {
        public DBHelper oCN = new DBHelper();
 
        public ReadCSV()
        {
            InitializeComponent();
        }
        public static int num = 0;
 
 
 
        //更换条形码
        private void btnGH_Click(object sender, EventArgs e)
        {
            this.txtHBardCode.Text = "";
            this.txtHBath.Text = "";
            this.txtHBardCode.ReadOnly = false;
        }
 
        //查询条形码
        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.txtHBath.Text = HProjectNum;
                    this.txtHBardCode.ReadOnly = true;
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(this, e.Message, "提示");
            }
        }
 
        private void txtHBardCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SelectHBardCode(this.txtHBardCode.Text);
            }
        }
 
        //员工编码回车
        private void HEmpCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SelectHEmpCode(this.HEmpCode.Text);
            }
        }
 
        //查询员工
        public void SelectHEmpCode(string HNumber)
        {
            try
            {
                DataSet ds = oCN.RunProcReturn("select  * from Gy_Employee where HNumber='" + HNumber + "' ", "Gy_Employee");
                if (ds.Tables[0].Rows.Count == 0)
                {
                    MessageBox.Show("查无数据!");
                }
                else
                {
                    string HEmpCodeName = ds.Tables[0].Rows[0]["HName"].ToString();
                    string HEmpCodeNumber = ds.Tables[0].Rows[0]["HNumber"].ToString();
                    this.HEmpCodeName.Text = HEmpCodeName;
                    this.HEmpCode.Text = HEmpCodeNumber;
                    this.HEmpCode.ReadOnly = true;
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(this, e.Message, "提示");
            }
        }
 
        //更换员工
        private void btnSelect_Click(object sender, EventArgs e)
        {
            this.HEmpCode.ReadOnly = false;
            this.HEmpCode.Focus();
            this.HEmpCode.Text = "";
            this.HEmpCodeName.Text = "";
 
            this.HSouce.ReadOnly = false;
            this.HSouce.Focus();
            this.HSouce.Text = "";
            this.HSouceName.Text = "";
        }
 
        //读取数据
        private void btnReadData_Click(object sender, EventArgs e)
        {
 
            if (this.HEmpCode.Text == "" || this.txtHBath.Text == "")
            {
 
                MessageBox.Show("请输入员工编码或者条形码!");
            }
            else {
                if (num == 0)
                {
                    this.btnReadData.Text = "暂停";
                    this.timer1.Enabled = true;
                    num = 1;
                }
                else if (num == 1)
                {
                    this.btnReadData.Text = "启动";
                    this.timer1.Enabled = false;
                    num = 0;
                }
            }
        }
 
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (this.HEmpCode.Text == ""|| this.txtHBath.Text == "")
            {
 
                MessageBox.Show("请输入员工编码或者条形码!");
            }
            else
            {
                ReadCsv();
            }
        }
 
        //读取csv格式文件
        private void ReadCsv()
        {
            string HBath = this.txtHBath.Text;
            string csv_file_path = $@"C:\Users\admin\Desktop\新建文件夹 (2)\20231123.csv";
            //string csv_file_path = $@"D:\THLCR_Data\{HBath}\20231123.csv";
            bool flag = File.Exists(csv_file_path);
 
            if (flag)
            {
 
                DataTable dt = new DataTable();
                string contents = File.ReadAllText(csv_file_path, Encoding.GetEncoding("gb2312"));
 
                TextFieldParser parser = new TextFieldParser(new StringReader(contents));
 
                parser.HasFieldsEnclosedInQuotes = true;
                parser.SetDelimiters(",");
 
                string[] fields;
 
                while (!parser.EndOfData)
                {
                    fields = parser.ReadFields();
 
                    if (dt.Columns.Count == 0)
                    {
                        foreach (string field in fields)
                        {
                            dt.Columns.Add(new DataColumn(string.IsNullOrWhiteSpace(field.Trim('\"')) ? null : field.Trim('\"'), typeof(string)));
                        }
                    }
                    else
                    {
                        dt.Rows.Add(fields.Select(item => string.IsNullOrWhiteSpace(item.Trim('\"')) ? null : item.Trim('\"')).ToArray());
                    }
                }
                parser.Close();
 
                //获取当前时间
                DateTime ActionTime = DateTime.Parse(DateTime.Now.AddMinutes(-100).ToString("yyyy-MM-dd  HH:mm:ss"));
                DateTime EndTime = DateTime.Parse(DateTime.Now.AddMinutes(1).ToString("yyyy-MM-dd  HH:mm:00"));
 
                foreach (DataRow item in dt.Rows)
                {
                    DateTime NowTime = DateTime.Parse(item["测试时间"].ToString());
                    string HBadCodeSN = item["序号"].ToString();
 
                    if (NowTime >= ActionTime && NowTime < EndTime && HBadCodeSN != "")
                    {
 
                        string HSourceCode = "9994";//设备编号 组装1 9994 组装2 9995 组装3 9996
                        string HEmpCode = this.HEmpCode.Text;
                        string HBarCode = item["序号"].ToString();
                        string HCreateTime = item["测试时间"].ToString();
                        string HDate = DateTime.Parse(item["测试时间"].ToString()).ToString("yyyy-MM-dd");
                        string HResult = item["分选"].ToString() == "PASS" ? "OK" : "NG";
                        string HProcNumber = "105";
                        int HFlag = 0;
                        //string HEmpCode = "";
                        if (HBadCodeSN != "")
                        {
                            string HType = "";
                            int HCount = 1;
 
                            DataSet ds = oCN.RunProcReturn(@"select * from Sb_EquipMentCollection_SN where HBarCode='" + HBarCode + "' and HCreateTime='" + HCreateTime + "'", "Sb_EquipMentCollection_SN");
 
                            if (ds.Tables[0].Rows.Count == 0)
                            {
                                string sql = $@"insert into Sb_EquipMentCollection_SN(HSourceCode,HEmpCode,HType,HBarCode,HCount,HCreateTime,HDate,HResult,HProcNumber,HFlag)
                    values('{HSourceCode}','{HEmpCode}','{HType}','{HBarCode}','{HCount}','{HCreateTime}','{HDate}','{HResult}','{HProcNumber}','{HFlag}')";
                                oCN.RunProc(sql);
                                ListSelect.Items.Add("条码:" + HBarCode + ",日期:" + HCreateTime + ",结果:" + HResult);
                            }
                        }
 
                        for (int i = 0; i < dt.Columns.Count; i++)
                        {
                            if (dt.Columns[i].ColumnName.Contains("Ls"))
                            {
                                string HType = dt.Columns[i].ColumnName;
                                string HCount = item[HType].ToString();
 
                                DataSet ds = oCN.RunProcReturn(@"select * from Sb_EquipMentCollectionTechParam_SN where HBarCode='" + HBarCode + "' and HType='" + HType + "'", "Sb_EquipMentCollectionTechParam_SN");
 
                                if (ds.Tables[0].Rows.Count == 0)
                                {
                                    string sql = $@"insert into Sb_EquipMentCollectionTechParam_SN(HSourceCode,HEmpCode,HType,HBarCode,HCount,HCreateTime,HDate,HResult,HProcNumber,HFlag)
values('{HSourceCode}','{HEmpCode}','{HType}','{HBarCode}','{HCount}','{HCreateTime}','{HDate}','{HResult}','{HProcNumber}','{HFlag}')";
                                    oCN.RunProc(sql);
                                    //ListSelect.Items.Add("条码:" + HBarCode + ",日期:" + HCreateTime + ",结果:" + HResult);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}