EquipmentVisualService.cs
14.1 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
using Hh.Mes.Pojo.System;
using Hh.Mes.POJO.Entity;
using Hh.Mes.POJO.EnumEntitys;
using Hh.Mes.POJO.Response;
using Hh.Mes.POJO.ViewModel.Equipment;
using Hh.Mes.Service.Repository;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
namespace Hh.Mes.Service.WebService.Equipment
{
/// <summary>
/// 设备可视化参数
/// </summary>
public class EquipmentVisualService : RepositorySqlSugar<base_equipment>
{
/// <summary>
/// 设备健康表现、 操作手册、未处理设备报警记录
/// </summary>
public Response GetEquipmentFileList(base_equipment model)
{
var result = new Response();
//开机时间以及当天各状态时间及占比(通用方法,考虑报表接口共用)
var res = GetEquipmentHealth(model, DateTime.Today, true);
var alarmRecords = res.alarmRecords;
var alarmTotal = res.vm.AlarmTotal;
result.Result = new
{
equipmentHealth = res.vm,
fileList = res.dt.Tables[0],
eqInfo = res.dt.Tables[1],
alarmList = alarmRecords,
alarmTotal,
};
result.Count = 1;
return result;
}
public (EquipmentHealthVM vm, List<daq_equipment_alarm_record> alarmRecords, dynamic dt) GetEquipmentHealth(base_equipment model, DateTime day, bool isSelectAlarmList = false)
{
var stringBuilder = new StringBuilder();
stringBuilder.AppendLine($@" select * from sys_File
where targetId=@targetId and targetTableName='base_equipment'
union all
select * from sys_File
where targetId=@targetId2 and targetTableName='base_equipment_type';
SELECT t.EquipmentCode,
ISNULL(DATEDIFF(second, t1.UpdateTime, GETDATE()), -1) AS seconds,
t1.Status,
t.ip,
simNumber= (
SELECT STUFF((
SELECT ',' + CAST(rel.simNumber AS VARCHAR(4000))
FROM base_sim_equipment_rel rel
WHERE rel.EquipmentCode = t.EquipmentCode
FOR XML PATH('')
), 1, 1, '')
)
FROM base_equipment t
LEFT JOIN daq_equipment_status_record t1 WITH(NOLOCK) ON t1.EquipmentCode = t.otherCode
WHERE t.EquipmentCode=@EquipmentCode");
var dt = Context.Ado.GetDataSetAll(stringBuilder.ToString(), new List<SugarParameter>(){
new SugarParameter("@targetId", model.equipmentCode),
new SugarParameter("@targetId2", model.equipmentTypeCode),
new SugarParameter("@EquipmentCode", model.equipmentCode),
new SugarParameter("@OtherEquipmentCode", model.otherCode),
});
//今天的记录
var startTime = day;
var records = Context.Queryable<daq_equipment_status_record_history>().Where(x => x.equipmentCode == model.otherCode && x.createTime >= startTime).Select(x => new
{
x.id,
x.statusDuration,
x.status
}).ToList();
var currentMonthStartTime = startTime.AddDays(1 - startTime.Day);
//设备饼图当天 空闲时间
var freeTimeSeconds = records.Where(x => x.status == EquipmentStatus.Free.ToString()).Sum(x => x.statusDuration);
//设备饼图当天 运行时间
var runTimeSeconds = records.Where(x => x.status == EquipmentStatus.Running.ToString()).Sum(x => x.statusDuration);
//设备饼图当天 总故障时间
var alarmTimeSeconds = Context.Queryable<daq_equipment_alarm_record>()
.Where(x => x.equipmentCode == model.otherCode && x.createTime >= startTime)
.Select(x => new
{
x.id,
x.errorduration
})
.ToList()
.Sum(x => x.errorduration);
//设备饼图当天 总时间
var dayTime = DateTime.Now;
if (!startTime.Date.Equals(dayTime.Date)) dayTime = DateTime.Parse(startTime.ToString("yyyy-MM-dd") + " 23:59:59");
var currentTotalSecondss = (dayTime - startTime).TotalSeconds;
//设备饼图当天 离线时间
var offlineTimeTicks = currentTotalSecondss - (alarmTimeSeconds + freeTimeSeconds + runTimeSeconds);
if (offlineTimeTicks < 0)
{
offlineTimeTicks = 0;
}
var equipmentHealth = new EquipmentHealthVM
{
EquipmentStatus = new EquipmentStatusDto
{
TodayPowerTime = TimeSpan.FromSeconds(records.Sum(x => x.statusDuration)).TotalSeconds,
CurrentMonthPowerTime = TimeSpan.FromSeconds(Context.Queryable<daq_equipment_status_record_history>()
.Where(x => x.equipmentCode == model.otherCode && x.createTime >= currentMonthStartTime)
.Sum(x => x.statusDuration)).TotalSeconds,
RunTime = runTimeSeconds,
FreeTime = freeTimeSeconds,
AlarmTime = alarmTimeSeconds,
OfflineTime = offlineTimeTicks,
RunProportion = Math.Round(runTimeSeconds * 100d / currentTotalSecondss, 1),
FreeProportion = Math.Round(freeTimeSeconds * 100d / currentTotalSecondss, 1),
AlarmProportion = Math.Round(alarmTimeSeconds * 100d / currentTotalSecondss, 1),
OfflineProportion = Math.Round(offlineTimeTicks * 100d / currentTotalSecondss, 1),
}
};
//当前设备未处理的报警总数
var alarmQuery = Context.Queryable<daq_equipment_alarm_record>().Where(x => x.equipmentCode == model.otherCode && !x.isHandled);
var alarmTotal = alarmQuery.Count();
var alarmRecords = new List<daq_equipment_alarm_record>();
if (isSelectAlarmList)//设备可视化展示的数据
{
alarmRecords = alarmQuery.OrderBy(x => x.createTime, OrderByType.Desc).Take(200).ToList();
}
//保养提醒
var maintainCount = Context.Queryable<bus_equipment_maintain_record_detail>().Where(x => x.equipmentCode == model.otherCode && x.status != 20).Count();
equipmentHealth.AlarmTotal = alarmTotal;
equipmentHealth.MaintainTotal = maintainCount;
equipmentHealth.EquipmentCode = model.equipmentCode;
equipmentHealth.EquipmentName = model.equipmentName;
equipmentHealth.ProjectName = Context.Queryable<base_project>().Where(x => x.keys == model.projectKeys).Select(x => x.projectName).First();
equipmentHealth.IP = model.ip;
//设备状态赋值
var statusText = "在线";
var status = dt.Tables[1].Rows[0]["status"].ToString();
int seconds = (int)dt.Tables[1].Rows[0]["seconds"];
if (status == "Failure" || status == "Error")
{
statusText = "故障";
}
if (seconds > 300 || seconds == -1)
{
statusText = "离线";
}
else
{
statusText = "空闲";
}
equipmentHealth.Status = statusText;
return (equipmentHealth, alarmRecords, dt);
}
/// <summary>
/// 设备保养信息tab 概述总览数据、部件列表
/// </summary>
public Response GetEquipmentMaintainList(base_equipment model)
{
var result = new Response();
var stringBuilder = new StringBuilder();
//设备保养概述总表关联部件基础信息表
stringBuilder.AppendLine(@" SELECT t.EquipmentCode,
t.LastMaintainTime,
t.SumAmount,
t1.Status,
ISNULL(DATEDIFF(second, t1.UpdateTime, GETDATE()), -1) AS seconds,
t.MaintainNumber,
t.ToBeMaintainedNumber,
t3.ip,
t3.imgSrc
FROM
bus_equipment_maintain_record_top t
LEFT JOIN daq_equipment_status_record t1 WITH(NOLOCK) ON t1.EquipmentCode = t.EquipmentCode
LEFT JOIN base_equipment t3 WITH(NOLOCK) ON t3.equipmentCode = t.EquipmentCode
WHERE t.EquipmentCode=@equipmentCode;
select p.*
from bus_equipment_maintain_record_head p
where p.EquipmentCode=@equipmentCode ");
var dt = base.Context.Ado.GetDataSetAll(stringBuilder.ToString(), new List<SugarParameter>(){
new SugarParameter("@equipmentCode", model.equipmentCode),
});
if (dt == null || dt.Tables[0].Rows.Count == 0) return result.ResponseError(SystemVariable.queryNotData);
result.Result = new
{
main = dt.Tables[0],
eqmaintainHeadList = dt.Tables[1]
};
return result;
}
/// <summary>
/// 设备保养信息tab 已保养设备部件 未保养设备部件
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public Response GetEquipmentMaintainRecordList(string eqCode, string partCode)
{
var result = new Response();
var stringBuilder = new StringBuilder();
stringBuilder.AppendLine(@"SELECT h.EquipmentCode,
h.PartCode,
h.PartName,
h.LastMaintainTime,
h.NextMaintainTime,
d.MaintainTime,
d.Maintainer,
d.MaintainContent,
d.MaintainPrice,
d.PushStatus,
d.Remark,
url=STUFF(( SELECT ',' + f.url FROM sys_File f
WHERE targetTableName='bus_equipment_maintain_record_detail' and f.targetId = d.id
FOR XML PATH('')
), 1, 1, '')
FROM dbo.bus_equipment_maintain_record_detail d LEFT
JOIN dbo.bus_equipment_maintain_record_head h
ON h.HeadKeys=d.HeadKeys
WHERE h.EquipmentCode=@equipmentCode AND d.Status=@status");
if (!string.IsNullOrEmpty(partCode))
{
//非空 附加部件条件
stringBuilder.AppendLine(" AND h.PartCode=@partCode");
}
//已保养
var dt = base.Context.Ado.GetDataTable(stringBuilder.ToString(), new List<SugarParameter>(){
new SugarParameter("@equipmentCode", eqCode),
new SugarParameter("@partCode", partCode),
new SugarParameter("@status", (int)EnumMaintainStatus.Maintained),
});
//待保养
var dt_tobe = base.Context.Ado.GetDataTable(stringBuilder.ToString(), new List<SugarParameter>(){
new SugarParameter("@equipmentCode", eqCode),
new SugarParameter("@partCode", partCode),
new SugarParameter("@status", (int)EnumMaintainStatus.ToBeMaintain),
});
result.Result = new
{
maintainList = dt,
tobemaintainList = dt_tobe
};
return result;
}
/// <summary>
///
/// </summary>
/// <param name="startTime">开始时间</param>
/// <param name="endTime">结束时间</param>
/// <param name="comparisonValue">比较值</param>
/// <returns></returns>
private long ConvertToTicks(DateTime startTime, DateTime endTime, DateTime comparisonValue)
{
var tmep = startTime;
if (startTime < comparisonValue)
{
tmep = comparisonValue;
}
return (endTime - tmep).Ticks;
}
}
}