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
|
using NPOI.POIFS.FileSystem;
|
赖素文
authored
|
18
|
|
HuXiYu
authored
|
19
20
|
namespace Hh.Mes.Service.Configure
{
|
唐召明
authored
|
21
22
|
public class BaseProjectService : RepositorySqlSugar<base_project>
{
|
赖素文
authored
|
23
24
|
#region 项目列表
public dynamic Load(PageReq pageReq, base_project entity)
|
唐召明
authored
|
25
26
27
|
{
return ExceptionsHelp.Instance.ExecuteT(() =>
{
|
赖素文
authored
|
28
29
|
var result = new Response();
var expression = LinqProjectWhere(entity);
|
唐召明
authored
|
30
|
//先组合查询表达式
|
赖素文
authored
|
31
32
|
var query = Context.Queryable<base_project>()
.Where(expression);
|
唐召明
authored
|
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
//Exel为ture就不分页,因为导出的话是全部导出
if (pageReq != null && !entity.Exel)
{
int total = 0;
var list = query.ToOffsetPage(pageReq.page, pageReq.limit, ref total);
result.Result = DataHandle(list);
result.Count = total;
return result;
}
result.Result = DataHandle(query.ToList());
result.Count = result.Result.Count;
return result;
}, catchRetrunValue: "list");
}
private List<base_project> DataHandle(List<base_project> data)
{
var projectKeys = data.Select(item => item.keys).ToList();
|
赖素文
authored
|
52
53
54
55
|
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
|
56
57
|
{
pro.keys,
|
赖素文
authored
|
58
|
upRel.userAccount
|
唐召明
authored
|
59
60
61
62
63
64
65
66
|
})
.ToList();
//给项目 赋值 登入用户、项目关联绑定客户信息
foreach (var item in data)
{
var projectUsers = projectQuery.Where(x => x.keys == item.keys)
.Select(x => x.userAccount)
.ToArray();
|
赖素文
authored
|
67
|
item.projectUsers = string.Join(",", projectUsers);
|
唐召明
authored
|
68
69
70
71
72
|
}
return data;
}
|
赖素文
authored
|
73
|
public Expression<Func<base_project, bool>> LinqProjectWhere(base_project model)
|
唐召明
authored
|
74
|
{
|
赖素文
authored
|
75
|
try
|
唐召明
authored
|
76
|
{
|
赖素文
authored
|
77
78
79
|
var exp = Expressionable.Create<base_project>();
//数据过滤条件
if (!string.IsNullOrWhiteSpace(model.projectName))
|
赖素文
authored
|
80
|
{
|
赖素文
authored
|
81
|
exp.And(x => x.projectName.Contains(model.projectName));
|
赖素文
authored
|
82
|
}
|
赖素文
authored
|
83
|
if (!string.IsNullOrWhiteSpace(model.projectAddress))
|
唐召明
authored
|
84
|
{
|
赖素文
authored
|
85
|
exp.And(x => x.projectAddress.Contains(model.projectAddress));
|
唐召明
authored
|
86
|
}
|
赖素文
authored
|
87
|
if (!string.IsNullOrWhiteSpace(model.projectManager))
|
唐召明
authored
|
88
|
{
|
赖素文
authored
|
89
|
exp.And(x => x.projectManager.Contains(model.projectManager));
|
唐召明
authored
|
90
|
}
|
赖素文
authored
|
91
92
93
|
//非管理员,查询登陆用户绑定客户管理的项目
string user = sysWebUser.Account;
if (user != SystemVariable.DefaultCreated)
|
唐召明
authored
|
94
|
{
|
赖素文
authored
|
95
96
97
98
99
100
101
102
103
104
105
|
var projectKeys = Context.Queryable<sys_user_project_rel>().Where(x => x.userAccount == user)
.Select(x => x.clientKeys)
.ToList();
if (projectKeys.Count > 0) exp.And(x => projectKeys.Contains(x.keys));
}
return exp.ToExpression();//拼接表达式
}
catch (Exception ex)
{
throw new Exception($"{ex.Message}");
}
|
唐召明
authored
|
106
|
}
|
赖素文
authored
|
107
|
#endregion
|
赖素文
authored
|
108
109
|
public dynamic Ins(base_project entity)
|
唐召明
authored
|
110
111
112
113
114
115
116
117
118
|
{
return ExceptionsHelp.Instance.ExecuteT(() =>
{
var response = new Response();
var projectKey = Guid.NewGuid();
entity.keys = projectKey;
entity.createBy = sysWebUser.Account;
entity.createTime = DateTime.Now;
|
赖素文
authored
|
119
120
121
122
123
124
|
if (entity.projectClientInfoKeys!=Guid.Empty)
{
var clientName= Context.Queryable<base_project_client_info>().First(x => x.keys == entity.projectClientInfoKeys).clientName;
entity.projectClientName = clientName;
}
|
唐召明
authored
|
125
126
127
128
129
|
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
|
130
|
var factory = new base_factory
|
唐召明
authored
|
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
{
keys = Guid.NewGuid(),
factoryCode = factoryCode,
factoryName = entity.projectName + "默认一厂",
createBy = sysWebUser.Account,
createTime = DateTime.Now,
projectKeys = projectKey
};
Context.Insertable(factory).AddQueue();
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
|
154
155
156
157
158
|
if (entity.projectClientInfoKeys != Guid.Empty)
{
var clientName = Context.Queryable<base_project_client_info>().First(x => x.keys == entity.projectClientInfoKeys).clientName;
entity.projectClientName = clientName;
}
|
唐召明
authored
|
159
160
161
162
163
164
165
166
167
168
169
170
|
response.Status = Update(entity);
if (!response.Status) response.Message = SystemVariable.dataActionError;
return response;
});
}
public dynamic DelByIds(Guid[] keysList)
{
return ExceptionsHelp.Instance.ExecuteT(() =>
{
var response = new Response();
//1.1删除项目需要判断绑定客户
|
赖素文
authored
|
171
|
var isBindClient = Context.Queryable<sys_user_project_rel>().Any(x => keysList.Contains(x.clientKeys));
|
唐召明
authored
|
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
|
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();
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
|
197
198
|
#region 项目地图 首页
public dynamic GetProjectMapList()
|
唐召明
authored
|
199
|
{
|
赖素文
authored
|
200
|
return ExceptionsHelp.Instance.ExecuteT(() =>
|
唐召明
authored
|
201
|
{
|
赖素文
authored
|
202
203
204
|
var result = new Response();
var expression = GetProjectMapListWhere();
var list = Context.Queryable<base_project, sys_user_project_rel>((pro,upRel) =>
|
赖素文
authored
|
205
|
new JoinQueryInfos(JoinType.Left, pro.keys == upRel.clientKeys ))
|
赖素文
authored
|
206
207
208
209
210
211
|
.Where(expression)
.ToList();
decimal sumEqCount = 0;
decimal sumEqRunCount = 0;
decimal sumEqFreeCountCount = 0;
|
唐召明
authored
|
212
|
|
赖素文
authored
|
213
214
215
216
217
218
219
220
221
222
223
224
|
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
|
225
|
{
|
赖素文
authored
|
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
|
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;
sumEqCount += i.eqCount;//关联项目 总设备数量
sumEqRunCount += i.eqRunCount;
sumEqFreeCountCount += i.eqFreeCount;
//查询每个项目下是否有报警未处理和是否有待保养
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
|
246
|
}
|
赖素文
authored
|
247
248
249
|
//返回 项目数据、总设备数、在线率、是否有报警、是否有待保养
string onlineRate = "0%";
if (sumEqCount > 0)
|
唐召明
authored
|
250
|
{
|
赖素文
authored
|
251
252
|
decimal onlineRateFormatted = Math.Round((sumEqRunCount + sumEqFreeCountCount) / sumEqCount, 2);
onlineRate = (onlineRateFormatted * 100m).ToString("0.##") + "%";
|
唐召明
authored
|
253
|
}
|
赖素文
authored
|
254
|
result.Result = new
|
唐召明
authored
|
255
|
{
|
赖素文
authored
|
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
|
list = list,
sumEqCount = sumEqCount,
onlineRate = onlineRate
};
return result;
});
}
public Expression<Func<base_project, sys_user_project_rel, bool>> GetProjectMapListWhere()
{
try
{
var exp = Expressionable.Create<base_project, sys_user_project_rel>();
//非管理员,查询登陆用户绑定客户管理的项目
string currentUser = sysWebUser.Account;
if (currentUser != SystemVariable.DefaultCreated)
|
唐召明
authored
|
272
|
{
|
赖素文
authored
|
273
|
exp.And((pro, upRel) => upRel.userAccount == currentUser);
|
唐召明
authored
|
274
275
276
277
278
279
280
|
}
return exp.ToExpression();//拼接表达式
}
catch (Exception ex)
{
throw new Exception($"{ex.Message}");
}
|
赖素文
authored
|
281
282
|
}
#endregion
|
唐召明
authored
|
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
|
#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
|
334
335
|
seconds = ISNULL(DATEDIFF(second, t2.UpdateTime, GETDATE()), -1)
FROM base_equipment t1
|
赖素文
authored
|
336
|
LEFT JOIN daq_equipment_status_record t2 WITH(NOLOCK) ON t1.otherCode = t2.EquipmentCode");
|
唐召明
authored
|
337
|
stringBuilder.AppendFormat("WHERE t1.projectKeys = '{0}' ", projectKeys);
|
赖素文
authored
|
338
|
|
赖素文
authored
|
339
340
341
|
string notInCondition = $"AND t1.equipmentTypeCode NOT IN ('{SystemVariable.IotNotContainDevice}')";
stringBuilder.AppendLine(notInCondition);
|
赖素文
authored
|
342
|
stringBuilder.AppendLine(" order by t1.equipmentTypeCode, t2.Status ");
|
唐召明
authored
|
343
|
|
赖素文
authored
|
344
345
|
string sqlQuery = stringBuilder.ToString();
|
唐召明
authored
|
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
|
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
|
364
|
DateTime startDateTime = date.AddDays(-6).Date;//获取第前7天时间
|
唐召明
authored
|
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
|
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
|
418
419
|
result.Result = new
|
唐召明
authored
|
420
421
422
423
424
425
426
427
428
|
{
failureTime = failureTime,//设备故障总时间 每台设备妥善率
failureCount = failureCount,//故障发生次数
failureOverFourty = failureOverFourty//超40分钟报警
};
return result;
}, catchRetrunValue: "list");
}
|
|
429
|
|
|
430
431
432
433
434
435
436
437
438
439
440
|
/// <summary>
/// 获取近七天设备开机时间
/// </summary>
/// <param name="projectKeys"></param>
/// <returns></returns>
public dynamic GetEqPowerOnTime(Guid projectKeys)
{
return ExceptionsHelp.Instance.ExecuteT(() =>
{
var response = new Response();
|
赖素文
authored
|
441
442
443
444
445
446
447
448
449
450
451
452
453
|
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))
.Select(x => new { x.equipmentCode, x.equipmentName })
.ToList();
if (eqList.Count==0) return response.ResponseError(SystemVariable.queryNotData);
//设备编码集合
var equipmentCodeList = eqList.Select(t => t.equipmentCode).ToList();
var eqRecordHistoryList = Context.Queryable<daq_equipment_status_record_history>()
.Where(x => equipmentCodeList.Contains(x.equipmentCode) && x.createTime >= startDateTime
&& x.createTime <= endDateTime)
|
赖素文
authored
|
454
455
|
.Select(x => new { x.equipmentCode, x.createTime,x.statusDuration })
.ToList();
|
赖素文
authored
|
456
457
458
459
460
461
|
var result = new List<ProjectPowerOnDto>();
foreach (var e in eqList)
{
for (int i = -6; i <= 0; i++)
{
|
赖素文
authored
|
462
|
var d = new ProjectPowerOnDto();
|
赖素文
authored
|
463
464
465
466
467
468
469
|
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");
var PowerOnTime = eqRecordHistoryList.Where(x => x.equipmentCode == e.equipmentCode && x.createTime >= d.Date && x.createTime <= endTime)
.Sum(x => x.statusDuration);
|
赖素文
authored
|
470
|
d.PowerOnTime = Math.Round(TimeSpan.FromSeconds(PowerOnTime).TotalHours, 2);
|
赖素文
authored
|
471
|
|
赖素文
authored
|
472
473
|
//d.PowerOnTimeSeconds = TimeSpan.FromSeconds(PowerOnTime).TotalSeconds;
result.Add(d);
|
赖素文
authored
|
474
475
476
477
|
}
}
response.Result = result;
|
|
478
479
480
|
return response;
});
}
|
唐召明
authored
|
481
482
|
#endregion
}
|
HuXiYu
authored
|
483
|
}
|