HuXiYu
authored
|
1
2
3
4
5
6
7
8
9
10
11
12
|
using Hh.Mes.Common.Infrastructure;
using Hh.Mes.Common.log;
using Hh.Mes.Common.Request;
using Hh.Mes.POJO.Entity;
using Hh.Mes.POJO.Response;
using Hh.Mes.Service.Repository;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Hh.Mes.Pojo.System;
|
HuXiYu
authored
|
13
|
using System.Data;
|
赖素文
authored
|
14
|
using Hh.Mes.POJO.EnumEntitys;
|
赖素文
authored
|
15
|
using System.Text;
|
|
16
|
using Hh.Mes.POJO.ViewModel.Project;
|
赖素文
authored
|
17
|
|
HuXiYu
authored
|
18
19
|
namespace Hh.Mes.Service.Configure
{
|
唐召明
authored
|
20
21
|
public class BaseProjectService : RepositorySqlSugar<base_project>
{
|
赖素文
authored
|
22
23
|
#region 项目列表
public dynamic Load(PageReq pageReq, base_project entity)
|
唐召明
authored
|
24
25
26
|
{
return ExceptionsHelp.Instance.ExecuteT(() =>
{
|
唐召明
authored
|
27
|
var result = new Response();
|
赖素文
authored
|
28
|
var expression = LinqProjectWhere(entity);
|
唐召明
authored
|
29
|
//先组合查询表达式
|
赖素文
authored
|
30
31
|
var query = Context.Queryable<base_project>()
.Where(expression);
|
赖素文
authored
|
32
33
|
string user = sysWebUser.Account.ToString();
var projectRoseList = base.GetProjectRoleKeys(user);
|
唐召明
authored
|
34
35
36
37
38
|
//Exel为ture就不分页,因为导出的话是全部导出
if (pageReq != null && !entity.Exel)
{
int total = 0;
var list = query.ToOffsetPage(pageReq.page, pageReq.limit, ref total);
|
赖素文
authored
|
39
|
result.Result = DataHandleProjectRole(list);
|
唐召明
authored
|
40
41
42
|
result.Count = total;
return result;
}
|
唐召明
authored
|
43
|
result.Result = DataHandleProjectRole(query.ToList());
|
唐召明
authored
|
44
|
result.Count = result.Result.Count;
|
赖素文
authored
|
45
|
result.Message = result.Result.Count;
|
唐召明
authored
|
46
47
48
49
50
51
52
|
return result;
}, catchRetrunValue: "list");
}
private List<base_project> DataHandle(List<base_project> data)
{
var projectKeys = data.Select(item => item.keys).ToList();
|
赖素文
authored
|
53
54
55
56
|
var projectQuery = Context.Queryable<base_project, sys_user_project_rel>((pro, upRel) =>
new JoinQueryInfos(JoinType.Left, pro.keys == upRel.clientKeys))
.Where((pro, upRel) => projectKeys.Contains(pro.keys))
.Select((pro, upRel) => new
|
唐召明
authored
|
57
58
|
{
pro.keys,
|
赖素文
authored
|
59
|
upRel.userAccount
|
唐召明
authored
|
60
61
62
63
64
65
66
67
|
})
.ToList();
//给项目 赋值 登入用户、项目关联绑定客户信息
foreach (var item in data)
{
var projectUsers = projectQuery.Where(x => x.keys == item.keys)
.Select(x => x.userAccount)
.ToArray();
|
唐召明
authored
|
68
|
item.projectUsers = string.Join(",", projectUsers);
|
唐召明
authored
|
69
70
71
72
73
|
}
return data;
}
|
赖素文
authored
|
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
/// <summary>
/// 获取项目对应的角色
/// </summary>
private List<base_project> DataHandleProjectRole(List<base_project> data)
{
var projectKeys = data.Select(item => item.keys).ToList();
var projectQuery = Context.Queryable<sys_role_projects_rel>()
.Where(t => projectKeys.Contains(t.project_key))
.Select(t => new
{
t.project_key,
t.project_roles_key
})
.ToList();
//给项目 赋值 所有的角色
foreach (var item in data)
{
var projectUsers = projectQuery.Where(x => x.project_key == item.keys)
.Select(x => x.project_roles_key)
.ToArray();
item.projectRoles = string.Join(",", projectUsers);
|
唐召明
authored
|
95
|
}
|
赖素文
authored
|
96
97
98
|
return data;
}
|
赖素文
authored
|
99
|
public Expression<Func<base_project, bool>> LinqProjectWhere(base_project model)
|
唐召明
authored
|
100
|
{
|
赖素文
authored
|
101
|
try
|
唐召明
authored
|
102
|
{
|
赖素文
authored
|
103
104
105
|
var exp = Expressionable.Create<base_project>();
//数据过滤条件
if (!string.IsNullOrWhiteSpace(model.projectName))
|
赖素文
authored
|
106
|
{
|
赖素文
authored
|
107
|
exp.And(x => x.projectName.Contains(model.projectName));
|
赖素文
authored
|
108
|
}
|
赖素文
authored
|
109
|
if (!string.IsNullOrWhiteSpace(model.projectAddress))
|
唐召明
authored
|
110
|
{
|
赖素文
authored
|
111
|
exp.And(x => x.projectAddress.Contains(model.projectAddress));
|
唐召明
authored
|
112
|
}
|
赖素文
authored
|
113
|
if (!string.IsNullOrWhiteSpace(model.projectManager))
|
唐召明
authored
|
114
|
{
|
赖素文
authored
|
115
|
exp.And(x => x.projectManager.Contains(model.projectManager));
|
唐召明
authored
|
116
|
}
|
唐召明
authored
|
117
|
|
赖素文
authored
|
118
119
120
|
//非管理员,查询登陆用户绑定客户管理的项目
string user = sysWebUser.Account;
if (user != SystemVariable.DefaultCreated)
|
唐召明
authored
|
121
|
{
|
唐召明
authored
|
122
123
|
var projectRoleKeys = GetProjectRoleKeys(user);
exp.And(x => SqlFunc.Subqueryable<sys_role_projects_rel>().Where(c => projectRoleKeys.Contains(c.project_roles_key) && c.project_key == x.keys).Any());
|
赖素文
authored
|
124
125
126
127
128
129
130
|
}
return exp.ToExpression();//拼接表达式
}
catch (Exception ex)
{
throw new Exception($"{ex.Message}");
}
|
唐召明
authored
|
131
|
}
|
唐召明
authored
|
132
|
|
赖素文
authored
|
133
|
#endregion
|
赖素文
authored
|
134
|
|
赖素文
authored
|
135
|
#region 项目列表 新增、编辑、删除、导出
|
赖素文
authored
|
136
|
public dynamic Ins(base_project entity)
|
唐召明
authored
|
137
138
139
140
141
142
143
144
145
|
{
return ExceptionsHelp.Instance.ExecuteT(() =>
{
var response = new Response();
var projectKey = Guid.NewGuid();
entity.keys = projectKey;
entity.createBy = sysWebUser.Account;
entity.createTime = DateTime.Now;
|
赖素文
authored
|
146
|
if (entity.projectClientInfoKeys != Guid.Empty)
|
赖素文
authored
|
147
|
{
|
赖素文
authored
|
148
|
var clientName = Context.Queryable<base_project_client_info>().First(x => x.keys == entity.projectClientInfoKeys).clientName;
|
赖素文
authored
|
149
150
151
|
entity.projectClientName = clientName;
}
|
唐召明
authored
|
152
153
154
155
156
|
Context.Insertable(entity).AddQueue();
//厂房编码=项目编码
var factoryCount = Context.Queryable<base_factory>().Where(x => x.projectKeys == entity.keys).Count() + 1;
var factoryCode = entity.projectCode + factoryCount.ToString().PadLeft(2, '0');
|
赖素文
authored
|
157
|
var factory = new base_factory
|
唐召明
authored
|
158
159
160
161
162
163
164
165
166
|
{
keys = Guid.NewGuid(),
factoryCode = factoryCode,
factoryName = entity.projectName + "默认一厂",
createBy = sysWebUser.Account,
createTime = DateTime.Now,
projectKeys = projectKey
};
Context.Insertable(factory).AddQueue();
|
赖素文
authored
|
167
168
|
//创建项目用户角色
|
唐召明
authored
|
169
|
var newRoleKeys = entity.projectRoles.Split(",", StringSplitOptions.RemoveEmptyEntries).Select(x =>
|
赖素文
authored
|
170
|
{
|
唐召明
authored
|
171
172
173
174
175
176
177
178
179
180
181
182
183
|
_ = Guid.TryParse(x, out var key);
return key;
}).Where(x => x != Guid.Empty).ToList();
var rolesList = newRoleKeys.Select(x => new sys_role_projects_rel
{
keys = Guid.NewGuid(),
project_key = entity.keys,
project_roles_key = x,
createTime = DateTime.Now,
createBy = sysWebUser.Account
}).ToList();
|
赖素文
authored
|
184
185
|
Context.Insertable(rolesList).AddQueue();
|
唐召明
authored
|
186
187
188
189
190
191
192
193
194
195
196
197
198
199
|
var result = Context.SaveQueuesAsync().Result > 0;
response.Status = result;
if (!result) response.Message = SystemVariable.dataActionError;
return response;
});
}
public dynamic Upd(base_project entity)
{
return ExceptionsHelp.Instance.ExecuteT(() =>
{
var response = new Response();
entity.updateBy = sysWebUser.Account;
entity.updateTime = DateTime.Now;
|
赖素文
authored
|
200
201
202
203
204
|
if (entity.projectClientInfoKeys != Guid.Empty)
{
var clientName = Context.Queryable<base_project_client_info>().First(x => x.keys == entity.projectClientInfoKeys).clientName;
entity.projectClientName = clientName;
}
|
赖素文
authored
|
205
206
|
Context.Updateable(entity).AddQueue();
|
赖素文
authored
|
207
|
#region 项目角色 先全部删除在新增
|
赖素文
authored
|
208
209
210
|
var roles = entity.projectRoles.Split(",", StringSplitOptions.RemoveEmptyEntries);
var rolesList = new List<sys_role_projects_rel>();
foreach (var item in roles)
|
唐召明
authored
|
211
|
{
|
赖素文
authored
|
212
213
214
215
|
Context.Deleteable<sys_role_projects_rel>().Where(x => x.project_key == entity.keys).AddQueue();
var temp = CreateRoleProjectsRel(entity, item);
rolesList.Add(temp);
}
|
唐召明
authored
|
216
|
Context.Insertable(rolesList).AddQueue();
|
赖素文
authored
|
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
|
#endregion
#region 项目角色 排查后删除在新增
/*var newRoleKeys = entity.projectRoles.Split(",", StringSplitOptions.RemoveEmptyEntries).Select(x =>
{
_ = Guid.TryParse(x, out var key);
return key;
}).Where(x => x != Guid.Empty).ToList();
var oldSysRoleProjectsRel = Context.Queryable<sys_role_projects_rel>().Where(x => x.project_key == entity.keys).ToList();
var oldRoleKeys = oldSysRoleProjectsRel.Select(x => x.project_roles_key).Distinct().ToList();
var addTemps = newRoleKeys.Except(oldRoleKeys);
var removeTemps = oldRoleKeys.Except(newRoleKeys);
Context.Deleteable<sys_role_projects_rel>().Where(x => x.project_key == entity.keys && removeTemps.Contains(x.project_roles_key)).AddQueue();
var rolesList = addTemps.Select(x => new sys_role_projects_rel
{
keys = Guid.NewGuid(),
project_key = entity.keys,
project_roles_key = x,
createTime = DateTime.Now,
createBy = sysWebUser.Account
}).ToList();
Context.Insertable(rolesList).AddQueue(); */
#endregion
|
赖素文
authored
|
246
|
|
赖素文
authored
|
247
248
249
|
var result = Context.SaveQueuesAsync().Result > 0;
response.Status = result;
if (!result) response.Message = SystemVariable.dataActionError;
|
唐召明
authored
|
250
251
252
253
254
255
256
257
258
259
|
return response;
});
}
public dynamic DelByIds(Guid[] keysList)
{
return ExceptionsHelp.Instance.ExecuteT(() =>
{
var response = new Response();
//1.1删除项目需要判断绑定客户
|
赖素文
authored
|
260
|
var isBindClient = Context.Queryable<sys_user_project_rel>().Any(x => keysList.Contains(x.clientKeys));
|
唐召明
authored
|
261
262
263
264
265
266
267
268
269
270
271
272
|
if (isBindClient)
{
return response.ResponseError("选中删除的项目已关联绑定客户,不能直接删除,请选取消项目关联客户!");
}
//1.2 项目是否绑定设备 通过设备绑定是否存在projectKeys
//var isBindEq = Context.Queryable<base_equipment>().Any(x => keysList.Contains(x.projectKeys));
//if (isBindEq)
//{
// return response.ResponseError("选中删除的项目已关联绑定设备,不能直接删除,请删除设备后在删除项目!");
//}
Context.Deleteable<base_project>(t => keysList.Contains(t.keys)).AddQueue();
Context.Deleteable<base_factory>(t => keysList.Contains(t.projectKeys)).AddQueue();
|
赖素文
authored
|
273
|
Context.Deleteable<sys_role_projects_rel>().Where(t => keysList.Contains(t.project_key)).AddQueue();
|
唐召明
authored
|
274
275
276
277
278
279
280
281
282
283
284
285
286
|
var result = Context.SaveQueues() > 0;
response.Status = result;
if (!result) response.Message = SystemVariable.dataActionError;
return response;
});
}
public Response ExportData(base_project entity)
{
return Load(null, entity);
}
|
赖素文
authored
|
287
288
289
290
291
292
293
294
295
296
297
298
|
private sys_role_projects_rel CreateRoleProjectsRel(base_project entity, string roles_key)
{
return new sys_role_projects_rel
{
keys = Guid.NewGuid(),
project_key = entity.keys,
project_roles_key = Guid.Parse(roles_key),
createTime = DateTime.Now,
createBy = sysWebUser.Account
};
}
|
赖素文
authored
|
299
300
|
#endregion
|
赖素文
authored
|
301
302
|
#region 项目地图 首页
public dynamic GetProjectMapList()
|
唐召明
authored
|
303
|
{
|
赖素文
authored
|
304
|
return ExceptionsHelp.Instance.ExecuteT(() =>
|
唐召明
authored
|
305
|
{
|
赖素文
authored
|
306
307
|
var result = new Response();
var expression = GetProjectMapListWhere();
|
唐召明
authored
|
308
|
var list = Context.Queryable<base_project>().Where(expression).ToList();
|
赖素文
authored
|
309
|
|
赖素文
authored
|
310
|
var sumEqCount = new Dictionary<string, int>();
|
唐召明
authored
|
311
312
|
var sumEqRunCount = new Dictionary<string, int>();
var sumEqFreeCountCount = new Dictionary<string, int>();
|
唐召明
authored
|
313
|
|
赖素文
authored
|
314
315
316
317
318
319
320
321
322
323
324
325
|
var eqStatus = Context.Queryable<base_equipment, daq_equipment_status_record>(
(x, y) => new JoinQueryInfos(JoinType.Inner, x.equipmentCode == y.equipmentCode))
.Where((x, y) => !SystemVariable.IotNotContainDevice.Contains(x.equipmentTypeCode))
.Select((x, y) => new
{
x.projectKeys,
y.updateTime,
y.status
})
.ToList();
var currentTime = DateTime.Now;
foreach (var item in list)
|
唐召明
authored
|
326
|
{
|
赖素文
authored
|
327
328
329
330
331
332
333
334
335
336
337
338
339
|
var i = item; // 将每个元素赋值给一个新的变量 i
i.eqCount = item.eqCount;//单个项目设备数
i.eqRunCount = eqStatus.Count((x) => x.projectKeys == i.keys && x.status == EquipmentStatus.Running.ToString()
&& x.updateTime != null && (currentTime - x.updateTime).TotalMinutes <= 5);
i.eqFreeCount = eqStatus.Count((x) => x.projectKeys == i.keys && x.status == EquipmentStatus.Free.ToString()
&& x.updateTime != null && (currentTime - x.updateTime).TotalMinutes <= 5);
//故障数
i.eqFailureCount = eqStatus.Count((x) => x.projectKeys == i.keys && (x.status == EquipmentStatus.Failure.ToString() ||
x.status == EquipmentStatus.Error.ToString())
&& x.updateTime != null && (currentTime - x.updateTime).TotalMinutes <= 5);
i.eqOfflineCount = i.eqCount - i.eqRunCount - i.eqFreeCount - i.eqFailureCount;
|
赖素文
authored
|
340
|
//sumEqCount += i.eqCount;//关联项目 总设备数量
|
赖素文
authored
|
341
|
if (!sumEqCount.ContainsKey(item.projectCode)) sumEqCount.Add(item.projectCode, i.eqCount);
|
赖素文
authored
|
342
343
|
if (!sumEqRunCount.ContainsKey(item.projectCode)) sumEqRunCount.Add(item.projectCode, i.eqRunCount);
if (!sumEqFreeCountCount.ContainsKey(item.projectCode)) sumEqFreeCountCount.Add(item.projectCode, i.eqFreeCount);
|
赖素文
authored
|
344
345
346
347
|
//查询每个项目下是否有报警未处理和是否有待保养
i.isHaveMaintain = Context.Queryable<base_equipment>().Any(x => x.projectKeys == i.keys && x.isMaintain);//返回true是有待保养
i.isHaveAlarm = Context.Queryable<base_equipment>().Any(x => x.projectKeys == i.keys && x.remark.Contains("有未处理报警记录"));//返回true是有未处理报警
|
唐召明
authored
|
348
|
}
|
唐召明
authored
|
349
|
decimal tempSumEqCount = sumEqCount.Values.Sum();
|
赖素文
authored
|
350
351
|
//返回 项目数据、总设备数、在线率、是否有报警、是否有待保养
string onlineRate = "0%";
|
赖素文
authored
|
352
|
if (tempSumEqCount > 0)
|
唐召明
authored
|
353
|
{
|
赖素文
authored
|
354
|
decimal onlineRateFormatted = Math.Round((sumEqRunCount.Values.Sum() + sumEqFreeCountCount.Values.Sum()) / tempSumEqCount, 2);
|
赖素文
authored
|
355
|
onlineRate = (onlineRateFormatted * 100m).ToString("0.##") + "%";
|
唐召明
authored
|
356
|
}
|
赖素文
authored
|
357
|
result.Result = new
|
唐召明
authored
|
358
|
{
|
赖素文
authored
|
359
|
list = list,
|
赖素文
authored
|
360
|
sumEqCount = tempSumEqCount,
|
赖素文
authored
|
361
362
363
364
365
366
|
onlineRate = onlineRate
};
return result;
});
}
|
唐召明
authored
|
367
|
public Expression<Func<base_project, bool>> GetProjectMapListWhere()
|
赖素文
authored
|
368
369
370
|
{
try
{
|
唐召明
authored
|
371
|
var exp = Expressionable.Create<base_project>();
|
赖素文
authored
|
372
373
374
|
//非管理员,查询登陆用户绑定客户管理的项目
string currentUser = sysWebUser.Account;
if (currentUser != SystemVariable.DefaultCreated)
|
唐召明
authored
|
375
|
{
|
唐召明
authored
|
376
377
|
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());
|
唐召明
authored
|
378
379
380
381
382
383
384
|
}
return exp.ToExpression();//拼接表达式
}
catch (Exception ex)
{
throw new Exception($"{ex.Message}");
}
|
唐召明
authored
|
385
|
}
|
赖素文
authored
|
386
|
#endregion
|
唐召明
authored
|
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
|
#region 项目概述
public dynamic LoadProjectOverview(base_project entity)
{
return ExceptionsHelp.Instance.ExecuteT(() =>
{
var result = new Response();
//项目地图调用 返回结果
project_overview po = new project_overview();
//设备概述
po.eqSumCount = Context.Queryable<base_equipment>()
.Where(x => x.projectKeys == entity.keys && !SystemVariable.IotNotContainDevice.Contains(x.equipmentTypeCode))
.ToList().Count;//单个项目 设备总数
var eqInfo = Context.Queryable<base_equipment, daq_equipment_status_record>(
(x, y) => new JoinQueryInfos(JoinType.Inner, x.otherCode == y.equipmentCode))
.Where((x, y) => x.projectKeys == entity.keys && SqlFunc.SqlServer_DateDiff("Minute", y.updateTime, DateTime.Now) <= 5 &&
!SystemVariable.IotNotContainDevice.Contains(x.equipmentTypeCode))
.Select((x, y) => new { y.status })
.ToList();
po.eqRunCount = eqInfo.Where(i => i.status == EquipmentStatus.Running.ToString()).ToList().Count;//在线数
po.eqStandByCount = eqInfo.Where(i => i.status == EquipmentStatus.Free.ToString()).ToList().Count;//待机数
po.eqErrorCount = eqInfo.Where(i => i.status == EquipmentStatus.Failure.ToString() || i.status == EquipmentStatus.Error.ToString()).ToList().Count;//故障数
po.eqOffLineCount = po.eqSumCount - po.eqRunCount - po.eqStandByCount - po.eqErrorCount;
//设备运行(当天):故障时间、运行时间、待机时间、在线率
decimal d = 0;
if (po.eqSumCount > 0)
{
d = (decimal)(po.eqRunCount + po.eqStandByCount) / po.eqSumCount;//在线数÷总设备数
}
string onlineRate = string.Format("{0:P2}", Math.Round(d, 4));//在线率
po.runningRate = onlineRate == "0.00%" ? "0%" : onlineRate;
result.Result = po;
return result;
}, catchRetrunValue: "list");
}
/// <summary>
/// 设备在线状况
/// </summary>
/// <param name="projectKeys"></param>
/// <returns></returns>
public dynamic GetEqInfoByProjectKeys(Guid projectKeys)
{
return ExceptionsHelp.Instance.ExecuteT(() =>
{
var result = new Response();
var stringBuilder = new StringBuilder();
stringBuilder.AppendLine($@"SELECT t1.equipmentCode, t1.equipmentName, t2.Status, t2.UpdateTime,
|
赖素文
authored
|
438
439
|
seconds = ISNULL(DATEDIFF(second, t2.UpdateTime, GETDATE()), -1)
FROM base_equipment t1
|
赖素文
authored
|
440
|
LEFT JOIN daq_equipment_status_record t2 WITH(NOLOCK) ON t1.otherCode = t2.EquipmentCode");
|
唐召明
authored
|
441
|
stringBuilder.AppendFormat("WHERE t1.projectKeys = '{0}' ", projectKeys);
|
赖素文
authored
|
442
|
|
赖素文
authored
|
443
444
445
|
string notInCondition = $"AND t1.equipmentTypeCode NOT IN ('{SystemVariable.IotNotContainDevice}')";
stringBuilder.AppendLine(notInCondition);
|
赖素文
authored
|
446
|
stringBuilder.AppendLine(" order by t1.equipmentTypeCode, t2.Status ");
|
唐召明
authored
|
447
|
|
赖素文
authored
|
448
449
|
string sqlQuery = stringBuilder.ToString();
|
唐召明
authored
|
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
|
var dt = base.Context.Ado.GetDataTable(stringBuilder.ToString());
result.Result = dt;
result.Count = dt == null ? 0 : dt.Rows.Count;
return result;
}, catchRetrunValue: "list");
}
/// <summary>
/// 设备故障总时间、设备故障发生次数、设备超40分钟报警
/// </summary>
/// <param name="projectKeys"></param>
/// <returns></returns>
public dynamic GetFailureList(Guid projectKeys)
{
return ExceptionsHelp.Instance.ExecuteT(() =>
{
var result = new Response();
DateTime date = DateTime.Now;
|
赖素文
authored
|
468
|
DateTime startDateTime = date.AddDays(-6).Date;//获取第前7天时间
|
唐召明
authored
|
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
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
520
521
|
DateTime endDateTime = DateTime.Parse(date.ToString("yyyy-MM-dd") + " 23:59:59");//获得当天时间
//计算故障时间
var eqInfo = Context.Queryable<base_equipment>()
.Where(i => i.projectKeys == projectKeys && i.equipmentTypeCode != "StationMonitor")
.Select(i => i.otherCode).ToList();
var alarmsList = Context.Queryable<daq_equipment_alarm_record>()
.Where(i => i.createTime >= startDateTime && i.createTime <= endDateTime && eqInfo.Contains(i.equipmentCode))
.ToList();
var alarmInfo = alarmsList.GroupBy(i => new
{
Date = i.createTime.Date.ToString("yyyy-MM-dd"),//按(年月日)日期排序
i.equipmentCode,
i.equipmentName
}).ToList();
//计算故障总时间
var failureTime = alarmInfo.Select(i => new
{
i.Key.equipmentCode,
i.Key.equipmentName,
Date = i.Key.Date,
sumTime = i.Sum(t => t.errorduration)
}).ToList();
//计算故障次数
var failureCount = alarmsList.GroupBy(i => new
{
i.equipmentCode,
i.equipmentName
}).Select(i => new
{
i.Key.equipmentCode,
i.Key.equipmentName,
count = i.Count()
})
.OrderByDescending(i => i.count)
.ToList();
//计算设备故障(超40分钟)
var min40 = 40 * 60;
var failureOverFourty = alarmsList.Where(i => i.errorduration > min40)
.OrderByDescending(x => x.equipmentCode).ThenByDescending(x => x.errorduration)
.Select(i => new
{
i.equipmentCode,
i.equipmentName,
i.alarmMessage,
i.createTime,
i.updateTime,
i.errorduration,
i.handleTime,
i.remark
})
.ToList();
|
赖素文
authored
|
522
523
|
result.Result = new
|
唐召明
authored
|
524
525
526
527
528
529
530
531
532
|
{
failureTime = failureTime,//设备故障总时间 每台设备妥善率
failureCount = failureCount,//故障发生次数
failureOverFourty = failureOverFourty//超40分钟报警
};
return result;
}, catchRetrunValue: "list");
}
|
|
533
|
|
|
534
535
536
537
538
539
540
541
542
543
544
|
/// <summary>
/// 获取近七天设备开机时间
/// </summary>
/// <param name="projectKeys"></param>
/// <returns></returns>
public dynamic GetEqPowerOnTime(Guid projectKeys)
{
return ExceptionsHelp.Instance.ExecuteT(() =>
{
var response = new Response();
|
唐召明
authored
|
545
546
547
548
|
var date = DateTime.Now;
var startDateTime = date.AddDays(-6);
var endDateTime = DateTime.Parse(date.ToString("yyyy-MM-dd") + " 23:59:59");
var eqList = Context.Queryable<base_equipment>().Where(x => x.projectKeys == projectKeys && !SystemVariable.IotNotContainDevice.Contains(x.equipmentTypeCode))
|
赖素文
authored
|
549
550
|
.Select(x => new { x.equipmentCode, x.equipmentName })
.ToList();
|
唐召明
authored
|
551
|
if (eqList.Count == 0) return response.ResponseError(SystemVariable.queryNotData);
|
赖素文
authored
|
552
|
|
唐召明
authored
|
553
554
555
|
//设备编码集合
var equipmentCodeList = eqList.Select(t => t.equipmentCode).ToList();
var eqRecordHistoryList = Context.Queryable<daq_equipment_status_record_history>()
|
赖素文
authored
|
556
557
|
.Where(x => equipmentCodeList.Contains(x.equipmentCode) && x.createTime >= startDateTime
&& x.createTime <= endDateTime)
|
唐召明
authored
|
558
|
.Select(x => new { x.equipmentCode, x.createTime, x.statusDuration })
|
赖素文
authored
|
559
|
.ToList();
|
赖素文
authored
|
560
|
|
唐召明
authored
|
561
562
563
564
565
566
567
568
569
570
|
var result = new List<ProjectPowerOnDto>();
foreach (var e in eqList)
{
for (int i = -6; i <= 0; i++)
{
var d = new ProjectPowerOnDto();
d.EquipmentCode = e.equipmentCode;
d.EquipmentName = e.equipmentName;
d.Date = date.AddDays(i).Date;
DateTime endTime = DateTime.Parse(d.Date.ToString("yyyy-MM-dd") + " 23:59:59");
|
赖素文
authored
|
571
572
573
|
var PowerOnTime = eqRecordHistoryList.Where(x => x.equipmentCode == e.equipmentCode && x.createTime >= d.Date && x.createTime <= endTime)
.Sum(x => x.statusDuration);
|
唐召明
authored
|
574
|
d.PowerOnTime = Math.Round(TimeSpan.FromSeconds(PowerOnTime).TotalHours, 2);
|
赖素文
authored
|
575
|
|
赖素文
authored
|
576
577
|
//d.PowerOnTimeSeconds = TimeSpan.FromSeconds(PowerOnTime).TotalSeconds;
result.Add(d);
|
唐召明
authored
|
578
|
}
|
赖素文
authored
|
579
|
|
唐召明
authored
|
580
581
|
}
response.Result = result;
|
|
582
583
584
|
return response;
});
}
|
唐召明
authored
|
585
|
#endregion
|
赖素文
authored
|
586
587
|
|
唐召明
authored
|
588
|
}
|
HuXiYu
authored
|
589
|
}
|