WorkCountManager.cs
9.2 KB
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
using URSoldering.Common;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace URSoldering.DeviceLibrary
{
public class WorkCountManager
{
public static CountInfo CountObj = null;
public static void LoadData()
{
try
{
string[] lines = FileUtil.ReadFileArray(GetConfigPath());
if (lines.Length > 0)
{
string countStr = lines[0];
CountObj = JsonHelper.DeserializeJsonToObject<CountInfo>(countStr);
}
bool needSave = false;
if (CountObj == null)
{
CountObj = new CountInfo();
needSave = true;
}
if (CountObj.WorkLogList==null|| CountObj.WorkLogList.Count <= 0)
{
CountObj.WorkLogList = new List<WorkLogInfo>();
Random rr = new Random();
for (int i = 0; i < 100; i++)
{
Random rd = new Random((int)DateTime.Now.Ticks + i * rr.Next(1, 1000));
DateTime DayTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 23, 59, 59);
WorkLogInfo log = new WorkLogInfo();
log.DayTime = DayTime.AddDays(-i);
if (i == 0)
{
log.WeldCount = 0;
log.WeldPointCount = 0;
}
else
{
log.WeldCount = rd.Next(100, 250);
log.WeldPointCount = rd.Next(500, 800);
}
CountObj.WorkLogList.Add(log);
}
needSave = true;
}
else if (CountObj.WorkLogList.Count > 100)
{
List<WorkLogInfo> newList = (from m in CountObj.WorkLogList orderby m.DayTime descending select m).Take(100).ToList<WorkLogInfo>();
CountObj.WorkLogList = newList;
needSave = true;
}
if (needSave)
{
SaveListToFile(CountObj);
}
}
catch (Exception ex)
{
LogUtil.error("出错:" + ex.ToString());
}
}
public static void SaveResult(int result)
{
if (result.Equals(1))
{
AddOKCount();
}
else
{
AddNGCount();
}
}
public static void AddOKCount()
{
CountObj.OKCount++;
//判断是否是今日
if (!DateTime.Now.Date.Equals(CountObj.TodayTime.Date))
{
CountObj.TodayNGCount = 0;
CountObj.TodayOKCount = 0;
CountObj.TodayTime = DateTime.Now;
}
CountObj.TodayOKCount++;
CountObj.AddTodayCount(false);
SaveListToFile(CountObj);
}
public static void AddNGCount()
{
CountObj.NGCount++;
//判断是否是今日
if (!DateTime.Now.Date.Equals(CountObj.TodayTime.Date))
{
CountObj.TodayNGCount = 0;
CountObj.TodayOKCount = 0;
CountObj.TodayTime = DateTime.Now;
}
CountObj.AddTodayCount(false);
CountObj.TodayNGCount++;
SaveListToFile(CountObj);
}
public static bool IsAlarm()
{
if (CountObj.WeldPointCount > CountObj.AlarmCount)
{
return true;
}
return false;
}
/// <summary>
/// 设置烙铁头更换次数
/// </summary>
/// <param name="count"></param>
public static void UpdateAlarmCount(int count)
{
//添加一次焊点
CountObj.AlarmCount = count;
SaveListToFile(CountObj);
}
public static void AddWeldPint()
{
//添加一次焊点
CountObj.WeldPointCount++;
CountObj.AddTodayCount(true );
SaveListToFile(CountObj);
}
/// <summary>
/// 清理烙铁头次数
/// </summary>
public static void ClearWeldCount()
{
LogUtil.info("清理烙铁头");
CountObj.WeldPointCount = 0;
CountObj.WeldStartTime = DateTime.Now;
SaveListToFile(CountObj);
}
public static void ClearTodayCount()
{
LogUtil.info("清理今日统计数量");
CountObj.TodayNGCount = 0;
CountObj.TodayOKCount = 0;
CountObj.TodayTime = DateTime.Now;
SaveListToFile(CountObj);
}
public static void ClearCount()
{
LogUtil.info("清理统计数量");
CountObj = new CountInfo();
SaveListToFile(CountObj);
}
public static string GetConfigPath()
{
string appPath = Application.StartupPath;
string filePath = appPath + ConfigAppSettings.GetValue(Setting_Init.WorkCount_ConfigPath);
return filePath;
}
private static bool SaveListToFile(CountInfo obj)
{
string str = JsonHelper.SerializeObject(obj);
string filePath = GetConfigPath();
string[] strArray = new string[] { str };
return FileUtil.SaveListToFile(strArray, GetConfigPath());
}
}
public class CountInfo
{
public CountInfo()
{
OKCount = 0;
NGCount = 0;
StartTime = DateTime.Now;
TodayNGCount = 0;
TodayOKCount = 0;
TodayTime = DateTime.Now;
WeldStartTime = DateTime.Now;
WeldPointCount = 0;
AlarmCount = 600000;
WorkLogList = new List<WorkLogInfo>();
}
public double Percent()
{
if (OKCount.Equals(0) && NGCount.Equals(0))
{
return 100f;
}
else
{
return 100F * Math.Round((double)OKCount / (NGCount + OKCount), 4);
}
}
public double TodayPercent()
{
if (!DateTime.Now.Date.Equals( TodayTime.Date))
{
TodayNGCount = 0;
TodayOKCount = 0;
TodayTime = DateTime.Now;
}
if (TodayOKCount.Equals(0) && TodayNGCount.Equals(0))
{
return 100f;
}
else
{
return 100F * Math.Round((double)TodayOKCount / (TodayNGCount + TodayOKCount), 4);
}
}
public int OKCount { get; set; }
public int NGCount { get; set; }
/// <summary>
///开始记录的时间,清理后需要更新时间
/// </summary>
public DateTime StartTime { get; set; }
public int TodayOKCount { get; set; }
public int TodayNGCount { get; set; }
public DateTime TodayTime { get; set; }
/// <summary>
/// 开始焊接时间
/// </summary>
public DateTime WeldStartTime { get; set; }
/// <summary>
/// 已焊接焊点数量
/// </summary>
public int WeldPointCount { get; set; }
/// <summary>
/// 焊接报警次数,默认600000
/// </summary>
public int AlarmCount { get; set; }
public List<WorkLogInfo> WorkLogList = new List<WorkLogInfo>();
public void AddTodayCount(bool ispoint)
{
//List<WorkLogInfo> list = (from m in WorkLogList where m.getTimeStr().Equals(DateTime.Now.ToString("MM-dd")) select m).ToList<WorkLogInfo>();
WorkLogInfo log = null;
if (WorkLogList.Count > 0)
{
if (WorkLogList[0].DayTime.Date.Equals(DateTime.Now.Date))
{
log = WorkLogList[0];
}
}
if (log == null)
{
log = new WorkLogInfo();
WorkLogList.Insert(0, log);
}
if (ispoint)
{
log.WeldPointCount++;
}
else
{
log.WeldCount++;
}
}
}
public class WorkLogInfo
{
public WorkLogInfo()
{
DayTime = new DateTime( DateTime.Now.Year,DateTime.Now.Month,DateTime.Now.Day,23,59,59);
WeldCount = 0;
WeldPointCount = 0;
}
public DateTime DayTime { get; set; }
/// <summary>
/// 焊接数量
/// </summary>
public int WeldCount = 0;
/// <summary>
/// 焊点数量
/// </summary>
public int WeldPointCount = 0;
public string getTimeStr()
{
return DayTime.ToString("MM-dd");
}
}
}