EquipmentController.cs
8.33 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
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.Areas.IOTCloud.Controllers
{
[Area("IOTCloud")]
[Authorize]
public class EquipmentController : Controller
{
private readonly IFreeSql<IOTCloudFlag> _iotCloudFreeSql;
private readonly CommonService _commonService;
private readonly ILogger<EquipmentController> _logger;
public EquipmentController(IFreeSql<IOTCloudFlag> iotCloudFreeSql, CommonService commonService, ILogger<EquipmentController> logger)
{
_iotCloudFreeSql = iotCloudFreeSql;
_commonService = commonService;
_logger = logger;
}
public IActionResult Index()
{
try
{
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 = _iotCloudFreeSql.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;
}
[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 _iotCloudFreeSql.Insert(model).ExecuteAffrowsAsync();
var equipmentId = _iotCloudFreeSql.Queryable<Equipment>().Where(x => x.Code == model.Code).First(x => x.Id);
var equipmetPropTemps = _iotCloudFreeSql.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 _iotCloudFreeSql.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 _iotCloudFreeSql.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 _iotCloudFreeSql.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 _iotCloudFreeSql.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
});
}
}
}
}