EquipmentStatusRecordService.cs
10.6 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
using HHECS.Dal.Repository;
using HHECS.Infrastructure.CommonHelper;
using HHECS.Model.Entities;
using HHECS.Model.Enums;
using System;
using System.Linq;
using System.Collections.Generic;
using System.Linq.Expressions;
using HHECS.Model.Dtos;
using HHECS.Infrastructure.Json;
namespace HHECS.Application.Service
{
public class EquipmentStatusRecordService : BaseService
{
public string Load(EquipmentStatusRecord entity, int page, int limit)
{
return Execute(() =>
{
using var warehouseRepository = new WarehouseRepository();
using var equipmentDataRecordRepository = new EquipmentDataRecordRepository();
using var equipmentAlarm = new EquipmentStatusRecordRepository();
var result = equipmentAlarm.Where(GetExpression(entity));
var total = result.Count();
var equipmentStatusList = result.OrderBy(x => x.IsEnd).OrderByDescending(t => t.Created).Page(page, limit).ToList(t => new EquipmentStatusRecordDto());
var equipmentCodeList = equipmentStatusList.Select(t => t.EquipmentCode).ToList();
var equipmentList = equipmentDataRecordRepository.Where(x => equipmentCodeList.Contains(x.Code)).ToList();
var warehouseCodeList = equipmentStatusList.Select(t => t.WarehouseCode).ToList();
var warehouseList = warehouseRepository.Where(x => warehouseCodeList.Contains(x.Code)).ToList();
equipmentStatusList.ForEach(t =>
{
t.Duration = string.Format("{0:dd}天 {0:hh}小时 {0:mm}分 {0:ss}秒", t.Time);
var online = equipmentList.FirstOrDefault(x => x.Code == t.EquipmentCode);
//温度和 震动
t.WalkTemperature = online != null ? online.WalkTemperature.ToString() + "℃" : "0℃";
t.WalkVibrationVelocity = online != null ? Math.Round(online.WalkVibrationVelocity, 2).ToString() + "(mm/s)" : "0(mm/s)";
t.LiftTemperature = online != null ? online.LiftTemperature.ToString() + "℃" : "0℃";
t.LiftVibrationVelocity = online != null ? Math.Round(online.LiftVibrationVelocity, 2).ToString() + "(mm/s)" : "0(mm/s)";
t.runCurrent = online != null ? Math.Round(online.runCurrent, 2).ToString() + "(A)" : "0(A)";
t.liftCurrent = online != null ? Math.Round(online.liftCurrent, 2).ToString() + "(A)" : "0(A)";
var warehouse = (t.WarehouseCode == "LM1") ? warehouseList.FirstOrDefault(x => x.Code == "BM2") : warehouseList.FirstOrDefault(x => x.Code == t.WarehouseCode);
//var warehouse = warehouseRepository.Where(x => x.Code == t.WarehouseCode).First();
t.WarehouseName = warehouse != null ? warehouse.Name : t.WarehouseCode;
});
Response.Result = equipmentStatusList;
Response.Count = total;
return Response.ToJson();
});
}
public string LoadDesc(EquipmentStatusRecord entity, int page, int limit)
{
return Execute(() =>
{
using var equipmentStatusRecordRepository = new EquipmentStatusRecordRepository();
var temps = equipmentStatusRecordRepository.Where(GetExpression(entity)).ToList(x => new EquipmentStatusRecord
{
EquipmentCode = x.EquipmentCode,
EquipmentName = x.EquipmentName,
EquipmentStatus = x.EquipmentStatus,
Created = x.Created,
Updated = x.Updated,
});
var groupQuery = temps.GroupBy(x => new
{
x.EquipmentCode,
x.EquipmentName,
x.EquipmentStatus,
});
var data = groupQuery.OrderBy(x => x.Key.EquipmentCode).Skip((page - 1) * limit).Take(limit).Select(x => new EquipmentStatusRecordDto
{
EquipmentCode = x.Key.EquipmentCode,
EquipmentName = x.Key.EquipmentName,
EquipmentStatus = x.Key.EquipmentStatus,
Count = x.Count(),
Duration = SumTime(x.ToList()),
BeginDateTime = entity.Created,
EndDateTime = entity.Updated,
}).ToList();
Response.Result = data;
Response.Count = groupQuery.Count();
return Response.ToJson();
});
}
public string SumTime(List<EquipmentStatusRecord> equipmentStatusRecords)
{
TimeSpan? time = new TimeSpan();
foreach (var t in equipmentStatusRecords)
{
time += t.Time;
}
return string.Format("{0:dd}天 {0:hh}小时 {0:mm}分 {0:ss}秒", time);
}
private Expression<Func<EquipmentStatusRecord, bool>> GetExpression(EquipmentStatusRecord entity)
{
Expression<Func<EquipmentStatusRecord, bool>> filter = t => true;
//if (!string.IsNullOrWhiteSpace(entity.WarehouseCode))
//{
// if (entity.WarehouseCode == "BM2")
// {
// filter = filter.And(x => x.WarehouseCode.Equals("BM2") || x.WarehouseCode.Equals("LM1"));
// }
// else
// {
// filter = filter.And(x => x.WarehouseCode.Equals(entity.WarehouseCode));
// }
//}
if (!string.IsNullOrWhiteSpace(entity.EquipmentCode))
{
filter = filter.And(x => x.EquipmentCode.Equals(entity.EquipmentCode));
}
if (!string.IsNullOrWhiteSpace(entity.EquipmentName))
{
filter = filter.And(x => x.EquipmentName.Contains(entity.EquipmentName));
}
if (!string.IsNullOrWhiteSpace(entity.WarehouseCodeExt))
{
filter = filter.And(x => entity.WarehouseCodeExt.IndexOf(x.EquipmentCode) >= 0);
}
filter = filter.And(x => !x.EquipmentStatus.Equals("OutLine"));
if (!string.IsNullOrWhiteSpace(entity.EquipmentStatus))
{
filter = filter.And(x => x.EquipmentStatus.Equals(entity.EquipmentStatus));
}
if (entity.Created != null && entity.Updated != null)
{
//结束时间在转换过程中精度不一致,这里需要增加1秒
filter = filter.And(t => t.Created >= entity.Created && t.Updated < entity.Updated.Value.AddSeconds(1));
}
if (entity.Created != null && entity.Updated == null)
{
filter = filter.And(t => t.Created >= entity.Created);
}
if (entity.Updated != null && entity.Created == null)
{
//结束时间在转换过程中精度不一致,这里需要增加1秒
filter = filter.And(t => t.Updated < entity.Updated.Value.AddSeconds(1));
}
return filter;
}
public Dictionary<string, string> GetEquipmentStatusType()
{
var result = new Dictionary<string, string>
{
{ "全部", "" }
};
foreach (var item in EnumHelper.EnumListDicString<EquipmentStatusRecordStatus>())
{
result.Add(item.Key, item.Value.ToString());
}
return result;
}
public string Export(EquipmentStatusRecord entity)
{
return Execute(() =>
{
using var equipmentStatusRecordRepository = new EquipmentStatusRecordRepository();
var result = equipmentStatusRecordRepository.Where(GetExpression(entity)).OrderByDescending(a => a.Created).ToList(t => new EquipmentStatusRecordDto());
result.ForEach(t =>
{
t.Duration = string.Format("{0:dd}天 {0:hh}小时 {0:mm}分 {0:ss}秒", t.Time);
});
Response.Result = result;
Response.Count = result.Count;
return Response.ToJson();
});
}
public string ExportDesc(EquipmentStatusRecord entity)
{
return Execute(() =>
{
using var equipmentStatusRecordRepository = new EquipmentStatusRecordRepository();
var result = equipmentStatusRecordRepository.Where(GetExpression(entity)).OrderByDescending(a => a.Created).ToList(x => new EquipmentStatusRecord
{
EquipmentCode = x.EquipmentCode,
EquipmentName = x.EquipmentName,
EquipmentStatus = x.EquipmentStatus,
Created = x.Created,
Updated = x.Updated,
});
var data = result.GroupBy(a => new { a.EquipmentCode, a.EquipmentName, a.EquipmentStatus }).Select(x => new EquipmentStatusRecordDto
{
EquipmentCode = x.Key.EquipmentCode,
EquipmentName = x.Key.EquipmentName,
EquipmentStatus = x.Key.EquipmentStatus,
Count = x.Count(),
Duration = SumTime(x.ToList()),
BeginDateTime = entity.Created,
EndDateTime = entity.Updated
}).ToList();
Response.Result = data;
Response.Count = data.Count;
return Response.ToJson();
});
}
/// <summary>
/// 获取设备状态
/// </summary>
/// <param name="equipmentCode">设备编码</param>
/// <returns></returns>
public EquipmentStatusRecord GetEquipmentStatusByCode(string equipmentCode)
{
using var statusRecordRepository = new EquipmentStatusRecordRepository();
return statusRecordRepository.Where(x => x.EquipmentCode.Equals(equipmentCode)).OrderBy(t => t.IsEnd).OrderByDescending(x => x.Created).First();
}
/// <summary>
/// 获取设备状态
/// </summary>
/// <param name="equipmentCode">设备编码</param>
/// <returns></returns>
public List<EquipmentStatusRecord> GetEquipmentHistoryStatusByCode(string equipmentCode)
{
using EquipmentStatusRecordRepository statusRecordRepository = new EquipmentStatusRecordRepository();
return statusRecordRepository.Where(x => x.EquipmentCode.Equals(equipmentCode)).ToList();
}
}
}