jace.tang
2022-06-13 ab94f070d797edbe927d7d58a460cc4eddf48b03
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
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Text;
 
namespace M423project
{
    public class SpotCheckData
    {
        private SqlConnection connection;
 
        public SpotCheckData()
        {
            string connStr = ConfigurationManager.AppSettings["connectionString"];
            connection = new SqlConnection(connStr);
            try
            {
                connection.Open();
            }
            catch (Exception)
            {
                CommonUtil.WriteLog(LogType.Err, "连接数据库失败!");
            }
        }
 
        public DataTable GetTodayFinishedSpot()
        {
            DataTable dt = new DataTable();
            string sql = @"select * from SpotCheckDetail
                        where DATEDIFF(DAY, PickTime, GETDATE())= 0";
            SqlDataReader dr = null;
            using (SqlCommand cmd = new SqlCommand(sql, connection))
            {
                try
                {
                    dr = cmd.ExecuteReader();
                    dt.Load(dr);
                }
                catch (Exception e)
                {
                    CommonUtil.WriteLog(LogType.Inf, string.Format("获取点检数据数据时出现异常:{0}", e.Message));
                }
                finally
                {
                    if (dr != null)
                        dr.Close();
                }
            }
            return dt;
        }
 
        public DataTable GetTodayCheckPlan()
        {
            DataTable dt = new DataTable();
            string sql = @"select * from SpotCheck
                        where DATEDIFF(DAY,BeginTime,GETDATE())=0";
            SqlDataReader dr = null;
            using (SqlCommand cmd = new SqlCommand(sql, connection))
            {
                try
                {
                    dr = cmd.ExecuteReader();
                    dt.Load(dr);
                }
                catch (Exception e)
                {
                    CommonUtil.WriteLog(LogType.Inf, string.Format("获取执行中的点检计划数据时出现异常:{0}", e.Message));
                }
                finally
                {
                    if (dr != null)
                        dr.Close();
                }
            }
            return dt;
        }
 
