EquipmentController.cs
12.4 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
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
334
using HHECS.DAQShared.Models;
using HHECS.DAQWebClient.Data;
using HHECS.DAQWebClient.Dtos;
using HHECS.DAQWebClient.Models;
using HHECS.DAQWebClient.Services;
using HHECS.DAQWebClient.ViewModel;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Linq.Expressions;
namespace HHECS.DAQWebClient.Controllers
{
[Authorize]
public class EquipmentController : Controller
{
private readonly IFreeSql<LocalFlag> _freeSql;
private readonly IFreeSql<IOTCloudFlag> _iotCloudFreeSql;
private readonly CommonService _commonService;
private readonly ILogger<EquipmentController> _logger;
public EquipmentController(IFreeSql<LocalFlag> freeSql, IFreeSql<IOTCloudFlag> iotCloudFreeSql, CommonService commonService, ILogger<EquipmentController> logger)
{
_freeSql = freeSql;
_iotCloudFreeSql = iotCloudFreeSql;
_commonService = commonService;
_logger = logger;
}
public IActionResult Index()
{
try
{
var area = _freeSql.Queryable<LocalConfig>().Where(x => x.Code == ConfigType.Area.ToString()).First(x => x.Value);
var model = new EquipmentQueryModel
{
DestinationArea = area
};
return View(model);
}
catch (Exception)
{
return View();
}
}
[HttpGet]
public async Task<IActionResult> Load(EquipmentQueryModel model, int page, int limit)
{
try
{
var query = _freeSql.Queryable<Equipment>().Where(GetFilter(model));
var result = await query.Skip((page - 1) * limit).Take(limit).ToListAsync();
var total = await query.CountAsync();
return Ok(new ResultDto<List<Equipment>>
{
Code = 0,
Count = total,
Data = result
});
}
catch (Exception ex)
{
return Ok(new ResultDto<List<Equipment>>
{
Code = 500,
Msg = ex.Message
});
}
}
private static Expression<Func<Equipment, bool>> GetFilter(EquipmentQueryModel model)
{
Expression<Func<Equipment, bool>> filter = x => true;
if (!string.IsNullOrWhiteSpace(model.Code))
{
filter = filter.And(x => x.Code.Contains(model.Code));
}
if (!string.IsNullOrWhiteSpace(model.Name))
{
filter = filter.And(x => x.Name.Contains(model.Name));
}
if (model.EquipmentTypeId != 0)
{
filter = filter.And(x => x.EquipmentTypeId == model.EquipmentTypeId);
}
if (!string.IsNullOrWhiteSpace(model.DestinationArea))
{
filter = filter.And(x => x.DestinationArea == model.DestinationArea);
}
return filter;
}
[HttpGet]
public async Task<IActionResult> SyncData()
{
using var uow = _freeSql.CreateUnitOfWork();
try
{
if (_commonService.IsStart)
{
return Ok(new ResultDto
{
Code = 500,
Msg = "服务正在运行,请在菜单栏的控制台页码点击“停止”后,再进行数据同步操作!"
});
}
var clientIdValue = await _freeSql.Queryable<LocalConfig>().Where(x => x.Code == ConfigType.ClientId.ToString()).FirstAsync(x => x.Value);
_ = Guid.TryParse(clientIdValue, out var clientId);
if (clientId == default)
{
return Ok(new ResultDto
{
Code = 500,
Msg = "当前客户端未配置“ClientId”,请检查配置信息后再试!"
});
}
var iotEquipmentTypes = await _iotCloudFreeSql.Queryable<EquipmentType>().ToListAsync();
var iotEquipmentTypeIds = iotEquipmentTypes.Select(x => x.Id).ToList();
var iotEquipmentTypePropTemplates = await _iotCloudFreeSql.Queryable<EquipmentTypePropTemplate>().Where(x => iotEquipmentTypeIds.Contains(x.EquipmentTypeId)).ToListAsync();
var user = HttpContext.User.Identity?.Name ?? string.Empty;
//事务操作
using var equipmentTypeRepository = uow.GetRepository<EquipmentType>();
using var equipmentTypePropTemplateRepository = uow.GetRepository<EquipmentTypePropTemplate>();
//清空本地数据
await equipmentTypePropTemplateRepository.DeleteAsync(x => true);
await equipmentTypeRepository.DeleteAsync(x => true);
//同步设备类型
foreach (var equipmentType in iotEquipmentTypes)
{
var temps = iotEquipmentTypePropTemplates.Where(x => x.EquipmentTypeId == equipmentType.Id).ToList();
equipmentType.Id = 0;
equipmentType.Created = DateTime.Now;
equipmentType.CreatedBy = user;
equipmentType.Updated = null;
equipmentType.UpdatedBy = string.Empty;
equipmentType.Id = (await equipmentTypeRepository.InsertAsync(equipmentType)).Id;
//同步设备模板
var equipmentTypePropTemplates = new List<EquipmentTypePropTemplate>();
foreach (var equipmentTypePropTemplate in temps)
{
equipmentTypePropTemplate.Id = 0;
equipmentTypePropTemplate.EquipmentTypeId = equipmentType.Id;
equipmentTypePropTemplate.Created = DateTime.Now;
equipmentTypePropTemplate.CreatedBy = user;
equipmentTypePropTemplate.Updated = null;
equipmentTypePropTemplate.UpdatedBy = string.Empty;
equipmentTypePropTemplates.Add(equipmentTypePropTemplate);
}
await equipmentTypePropTemplateRepository.InsertAsync(equipmentTypePropTemplates);
}
uow.Commit();
var msg = "数据同步成功";
_logger.LogInformation(msg);
return Ok(new ResultDto
{
Code = 0,
Msg = msg
});
}
catch (Exception ex)
{
uow.Rollback();
var msg = $"数据同步失败:{ex.Message}";
_logger.LogError(msg);
return Ok(new ResultDto
{
Code = 500,
Msg = msg
});
}
}
[HttpPost]
[Authorize(Roles = $"{nameof(RoleType.Admin)}")]
public async Task<IActionResult> Add(Equipment model)
{
try
{
var currentUser = HttpContext.User.Identity?.Name ?? string.Empty;
model.CreatedBy = currentUser;
model.Created = DateTime.Now;
await _freeSql.Insert<Equipment>(model).ExecuteAffrowsAsync();
var equipmentId = _freeSql.Queryable<Equipment>().Where(x => x.Code == model.Code).First(x => x.Id);
var equipmetPropTemps = _freeSql.Queryable<EquipmentTypePropTemplate>().Where(x => x.EquipmentTypeId == model.EquipmentTypeId).ToList(x => new EquipmentProp
{
EquipmentId = equipmentId,
Code = x.Code,
Name = x.Name,
Address = x.Address,
PropType = x.PropType,
DataType = x.DataType,
MonitorCompareValue = x.MonitorCompareValue,
MonitorFailure = x.MonitorFailure,
MonitorNormal = x.MonitorNormal,
Remark = x.Description,
Value = string.Empty,
EquipmentAlarmTypeCode = x.EquipmentAlarmTypeCode,
AcquisitionRate = x.AcquisitionRate,
Created = DateTime.Now,
CreatedBy = currentUser,
});
await _freeSql.Insert(equipmetPropTemps).ExecuteAffrowsAsync();
var msg = "新增成功";
_logger.LogInformation(msg);
return Ok(new ResultDto
{
Code = 0,
Msg = msg
});
}
catch (Exception ex)
{
var msg = $"新增失败:{ex.Message}";
_logger.LogError(msg);
return Ok(new ResultDto
{
Code = 500,
Msg = msg
});
}
}
[HttpPost]
public async Task<IActionResult> UpdateStatus(Equipment model)
{
try
{
var user = HttpContext.User.Identity?.Name ?? string.Empty;
await _freeSql.Update<Equipment>(model.Id).Set(x => new Equipment
{
Disable = model.Disable,
UpdatedBy = user,
Updated = DateTime.Now
}).ExecuteAffrowsAsync();
var msg = $"更新状态成功";
_logger.LogInformation(msg);
return Ok(new ResultDto
{
Code = 0,
Msg = msg
});
}
catch (Exception ex)
{
var msg = $"更新状态失败:{ex.Message}";
_logger.LogError(msg);
return Ok(new ResultDto
{
Code = 500,
Msg = msg
});
}
}
[HttpPost]
[Authorize(Roles = $"{nameof(RoleType.Admin)}")]
public async Task<IActionResult> Update(Equipment model)
{
try
{
var user = HttpContext.User.Identity?.Name ?? string.Empty;
await _freeSql.Update<Equipment>(model.Id).Set(x => new Equipment
{
Code = model.Code,
Name = model.Name,
EquipmentTypeId = model.EquipmentTypeId,
ConnectName = model.ConnectName,
IP = model.IP,
Disable = model.Disable,
FactoryCode = model.FactoryCode,
ProjectCode = model.ProjectCode,
DestinationArea = model.DestinationArea,
Description = model.Description,
UpdatedBy = user,
Updated = DateTime.Now
}).ExecuteAffrowsAsync();
var msg = $"更新成功";
_logger.LogInformation(msg);
return Ok(new ResultDto
{
Code = 0,
Msg = msg
});
}
catch (Exception ex)
{
var msg = $"更新失败:{ex.Message}";
_logger.LogError(msg);
return Ok(new ResultDto
{
Code = 500,
Msg = msg
});
}
}
[HttpPost]
[Authorize(Roles = $"{nameof(RoleType.Admin)}")]
public async Task<IActionResult> Delete(int[] ids)
{
try
{
await _freeSql.Delete<Equipment>().Where(x => ids.Contains(x.Id)).ExecuteAffrowsAsync();
var msg = "删除成功";
_logger.LogInformation(msg);
return Ok(new ResultDto
{
Code = 0,
Msg = msg
});
}
catch (Exception ex)
{
var msg = $"删除失败:{ex.Message}";
_logger.LogError(msg);
return Ok(new ResultDto
{
Code = 500,
Msg = msg
});
}
}
}
}