EquipmentVM.cs
17.2 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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
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
418
419
420
421
using AutoMapper;
using HHECS.Application.Enums;
using HHECS.BllModel;
using HHECS.Infrastructure.Excel;
using HHECS.Model.Entities;
using HHECS.Model.ExcelModels;
using HHECS.WinCommon.ViewModel;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using MessageBox = HandyControl.Controls.MessageBox;
namespace HHECS.WinClient.View.EquipmentInfo
{
public class EquipmentVM : VMBase
{
#region 属性
#region Equipment
public string Code { get; set; }
public string Name { get; set; }
public int TypeId { get; set; }
public string IP { get; set; }
public string DestinationArea { get; set; }
public List<Equipment> Equipments { get; set; }
public PageInfo PageInfo { get; set; } = new PageInfo();
public Dictionary<string, object> EquipmentTypes { get; set; }
public bool IsTabMainSelected { get; set; } = true;
#endregion
#region EquipmentProp
public string PropCode { get; set; }
public string Adress { get; set; }
public Equipment CurrentEquipment { get; set; }
public List<EquipmentProp> EquipmentProps { get; set; }
public bool IsTabDetailSelected { get; set; } = false;
#endregion
#endregion
public EquipmentVM()
{
var result = App.EquipmentService.GetEquipmentTypes(t => true);
if (result.Success)
{
var types = result.Data;
types.Insert(0, new EquipmentType() { Id = 0, Name = "全部" });
EquipmentTypes = types.ToDictionary(t => t.Name, x => (object)x.Id);
}
}
#region 方法
#region Equipment
public void Query()
{
Expression<Func<Equipment, bool>> filter = t => true;
if (!String.IsNullOrWhiteSpace(Code))
{
filter = filter.And(t => t.Code.Contains(Code));
}
if (!String.IsNullOrWhiteSpace(Name))
{
filter = filter.And(t => t.Name.Contains(Name));
}
if (TypeId != 0)
{
filter = filter.And(t => t.EquipmentTypeId == TypeId);
}
if (!String.IsNullOrWhiteSpace(IP))
{
filter = filter.And(t => t.IP.Contains(IP));
}
var result = App.EquipmentService.GetEquipments(filter, PageInfo.PageIndex, PageInfo.PageSize, out long totalCount);
if (result.Success)
{
PageInfo.TotalCount = totalCount;
Equipments = result.Data;
}
else
{
MessageBox.Show($"查询失败:{result.Msg}");
}
}
public void New()
{
WinEquipmentAddOrEdit win = new WinEquipmentAddOrEdit(0);
win.ShowDialog();
Query();
}
public void ItemCopy(Equipment equipment)
{
if (equipment == null)
{
MessageBox.Show("请选中一条数据");
return;
}
var result = App.EquipmentService.CopyEquipment(equipment.Id, App.User.UserCode);
if (result.Success)
{
App.LogService.LogOperation(Title.EquipmentCopy, ModuleConst.Equipment, $"设备复制成功.数据:{JsonConvert.SerializeObject(new { Code = equipment.Code + 2, Name = equipment.Name })}", result.Code.ToString(), App.User.UserCode);
MessageBox.Show("复制成功");
}
else
{
App.LogService.LogOperation(Title.EquipmentCopy, ModuleConst.Equipment, $"设备复制失败.数据:{JsonConvert.SerializeObject(new { Code = equipment.Code + 2, Name = equipment.Name })}.详情:{result.Msg}", result.Code.ToString(), App.User.UserCode);
MessageBox.Show($"复制失败:{result.Msg}");
}
Query();
}
public void Edit(Equipment equipment)
{
if (equipment == null)
{
MessageBox.Show("请选中一条数据");
return;
}
WinEquipmentAddOrEdit win = new WinEquipmentAddOrEdit(equipment.Id);
win.ShowDialog();
Query();
}
public void Delete(List<Equipment> equipments)
{
if (equipments == null || equipments.Count == 0)
{
MessageBox.Show("未选中数据");
return;
}
if (MessageBox.Show("是否确认删除?", "警告", MessageBoxButton.YesNo) != MessageBoxResult.Yes)
{
return;
}
var bllResult = App.EquipmentService.DeleteEquipments(equipments);
if (bllResult.Success)
{
App.LogService.LogOperation(Title.EquipmentDelete, ModuleConst.Equipment, $"设备删除成功.数据:{JsonConvert.SerializeObject(equipments.Select(t => new { Id = t.Id, Code = t.Code, Name = t.Name }))}", bllResult.Code.ToString(), App.User.UserCode);
MessageBox.Show("删除成功");
}
else
{
App.LogService.LogOperation(Title.EquipmentDelete, ModuleConst.Equipment, $"设备删除失败.数据:{JsonConvert.SerializeObject(equipments.Select(t => new { Id = t.Id, Code = t.Code, Name = t.Name }))}.详情:{bllResult.Msg}", bllResult.Code.ToString(), App.User.UserCode);
MessageBox.Error($"删除失败:{bllResult.Msg}");
}
Query();
}
public async void Export()
{
try
{
if (MessageBox.Show("是否确认导出?", "注意", MessageBoxButton.YesNo) != MessageBoxResult.Yes)
{
return;
}
Expression<Func<Equipment, bool>> filter = t => true;
var result = App.EquipmentService.GetEquipments(filter);
if (result.Success)
{
var equipments = result.Data;
var config = new MapperConfiguration(cfg => cfg.CreateMap<Equipment, EquipmentExcelModel>());
var mapper = config.CreateMapper();
List<EquipmentExcelModel> models = new List<EquipmentExcelModel>();
equipments.ForEach(t =>
{
var temp = mapper.Map<EquipmentExcelModel>(t);
models.Add(temp);
});
string TimeTxt = DateTime.Now.ToString("yyyy年MM月dd日HH点mm分ss秒");
await NPIOHelper.ExportDTtoExcelAsync<EquipmentExcelModel>(models, "设备", string.Format($"{App.ExportPath}设备{TimeTxt}.xlsx"), App.ExportPath);
MessageBox.Show($"设备导出成功");
}
else
{
MessageBox.Show($"导出失败:{result.Msg}");
}
}
catch (Exception ex)
{
MessageBox.Show($"导出异常:{ex.Message}");
}
}
#endregion
#region EquipmentProp
public void QueryDetail(Equipment equipment)
{
if (equipment == null)
{
MessageBox.Show("未选中主数据!");
IsTabMainSelected = true;
return;
}
//重新查询一遍主数据,防止同步删除
var result = App.EquipmentService.GetEquipments(t => t.Id == equipment.Id);
if (result.Success && result.Data.Count > 0)
{
CurrentEquipment = result.Data[0];
Expression<Func<EquipmentProp, bool>> filter = t => t.EquipmentId == equipment.Id;
if (!String.IsNullOrWhiteSpace(PropCode))
{
filter = filter.And(t => t.EquipmentTypePropTemplateCode.Contains(PropCode));
}
if (!String.IsNullOrWhiteSpace(Adress))
{
filter = filter.And(t => t.Address.Contains(Adress));
}
var detailResult = App.EquipmentService.GetEquipmentProps(filter);
if (detailResult.Success)
{
EquipmentProps = detailResult.Data;
}
else
{
EquipmentProps = null;
MessageBox.Show($"查询当前设备的属性模板失败:{detailResult.Msg}");
}
IsTabDetailSelected = true;
}
else
{
MessageBox.Show($"未能查询到主数据,请刷新");
IsTabMainSelected = true;
}
}
public void SaveDetail()
{
if (EquipmentProps == null || EquipmentProps.Count == 0)
{
MessageBox.Show("无可更新项");
return;
}
var result = App.EquipmentService.SaveEquipmentProps(EquipmentProps, App.User.UserCode);
if (result.Success)
{
App.LogService.LogOperation(Title.EquipmentPropSave, ModuleConst.Equipment, $"Id为{CurrentEquipment.Id}的设备属性保存成功.数据:{JsonConvert.SerializeObject(EquipmentProps.Select(t => new { PropCode = t.EquipmentTypePropTemplateCode, Value = t.Value }))}", result.Code.ToString(), App.User.UserCode);
MessageBox.Show("保存成功");
}
else
{
App.LogService.LogOperation(Title.EquipmentPropSave, ModuleConst.Equipment, $"Id为{CurrentEquipment.Id}的设备属性保存失败.数据:{JsonConvert.SerializeObject(EquipmentProps.Select(t => new { PropCode = t.EquipmentTypePropTemplateCode, Value = t.Value }))}.详情:{result.Msg}", result.Code.ToString(), App.User.UserCode);
MessageBox.Show($"保存失败:{result.Msg}");
}
QueryDetail(CurrentEquipment);
}
public void SyncDetail()
{
if (CurrentEquipment == null)
{
MessageBox.Show("未选中主数据!");
IsTabMainSelected = true;
return;
}
var result = App.EquipmentService.SyncEquipmentProp(CurrentEquipment.Id, App.User.UserCode);
if (result.Success)
{
App.LogService.LogOperation(Title.EquipmentPropSync, ModuleConst.Equipment, $"Id为{CurrentEquipment.Id}的设备属性同步成功.", result.Code.ToString(), App.User.UserCode);
MessageBox.Show("同步成功");
}
else
{
App.LogService.LogOperation(Title.EquipmentPropSync, ModuleConst.Equipment, $"Id为{CurrentEquipment.Id}的设备属性同步失败.详情:{result.Msg}", result.Code.ToString(), App.User.UserCode);
MessageBox.Error($"同步失败:{result.Msg}");
}
QueryDetail(CurrentEquipment);
}
public void DeleteDetail(List<EquipmentProp> equipmentProps)
{
if (equipmentProps == null || equipmentProps.Count == 0)
{
MessageBox.Show("未选中数据");
return;
}
if (MessageBox.Show("是否确认删除?", "警告", MessageBoxButton.YesNo) != MessageBoxResult.Yes)
{
return;
}
var bllResult = App.EquipmentService.DeleteEquipmentProps(equipmentProps);
if (bllResult.Success)
{
App.LogService.LogOperation(Title.EquipmentPropDelete, ModuleConst.Equipment, $"设备属性删除成功.设备Id:{CurrentEquipment.Id},数据:{JsonConvert.SerializeObject(equipmentProps.Select(t => new { Id = t.Id, PropCode = t.EquipmentTypePropTemplateCode, Value = t.Value }))}.", bllResult.Code.ToString(), App.User.UserCode);
MessageBox.Show("删除成功");
}
else
{
App.LogService.LogOperation(Title.EquipmentPropDelete, ModuleConst.Equipment, $"设备属性删除失败.设备Id:{CurrentEquipment.Id},数据:{JsonConvert.SerializeObject(equipmentProps.Select(t => new { Id = t.Id, PropCode = t.EquipmentTypePropTemplateCode, Value = t.Value }))}.详情:{bllResult.Msg}", bllResult.Code.ToString(), App.User.UserCode);
MessageBox.Error($"删除失败:{bllResult.Msg}");
}
QueryDetail(CurrentEquipment);
}
public async void ExportDetail()
{
MessageBoxResult msbResult = MessageBox.Show("是否导出所有设备的属性明细\n\n提示:\n “是”:导出所有设备的属性明细\n “否”:导出当前设备的属性明细\n “取消”:取消导出操作", "注意", MessageBoxButton.YesNoCancel);
bool isAllExport = false;
switch (msbResult)
{
case MessageBoxResult.Yes: isAllExport = true; break;
case MessageBoxResult.No: isAllExport = false; break;
default: return;
}
try
{
BllResult<List<Equipment>> result = null;
if (isAllExport)
{
result = App.EquipmentService.GetEquipments(a => true);
}
else
{
result = App.EquipmentService.GetEquipments(a => a.Id == CurrentEquipment.Id);
}
if (result.Success && result.Data.Count > 0)
{
var equipments = result.Data;
var propsResult = App.EquipmentService.GetEquipmentProps(a => true);
if (propsResult.Success)
{
var props = propsResult.Data;
List<EquipmentPropExcelModel> models = new List<EquipmentPropExcelModel>();
int flag = 1;
int count = 0;
foreach (var equipment in equipments)
{
flag = 1;
foreach (var prop in props)
{
if (equipment.Id == prop.EquipmentId)
{
EquipmentPropExcelModel propmodel = new EquipmentPropExcelModel();
propmodel.EquipmentId = equipment.Id;
propmodel.EquipmentCode = equipment.Code;
propmodel.EquipmentName = equipment.Name;
propmodel.Id = prop.Id;
propmodel.PropCode = prop.EquipmentTypePropTemplateCode;
propmodel.PropName = prop.EquipmentTypePropTemplate.Name;
propmodel.PropType = prop.EquipmentTypePropTemplate.PropType;
propmodel.Address = prop.Address;
propmodel.Value = prop.Value;
propmodel.MonitorFailure = prop.EquipmentTypePropTemplate.MonitorFailure;
propmodel.Remark = prop.Remark;
propmodel.Created = prop.Created;
propmodel.CreatedBy = prop.CreatedBy;
propmodel.Updated = prop.Updated;
propmodel.UpdatedBy = prop.UpdatedBy;
models.Add(propmodel);
flag = 0;
}
}
if (flag == 1)
{
count++;
}
}
//获取实时时间并格式化
string TimeTxt = DateTime.Now.ToString("yyyy年MM月dd日HH点mm分ss秒");
//将数据导出到excel
await NPIOHelper.ExportDTtoExcelAsync<EquipmentPropExcelModel>(models, "设备属性明细", string.Format($"{App.ExportPath}设备属性明细{TimeTxt}.xlsx"), App.ExportPath);
string str = "";
if (count > 0)
{
str += $"\n其中{count}个设备没有属性明细,已跳过";
}
MessageBox.Show($"设备属性明细导出成功" + str);
}
else
{
MessageBox.Show($"获取数据出错:{propsResult.Msg}");
}
}
else
{
MessageBox.Show($"导出失败:未获取到设备主数据.");
}
}
catch (Exception ex)
{
MessageBox.Show($"导出异常:{ex.Message}");
}
}
#endregion
#endregion
}
}