        public bool IsSpotCheck()
        {
            try
            {
                int count = 0;
                StringBuilder sb = new StringBuilder();
                sb.AppendFormat(@"select COUNT(ID) from SpotCheck
                                where DATEDIFF(DAY,BeginTime,GETDATE())=0");
                using (SqlCommand cmd = new SqlCommand(sb.ToString(), connection))
                {
                    count = Convert.ToInt32(cmd.ExecuteScalar());
                }
                return count > 0;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
 
        public DataTable GetTodaySpotCheck()
        {
            DataTable dt = new DataTable();
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat(@"select * from SpotCheck
                            where DATEDIFF(DAY,BeginTime,GETDATE())=0");
            SqlDataReader dr = null;
            using (SqlCommand cmd = new SqlCommand(sb.ToString(), connection))
            {
                try
                {
                    dr = cmd.ExecuteReader();
                    dt.Load(dr);
                }
                catch (Exception e)
                {
                    CommonUtil.WriteLog(LogType.Inf, string.Format("获取抽檢数据时出现异常:{0}", e.Message));
                }
                finally
                {
                    if (dr != null)
                        dr.Close();
                }
            }
            return dt;
        }
 
        public void SaveSpotCheck(SpotCheck spotCheck)
        {
            try
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendFormat(@"insert into SpotCheck
                                (BeginTime,TotalQty,FinishedQty,PickCount,LastBreakProductQty,LastProductQty)
                                values(GETDATE(),{0},{1},{2},{3},{4})", spotCheck.TotalQty, spotCheck.FinishedQty, spotCheck.PickCount,spotCheck.LastBreakProductQty,spotCheck.LastProductQty);
                using (SqlCommand cmd = new SqlCommand(sb.ToString(), connection))
                {
                    cmd.ExecuteNonQuery();
                }
            }
            catch (Exception ex)
            {
                CommonUtil.WriteLog(LogType.Err, ex.ToString());
            }
        }
 
        public void SaveSpotCheckDetail(SpotCheckDetail spotCheckDetail)
        {
            try
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendFormat(@"insert into SpotCheckDetail
                                (PickTime,ProductNo,ProductHeight,ProductLength,ProductWidth,Type)
                                values(GETDATE(),'{0}','{1}','{2}','{3}',N'{4}')", spotCheckDetail.ProductNo,
                                spotCheckDetail.ProductHeight, spotCheckDetail.ProductLength,
                                spotCheckDetail.ProductWidth, spotCheckDetail.Type);
                using (SqlCommand cmd = new SqlCommand(sb.ToString(), connection))
                {
                    cmd.ExecuteNonQuery();
                }
            }
            catch (Exception ex)
            {
                CommonUtil.WriteLog(LogType.Err, ex.ToString());
            }
        }
 
        public void UpdateFinishedQty()
        {
            try
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendFormat(@"update SpotCheck
                                set FinishedQty=FinishedQty+1
                                where DATEDIFF(DAY,BeginTime,GETDATE())=0");
                using (SqlCommand cmd = new SqlCommand(sb.ToString(), connection))
                {
                    cmd.ExecuteNonQuery();
                }
            }
            catch (Exception ex)
            {
                CommonUtil.WriteLog(LogType.Err, ex.ToString());
            }
        }
 
        public void UpdateLastBreakProductQty(int qty)
        {
            try
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendFormat(@"update SpotCheck
                                set LastBreakProductQty={0}
                                where DATEDIFF(DAY,BeginTime,GETDATE())=0",qty);
                using (SqlCommand cmd = new SqlCommand(sb.ToString(), connection))
                {
                    cmd.ExecuteNonQuery();
                }
            }
            catch (Exception ex)
            {
                CommonUtil.WriteLog(LogType.Err, ex.ToString());
            }
        }
 
        public void UpdateLastProductQty(int qty)
        {
            try
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendFormat(@"update SpotCheck
                                set LastProductQty={0}
                                where DATEDIFF(DAY,BeginTime,GETDATE())=0", qty);
                using (SqlCommand cmd = new SqlCommand(sb.ToString(), connection))
                {
                    cmd.ExecuteNonQuery();
                }
            }
            catch (Exception ex)
            {
                CommonUtil.WriteLog(LogType.Err, ex.ToString());
            }
        }
 
        public void SaveCheckToCsv(SpotCheckDetail detail)
        {
            string fileName = @"E:\CheckReport\CheckReport" + DateTime.Now.ToString("yyyyMMdd") + ".csv";
            bool isNew = !File.Exists(fileName);
            using (FileStream myStream = new FileStream(fileName, FileMode.OpenOrCreate))
            {
                myStream.Seek(0, SeekOrigin.End);
                using (StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.UTF8))
                {
                    try
                    {
                        if (isNew)
                        {
                            sw.WriteLine("產品條碼,拾取時間,長度,寬度,高度");
                        }
                        sw.WriteLine(
                            string.Format("{0},{1},{2},{3},{4}",
                            detail.ProductNo, detail.PickTime, detail.ProductLength, detail.ProductWidth, detail.ProductHeight
                            ));
                    }
                    catch (Exception e)
                    {
                        CommonUtil.WriteLog(LogType.Inf, string.Format("记录抽检结果出现异常:{0}", e.Message));
                    }
                }
            }
        }
 
        public void ClearTodayCheck()
        {
            try
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendFormat(@"delete from SpotCheck
                                where DATEDIFF(DAY,BeginTime,GETDATE())=0");
                sb.AppendFormat(@"delete from SpotCheckDetail
                                where DATEDIFF(DAY,PickTime,GETDATE())=0");
                using (SqlCommand cmd = new SqlCommand(sb.ToString(), connection))
                {
                    cmd.ExecuteNonQuery();
                }
            }
            catch (Exception ex)
            {
                CommonUtil.WriteLog(LogType.Err, ex.ToString());
            }
        }
    }
 
    public class SpotCheck
    {
        /// <summary>
        /// 抽检开始时间
        /// </summary>
        public DateTime BeginTime { get; set; }
 
        /// <summary>
        /// 总抽检数
        /// </summary>
        public int TotalQty { get; set; }
 
        /// <summary>
        /// 已完成抽检数
        /// </summary>
        public int FinishedQty { get; set; }
 
        /// <summary>
        /// 抽检跨度(每隔多少个抽检下一个)
        /// </summary>
        public int PickCount { get; set; }
 
        /// <summary>
        /// 上一次程序关闭时记录下的界面生产数量
        /// </summary>
        public int LastBreakProductQty { get; set; }
 
        /// <summary>
        /// 上一次拾取时记录下的界面生产数量
        /// </summary>
        public int LastProductQty { get; set; }
    }
 
    public class SpotCheckDetail
    {
        /// <summary>
        /// 产品条码
        /// </summary>
        public string ProductNo { get; set; }
 
        /// <summary>
        /// 拾取时间
        /// </summary>
        public DateTime PickTime { get; set; }
 
        public double ProductHeight { get; set; }
 
        public double ProductLength { get; set; }
 
        public double ProductWidth { get; set; }
 
        /// <summary>
        /// 拾取类型(自动拾取、手动拾取)
        /// </summary>
        public string Type { get; set; }
 
    }
}