赖素文
authored
10 months ago
1
2
using Hh.Mes.Common.DataTableTo ;
using Hh.Mes.Common.log ;
赖素文
authored
about a year ago
3
using Hh.Mes.Pojo.System ;
4
using Hh.Mes.POJO.ApiEntity ;
赖素文
authored
about a year ago
5
using Hh.Mes.POJO.Entity ;
6
using Hh.Mes.POJO.EnumEntitys ;
赖素文
authored
about a year ago
7
using Hh.Mes.POJO.Response ;
8
using Hh.Mes.POJO.ViewModel.Equipment ;
赖素文
authored
about a year ago
9
using Hh.Mes.Service.Repository ;
10
using Hh.Mes.Service.WebService.Equipment ;
11
using SqlSugar ;
赖素文
authored
about a year ago
12
13
using System ;
using System.Collections.Generic ;
赖素文
authored
11 months ago
14
15
using System.Data ;
using System.Linq ;
赖素文
authored
9 months ago
16
using System.Linq.Expressions ;
赖素文
authored
about a year ago
17
18
19
20
21
namespace Hh.Mes.Service
{
public class BulletinBoardService : RepositorySqlSugar < sys_user >
{
22
# region 故障维护报表相关
赖素文
authored
about a year ago
23
/// < summary >
赖素文
authored
11 months ago
24
/// 故障报告
赖素文
authored
about a year ago
25
/// </ summary >
赖素文
authored
11 months ago
26
27
28
29
30
/// < param name = "yyyyMonth" > 日期 </ param >
/// < param name = "project" > 项目 </ param >
/// < param name = "equipmentType" > 设备类型 </ param >
/// < returns ></ returns >
public dynamic GetFaultReport ( string yyyyMonth , string projectCode , string equipmentTypeCode )
赖素文
authored
about a year ago
31
{
赖素文
authored
11 months ago
32
var response = new ResponseNew ();
赖素文
authored
about a year ago
33
34
return ExceptionsHelp . Instance . ExecuteT (() =>
{
赖素文
authored
11 months ago
35
36
37
38
39
var month = Convert . ToDateTime ( yyyyMonth );
var whereSql = @" where t1.CreateTime>='{0}' and t1.CreateTime<='{1}' and t1.EquipmentTypeCode not in ('{2}')" ;
// 月初、月末
var monthStart = month . AddDays ( 1 - month . Day );
var monthEnd = month . AddDays ( 1 - month . Day ). Date . AddMonths ( 1 ). AddSeconds (- 1 );
40
var sqlWhereMonth = string . Format ( whereSql , monthStart . ToString ( "yyyy-MM-dd HH:mm:ss" ), monthEnd . ToString ( "yyyy-MM-dd HH:mm:ss" ), SystemVariable . IotNotContainDevice );// 月数据源查询条件
赖素文
authored
11 months ago
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// 周一、周末
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" );
var sqlWhereWeek = string . Format ( whereSql , weekOne , weekend , SystemVariable . IotNotContainDevice );
var sql = GetSql ( sqlWhereMonth , sqlWhereWeek , projectCode , equipmentTypeCode );
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" );
Tuple < DataSet , int , string , string > dss = new Tuple < DataSet , int , string , string >( ds , diffDay , monthStr , weekStr );
// 当天是否在本周内
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 ;
response . data = new
赖素文
authored
about a year ago
64
{
赖素文
authored
11 months ago
65
66
67
68
69
70
71
72
73
74
75
76
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 ],
赖素文
authored
10 months ago
77
78
isInterval = isInterval ,
79
projectAlarmSum = dss . Item1 . Tables [ 6 ],
赖素文
authored
10 months ago
80
projectAlarmSumInfo = dss . Item1 . Tables [ 7 ]
赖素文
authored
about a year ago
81
82
83
};
return response ;
});
赖素文
authored
11 months ago
84
85
86
87
88
89
90
}
public string GetSql ( string sqlWhereMonth , string sqlWhereWeek , string projectCode , string equipmentTypeCode )
{
var selectSql = "SELECT ProjectCode,projectName,sumTime=SUM(ErrorDuration) " ;
var sqlMonth = selectSql + "FROM #listMonth t" ;
var sqlWeek = selectSql + "FROM #listWeek t" ;
赖素文
authored
10 months ago
91
var sqlAlarm40 = "" ;
92
if (! string . IsNullOrEmpty ( projectCode ))
赖素文
authored
11 months ago
93
94
95
96
{
var whereSql = $ " WHERE t.projectCode='{projectCode}'" ;
sqlMonth += whereSql ;
sqlWeek += whereSql ;
赖素文
authored
10 months ago
97
sqlAlarm40 = $ " and t.projectCode='{projectCode}'" ;
赖素文
authored
11 months ago
98
99
100
101
102
}
var groupBySql = " group by projectCode,projectName order by sumTime desc" ;
sqlMonth += groupBySql ;
sqlWeek += groupBySql ;
赖素文
authored
9 months ago
103
var projectKeys = base . GetProjectInfoKeys ( base . sysUserApiAccount );
赖素文
authored
10 months ago
104
var sql = @$" ----------------- 本月公共数据源 ErrorDuration 已转化分) --------------------------------------------
赖素文
authored
11 months ago
105
106
107
IF OBJECT_ID ( ' Tempdb .. # listMonth ' ) IS NOT NULL
DROP TABLE # listMonth ;
SELECT * INTO # listMonth FROM (
赖素文
authored
10 months ago
108
109
110
SELECT rownumber = ROW_NUMBER () OVER ( PARTITION BY EquipmentCode , CONVERT ( VARCHAR ( 16 ), t1 . CreateTime , 120 ), CONVERT ( VARCHAR ( 16 ), t1 . UpdateTime , 120 )
ORDER BY EquipmentCode ),
t1 . CreateTime AS Created ,
赖素文
authored
11 months ago
111
t1 . UpdateTime AS Updated ,
112
ErrorDuration = CAST ( t1 . ErrorDuration / 60.0 as DECIMAL ( 10 , 2 )) ,
赖素文
authored
11 months ago
113
t1 . EquipmentCode ,
114
t1 . AlarmMessage + ';' + ISNULL ( t1 . Remark , '' ) AS Alarm ,
赖素文
authored
11 months ago
115
116
117
118
t1 . EquipmentName ,
t1 . EquipmentPropName ,
t1 . EquipmentTypeCode ,
t1 . ProjectCode ,
赖素文
authored
10 months ago
119
120
121
122
p . projectName AS ProjectName
FROM dbo . daq_equipment_alarm_record t1 with ( nolock )
LEFT JOIN dbo . base_project p ON p . projectCode = t1 . ProjectCode
{ sqlWhereMonth }
赖素文
authored
9 months ago
123
and p . keys in ( ' { string . Join ( "','" , projectKeys )} ' )
赖素文
authored
11 months ago
124
125
) t ;
----------------< # listMonth 处理相同设备在同年月日时分数据 >----------------
赖素文
authored
10 months ago
126
DELETE FROM # listMonth WHERE rownumber > 1 ;
赖素文
authored
11 months ago
127
赖素文
authored
10 months ago
128
----------------- 本周公共数据源 # listWeek ErrorDuration 已转化分 --------------------------------------------
赖素文
authored
11 months ago
129
130
131
132
133
134
IF OBJECT_ID ( ' Tempdb .. # listWeek ' ) IS NOT NULL
DROP TABLE # listWeek ;
SELECT *
INTO # listWeek
FROM
(
赖素文
authored
10 months ago
135
136
137
SELECT rownumber = ROW_NUMBER () OVER ( PARTITION BY EquipmentCode , CONVERT ( VARCHAR ( 16 ), t1 . CreateTime , 120 ), CONVERT ( VARCHAR ( 16 ), t1 . UpdateTime , 120 )
ORDER BY EquipmentCode ),
t1 . CreateTime AS created ,
赖素文
authored
11 months ago
138
t1 . UpdateTime AS updated ,
139
ErrorDuration = CAST ( t1 . ErrorDuration / 60.0 as DECIMAL ( 10 , 2 )),
赖素文
authored
11 months ago
140
141
142
143
t1 . EquipmentCode ,
t1 . EquipmentName ,
t1 . EquipmentPropName ,
t1 . Id AS EquipmentAlarmId ,
144
t1 . AlarmMessage + ';' + ISNULL ( t1 . Remark , '' ) AS Alarm ,
赖素文
authored
11 months ago
145
146
147
t1 . ProjectCode ,
t1 . EquipmentTypeCode ,
p . projectName AS ProjectName
148
FROM dbo . daq_equipment_alarm_record t1 with ( nolock )
赖素文
authored
10 months ago
149
150
LEFT JOIN dbo . base_project p ON p . projectCode = t1 . ProjectCode
{ sqlWhereWeek }
赖素文
authored
9 months ago
151
and p . keys in ( ' { string . Join ( "','" , projectKeys )} ' )
赖素文
authored
11 months ago
152
153
154
) t ;
---------------- listWeek 处理相同设备在同年月日时分数据 >----------------
赖素文
authored
10 months ago
155
DELETE FROM # listWeek WHERE rownumber > 1 ;
赖素文
authored
11 months ago
156
157
158
159
160
161
162
163
164
---------------- 故障時間本月开始 listMonth0 表格 1 -------------------------------
{ sqlMonth }
---------------- 故障時間 月初、月末结束 -------------------------------
---------------- 故障時間本周开始 listWeek1 表格 1 -------------------------------
{ sqlWeek }
---------------- 故障時間本周结束 -------------------------------
赖素文
authored
10 months ago
165
-------------- 本周故障時間列表超過 40 分开始 ----------------
赖素文
authored
11 months ago
166
167
168
169
170
SELECT t .*,
eh . HandleInfo ,
eh . HandleUser ,
eh . Reason
FROM # listweek t
赖素文
authored
10 months ago
171
LEFT JOIN bus_equipment_alarm_handle eh ON eh . EquipmentAlarmRecordId = t . EquipmentAlarmId
172
WHERE t . ErrorDuration > 40 { sqlAlarm40 }
赖素文
authored
11 months ago
173
174
175
176
177
178
179
180
181
182
183
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 '
赖素文
authored
10 months ago
184
185
-- 项目设备报警统计
186
select t . ProjectCode , xAxis = convert ( varchar ( 10 ), created , 120 ), sumVal = SUM ( t . ErrorDuration ) from # listWeek t
赖素文
authored
10 months ago
187
group by t . ProjectCode , convert ( varchar ( 10 ), created , 120 )
赖素文
authored
11 months ago
188
赖素文
authored
10 months ago
189
-- 项目设备报警统计明细信息
赖素文
authored
10 months ago
190
select xAxis = convert ( varchar ( 10 ), created , 120 ), timeVal = ErrorDuration ,* from # listWeek " ;
赖素文
authored
11 months ago
191
192
193
194
195
196
197
return sql ;
}
/// < summary >
/// 故障統計分析
/// </ summary >
/// < returns ></ returns >
198
public dynamic FaultStatistics ( string begin , string end , string projectCode , string equipmentCode )
赖素文
authored
11 months ago
199
200
201
202
203
{
var response = new ResponseNew ();
return ExceptionsHelp . Instance . ExecuteT (() =>
{
var whereSql = "" ;
赖素文
authored
10 months ago
204
if (! string . IsNullOrEmpty ( projectCode )) whereSql = $ "and t1.projectCode='{projectCode}'" ;
205
if (! string . IsNullOrEmpty ( equipmentCode )) whereSql = $ " and t1.equipmentCode='{equipmentCode}'" ;
赖素文
authored
9 months ago
206
207
208
var projectKeys = base . GetProjectInfoKeys ( base . sysUserApiAccount );
var sql = GetAlarmSql ( whereSql , begin , end , projectKeys );
赖素文
authored
11 months ago
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
var dt = Context . Ado . GetDataTable ( sql );
response . data = dt ;
response . count = dt . Rows . Count ;
response . code = 200 ;
return response ;
}, catchRetrunValue : "list" );
}
/// < 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 >
227
public dynamic GetProperRate ( string startTime , string endTime , string yearOrdaySelectVal , string timeFlag , string projectCode , string equipmentCode )
赖素文
authored
11 months ago
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
{
var response = new ResponseNew ();
return ExceptionsHelp . Instance . ExecuteT (() =>
{
if ( timeFlag == "year" )
{
startTime = yearOrdaySelectVal + "-01-01" ;
endTime = yearOrdaySelectVal + "-12-31" ;
}
else if ( timeFlag == "day" )
{
startTime = yearOrdaySelectVal ;
endTime = yearOrdaySelectVal ;
}
var whereSql = "" ;
244
if (! string . IsNullOrEmpty ( projectCode ) && ! string . IsNullOrEmpty ( equipmentCode ))
赖素文
authored
11 months ago
245
{
246
whereSql = $ "and t.projectCode='{projectCode}' and t.EquipmentCode='{equipmentCode}'" ;
赖素文
authored
11 months ago
247
}
248
if (! string . IsNullOrEmpty ( projectCode ) && string . IsNullOrEmpty ( equipmentCode ))
赖素文
authored
11 months ago
249
{
250
whereSql = $ "and t.projectCode='{projectCode}'" ;
赖素文
authored
11 months ago
251
}
252
if ( string . IsNullOrEmpty ( projectCode ) && ! string . IsNullOrEmpty ( equipmentCode ))
赖素文
authored
11 months ago
253
{
254
whereSql = $ "and t.EquipmentCode='{equipmentCode}'" ;
赖素文
authored
11 months ago
255
256
257
258
259
}
var startDate = startTime + " 00:00:00" ;
var endDate = endTime + " 23:59:59" ;
260
261
var sql = GetAlarmSql ( "" , startDate , endDate ) + GetRateSql ( timeFlag , whereSql );
var ds = Context . Ado . GetDataSetAll ( sql );
赖素文
authored
11 months ago
262
263
264
265
var resDt = ds . Tables [ 1 ];
// 查询项目所有设备
266
if ( string . IsNullOrEmpty ( equipmentCode ))
267
{
268
resDt = HandleDt ( resDt , projectCode , startTime );
269
270
271
}
response . data = resDt ;
272
response . count = ds . Tables [ 1 ]. Rows . Count ;
赖素文
authored
11 months ago
273
274
275
276
return response ;
}, catchRetrunValue : "list" );
}
277
public string GetRateSql ( string timeFlag , string whereSql )
赖素文
authored
11 months ago
278
{
279
var sql = @" select { 0 } as date ,
赖素文
authored
11 months ago
280
sumTime = sum ( ErrorDuration ),
281
ProjectName , EquipmentCode , EquipmentName , Sequence
赖素文
authored
11 months ago
282
from # tempdbRate t
283
284
where 1 = 1 { 1 }
group by { 0 },
285
286
ProjectName , EquipmentCode , EquipmentName , Sequence
order by Sequence " ;
287
288
if ( timeFlag == "year" )
{
289
sql = string . Format ( sql , "CONVERT(varchar(7),created,120)" , whereSql );// 年月分组
赖素文
authored
11 months ago
290
291
292
}
else
{
293
sql = string . Format ( sql , "CONVERT(varchar,created,23)" , whereSql );// 年月日分组
赖素文
authored
11 months ago
294
}
295
return sql ;
赖素文
authored
11 months ago
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
}
/// < 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 )
{
var response = new ResponseNew ();
return ExceptionsHelp . Instance . ExecuteT (() =>
{
if ( string . IsNullOrEmpty ( projectCode ))
{
response . code = 500 ;
response . status = false ;
response . message = "请选择一个项目" ;
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 );// 得出最后一天
321
322
323
324
325
326
327
328
329
330
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}'" ;
}
var sql = GetAlarmSql ( whereSql , startTime . ToString (), endTime . ToString ());
331
sql = sql + @"
332
333
334
335
336
337
SELECT Alarm = t1 . AlarmMessage ,
SumTime = SUM ( t1 . ErrorDuration ) ,
YearMonth = CONVERT ( CHAR ( 7 ), t1 . CreateTime , 120 )
FROM # tempdbRate t1
GROUP BY CONVERT ( CHAR ( 7 ), t1 . CreateTime , 120 ), t1 . AlarmMessage
ORDER BY CONVERT ( CHAR ( 7 ), t1 . CreateTime , 120 ); " ;
338
DataSet ds = Context . Ado . GetDataSetAll ( sql );
赖素文
authored
11 months ago
339
// 处理没有数据的赋值 0
340
var dt = ds . Tables [ 1 ];
赖素文
authored
11 months ago
341
var alarmDt = dt . AsEnumerable (). GroupBy ( x => x [ "Alarm" ]). Distinct (). Select ( x => x . Key ). ToList ();
赖素文
authored
10 months ago
342
343
var sumTime = dt . GetColumnValues < decimal >( "SumTime" ). Sum ();
var sumMsgInfo = "故障月份对比 " ;
赖素文
authored
11 months ago
344
345
346
347
for ( int i = 0 ; i < 3 ; i ++)
{
DateTime t = new DateTime ( year , month , 1 ). AddMonths (- i );
string currentTime = t . ToString ( "yyyy-MM" );
赖素文
authored
10 months ago
348
349
var totalSumTime = dt . CalculateSumTime ( "YearMonth" , "SumTime" , currentTime );
sumMsgInfo += currentTime + "故障分钟:" + totalSumTime + "," ;
赖素文
authored
11 months ago
350
351
352
353
354
355
356
357
358
359
360
foreach ( string alarm in alarmDt )
{
var isHave = dt . AsEnumerable (). Any ( x => x [ "YearMonth" ]. ToString () == currentTime && x [ "Alarm" ]. ToString (). TrimEnd () == alarm );
if (! isHave )
{
dt . Rows . Add ( alarm , 0 , currentTime );
}
}
}
response . code = 200 ;
361
362
363
response . data = new
{
data = dt ,
赖素文
authored
10 months ago
364
allAlarm = ds . Tables [ 0 ],
365
sumMsgInfo = sumMsgInfo + " 总故障时间:" + sumTime + "(单位分钟)"
366
};
赖素文
authored
11 months ago
367
368
369
response . count = dt . Rows . Count ;
return response ;
}, catchRetrunValue : "list" );
赖素文
authored
about a year ago
370
}
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/// < summary >
/// 项目月 四周 故障次数 & 时间
/// </ summary >
/// < param name = "projectCode" > 项目号 </ param >
/// < param name = "currentDate" > 时间日期 </ param >
/// < returns ></ returns >
public dynamic GetFailureMonthWeekCountAndTime ( string projectCode , DateTime currentDate )
{
var response = new ResponseNew ();
return ExceptionsHelp . Instance . ExecuteT (() =>
{
List < EquipmentAlarmCountTimeEntity > list = new List < EquipmentAlarmCountTimeEntity >();
// 数据源查出来
385
386
387
388
389
390
391
392
393
var whereSql = "" ;
if (! string . IsNullOrEmpty ( projectCode ))
{
whereSql = $ "and t1.projectCode='{projectCode}'" ;
}
var start = currentDate . AddDays (- 28 ). ToString ();
var end = currentDate . ToString ();
var sql = GetAlarmSql ( whereSql , start , end );
DataTable allAlarmDt = Context . Ado . GetDataTable ( sql );
394
395
396
397
398
399
// 近四周
for ( int i = 0 ; i < 4 ; i ++)
{
EquipmentAlarmCountTimeEntity entity = new EquipmentAlarmCountTimeEntity ();
DateTime startTime = currentDate . AddDays (-( 7 * ( i + 1 )));
DateTime endTime = currentDate . AddDays (-( 7 * i ));
400
401
402
var dt = allAlarmDt . AsEnumerable ()
. Where ( i => i . Field < DateTime >( "CreateTime" ) >= startTime && i . Field < DateTime >( "CreateTime" ) < endTime )
. ToList ();
403
404
entity . WeekNo = $ "第{4 - i}周" ;
405
406
entity . TimePeriod = $ "{startTime.ToString().Replace(" 0 : 00 : 00 ", "")}~{endTime.ToString().Replace(" 0 : 00 : 00 ", "")}" ;
entity . AlarmSumTime = ( decimal ) dt . Sum ( i => i . Field < decimal >( "ErrorDuration" ));
407
entity . AlarmCount = dt . Count ;
408
entity . AlarmListData = dt . Count > 0 ? dt . CopyToDataTable () : null ;
409
410
list . Add ( entity );
}
411
response . count = allAlarmDt . Rows . Count ;
412
response . data = list ;
413
414
415
416
return response ;
});
}
417
418
419
420
421
422
423
/// < summary >
/// 项目设备故障一天每小时故障次数
/// </ summary >
/// < param name = "projectCode" ></ param >
/// < param name = "equipmentCode" ></ param >
/// < param name = "currentDate" ></ param >
/// < returns ></ returns >
424
425
426
427
428
public dynamic GetFailureEveryHourCount ( string projectCode , string equipmentCode , DateTime currentDate )
{
var response = new ResponseNew ();
return ExceptionsHelp . Instance . ExecuteT (() =>
{
429
430
431
432
433
string startTime = currentDate . ToString ( "yyyy-MM-dd" ) + " 00:00:00" ;
string endTime = currentDate . ToString ( "yyyy-MM-dd" ) + " 23:59:59" ;
var sqlWhere = "" ;
if (! string . IsNullOrEmpty ( projectCode )) sqlWhere = $ "and t1.projectCode='{projectCode}'" ;
var sql = GetAlarmSql ( sqlWhere , startTime , endTime );
434
435
436
437
438
var whereSql = "" ;
if (! string . IsNullOrEmpty ( equipmentCode ))
{
whereSql = $ @"where t1.EquipmentCode = '{equipmentCode}'" ;
}
赖素文
authored
10 months ago
439
440
sql = sql + @"
select COUNT (*) as alarmCount , CAST ( FORMAT ( t1 . CreateTime , ' HH ' ) AS INT ) as day_hour
441
from # tempdbRate t1 { 0 }
赖素文
authored
10 months ago
442
443
group by CAST ( FORMAT ( t1 . CreateTime , ' HH ' ) AS INT )
order by CAST ( FORMAT ( t1 . CreateTime , ' HH ' ) AS INT );
444
445
select * from # tempdbRate t1 { 0 }
赖素文
authored
10 months ago
446
447
select sumVal = sum ( ErrorDuration ) from # tempdbRate t1 { 0 } " ;
448
sql = string . Format ( sql , whereSql );
449
DataSet ds = Context . Ado . GetDataSetAll ( sql );
450
451
452
453
List < EquipmentAlarmCountTimeEntity > list = new List < EquipmentAlarmCountTimeEntity >();
var result = ds . Tables [ 1 ];
foreach ( DataRow dr in result . Rows )
454
{
455
456
457
458
459
460
461
462
463
464
int hour = ( int ) dr [ "day_hour" ];
EquipmentAlarmCountTimeEntity entity = new EquipmentAlarmCountTimeEntity ();
entity . AlarmCount = ( int ) dr [ "alarmCount" ];
entity . TimePeriod = hour . ToString ();
entity . AlarmListData = ds . Tables [ 2 ]. AsEnumerable (). Where ( x => x . Field < DateTime >( "CreateTime" ). Hour == hour ). CopyToDataTable ();
list . Add ( entity );
}
response . data = list ;
response . count = ds . Tables [ 2 ]. Rows . Count ;
465
response . message = list . Count == 0 ? " " : "故障次数天每小时对比,总次数:" + ds . Tables [ 2 ]. Rows . Count + ",总故障分钟数:" + ds . Tables [ 3 ]. Rows [ 0 ][ "sumVal" ]. ToString ();
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
return response ;
});
}
/// < summary >
/// 故障次数
/// </ summary >
/// < param name = "projectCode" ></ param >
/// < param name = "startTime" ></ param >
/// < param name = "endTime" ></ param >
/// < returns ></ returns >
public dynamic GetFailureAlarmCount ( string projectCode , DateTime startTime , DateTime endTime )
{
var response = new ResponseNew ();
return ExceptionsHelp . Instance . ExecuteT (() =>
{
var begin = startTime ;
var end = DateTime . Parse ( endTime . ToString ( "yyyy-MM-dd" ) + " 23:59:59" );
var data = Context . Queryable < daq_equipment_alarm_record , base_equipment_type >(( x , y ) => x . EquipmentTypeCode == y . code )
. Where (( x , y ) => (! SystemVariable . IotNotContainDevice . Contains ( x . EquipmentTypeCode )) && x . ProjectCode == projectCode && x . createTime >= begin && x . createTime <= end )
. Select (( x , y ) => new
{
x . equipmentCode ,
x . equipmentName ,
490
// errorduration = x . errorduration > 0 ? Math . Round ( x . errorduration / 60.0 , 2 ) : 0 ,
491
492
493
494
495
496
497
498
x . EquipmentTypeCode ,
EquipmentTypeName = y . name ,
x . alarmMessage ,
x . remark ,
x . createTime ,
x . updateTime
})
. ToList ();
499
// 设备故障分组
500
501
502
503
504
505
506
507
508
509
510
var res = data . GroupBy ( x => new { x . equipmentCode , x . equipmentName , x . EquipmentTypeName , x . alarmMessage })
. Select ( x => new
{
equipmentCode = x . Key . equipmentCode . ToString (),
equipmentName = x . Key . equipmentName . ToString (),
equipmentTypeName = x . Key . EquipmentTypeName ,
alarmMessage = x . Key . alarmMessage ,
alarmCount = x . Count ()
})
. OrderBy ( x => x . equipmentCode )
. ToList ();
511
512
513
514
515
516
517
// 故障次数分组
var alarmRes = data . GroupBy ( x => new { x . alarmMessage })
. Select ( x => new
{
alarmMessage = x . Key . alarmMessage ,
alarmCount = x . Count ()
})
518
. OrderByDescending ( x => x . alarmCount )
519
520
521
522
523
524
. ToList ();
response . data = new
{
res = res ,
alarmRes = alarmRes
};
525
response . count = res . Count ;
526
527
528
529
return response ;
});
}
530
/// < summary >
531
532
533
534
535
536
537
538
539
/// 查询项目列表
/// </ summary >
/// < param name = "agvTag" ></ param >
/// < returns ></ returns >
public dynamic GetProjects ( bool agvTag )
{
var response = new ResponseNew ();
return ExceptionsHelp . Instance . ExecuteT (() =>
{
赖素文
authored
9 months ago
540
var expression = GetProjectsWhere ();
541
var result = Context . Queryable < base_project >()
赖素文
authored
9 months ago
542
. Where ( expression )
543
. OrderBy ( x => x . createTime )
544
. Select ( x => new { x . projectCode , x . projectName , x . createTime })
545
. ToList ();
赖素文
authored
9 months ago
546
547
548
549
550
// agvTag 标记 true ,只查询含 agv 设备的项目
if ( agvTag )
{
赖素文
authored
9 months ago
551
var expressionAgv = GetProjectsAgvWhere ();
552
result = Context . Queryable < base_project , base_equipment >(( x , y ) => x . keys == y . projectKeys )
赖素文
authored
9 months ago
553
. Where ( expressionAgv )
554
555
. OrderBy (( x , y ) => x . createTime )
. Select (( x , y ) => new { x . projectCode , x . projectName , x . createTime })
556
557
558
. Distinct ()
. ToList ();
}
赖素文
authored
9 months ago
559
560
561
562
563
564
565
response . data = result ;
response . count = result . Count ;
return response ;
});
}
赖素文
authored
9 months ago
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
public Expression < Func < base_project , bool >> GetProjectsWhere ()
{
try
{
var exp = Expressionable . Create < base_project >();
// 非管理员,查询登陆用户绑定客户管理的项目
string currentUser = sysUserApiAccount ;
if ( currentUser != SystemVariable . DefaultCreated )
{
var projectRoleKeys = GetProjectRoleKeys ( currentUser );
exp . And ( x => SqlFunc . Subqueryable < sys_role_projects_rel >(). Where ( c => projectRoleKeys . Contains ( c . project_roles_key ) && c . project_key == x . keys ). Any ());
}
return exp . ToExpression ();// 拼接表达式
}
catch ( Exception ex )
{
throw new Exception ( $ "{ex.Message}" );
}
}
public Expression < Func < base_project , base_equipment , bool >> GetProjectsAgvWhere ()
{
try
{
var exp = Expressionable . Create < base_project , base_equipment >();
// 非管理员,查询登陆用户绑定客户管理的项目
string currentUser = sysUserApiAccount ;
if ( currentUser != SystemVariable . DefaultCreated )
{
var projectRoleKeys = GetProjectRoleKeys ( currentUser );
596
exp . And (( x , y ) => SqlFunc . Subqueryable < sys_role_projects_rel >(). Where ( c => projectRoleKeys . Contains ( c . project_roles_key ) && c . project_key == x . keys ). Any ());
赖素文
authored
9 months ago
597
598
599
600
601
602
603
604
605
}
exp . And (( x , y ) => SystemVariable . IotAGVContainDevice . Contains ( y . equipmentTypeCode ));
return exp . ToExpression ();// 拼接表达式
}
catch ( Exception ex )
{
throw new Exception ( $ "{ex.Message}" );
}
}
606
607
/// < summary >
608
609
610
611
612
/// 公共方法: 项目查询设备
/// </ summary >
/// < param name = "projectCode" ></ param >
/// < param name = "agvTag" ></ param >
/// < returns ></ returns >
613
public dynamic GetEquipmentsByProject ( string projectCode , bool agvTag )
614
615
616
617
618
{
var response = new ResponseNew ();
return ExceptionsHelp . Instance . ExecuteT (() =>
{
var result = Context . Queryable < base_equipment , base_project >(( x , y ) => x . projectKeys == y . keys )
619
620
. Where (( x , y ) => y . projectCode == projectCode
&& ! SystemVariable . IotNotContainDevice . Contains ( x . equipmentTypeCode ))
621
. OrderBy (( x , y ) => x . equipmentCode )
622
. Select (( x , y ) => new { x . equipmentCode , x . equipmentName , x . equipmentTypeCode })
623
. ToList ();
624
625
626
// agvTag 标记 true ,只查询 agv 设备
if ( agvTag )
{
627
result = result . AsEnumerable (). Where ( x => SystemVariable . IotAGVContainDevice . Contains ( x . equipmentTypeCode )). ToList ();
628
629
}
630
631
632
633
634
response . data = result ;
response . count = result . Count ;
return response ;
});
}
635
636
637
638
# endregion
# region AGV 设备报表相关
639
640
641
642
/// < summary >
/// 公共方法: AGV 报警列表
/// </ summary >
/// < returns ></ returns >
643
public dynamic GetAGVAlarmList ( string projectCode , string equipmentCode )
644
645
646
647
648
{
var response = new ResponseNew ();
return ExceptionsHelp . Instance . ExecuteT (() =>
{
var result = Context . Queryable < daq_equipment_alarm_record >()
649
. Where ( x => SystemVariable . IotAGVContainDevice . Contains ( x . EquipmentTypeCode ))
650
651
. Where ( x => string . IsNullOrEmpty ( projectCode ) || x . ProjectCode == projectCode )
. Where ( x => string . IsNullOrEmpty ( equipmentCode ) || x . equipmentCode == equipmentCode )
652
653
654
655
656
. Select ( x => new
{
key = x . alarmMessage ,
value = x . alarmMessage
})
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
. Distinct (). ToList ();
response . data = result ;
response . count = result . Count ;
return response ;
});
}
/// < summary >
/// AGV 设备异常分析(点位维度)
/// </ summary >
/// < returns ></ returns >
public dynamic GetAGVCodePointAlarmRecord ( string startTime , string endTime , string projectCode , string equipmentCode , string alarmMsg )
{
var response = new ResponseNew ();
return ExceptionsHelp . Instance . ExecuteT (() =>
{
DataSet ds = new DataSet ();
var startDate = startTime + " 00:00:00" ;
var endDate = endTime + " 23:59:59" ;
676
var sqlWhere = $ " and t1.EquipmentTypeCode in ('{SystemVariable.IotAGVContainDevice}')" ;
677
678
679
if (! string . IsNullOrEmpty ( projectCode )) sqlWhere += $ " and t1.ProjectCode = '{projectCode}' " ;
if (! string . IsNullOrEmpty ( equipmentCode )) sqlWhere += $ " and t1.EquipmentCode = '{equipmentCode}' " ;
if (! string . IsNullOrEmpty ( alarmMsg )) sqlWhere += $ " and t1.AlarmMessage = '{alarmMsg}' " ;
680
var sql = GetAlarmSql ( sqlWhere , startDate , endDate ) + GetAGVCodePointAlarmRecordSql ( projectCode , alarmMsg );
681
ds = Context . Ado . GetDataSetAll ( sql );
682
683
684
DataTable resDt = ds . Tables [ 2 ];// 故障数据
DataTable barCodeDt = ds . Tables [ 1 ];// 所有点位基础数据
685
686
687
688
689
690
691
// 查询项目所有设备
if (! string . IsNullOrEmpty ( projectCode ) && string . IsNullOrEmpty ( equipmentCode ))
{
var equipmentList = Context . Queryable < base_equipment , base_project >(( x , y ) => x . projectKeys == y . keys )
. Where (( x , y ) => y . projectCode == projectCode && x . equipmentTypeCode . Contains ( "AGV" ))
. Select (( x , y ) => new { x . equipmentCode , x . equipmentName , y . projectName })
. ToList ();
692
if ( resDt != null && resDt . Rows . Count > 0 )
693
694
695
696
697
698
699
700
701
702
703
704
{
foreach ( var i in equipmentList )
{
bool exists = resDt . AsEnumerable (). Any ( r => r . Field < string >( "EquipmentName" ) == i . equipmentName );
if (! exists )
{
resDt . Rows . Add ( 0 , i . equipmentCode , i . equipmentName , resDt . Rows [ 0 ][ "CodePoint" ]);
}
}
}
else
{
705
706
bool isHaveBarCode = barCodeDt . Rows . Count > 0 ;
var defaultBarCode = "0" ;
707
foreach ( var i in equipmentList )
708
{
709
710
if ( isHaveBarCode ) defaultBarCode = barCodeDt . Rows [ 0 ][ "BarCode" ]. ToString ();
resDt . Rows . Add ( 0 , i . equipmentCode , i . equipmentName , defaultBarCode );
711
712
}
}
713
714
715
}
716
717
718
response . data = new
{
codepoints = ds . Tables [ 1 ],// 该项目所有点位
719
result = resDt ,// 有报警的点位以及信息
720
};
721
response . count = resDt . Rows . Count ;
722
723
724
725
726
return response ;
});
}
/// < summary >
727
/// AGV 点位维度异常分析
728
729
730
/// </ summary >
/// < param name = "alarmMsg" ></ param >
/// < returns ></ returns >
731
public string GetAGVCodePointAlarmRecordSql ( string projectCode , string alarmMsg )
732
{
733
var sql = $ "select distinct ProjectCode,BarCode from daq_agv_point where ProjectCode='{projectCode}' order by BarCode;" ;
734
735
if (! string . IsNullOrEmpty ( alarmMsg ))
{
736
sql += @" select COUNT (*) as count ,
737
EquipmentCode , EquipmentName ,
738
739
740
741
742
CodePoint ,
AlarmMessage
from
# tempdbRate t
group by
743
EquipmentCode , EquipmentName ,
744
745
CodePoint ,
AlarmMessage " ;
746
747
748
}
else
{
749
sql += @" select COUNT (*) as count ,
750
EquipmentCode , EquipmentName ,
751
CodePoint
752
753
754
from
# tempdbRate t
group by
755
EquipmentCode , EquipmentName ,
756
757
758
759
760
761
762
CodePoint " ;
}
return sql ;
}
/// < summary >
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
/// AGV 设备异常分析(时间维度)
/// </ summary >
/// < param name = "startTime" ></ param >
/// < param name = "endTime" ></ param >
/// < param name = "projectCode" ></ param >
/// < param name = "equipmentCode" ></ param >
/// < param name = "alarmMsg" ></ param >
/// < returns ></ returns >
public dynamic GetAGVAbnormalRecord ( string startTime , string endTime , string projectCode , string equipmentCode , string alarmMsg )
{
var response = new ResponseNew ();
return ExceptionsHelp . Instance . ExecuteT (() =>
{
DataSet ds = new DataSet ();
var startDate = startTime + " 00:00:00" ;
var endDate = endTime + " 23:59:59" ;
var sqlWhere = $ " and t1.EquipmentTypeCode in ('{SystemVariable.IotAGVContainDevice}')" ;
if (! string . IsNullOrEmpty ( projectCode )) sqlWhere += $ " and t1.ProjectCode = '{projectCode}' " ;
if (! string . IsNullOrEmpty ( equipmentCode )) sqlWhere += $ " and t1.EquipmentCode = '{equipmentCode}' " ;
if (! string . IsNullOrEmpty ( alarmMsg )) sqlWhere += $ " and t1.AlarmMessage = '{alarmMsg}' " ;
var sql = GetAlarmSql ( sqlWhere , startDate , endDate ) + GetAGVAbnormalRecordSql ( alarmMsg );
ds = Context . Ado . GetDataSetAll ( sql );
785
786
787
var resDt = ds . Tables [ 1 ];
// 查询项目所有设备
788
if (! string . IsNullOrEmpty ( projectCode ) && string . IsNullOrEmpty ( equipmentCode ) && resDt . Rows . Count > 0 )
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
{
var equipmentList = Context . Queryable < base_equipment , base_project >(( x , y ) => x . projectKeys == y . keys )
. Where (( x , y ) => y . projectCode == projectCode && x . equipmentTypeCode . Contains ( "AGV" ))
. Select (( x , y ) => new { x . equipmentCode , x . equipmentName , y . projectName })
. ToList ();
foreach ( var i in equipmentList )
{
bool exists = resDt . AsEnumerable (). Any ( r => r . Field < string >( "EquipmentName" ) == i . equipmentName );
if (! exists )
{
resDt . Rows . Add ( 0 , i . equipmentCode , i . equipmentName , startTime );
}
}
}
response . data = resDt ;
805
806
807
808
809
810
811
812
813
814
815
response . count = ds . Tables [ 1 ]. Rows . Count ;
return response ;
});
}
public string GetAGVAbnormalRecordSql ( string alarmMsg )
{
var sql = "" ;
if (! string . IsNullOrEmpty ( alarmMsg ))
{
sql += @" select COUNT (*) as count ,
816
EquipmentCode , EquipmentName ,
817
818
819
820
821
CONVERT ( varchar , created , 23 ) as date ,
AlarmMessage
from
# tempdbRate t
group by
822
EquipmentCode , EquipmentName ,
823
824
825
826
827
828
CONVERT ( varchar , created , 23 ),
AlarmMessage " ;
}
else
{
sql += @" select COUNT (*) as count ,
829
EquipmentCode , EquipmentName ,
830
831
832
833
CONVERT ( varchar , created , 23 ) as date
from
# tempdbRate t
group by
834
EquipmentCode , EquipmentName ,
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
CONVERT ( varchar , created , 23 ) " ;
}
return sql ;
}
/// < summary >
/// AGV 故障任务妥善率
/// </ summary >
/// < param name = "startTime" ></ param >
/// < param name = "endTime" ></ param >
/// < param name = "yearOrdaySelectVal" ></ param >
/// < param name = "timeFlag" ></ param >
/// < param name = "projectCode" ></ param >
/// < param name = "equipmentCode" ></ param >
/// < returns ></ returns >
public dynamic GetAGVProperRate ( string startTime , string endTime , string yearOrdaySelectVal , string timeFlag , string projectCode , string equipmentCode )
{
var response = new ResponseNew ();
return ExceptionsHelp . Instance . ExecuteT (() =>
{
// 时间段筛选
if ( timeFlag == "year" )
{
startTime = yearOrdaySelectVal + "-01-01" ;
endTime = yearOrdaySelectVal + "-12-31" ;
}
else if ( timeFlag == "day" )
{
startTime = yearOrdaySelectVal ;
endTime = yearOrdaySelectVal ;
}
866
string startDate = startTime ;
867
868
869
870
871
872
startTime = startTime + " 00:00:00" ;
endTime = endTime + " 23:59:59" ;
// 故障次数
var sqlWhere = $ " and t1.EquipmentTypeCode in ('{SystemVariable.IotAGVContainDevice}')" ;
if (! string . IsNullOrEmpty ( projectCode )) sqlWhere += $ " and t1.ProjectCode = '{projectCode}' " ;
if (! string . IsNullOrEmpty ( equipmentCode )) sqlWhere += $ " and t1.EquipmentCode = '{equipmentCode}' " ;
873
var sql = GetAlarmSql ( sqlWhere , startTime , endTime ) + GetAGVProperRateSql ( timeFlag , startTime , endTime , projectCode );
874
var ds = Context . Ado . GetDataSetAll ( sql );
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
var resDt = ds . Tables [ 1 ];
// 查询项目所有设备
if (! string . IsNullOrEmpty ( projectCode ) && string . IsNullOrEmpty ( equipmentCode ))
{
var equipmentList = Context . Queryable < base_equipment , base_project >(( x , y ) => x . projectKeys == y . keys )
. Where (( x , y ) => y . projectCode == projectCode && x . equipmentTypeCode . Contains ( "AGV" ))
. Select (( x , y ) => new { x . equipmentCode , x . equipmentName , y . projectName })
. ToList ();
foreach ( var i in equipmentList )
{
bool exists = resDt . AsEnumerable (). Any ( r => r . Field < string >( "EquipmentName" ) == i . equipmentName );
if (! exists )
{
resDt . Rows . Add ( i . equipmentCode , i . equipmentName , startDate , 0 , 0 );
}
}
}
resDt . Columns . Add ( "Rate" , typeof ( string ));
foreach ( DataRow row in resDt . Rows )
896
{
897
if (( decimal ) row [ "TotalTask" ] == 0 )
898
899
900
901
902
903
904
905
906
{
row [ "Rate" ] = "100%" ;
}
else
{
decimal result = Math . Round (( int ) row [ "counts" ] / ( decimal ) row [ "TotalTask" ], 2 );
var rate = ( 1 - result ). ToString ( "P2" );
row [ "Rate" ] = rate ;
}
907
}
908
909
910
911
912
response . data = resDt ;
response . count = resDt . Rows . Count ;
913
914
915
916
return response ;
});
}
917
public string GetAGVProperRateSql ( string timeFlag , string startTime , string endTime , string projectCode )
918
{
919
var sql = @" IF OBJECT_ID ( ' Tempdb .. # tempdbAlarmCount ' ) IS NOT NULL
920
921
922
923
924
925
DROP TABLE # tempdbAlarmCount ;
SELECT *
INTO # tempdbAlarmCount
FROM
(
select COUNT (*) as counts ,
926
EquipmentCode , EquipmentName ,
927
928
929
{ 0 } as dateVal
from # tempdbRate
group by { 0 },
930
EquipmentCode , EquipmentName
931
932
933
934
935
936
937
938
) t ;
select
task .*,
temp . counts
from
# tempdbAlarmCount temp join (
select
939
t . EquipmentCode , t . EquipmentName ,
940
941
942
943
944
945
946
t . dateVal ,
sum ( d . TotalTask ) as TotalTask
from
# tempdbAlarmCount t join daq_equipment_task d
on t . EquipmentCode = d . EquipmentCode
and t . dateVal = { 0 }
group by
947
t . EquipmentCode , t . EquipmentName ,
948
949
t . dateVal
) as task on task . EquipmentCode = temp . EquipmentCode
950
951
and task . dateVal = temp . dateVal
order by task . EquipmentCode " ;
952
953
954
955
956
957
958
959
960
961
962
963
if ( timeFlag == "year" )
{
sql = string . Format ( sql , "CONVERT(varchar(7),created,120)" );
}
else
{
sql = string . Format ( sql , "CONVERT(varchar,created,23)" );
}
return sql ;
}
/// < summary >
964
965
966
967
/// 公共方法:查询对应条件的故障(过滤同设备同年月日时分的数据) 临时表 # tempdbRate
/// </ summary >
/// < param name = "sqlWhere" ></ param >
/// < returns ></ returns >
968
public string GetAlarmSql ( string sqlWhere , string begin , string end , Guid [] projectKeys = null )
969
970
971
972
973
974
975
976
977
978
979
980
981
{
string sql = @" ----------------- 数据源 --------------------------------------------
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 . CreateTime , t1 . UpdateTime ,
ErrorDuration = CAST ( t1 . ErrorDuration / 60.0 as DECIMAL ( 10 , 2 )),
t1 . EquipmentCode , t1 . EquipmentName , t1 . EquipmentPropName , t1 . Id AS EquipmentAlarmId ,
982
t1 . AlarmMessage , t1 . AlarmMessage + ';' + ISNULL ( t1 . Remark , '' ) AS Alarm , ISNULL ( t1 . Remark , '' ) AS Remark ,
983
t1 . ProjectCode , t1 . EquipmentTypeCode , p . projectName AS ProjectName ,
984
t . name AS EquipmentTypeName , t1 . Points as CodePoint , e . sequence ,
985
986
987
988
989
990
991
992
h . HandleInfo , h . HandleUser , h . Reason , t1 . HandleTime , t1 . UserHandleTime
FROM dbo . daq_equipment_alarm_record t1 with ( nolock )
LEFT JOIN dbo . bus_equipment_alarm_handle h
ON h . EquipmentAlarmRecordId = t1 . Id
LEFT JOIN dbo . base_equipment_type t
ON t . code = t1 . EquipmentTypeCode
LEFT JOIN dbo . base_project p
ON p . projectCode = t1 . ProjectCode
993
994
LEFT JOIN dbo . base_equipment e
ON e . equipmentCode = t1 . EquipmentCode
995
996
997
998
WHERE 1 = 1 { 0 }
AND t1 . CreateTime >= ' { 1 } '
AND t1 . CreateTime <= ' { 2 } '
AND t1 . EquipmentTypeCode not in ( ' { 3 } ' )
999
{ 4 }
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
) t ;
---------------- tempdbRate 处理相同设备在同年月日时分数据 >----------------
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 ;
1012
SELECT * FROM # tempdbRate ORDER BY CreateTime desc ; " ;
1013
1014
1015
1016
1017
1018
1019
var wh = "" ;
if ( projectKeys != null )
{
wh = $ " and p.keys in('{string.Join(" ',' ", projectKeys)}')" ;
}
sql = string . Format ( sql , sqlWhere , begin , end , SystemVariable . IotNotContainDevice , wh );
1020
1021
return sql ;
}
1022
1023
private DataTable HandleDt ( DataTable resDt , string projectCode , string startDate )
1024
1025
1026
1027
1028
{
if (! string . IsNullOrEmpty ( projectCode ))
{
var equipmentList = Context . Queryable < base_equipment , base_project >(( x , y ) => x . projectKeys == y . keys )
. Where (( x , y ) => y . projectCode == projectCode )
1029
. Select (( x , y ) => new { x . equipmentCode , x . equipmentName , y . projectName , x . sequence })
1030
1031
1032
1033
1034
1035
. ToList ();
foreach ( var i in equipmentList )
{
bool exists = resDt . AsEnumerable (). Any ( r => r . Field < string >( "EquipmentName" ) == i . equipmentName );
if (! exists )
{
1036
resDt . Rows . Add ( startDate , 0 , i . projectName , i . equipmentCode , i . equipmentName , i . sequence );
1037
1038
1039
}
}
}
1040
DataView dv = new DataView ( resDt );
1041
1042
dv . Sort = "Sequence" ;
resDt = dv . ToTable ();
1043
1044
return resDt ;
}
1045
1046
1047
1048
1049
1050
1051
1052
1053
# endregion
/// < summary >
/// 获取保养报告
/// </ summary >
/// < param name = "projectKeys" ></ param >
/// < param name = "equipmentCode" ></ param >
/// < param name = "month" ></ param >
/// < returns ></ returns >
1054
public dynamic GetEqMaintainReport ( string projectCode , string equipmentCode , string yearMonth , bool thisMonth )
1055
1056
1057
1058
{
var response = new ResponseNew ();
return ExceptionsHelp . Instance . ExecuteT (() =>
{
1059
var day = GetTime ( yearMonth , false );
1060
1061
if ( thisMonth )
{
1062
day = GetTime ( yearMonth , true );// 只获取当月的汇总
1063
1064
}
string projectKeys = Context . Queryable < base_project >(). Where ( x => x . projectCode == projectCode ). Select ( x => x . keys ). First (). ToString ();
1065
List < bus_equipment_maintain_record_detail > detailList = GetEqMaintainInfo ( projectKeys , equipmentCode , day . Item1 , day . Item2 );
1066
var maintainCountDts = detailList . AsEnumerable ()
1067
1068
1069
1070
1071
. GroupBy ( x => new
{
EquipmentCode = x . equipmentCode ,
x . EquipmentName ,
x . RealStatus ,
1072
1073
Year = x . RealTime . Year ,
Month = x . RealTime . Month
1074
})
1075
. Select ( x => new
1076
{
1077
1078
1079
1080
1081
1082
1083
Count = x . Count (),
x . Key . EquipmentName ,
x . Key . EquipmentCode ,
x . Key . RealStatus ,
YearMonth = x . Key . Year + "-" + x . Key . Month . ToString ( "D2" )
})
. OrderBy ( x => x . EquipmentCode )
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
. ThenBy ( x => x . YearMonth ). ToList ();
//-- start 处理数据 --
var equipmentList = Context . Queryable < base_equipment >()
. Where ( x => x . projectKeys == Guid . Parse ( projectKeys ))
. Select ( x => new
{
x . equipmentCode ,
x . equipmentName
})
. OrderBy ( x => x . equipmentCode )
. ToList ();// 设备集合
if (! string . IsNullOrEmpty ( equipmentCode ))
{
equipmentList = Context . Queryable < base_equipment >()
. Where ( x => x . projectKeys == Guid . Parse ( projectKeys ) && x . equipmentCode == equipmentCode )
. Select ( x => new
{
x . equipmentCode ,
x . equipmentName
}). ToList ();
}
List < string > realStatusList = new List < string >() { "未保养" , "已保养" , "逾期" };// 状态集合
List < string > monthList = day . Item3 ;// 年月集合
// 使用 LINQ 来简化嵌套循环
List < EquipmentMaintainStatusDto > equipmentMaintainStatusDtos = equipmentList
. SelectMany ( equipment => monthList ,
( equipment , month ) => new { equipment , month })
. SelectMany ( em => realStatusList ,
( em , status ) => new EquipmentMaintainStatusDto
{
唐召明
authored
5 months ago
1114
1115
1116
1117
1118
equipmentCode = em . equipment . equipmentCode ,
equipmentName = em . equipment . equipmentName ,
count = 0 ,
yearMonth = em . month ,
realStatus = status
1119
1120
1121
1122
1123
1124
})
. ToList ();
// 更新 DTO 列表中的 Count 值
foreach ( var dto in equipmentMaintainStatusDtos )
{
var matchingRecord = maintainCountDts . FirstOrDefault ( m =>
唐召明
authored
5 months ago
1125
1126
1127
m . EquipmentCode == dto . equipmentCode &&
m . YearMonth == dto . yearMonth &&
m . RealStatus == dto . realStatus );
1128
1129
1130
if ( matchingRecord != null )
{
唐召明
authored
5 months ago
1131
dto . count = matchingRecord . Count ; // 替换为实际的 Count 值
1132
1133
1134
1135
}
}
//-- end 处理数据 --
// var maintainCountDt = new EquipmentMaintainVM () { EquipmentMaintainStatus = equipmentMaintainStatusDtos };
1136
1137
var res = new
{
1138
maintainCountDt = equipmentMaintainStatusDtos ,
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
detailList
};
response . data = res ;
response . count = res . detailList . Count ;
return response ;
});
}
/// < summary >
/// 获取所选时间、项目、设备下的保养状态详情数据
/// </ summary >
/// < param name = "projectKeys" ></ param >
/// < param name = "equipmentCode" ></ param >
/// < param name = "startTime" ></ param >
/// < param name = "endTime" ></ param >
/// < returns ></ returns >
1155
public List < bus_equipment_maintain_record_detail > GetEqMaintainInfo ( string projectKeys , string equipmentCode , DateTime startTime , DateTime endTime )
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
{
var sql = string . Format ( $@" select
e . equipmentName , t .*
from
(
-- 未保养:下次保养时间 + 状态
select '未保养' as RealStatus , NextMaintainTime as RealTime ,* from bus_equipment_maintain_record_detail where Status = ' 10 ' and NextMaintainTime > GETDATE ()
union
-- 已保养:保养时间 + 状态
select '已保养' as RealStatus , MaintainTime as RealTime ,* from bus_equipment_maintain_record_detail where Status = ' 20 '
union
-- 逾期:下次保养时间 + 跟当前时间比对
select '逾期' as RealStatus , NextMaintainTime as RealTime ,* from bus_equipment_maintain_record_detail where Status = ' 10 ' and NextMaintainTime <= GETDATE ()
) t inner join base_equipment e on t . EquipmentCode = e . equipmentCode
where t . ProjectKeys = ' { projectKeys } ' and t . RealTime >= ' { startTime } ' and t . RealTime <= ' { endTime } ' " );
if (! string . IsNullOrEmpty ( equipmentCode ))
{
sql += $ " and t.equipmentCode='{equipmentCode}' " ;
}
1175
// sql += " order by t.EquipmentCode,t.RealTime" ;
1176
1177
1178
1179
var res = Context . SqlQueryable < bus_equipment_maintain_record_detail >( sql );
List < bus_equipment_maintain_record_detail > resList = new List < bus_equipment_maintain_record_detail >();
if ( res != null )
{
1180
resList = res . ToList ();
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
foreach ( var x in resList )
{
// 保养操作档案附件
x . OperationFileUrl = string . Join ( ',' , Context . Queryable < Sys_File >()
. Where ( t => SqlFunc . Subqueryable < base_equipment_part >()
. Where ( s => s . partCode == x . partCode && s . id == t . targetId ). Any ()
&& t . targetTableName == nameof ( base_equipment_part ))
. Select ( t => t . url )
. ToList ());
// 确认保养附件
x . ConfirmFileUrl = string . Join ( ',' , Context . Queryable < Sys_File >()
. Where ( t => SqlFunc . Subqueryable < bus_equipment_maintain_record_detail >()
. Where ( s => s . partCode == x . partCode && s . id == t . targetId ). Any ()
&& t . targetTableName == nameof ( bus_equipment_maintain_record_detail ))
. Select ( t => t . url )
. ToList ());
// 保养等级
x . maintainLevel = Context . Queryable < sys_dict_data >()
. Where ( t => t . dictType == "MaintainLevel" && t . dictValue == x . maintainLevel )
. Select ( t => t . dictLabel ). First ();
// 推送状态
x . PushStatus = Enum . GetName ( typeof ( EnumMaintainPushStatus ), x . pushStatus );
}
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
}
return resList ;
}
/// < summary >
/// 获取每月所有设备保养次数总数
/// </ summary >
/// < param name = "projectkeys" > 项目 keys </ param >
/// < param name = "yearMonth" > 年月 </ param >
/// < returns ></ returns >
1214
public dynamic GetEqMaintainCount ( string projectCode , string yearMonth )
1215
1216
1217
1218
{
var response = new ResponseNew ();
return ExceptionsHelp . Instance . ExecuteT (() =>
{
1219
var dateTime = GetTime ( yearMonth , false );
1220
1221
1222
1223
string projectKeys = Context . Queryable < base_project >(). Where ( x => x . projectCode == projectCode ). Select ( x => x . keys ). First (). ToString ();
var dt = Context . Queryable < bus_equipment_maintain_record_detail >()
. Where ( x => x . maintainTime >= dateTime . Item1 && x . maintainTime <= dateTime . Item2 && x . projectKeys . Equals ( projectKeys ))
. ToList ();
1224
1225
1226
1227
1228
var resDt = dt . GroupBy ( x => new
{
Year = x . maintainTime . Value . Year ,
Month = x . maintainTime . Value . Month
})
1229
1230
. Select ( x => new
{
1231
1232
1233
1234
1235
YearMonth = x . Key . Year + "-" + x . Key . Month . ToString ( "D2" ),
Count = x . Count ()//,
// Records = x . ToList ()
})
. OrderBy ( x => x . YearMonth );
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
response . data = resDt ;
return response ;
});
}
/// < summary >
/// 公共方法:根据所传年月获取最近三个月的时间段
/// </ summary >
/// < param name = "yearMonth" ></ param >
/// < returns ></ returns >
1247
public ( DateTime , DateTime , List < string >) GetTime ( string yearMonth , bool thisMonth )
1248
1249
1250
1251
1252
1253
1254
1255
{
// 1 、获取包含本月在内的三个月数据
var yearMonthParts = yearMonth . Split ( '-' );
int year = int . Parse ( yearMonthParts [ 0 ]);
int month = int . Parse ( yearMonthParts [ 1 ]);
DateTime currentDate = new DateTime ( year , month , 1 );// 选择月份
// 计算三个月前的开始日期
DateTime startDate = new DateTime ( currentDate . Year , currentDate . Month , 1 ). AddMonths (- 2 );
1256
// 2 、计算当前月份的结束日期
1257
1258
1259
1260
1261
1262
DateTime endDate = new DateTime ( currentDate . Year , currentDate . Month , 1 ). AddMonths ( 1 ). AddDays (- 1 );
if ( thisMonth )
{
startDate = new DateTime ( currentDate . Year , currentDate . Month , 1 );
endDate = new DateTime ( currentDate . Year , currentDate . Month , DateTime . DaysInMonth ( year , month ));
}
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
endDate = DateTime . Parse ( endDate . ToString ( "yyyy-MM-dd" ) + " 23:59:59" );// 最后一天统一到 23 : 59 : 59 秒
// 3 、得出日期范围内所有年月份组合
List < string > months = new List < string >();
// 确保开始日期在结束日期之前
if ( startDate > endDate )
{
throw new ArgumentException ( "Start date must be earlier than end date." );
}
// 从开始日期开始,逐月增加
DateTime current = new DateTime ( startDate . Year , startDate . Month , 1 );
while ( current <= endDate )
{
months . Add ( current . ToString ( "yyyy-MM" )); // 格式化为 YYYY - MM
current = current . AddMonths ( 1 ); // 增加一个月
}
return ( startDate , endDate , months );
1281
}
1282
1283
public dynamic GetEquipmentMaintainStatus ( string projectCode , DateTime date , string equipmentCode , EquipmentVisualService visualService )
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
{
var response = new ResponseNew ();
return ExceptionsHelp . Instance . ExecuteT (() =>
{
List < base_equipment > eqList = new List < base_equipment >();
if (! string . IsNullOrEmpty ( equipmentCode ))
{
var eq = Context . Queryable < base_equipment >(). Where ( x => x . equipmentCode == equipmentCode ). First ();// 查询外部编码才能匹配查询 daq 相关表数据
eqList . Add ( eq );
}
else
{
var projectKeys = Context . Queryable < base_project >(). Where ( x => x . projectCode == projectCode ). Select ( x => x . keys ). First ();
1297
eqList = Context . Queryable < base_equipment >(). Where ( x => x . projectKeys == projectKeys ). OrderBy ( x => x . equipmentCode ). ToList ();
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
}
// 查询状态数据
List < EquipmentHealthVM > resList = new List < EquipmentHealthVM >();
foreach ( var eq in eqList )
{
var equipmentHealthVM = visualService . GetEquipmentHealth ( eq , date );
resList . Add ( equipmentHealthVM . vm );
}
response . data = resList ;
return response ;
});
}
赖素文
authored
about a year ago
1312
1313
}
}