唐召明
authored
7 months ago
1
using System ;
HuXiYu
authored
about a year ago
2
3
4
5
using System.Data ;
using SqlSugar ;
using Hh.Mes.Service.Repository ;
using Hh.Mes.POJO.Entity ;
HuXiYu
authored
about a year ago
6
7
8
using System.Linq ;
using Hh.Mes.Common.log ;
using Hh.Mes.POJO.Response ;
赖素文
authored
about a year ago
9
using Hh.Mes.Pojo.System ;
HuXiYu
authored
about a year ago
10
11
12
13
14
15
16
17
18
19
20
21
namespace Hh.Mes.Service.ApiService
{
public class DashboardService : RepositorySqlSugar < sys_user_online >
{
/// < summary >
/// 故障报告
/// </ summary >
/// < param name = "yyyyMonth" > 日期 </ param >
/// < param name = "project" > 项目 </ param >
/// < param name = "equipmentType" > 设备类型 </ param >
/// < returns ></ returns >
HuXiYu
authored
about a year ago
22
public dynamic GetFaultReport ( string yyyyMonth , string projectCode , string equipmentTypeCode )
HuXiYu
authored
about a year ago
23
{
HuXiYu
authored
about a year ago
24
var response = new ResponseNew ();
HuXiYu
authored
about a year ago
25
26
27
return ExceptionsHelp . Instance . ExecuteT (() =>
{
var month = Convert . ToDateTime ( yyyyMonth );
HuXiYu
authored
about a year ago
28
var whereSql = @" where t1.CreateTime>='{0}' and t1.CreateTime<='{1}' and t1.EquipmentTypeCode not in ('{2}')" ;
HuXiYu
authored
about a year ago
29
30
31
// 月初、月末
var monthStart = month . AddDays ( 1 - month . Day );
var monthEnd = month . AddDays ( 1 - month . Day ). Date . AddMonths ( 1 ). AddSeconds (- 1 );
HuXiYu
authored
about a year ago
32
var sqlWhereMonth = string . Format ( whereSql , monthStart , monthEnd , SystemVariable . IotNotContainDevice );// 月数据源查询条件
HuXiYu
authored
about a year ago
33
34
35
36
// 周一、周末
var weekOne = month . AddDays ( 0 - ( Convert . ToInt16 ( month . DayOfWeek ) == 0 ? 7 : Convert . ToInt16 ( month . DayOfWeek )) + 1 ). ToString ( "yyyy-MM-dd 00:00:00" );
var weekend = month . AddDays ( 6 - ( Convert . ToInt16 ( month . DayOfWeek ) == 0 ? 7 : Convert . ToInt16 ( month . DayOfWeek )) + 1 ). ToString ( "yyyy-MM-dd 23:59:59" );
HuXiYu
authored
about a year ago
37
var sqlWhereWeek = string . Format ( whereSql , weekOne , weekend , SystemVariable . IotNotContainDevice );
HuXiYu
authored
about a year ago
38
HuXiYu
authored
about a year ago
39
var sql = GetSql ( sqlWhereMonth , sqlWhereWeek , projectCode , equipmentTypeCode );
HuXiYu
authored
about a year ago
40
41
42
43
44
var ds = Context . Ado . GetDataSetAll ( sql );
var diffDay = DateTime . DaysInMonth ( month . Year , month . Month );
var monthStr = monthStart . ToString ( "MM-dd" ) + "~" + monthEnd . ToString ( "MM-dd" );
var weekStr = DateTime . Parse ( weekOne ). ToString ( "MM-dd" ) + "~" + DateTime . Parse ( weekend ). ToString ( "MM-dd" );
HuXiYu
authored
about a year ago
45
Tuple < DataSet , int , string , string > dss = new Tuple < DataSet , int , string , string >( ds , diffDay , monthStr , weekStr );
HuXiYu
authored
about a year ago
46
47
48
49
50
51
52
53
54
// 当天是否在本周内
var nowDay = DateTime . Now ;
var selectDay = Convert . ToDateTime ( yyyyMonth );
var startWeekDay = nowDay . AddDays (-( int ) nowDay . DayOfWeek + 1 ). ToString ( "yyyy-MM-dd 00:00:00" );// 当前周的周一日期
var weekOnes = selectDay . AddDays ( 0 - ( Convert . ToInt16 ( selectDay . DayOfWeek ) == 0 ? 7 : Convert . ToInt16 ( selectDay . DayOfWeek )) + 1 ). ToString ( "yyyy-MM-dd 00:00:00" );// 当前所选周的周一日期
var isInterval = false ;
if ( startWeekDay == weekOnes ) isInterval = true ;
HuXiYu
authored
about a year ago
55
response . data = new
HuXiYu
authored
about a year ago
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
{
listMonth = dss . Item1 . Tables [ 0 ],
listWeek = dss . Item1 . Tables [ 1 ],
listAlarmSum40 = dss . Item1 . Tables [ 2 ],
diffDay = dss . Item2 ,
monthStr = dss . Item3 ,
weekStr = dss . Item4 ,
weekDefault = dss . Item1 . Tables [ 3 ],
properlyTarget = dss . Item1 . Tables [ 4 ],
factoryTarget = dss . Item1 . Tables [ 5 ],
isInterval = isInterval
};
return response ;
});
HuXiYu
authored
about a year ago
73
HuXiYu
authored
about a year ago
74
}
HuXiYu
authored
about a year ago
75
public string GetSql ( string sqlWhereMonth , string sqlWhereWeek , string projectCode , string equipmentTypeCode )
HuXiYu
authored
about a year ago
76
{
HuXiYu
authored
about a year ago
77
var selectSql = "SELECT ProjectCode,projectName,sumTime=SUM(ErrorDuration) " ;
HuXiYu
authored
about a year ago
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
var sqlMonth = selectSql + "FROM #listMonth t" ;
var sqlWeek = selectSql + "FROM #listWeek t" ;
if (! string . IsNullOrEmpty ( projectCode ) && ! string . IsNullOrEmpty ( equipmentTypeCode ))
{
var whereSql = $ "WHERE t.projectCode='{projectCode}' and t.equipmentTypeCode='{equipmentTypeCode}'" ;
sqlMonth += whereSql ;
sqlWeek += whereSql ;
}
if (! string . IsNullOrEmpty ( projectCode ) && string . IsNullOrEmpty ( equipmentTypeCode ))
{
var whereSql = $ "WHERE t.projectCode='{projectCode}'" ;
sqlMonth += whereSql ;
sqlWeek += whereSql ;
}
if ( string . IsNullOrEmpty ( projectCode ) && ! string . IsNullOrEmpty ( equipmentTypeCode ))
{
var whereSql = $ "WHERE t.equipmentTypeCode='{equipmentTypeCode}'" ;
sqlMonth += whereSql ;
sqlWeek += whereSql ;
}
HuXiYu
authored
about a year ago
99
var groupBySql = " group by projectCode,projectName order by sumTime desc" ;
HuXiYu
authored
about a year ago
100
101
102
103
104
105
106
107
108
109
110
sqlMonth += groupBySql ;
sqlWeek += groupBySql ;
var sql = @$" ----------------- 本月公共数据源 --------------------------------------------
IF OBJECT_ID ( ' Tempdb .. # listMonth ' ) IS NOT NULL
DROP TABLE # listMonth ;
SELECT * INTO # listMonth FROM (
SELECT t1 . CreateTime AS Created ,
t1 . UpdateTime AS Updated ,
t1 . ErrorDuration ,
t1 . EquipmentCode ,
HuXiYu
authored
about a year ago
111
t1 . AlarmMessage + t1 . Remark AS Alarm ,
HuXiYu
authored
about a year ago
112
113
114
115
116
117
118
119
120
121
122
t1 . EquipmentName ,
t1 . EquipmentPropName ,
t1 . EquipmentTypeCode ,
t1 . ProjectCode ,
p . projectName AS ProjectName ,
t . name AS EquipmentTypeName
FROM dbo . daq_equipment_alarm_record t1
LEFT JOIN dbo . base_project p
ON p . projectCode = t1 . ProjectCode
LEFT JOIN dbo . base_equipment_type t
ON t . code = t1 . EquipmentTypeCode
HuXiYu
authored
about a year ago
123
{ sqlWhereMonth } AND t1 . ErrorDuration >= 60
HuXiYu
authored
about a year ago
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
) t ;
----------------< # listMonth 处理相同设备在同年月日时分数据 >----------------
WITH CTE
AS ( SELECT ROW_NUMBER () OVER ( PARTITION BY e . EquipmentCode ,
SUBSTRING ( CONVERT ( VARCHAR ( 50 ), e . Created , 120 ), 1 , 16 ),
SUBSTRING ( CONVERT ( VARCHAR ( 50 ), e . updated , 120 ), 1 , 16 )
ORDER BY EquipmentCode
) AS rownumber ,
*
FROM # listMonth e )
DELETE FROM CTE
WHERE rownumber > 1 ;
----------------- 本周公共数据源 # listWeek --------------------------------------------
IF OBJECT_ID ( ' Tempdb .. # listWeek ' ) IS NOT NULL
DROP TABLE # listWeek ;
SELECT *
INTO # listWeek
FROM
(
SELECT t1 . CreateTime AS created ,
t1 . UpdateTime AS updated ,
t1 . ErrorDuration ,
t1 . EquipmentCode ,
t1 . EquipmentName ,
t1 . EquipmentPropName ,
t1 . Id AS EquipmentAlarmId ,
HuXiYu
authored
about a year ago
151
t1 . AlarmMessage + t1 . Remark AS Alarm ,
HuXiYu
authored
about a year ago
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
t1 . ProjectCode ,
t1 . EquipmentTypeCode ,
t . name AS EquipmentTypeName ,
p . projectName AS ProjectName
FROM dbo . daq_equipment_alarm_record t1
LEFT JOIN dbo . base_equipment_type t
ON t . code = t1 . EquipmentTypeCode
LEFT JOIN dbo . base_project p
ON p . projectCode = t1 . ProjectCode
{ sqlWhereWeek } AND t1 . ErrorDuration >= 60
) t ;
---------------- listWeek 处理相同设备在同年月日时分数据 >----------------
WITH CTE
AS ( SELECT ROW_NUMBER () OVER ( PARTITION BY e . EquipmentCode ,
SUBSTRING ( CONVERT ( VARCHAR ( 50 ), e . Created , 120 ), 1 , 16 ),
SUBSTRING ( CONVERT ( VARCHAR ( 50 ), e . updated , 120 ), 1 , 16 )
ORDER BY EquipmentCode
) AS rownumber ,
*
FROM # listWeek e )
DELETE FROM CTE
WHERE rownumber > 1 ;
---------------- 故障時間本月开始 listMonth0 表格 1 -------------------------------
{ sqlMonth }
---------------- 故障時間 月初、月末结束 -------------------------------
---------------- 故障時間本周开始 listWeek1 表格 1 -------------------------------
{ sqlWeek }
---------------- 故障時間本周结束 -------------------------------
-------------- 本周故障時間列表超過 40 分开始 (数据表 ErrorDuration 字段是记录以秒为单位,所以换算一下 40 * 60 ) ----------------
SELECT t .*,
eh . HandleInfo ,
eh . HandleUser ,
eh . Reason
FROM # listweek t
LEFT JOIN bus_equipment_alarm_handle eh
ON eh . EquipmentAlarmRecordId = t . EquipmentAlarmId
WHERE t . ErrorDuration > 2400
ORDER BY t . ErrorDuration DESC ;
------------- 本周故障時間列表超過 40 分结束 -------------------------------------
--- weekDefault
SELECT dictValue FROM dbo . sys_dict_data WHERE dictLabel = ' weekDefault '
-- 妥善率目标
select dictValue FROM dbo . sys_dict_data WHERE dictLabel = ' properlyTarget '
-- 仓库可设定
select dictValue FROM dbo . sys_dict_data WHERE dictLabel = ' factoryTarget '
" ;
return sql ;
}
/// < summary >
/// 本周每天故障数据
/// </ summary >
/// < param name = "startDay" ></ param >
/// < param name = "endDay" ></ param >
/// < returns ></ returns >
HuXiYu
authored
about a year ago
216
public dynamic GetEquipmentWeekDayFaultInfo ( string startDay , string endDay , string projectCode )
HuXiYu
authored
about a year ago
217
{
HuXiYu
authored
about a year ago
218
var response = new ResponseNew ();
HuXiYu
authored
about a year ago
219
220
221
222
223
return ExceptionsHelp . Instance . ExecuteT (() =>
{
DataSet ds ;
string startTime = startDay . TrimEnd () + " 00:00:00" ;
string endTime = endDay . TrimEnd () + " 23:59:59" ;
HuXiYu
authored
about a year ago
224
225
226
string sqlWhereWeek = @$ " where t1.CreateTime>='{startTime}' and t1.CreateTime<='{endTime}' and t1.EquipmentTypeCode not in ('{SystemVariable.IotNotContainDevice}')" ;
// 按故障内容汇总
HuXiYu
authored
about a year ago
227
var sqlWeek = @" select name = t . ProjectName , -- 项目
HuXiYu
authored
about a year ago
228
229
alarm = t . Alarm , -- 故障内容
sumTime = sum ( t . ErrorDuration ) -- 故障 : 秒
HuXiYu
authored
about a year ago
230
231
232
233
234
from # listWeekFaultInfo t
where
t . ProjectCode = ' { 0 } ' and
t . created >= ' { 1 } ' and t . created <= ' { 2 } '
group by t . ProjectName , t . Alarm " ;
HuXiYu
authored
about a year ago
235
sqlWeek = string . Format ( sqlWeek , projectCode , startTime , endTime );
HuXiYu
authored
about a year ago
236
HuXiYu
authored
about a year ago
237
string sql = @$" ----------------- 本周公共数据源 --------------------------------------------
HuXiYu
authored
about a year ago
238
239
240
241
242
243
244
245
246
247
248
249
250
IF OBJECT_ID ( ' Tempdb .. # listWeekFaultInfo ' ) IS NOT NULL
DROP TABLE # listWeekFaultInfo ;
SELECT *
INTO # listWeekFaultInfo
FROM
(
SELECT t1 . CreateTime AS created ,
t1 . UpdateTime AS updated ,
t1 . ErrorDuration ,
t1 . EquipmentCode ,
t1 . EquipmentName ,
t1 . EquipmentPropName ,
t1 . Id ,
HuXiYu
authored
about a year ago
251
t1 . AlarmMessage + t1 . Remark AS Alarm ,
HuXiYu
authored
about a year ago
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
t1 . ProjectCode ,
t1 . EquipmentTypeCode ,
t . name AS EquipmentTypeName ,
p . projectName AS ProjectName
FROM dbo . daq_equipment_alarm_record t1
LEFT JOIN dbo . base_equipment_type t
ON t . code = t1 . EquipmentTypeCode
LEFT JOIN dbo . base_project p
ON p . projectCode = t1 . ProjectCode
{ sqlWhereWeek } AND t1 . ErrorDuration >= 60
) t ;
---------------- # listWeekFaultInfo 处理相同设备在同年月日时分数据 >----------------
WITH CTE
AS ( SELECT ROW_NUMBER () OVER ( PARTITION BY e . EquipmentCode ,
SUBSTRING ( CONVERT ( VARCHAR ( 50 ), e . Created , 120 ), 1 , 16 ),
SUBSTRING ( CONVERT ( VARCHAR ( 50 ), e . updated , 120 ), 1 , 16 )
ORDER BY EquipmentCode
) AS rownumber ,
*
FROM # listWeekFaultInfo e )
DELETE FROM CTE
WHERE rownumber > 1 ;
------- 本周所选设备每天故障分柱状图数据来源
{ sqlWeek }
----------- 本周每天设备故障详情 -------------
SELECT t . Id ,
t . ProjectName ,
t . EquipmentTypeName ,
t . EquipmentCode ,
t . EquipmentName ,
t . Alarm ,
sumtime = t . ErrorDuration ,
t . Created ,
t . Updated ,
h . Reason ,
h . HandleInfo ,
h . HandleUser
FROM # listWeekFaultInfo t
LEFT JOIN dbo . bus_equipment_alarm_handle h
ON h . EquipmentAlarmRecordId = t . Id
ORDER BY sumtime DESC ;
DROP TABLE # listWeekFaultInfo ; " ;
HuXiYu
authored
about a year ago
296
297
ds = Context . Ado . GetDataSetAll ( sql );
HuXiYu
authored
about a year ago
298
response . data = new
HuXiYu
authored
about a year ago
299
300
301
302
{
top = ds . Tables [ 0 ],
table = ds . Tables [ 1 ],
};
HuXiYu
authored
about a year ago
303
response . count = ds . Tables [ 1 ]. Rows . Count ;
HuXiYu
authored
about a year ago
304
305
return response ;
});
HuXiYu
authored
about a year ago
306
307
308
309
310
311
312
}
/// < summary >
/// 故障統計分析
/// </ summary >
/// < returns ></ returns >
HuXiYu
authored
about a year ago
313
public dynamic FaultStatistics ( string begin , string end , string projectCode , string equipmentTypeCode )
HuXiYu
authored
about a year ago
314
{
HuXiYu
authored
about a year ago
315
var response = new ResponseNew ();
HuXiYu
authored
about a year ago
316
317
318
return ExceptionsHelp . Instance . ExecuteT (() =>
{
var sql = @" IF OBJECT_ID ( ' tempdb .. # equipmentTempAlarm ' ) IS NOT NULL
HuXiYu
authored
about a year ago
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
DROP TABLE # equipmentTempAlarm ;
WITH CTE
AS ( SELECT ROW_NUMBER () OVER ( PARTITION BY EquipmentCode ,
CONVERT ( VARCHAR ( 16 ), t1 . CreateTime , 120 ),
CONVERT ( VARCHAR ( 16 ), t1 . UpdateTime , 120 )
ORDER BY EquipmentCode
) AS rownumber ,
t1 .*,
p . projectName ,
t . name AS equipmentTypeName
FROM dbo . daq_equipment_alarm_record t1
LEFT JOIN dbo . base_project p
ON p . projectCode = t1 . ProjectCode
LEFT JOIN dbo . base_equipment_type t
ON t . code = t1 . EquipmentTypeCode
WHERE 1 = 1 { 0 }
AND t1 . CreateTime >= ' { 1 } '
HuXiYu
authored
about a year ago
336
337
338
AND t1 . CreateTime <= ' { 2 } '
AND t1 . EquipmentTypeCode not in ( ' { 3 } ' )
)
HuXiYu
authored
about a year ago
339
340
341
342
343
344
345
346
347
348
349
SELECT *
INTO # equipmentTempAlarm
FROM CTE ;
DELETE FROM # equipmentTempAlarm
WHERE rownumber > 1 ;
SELECT *
FROM
(
SELECT t1 . EquipmentCode ,
t1 . EquipmentName ,
HuXiYu
authored
about a year ago
350
Alarm = t1 . AlarmMessage + ',' + t1 . Remark ,
HuXiYu
authored
about a year ago
351
352
353
354
355
356
357
358
359
360
361
362
363
AlarmStart = t1 . CreateTime ,
AlarmEnd = t1 . UpdateTime ,
sumTimeAlarm = ( t1 . ErrorDuration ),
t1 . projectName ,
t1 . equipmentTypeName ,
t2 . HandleInfo ,
HandleUser = ISNULL ( t2 . HandleUser , '' ),
HandleTime = t2 . CreateTime ,
Reason = t2 . Reason
FROM # equipmentTempAlarm t1
LEFT JOIN dbo . bus_equipment_alarm_handle t2
ON t1 . Id = t2 . EquipmentAlarmRecordId
" ;
HuXiYu
authored
about a year ago
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
sql += " )tt where sumTimeAlarm >= 60 " ;
sql += "order by tt.projectName,tt.EquipmentCode, tt.AlarmStart DESC " ;
var whereSql = "" ;
if (! string . IsNullOrEmpty ( projectCode ) && ! string . IsNullOrEmpty ( equipmentTypeCode ))
{
whereSql = $ "and t1.projectCode='{projectCode}' and t1.equipmentTypeCode='{equipmentTypeCode}'" ;
}
if (! string . IsNullOrEmpty ( projectCode ) && string . IsNullOrEmpty ( equipmentTypeCode ))
{
whereSql = $ "and t1.projectCode='{projectCode}'" ;
}
if ( string . IsNullOrEmpty ( projectCode ) && ! string . IsNullOrEmpty ( equipmentTypeCode ))
{
whereSql = $ "and t1.equipmentTypeCode='{equipmentTypeCode}'" ;
}
赖素文
authored
about a year ago
381
sql = string . Format ( sql , whereSql , begin , end , SystemVariable . IotNotContainDevice );
HuXiYu
authored
about a year ago
382
383
var dt = Context . Ado . GetDataTable ( sql );
HuXiYu
authored
about a year ago
384
385
386
response . data = dt ;
response . count = dt . Rows . Count ;
response . code = 200 ;
HuXiYu
authored
about a year ago
387
388
return response ;
}, catchRetrunValue : "list" );
HuXiYu
authored
about a year ago
389
390
391
392
393
394
395
396
397
398
399
400
401
}
/// < summary >
/// 妥善率报表查询
/// </ summary >
/// < param name = "startTime" ></ param >
/// < param name = "endTime" ></ param >
/// < param name = "eqCode" ></ param >
/// < param name = "yearOrdaySelectVal" > 传年或者日 </ param >
/// < param name = "timeFlag" > 值有 year 、 month 、 day </ param >
/// < returns ></ returns >
HuXiYu
authored
about a year ago
402
public dynamic GetProperRate ( string startTime , string endTime , string yearOrdaySelectVal , string timeFlag , string projectCode , string equipmentTypeCode )
HuXiYu
authored
about a year ago
403
{
HuXiYu
authored
about a year ago
404
var response = new ResponseNew ();
HuXiYu
authored
about a year ago
405
return ExceptionsHelp . Instance . ExecuteT (() =>
HuXiYu
authored
about a year ago
406
{
HuXiYu
authored
about a year ago
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
if ( timeFlag == "year" )
{
startTime = yearOrdaySelectVal + "-01-01" ;
endTime = yearOrdaySelectVal + "-12-31" ;
}
else if ( timeFlag == "day" )
{
startTime = yearOrdaySelectVal ;
endTime = yearOrdaySelectVal ;
}
var whereSql = "" ;
if (! string . IsNullOrEmpty ( projectCode ) && ! string . IsNullOrEmpty ( equipmentTypeCode ))
{
whereSql = $ "and t1.projectCode='{projectCode}' and t1.equipmentTypeCode='{equipmentTypeCode}'" ;
}
if (! string . IsNullOrEmpty ( projectCode ) && string . IsNullOrEmpty ( equipmentTypeCode ))
{
whereSql = $ "and t1.projectCode='{projectCode}'" ;
}
if ( string . IsNullOrEmpty ( projectCode ) && ! string . IsNullOrEmpty ( equipmentTypeCode ))
{
whereSql = $ "and t1.equipmentTypeCode='{equipmentTypeCode}'" ;
}
var startDate = startTime + " 00:00:00" ;
var endDate = endTime + " 23:59:59" ;
HuXiYu
authored
about a year ago
434
var sqlWhere = $ " where t1.CreateTime>='{startDate}' and t1.CreateTime<='{endDate}' and t1.EquipmentTypeCode not in ('{SystemVariable.IotNotContainDevice}')" ;
HuXiYu
authored
about a year ago
435
436
437
438
var sql = GetRateSql ( sqlWhere , timeFlag , whereSql );
var dt = Context . Ado . GetDataTable ( sql );
HuXiYu
authored
about a year ago
439
440
response . data = dt ;
response . count = dt . Rows . Count ;
HuXiYu
authored
about a year ago
441
442
return response ;
}, catchRetrunValue : "list" );
HuXiYu
authored
about a year ago
443
444
}
HuXiYu
authored
about a year ago
445
public string GetRateSql ( string sqlWhere , string timeFlag , string whereSql )
HuXiYu
authored
about a year ago
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
{
var sql = "" ;
if (! string . IsNullOrEmpty ( sql ))
{
sql += " union all " ;
}
if ( timeFlag == "year" )
{
sql += @" select CONVERT ( varchar ( 7 ), created , 120 ) as date ,
sumTime = sum ( ErrorDuration ),
ProjectName
from # tempdbRate t
where 1 = 1 { 0 }
group by CONVERT ( varchar ( 7 ), created , 120 ),
ProjectName " ;
}
else
{
sql += @" select CONVERT ( varchar , created , 23 ) as date ,
sumTime = sum ( ErrorDuration ),
ProjectName
from # tempdbRate t
where 1 = 1 { 0 }
group by CONVERT ( varchar , created , 23 ) ,
ProjectName " ;
}
HuXiYu
authored
about a year ago
473
sql = string . Format ( sql , whereSql );
HuXiYu
authored
about a year ago
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
var resSql = @$"
----------------- 数据源 --------------------------------------------
IF OBJECT_ID ( ' Tempdb .. # tempdbRate ' ) IS NOT NULL
DROP TABLE # tempdbRate ;
SELECT *
INTO # tempdbRate
FROM
(
SELECT t1 . CreateTime AS created ,
t1 . UpdateTime AS updated ,
t1 . ErrorDuration ,
t1 . EquipmentCode ,
t1 . EquipmentName ,
t1 . EquipmentPropName ,
t1 . Id AS EquipmentAlarmId ,
HuXiYu
authored
about a year ago
490
t1 . AlarmMessage + t1 . Remark AS Alarm ,
HuXiYu
authored
about a year ago
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
t1 . ProjectCode ,
t1 . EquipmentTypeCode ,
t . name AS EquipmentTypeName ,
p . projectName AS ProjectName
FROM dbo . daq_equipment_alarm_record t1
LEFT JOIN dbo . base_equipment_type t
ON t . code = t1 . EquipmentTypeCode
LEFT JOIN dbo . base_project p
ON p . projectCode = t1 . ProjectCode
{ sqlWhere } AND t1 . ErrorDuration >= 60
) t ;
---------------- listWeek 处理相同设备在同年月日时分数据 >----------------
WITH CTE
AS ( SELECT ROW_NUMBER () OVER ( PARTITION BY e . EquipmentCode ,
SUBSTRING ( CONVERT ( VARCHAR ( 50 ), e . Created , 120 ), 1 , 16 ),
SUBSTRING ( CONVERT ( VARCHAR ( 50 ), e . updated , 120 ), 1 , 16 )
ORDER BY EquipmentCode
) AS rownumber ,
*
FROM # tempdbRate e )
DELETE FROM CTE
WHERE rownumber > 1 ;
----------------- 时间段故障总结 --------------------------------------------
{ sql }
" ;
return resSql ;
}
HuXiYu
authored
about a year ago
520
521
522
523
524
525
526
527
528
529
530
/// < summary >
/// 查询故障月份对比数据(只查三个月)
/// </ summary >
/// < param name = "year" ></ param >
/// < param name = "month" ></ param >
/// < param name = "projectCode" > 项目 </ param >
/// < param name = "equipmentTypeCode" > 设备类型 </ param >
/// < returns ></ returns >
public dynamic GetFailureMonthCompare ( int year , int month , string projectCode , string equipmentTypeCode )
{
HuXiYu
authored
about a year ago
531
var response = new ResponseNew ();
HuXiYu
authored
about a year ago
532
533
534
535
return ExceptionsHelp . Instance . ExecuteT (() =>
{
if ( string . IsNullOrEmpty ( projectCode ))
{
HuXiYu
authored
about a year ago
536
537
538
response . code = 500 ;
response . status = false ;
response . message = "请选择一个项目" ;
HuXiYu
authored
about a year ago
539
540
541
542
543
544
545
546
547
548
549
550
551
return response ;
}
DateTime startTime = new DateTime ( year , month , 1 ). AddMonths (- 2 );// 所选日期近三月的第一天
DateTime endTime = new DateTime ( year , month , 1 , 23 , 59 , 59 ). AddMonths ( 1 ). AddDays (- 1 );// 得出最后一天
var whereSql = "" ;
if (! string . IsNullOrEmpty ( projectCode ) && ! string . IsNullOrEmpty ( equipmentTypeCode ))
{
whereSql = $ "and t1.projectCode='{projectCode}' and t1.equipmentTypeCode='{equipmentTypeCode}'" ;
}
if (! string . IsNullOrEmpty ( projectCode ) && string . IsNullOrEmpty ( equipmentTypeCode ))
{
whereSql = $ "and t1.projectCode='{projectCode}'" ;
}
HuXiYu
authored
about a year ago
552
var sql = @" SELECT t1 . AlarmMessage + t1 . Remark AS Alarm ,
HuXiYu
authored
about a year ago
553
554
555
556
557
SUM ( t1 . ErrorDuration ) AS SumTime ,
CONVERT ( CHAR ( 7 ), t1 . CreateTime , 120 ) AS YearMonth
FROM dbo . daq_equipment_alarm_record t1
WHERE t1 . CreateTime >= ' { 0 } '
AND t1 . CreateTime <= ' { 1 } '
HuXiYu
authored
about a year ago
558
559
AND t1 . EquipmentTypeCode not in ( ' { 2 } ' )
{ 3 }
HuXiYu
authored
about a year ago
560
GROUP BY CONVERT ( CHAR ( 7 ), t1 . CreateTime , 120 ),
HuXiYu
authored
about a year ago
561
t1 . AlarmMessage + t1 . Remark
HuXiYu
authored
about a year ago
562
ORDER BY CONVERT ( CHAR ( 7 ), t1 . CreateTime , 120 ); " ;
HuXiYu
authored
about a year ago
563
sql = string . Format ( sql , startTime , endTime , SystemVariable . IotNotContainDevice , whereSql );
HuXiYu
authored
about a year ago
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
DataTable dt = Context . Ado . GetDataTable ( sql );
// 处理没有数据的赋值 0
var alarmDt = dt . AsEnumerable (). GroupBy ( x => x [ "Alarm" ]). Distinct (). Select ( x => x . Key ). ToList ();
for ( int i = 0 ; i < 3 ; i ++)
{
DateTime t = new DateTime ( year , month , 1 ). AddMonths (- i );
string currentTime = t . ToString ( "yyyy-MM" );
foreach ( string alarm in alarmDt )
{
var isHave = dt . AsEnumerable (). Any ( x => x [ "YearMonth" ]. ToString () == currentTime && x [ "Alarm" ]. ToString (). TrimEnd () == alarm );
if (! isHave )
{
// DataRow newRow = dt . NewRow ();
// newRow [ "Alarm" ] = alarm ;
// newRow [ "SumTime" ] = 0 ;
// newRow [ "YearMonth" ] = currentTime ;
// dt . Rows . Add ( newRow );
dt . Rows . Add ( alarm , 0 , currentTime );
}
}
}
HuXiYu
authored
about a year ago
586
587
588
response . code = 200 ;
response . data = dt ;
response . count = dt . Rows . Count ;
HuXiYu
authored
about a year ago
589
590
591
return response ;
}, catchRetrunValue : "list" );
}
HuXiYu
authored
about a year ago
592
593
}
}