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
using FLY.OBJComponents.Server.Model;
using SQLite;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
namespace FLY.Blowing.DbViewer.Core
{
public class DbHelper : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public string DbDirPath { get; set; } = @"D:\blowingdata";
public string DbPathAirRing => System.IO.Path.Combine(DbDirPath, "airring.sqlite3");
public string DbPathWeight => System.IO.Path.Combine(DbDirPath, "weight.sqlite3");
public string DbPathWinder => System.IO.Path.Combine(DbDirPath, "winder.sqlite3");
public string DbPathIbc => System.IO.Path.Combine(DbDirPath, "ibc.sqlite3");
public bool IsAirRingExist { get; private set; }
public bool IsWeightExist { get; private set; }
public bool IsWinderExist { get; private set; }
public bool IsIbcExist { get; private set; }
public DateTime AirRingDbBeginTime { get; private set; }
public DateTime AirRingDbEndTime { get; private set; }
public DateTime WeightDbBeginTime { get; private set; }
public DateTime WeightDbEndTime { get; private set; }
public DateTime WinderDbBeginTime { get; private set; }
public DateTime WinderDbEndTime { get; private set; }
public DateTime IbcDbBeginTime { get; private set; }
public DateTime IbcDbEndTime { get; private set; }
FLY.FeedbackRenZiJia.Server.Model.DbModel dbModelAirRing = null;
FLY.Weight.Server.Model.DbModel dbModelWeight = null;
FLY.Winder.Server.Model.DbModel dbModelWinder = null;
FLY.IBC.Server.Model.DbModel dbModelIbc = null;
public DbHelper()
{
}
/// <summary>
/// 出错代码
/// </summary>
public string ErrorMsg { get; private set; }
public bool IsDbErr { get; private set; }
/// <summary>
/// 从表获取数据
/// </summary>
public bool Init()
{
IsAirRingExist = System.IO.File.Exists(DbPathAirRing);
IsWeightExist = System.IO.File.Exists(DbPathWeight);
IsWinderExist = System.IO.File.Exists(DbPathWinder);
IsIbcExist = System.IO.File.Exists(DbPathIbc);
if (IsAirRingExist)
{
if (!Init_airRing())
{
return false;
}
}
if (IsWeightExist)
{
if (!Init_weight())
{
return false;
}
}
if (IsWinderExist)
{
if (!Init_winder())
{
return false;
}
}
if (IsIbcExist)
{
if (!Init_ibc())
{
return false;
}
}
ErrorMsg = "数据库 加载成功";
IsDbErr = false;
return true;
}
bool Init_airRing()
{
try
{
dbModelAirRing = new FeedbackRenZiJia.Server.Model.DbModel(DbPathAirRing);
if (!dbModelAirRing.InitNoBuild())
{
ErrorMsg = $"{DbPathAirRing} 数据库格式异常";
IsDbErr = true;
return false;
}
string cmd = $"SELECT Time FROM {dbModelAirRing.TbThickHeat.TableName} ORDER BY ID LIMIT 1";
var ret = dbModelAirRing.sqliteHelper.ExecuteScalar(cmd);
AirRingDbBeginTime = System.Convert.ToDateTime(ret);
cmd = $"SELECT Time FROM {dbModelAirRing.TbThickHeat.TableName} ORDER BY ID DESC LIMIT 1";
ret = dbModelAirRing.sqliteHelper.ExecuteScalar(cmd);
AirRingDbEndTime = System.Convert.ToDateTime(ret);
}
catch (Exception e)
{
ErrorMsg = e.Message;
IsDbErr = true;
return false;
}
return true;
}
bool Init_weight()
{
try
{
dbModelWeight = new Weight.Server.Model.DbModel(DbPathWeight);
if (!dbModelWeight.InitNoBuild())
{
ErrorMsg = $"{DbPathWeight} 数据库格式异常";
IsDbErr = true;
return false;
}
string cmd = $"SELECT Time FROM {dbModelWeight.TbFlow.TableName} ORDER BY ID LIMIT 1";
var ret = dbModelWeight.sqliteHelper.ExecuteScalar(cmd);
WeightDbBeginTime = System.Convert.ToDateTime(ret);
cmd = $"SELECT Time FROM {dbModelWeight.TbFlow.TableName} ORDER BY ID DESC LIMIT 1";
ret = dbModelWeight.sqliteHelper.ExecuteScalar(cmd);
WeightDbEndTime = System.Convert.ToDateTime(ret);
}
catch (Exception e)
{
ErrorMsg = e.Message;
IsDbErr = true;
return false;
}
return true;
}
bool Init_winder()
{
try
{
dbModelWinder = new FLY.Winder.Server.Model.DbModel(DbPathWinder);
if (!dbModelWinder.InitNoBuild())
{
ErrorMsg = $"{DbPathWinder} 数据库格式异常";
IsDbErr = true;
return false;
}
string cmd = $"SELECT Time FROM {dbModelWinder.TbWinderInfo.TableName} ORDER BY ID LIMIT 1";
var ret = dbModelWinder.sqliteHelper.ExecuteScalar(cmd);
WinderDbBeginTime = System.Convert.ToDateTime(ret);
cmd = $"SELECT Time FROM {dbModelWinder.TbWinderInfo.TableName} ORDER BY ID DESC LIMIT 1";
ret = dbModelWinder.sqliteHelper.ExecuteScalar(cmd);
WinderDbEndTime = System.Convert.ToDateTime(ret);
}
catch (Exception e)
{
ErrorMsg = e.Message;
IsDbErr = true;
return false;
}
return true;
}
bool Init_ibc()
{
try
{
dbModelIbc = new FLY.IBC.Server.Model.DbModel(DbPathIbc);
if (!dbModelIbc.InitNoBuild())
{
ErrorMsg = $"{DbPathIbc} 数据库格式异常";
IsDbErr = true;
return false;
}
string cmd = $"SELECT Time FROM {dbModelIbc.TbWidth.TableName} ORDER BY ID LIMIT 1";
var ret = dbModelIbc.sqliteHelper.ExecuteScalar(cmd);
IbcDbBeginTime = System.Convert.ToDateTime(ret);
cmd = $"SELECT Time FROM {dbModelIbc.TbWidth.TableName} ORDER BY ID DESC LIMIT 1";
ret = dbModelIbc.sqliteHelper.ExecuteScalar(cmd);
IbcDbEndTime = System.Convert.ToDateTime(ret);
}
catch (Exception e)
{
ErrorMsg = e.Message;
IsDbErr = true;
return false;
}
return true;
}
public string GetPackMsg { get; private set; }
public DbProfilePack GetPack(DateTime startTime, DateTime endTime)
{
DbProfilePack profilePack = new DbProfilePack();
profilePack = GetProfilePack_AirRing(startTime, endTime, profilePack);
profilePack = GetProfilePack_Ibc(startTime, endTime, profilePack);
profilePack = GetProfilePack_Winder(startTime, endTime, profilePack);
profilePack = GetProfilePack_Weight(startTime, endTime, profilePack);
return profilePack;
}
DbProfilePack GetProfilePack_AirRing(DateTime startTime, DateTime endTime, DbProfilePack profilePack)
{
if (dbModelAirRing == null)
return profilePack;
GetPackMsg = "从风环数据库加载数据";
string startTime_str = startTime.ToStringOfSQLiteFieldType();
string endTime_str = endTime.ToStringOfSQLiteFieldType();
string condition =
$"WHERE Time>={startTime_str}" +
$" AND Time<={endTime_str}";
var db_thickHeats = dbModelAirRing.TbThickHeat.Find(condition);
var lc_thickHeats = FLY.FeedbackRenZiJia.Server.Model.Lc_AutoMapperProfile.Mapper.Map<
List<FLY.FeedbackRenZiJia.Server.Model.Db_ThickHeat>, List<FLY.FeedbackRenZiJia.Server.Model.Lc_ThickHeat>>(db_thickHeats);
profilePack.ThickHeats = lc_thickHeats;
return profilePack;
}
DbProfilePack GetProfilePack_Ibc(DateTime startTime, DateTime endTime, DbProfilePack profilePack)
{
if (dbModelIbc == null)
return profilePack;
GetPackMsg = "从IBC数据库加载数据";
string startTime_str = startTime.ToStringOfSQLiteFieldType();
string endTime_str = endTime.ToStringOfSQLiteFieldType();
string condition =
$"WHERE Time>={startTime_str}" +
$" AND Time<={endTime_str}";
var db_widths = dbModelIbc.TbWidth.Find(condition);
profilePack.Widths = db_widths;
return profilePack;
}
DbProfilePack GetProfilePack_Winder(DateTime startTime, DateTime endTime, DbProfilePack profilePack)
{
if (dbModelWinder == null)
return profilePack;
GetPackMsg = "从收卷数据库加载数据";
string startTime_str = startTime.ToStringOfSQLiteFieldType();
string endTime_str = endTime.ToStringOfSQLiteFieldType();
string condition =
$"WHERE Time>={startTime_str}" +
$" AND Time<={endTime_str}";
var db_winderInfos = dbModelWinder.TbWinderInfo.Find(condition);
profilePack.WinderInfos = db_winderInfos;
return profilePack;
}
DbProfilePack GetProfilePack_Weight(DateTime startTime, DateTime endTime, DbProfilePack profilePack)
{
if (dbModelWeight == null)
return profilePack;
GetPackMsg = "从称重数据库加载流量数据";
string startTime_str = startTime.ToStringOfSQLiteFieldType();
string endTime_str = endTime.ToStringOfSQLiteFieldType();
string condition =
$"WHERE Time>={startTime_str}" +
$" AND Time<={endTime_str}";
var db_flows = dbModelWeight.TbFlow.Find(condition);
var lc_flows = FLY.Weight.Server.Model.Lc_AutoMapperProfile.Mapper.Map<
List<FLY.Weight.Server.Model.Db_Flow>, List<FLY.Weight.Server.Model.Lc_Flow>>(db_flows);
profilePack.WeightFlows = lc_flows;
GetPackMsg = "从称重数据库加载混料数据";
var db_mixs = dbModelWeight.TbMix.Find(condition);
var lc_mixs = FLY.Weight.Server.Model.Lc_AutoMapperProfile.Mapper.Map<
List<FLY.Weight.Server.Model.Db_Mix>, List<FLY.Weight.Server.Model.Lc_Mix>>(db_mixs);
profilePack.WeightMixs = lc_mixs;
return profilePack;
}
}
#region 链接表的关系
/// <summary>
/// 包装 DB_Profile, 组织 DB_Profile, DB_ScanData....
/// </summary>
public class DbProfilePack
{
public List<FLY.FeedbackRenZiJia.Server.Model.Lc_ThickHeat> ThickHeats;
public List<FLY.IBC.Server.Model.Db_Width> Widths;
public List<FLY.Winder.Server.Model.Db_WinderInfo> WinderInfos;
public List<FLY.Weight.Server.Model.Lc_Flow> WeightFlows;
public List<FLY.Weight.Server.Model.Lc_Mix> WeightMixs;
public int GetTotalRow() {
int sum = 0;
sum += ThickHeats != null ? ThickHeats.Count() : 0;
sum += Widths != null ? Widths.Count() : 0;
sum += WinderInfos != null ? WinderInfos.Count() : 0;
sum += WeightFlows != null ? WeightFlows.Count() : 0;
sum += WeightMixs != null ? WeightMixs.Count() : 0;
return sum;
}
public List<string> GetMsgs() {
List<string> msgs = new List<string>();
if (ThickHeats != null && ThickHeats.Count() > 0)
{
msgs.Add($"风环记录 {ThickHeats.Count()}条");
}
if (WeightFlows != null && WeightFlows.Count() > 0)
{
msgs.Add($"称重记录 {WeightFlows.Count()}条");
}
if (WinderInfos != null && WinderInfos.Count() > 0)
{
msgs.Add($"收卷记录 {WinderInfos.Count()}条");
}
if (Widths != null && Widths.Count() > 0)
{
msgs.Add($"IBC记录 {Widths.Count()}条");
}
return msgs;
}
public void GetDateRange(out DateTime beginTime, out DateTime endTime) {
beginTime = DateTime.MinValue;
endTime = DateTime.MinValue;
if (ThickHeats != null && ThickHeats.Count() > 0)
{
var b = ThickHeats.First().Time;
var e = ThickHeats.Last().Time;
if (beginTime == DateTime.MinValue)
{
beginTime = b;
endTime = e;
}
else
{
if (b < beginTime)
beginTime = b;
if (e > endTime)
endTime = e;
}
}
if (WeightFlows != null && WeightFlows.Count() > 0)
{
var b = WeightFlows.First().Time;
var e = WeightFlows.Last().Time;
if (beginTime == DateTime.MinValue)
{
beginTime = b;
endTime = e;
}
else
{
if (b < beginTime)
beginTime = b;
if (e > endTime)
endTime = e;
}
}
if (WinderInfos != null && WinderInfos.Count() > 0)
{
var b = WinderInfos.First().Time;
var e = WinderInfos.Last().Time;
if (beginTime == DateTime.MinValue)
{
beginTime = b;
endTime = e;
}
else
{
if (b < beginTime)
beginTime = b;
if (e > endTime)
endTime = e;
}
}
if (Widths != null && Widths.Count() > 0)
{
var b = Widths.First().Time;
var e = Widths.Last().Time;
if (beginTime == DateTime.MinValue)
{
beginTime = b;
endTime = e;
}
else
{
if (b < beginTime)
beginTime = b;
if (e > endTime)
endTime = e;
}
}
}
}
#endregion
}