EquipmentVM.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
335
336
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using HandyControl.Data;
using HHECS.BllModel;
using HHECS.DAQClient.Model;
using HHECS.DAQClient.Services;
using HHECS.DAQClient.View.EquipmentPropView;
using HHECS.DAQClient.View.EquipmentView;
using HHECS.DAQShared.Models;
using LinqKit;
using System.Collections;
using System.Collections.ObjectModel;
using System.Configuration;
using System.Linq.Expressions;
using System.Windows;
using MessageBox = HandyControl.Controls.MessageBox;
namespace HHECS.DAQClient.ViewModel.EquipmentVM
{
public partial class EquipmentVM : ObservableObject
{
[ObservableProperty]
private string equipmentCode;
[ObservableProperty]
private string equipmentName;
[ObservableProperty]
private Dictionary<int, string> equipmentTypes = new Dictionary<int, string>();
[ObservableProperty]
private string destinationArea;
[ObservableProperty]
private int equipmentTypeId = 0;
[ObservableProperty]
private int pageIndex = 1;
[ObservableProperty]
private long maxPage;
[ObservableProperty]
private int pageSize = 30;
[ObservableProperty]
private ObservableCollection<Equipment> equipments = new ObservableCollection<Equipment>();
private readonly IFreeSql _freeSql;
private readonly CenterService _centerService;
private readonly HttpService _httpService;
public EquipmentVM(IFreeSql freeSql, CenterService centerService, HttpService httpService)
{
_freeSql = freeSql;
_centerService = centerService;
_httpService = httpService;
InitialData();
LoadData();
}
private void InitialData()
{
try
{
var result = _freeSql.Queryable<EquipmentType>().ToList();
var keyValuePairs = new Dictionary<int, string>
{
{ 0,"全部" }
};
foreach (var item in result)
{
keyValuePairs.Add(item.Id, item.Name);
}
EquipmentTypes = keyValuePairs;
DestinationArea = ConfigurationManager.AppSettings["Area"];
}
catch (Exception ex)
{
MessageBox.Error($"[{nameof(EquipmentVM)}]加载数据失败.{ex.Message}");
}
}
[RelayCommand]
public void Serach()
{
var result = LoadData();
if (!result.Success)
{
MessageBox.Error($"[{nameof(EquipmentVM)}]加载数据失败.{result.Msg}");
}
}
[RelayCommand]
public void Add()
{
var view = new EquipmentAddOrEditView();
var result = view.ShowDialog();
if (result == true)
{
LoadData();
}
}
[RelayCommand]
public void Edit(int equipmentId)
{
var view = new EquipmentAddOrEditView(equipmentId);
var result = view.ShowDialog();
if (result == true)
{
LoadData();
}
}
[RelayCommand]
public void Sync()
{
try
{
if (_centerService.IsStart)
{
MessageBox.Warning($"停止采集数据后才能进行数据同步操作!");
return;
}
var askResult = MessageBox.Ask("当前操作会获取服务端最新配置,并覆盖本地数据,请谨慎操作,确认继续?", "数据同步");
if (askResult != MessageBoxResult.OK)
{
return;
}
_ = Guid.TryParse(ConfigurationManager.AppSettings["ClientId"], out var clientId);
if (clientId == default)
{
MessageBox.Warning($"当前客户端未配置“ClientId”,请检查配置信息后再试!", "同步失败");
return;
}
var result = _httpService.GetClientEquipmentData(clientId);
if (!result.Success)
{
MessageBox.Error($"同步数据失败:{result.Msg}");
return;
}
//事务操作
_freeSql.Transaction(() =>
{
//清空本地数据
_freeSql.Delete<EquipmentType>().Where(x => true).ExecuteAffrows();
_freeSql.Delete<EquipmentTypePropTemplate>().Where(x => true).ExecuteAffrows();
_freeSql.Delete<Equipment>().Where(x => true).ExecuteAffrows();
_freeSql.Delete<EquipmentProp>().Where(x => true).ExecuteAffrows();
//同步设备类型
foreach (var equipmentType in result.Data)
{
equipmentType.Id = 0;
equipmentType.Created = DateTime.Now;
equipmentType.Id = (int)_freeSql.Insert(equipmentType).ExecuteIdentity();
//同步设备模板
var equipmentTypePropTemplates = new List<EquipmentTypePropTemplate>();
foreach (var equipmentTypePropTemplate in equipmentType.EquipmentTypePropTemplates)
{
equipmentTypePropTemplate.Id = 0;
equipmentTypePropTemplate.EquipmentTypeId = equipmentType.Id;
equipmentTypePropTemplate.Created = DateTime.Now;
equipmentTypePropTemplates.Add(equipmentTypePropTemplate);
}
_freeSql.Insert(equipmentTypePropTemplates).ExecuteAffrows();
//同步设备
foreach (var equipment in equipmentType.Equipments)
{
equipment.Id = 0;
equipment.EquipmentTypeId = equipmentType.Id;
equipment.Created = DateTime.Now;
equipment.Id = (int)_freeSql.Insert(equipment).ExecuteIdentity();
var equipmentProps = new List<EquipmentProp>();
foreach (var equipmentProp in equipment.EquipmentProps)
{
equipmentProp.Id = 0;
equipmentProp.EquipmentId = equipment.Id;
equipmentProp.Value = string.Empty;
equipmentProp.Created = DateTime.Now;
equipmentProps.Add(equipmentProp);
}
_freeSql.Insert(equipmentProps).ExecuteAffrows();
}
//新增默认连接对象
var equipmentIps = equipmentType.Equipments.Select(x => x.IP).Distinct().ToList();
var communicationConfigs = new List<CommunicationConfig>();
foreach (var ipAddress in equipmentIps)
{
if (_freeSql.Queryable<CommunicationConfig>().Where(x => x.IpAddress == ipAddress).Any())
{
continue;
}
var communicationConfig = new CommunicationConfig
{
Code = Guid.NewGuid().ToString()[..8],
Name = Guid.NewGuid().ToString()[..8],
CommunicationType = CommunicationTypeConst.Siemens_S1500,
Port = 102,
IpAddress = ipAddress,
Disable = false,
CreateTime = DateTime.Now,
};
communicationConfigs.Add(communicationConfig);
}
_freeSql.Insert(communicationConfigs).ExecuteAffrows();
}
});
LoadData();
MessageBox.Success("数据同步成功");
}
catch (Exception ex)
{
MessageBox.Error($"同步设备基础数据失败:{ex.Message}");
}
}
[RelayCommand]
public void Delete(int equipmentId)
{
try
{
var result = MessageBox.Ask($"确认删除数据?");
if (result != MessageBoxResult.OK)
{
return;
}
var propIds = _freeSql.Queryable<EquipmentProp>().Where(x => x.EquipmentId == equipmentId).ToList(x => x.Id);
_freeSql.Delete<Equipment>().Where(x => x.Id == equipmentId).ExecuteAffrows();
_freeSql.Delete<EquipmentProp>().Where(x => propIds.Contains(x.Id)).ExecuteAffrows();
LoadData();
MessageBox.Success("删除数据成功");
}
catch (Exception ex)
{
MessageBox.Error($"删除数据失败.{ex.Message}");
}
}
[RelayCommand]
public void Detail(Equipment equipment)
{
var view = new EquipmentPropMonitor(equipment);
view.ShowDialog();
}
[RelayCommand]
public void BatchDelete(IList sender)
{
try
{
var equipments = sender.Cast<Equipment>().ToList();
if (equipments.Count == 0)
{
MessageBox.Warning("未选中任何数据!");
return;
}
var result = MessageBox.Ask($"确认删除选中数据?");
if (result != MessageBoxResult.OK)
{
return;
}
var equipmentIds = equipments.Select(x => x.Id).ToList();
var equipmentPropIds = _freeSql.Queryable<EquipmentProp>().Where(x => equipmentIds.Contains(x.EquipmentId)).ToList(x => x.Id);
_freeSql.Delete<Equipment>().Where(x => equipmentIds.Contains(x.Id)).ExecuteAffrows();
_freeSql.Delete<EquipmentProp>().Where(x => equipmentPropIds.Contains(x.Id)).ExecuteAffrows();
LoadData();
MessageBox.Success($"成功删除{equipments.Count}条数据");
}
catch (Exception ex)
{
MessageBox.Error($"删除失败.{ex.Message}");
}
}
/// <summary>
/// 页码改变
/// </summary>
[RelayCommand]
private void PageUpdated(FunctionEventArgs<int> info)
{
PageIndex = info.Info;
LoadData();
}
private BllResult LoadData()
{
try
{
var query = _freeSql.Queryable<Equipment>().Where(GetFilter()).Include(x => x.EquipmentType);
var total = query.Count();
MaxPage = total / PageSize + Convert.ToInt32(total % PageSize != 0);
var result = query.Page(PageIndex, PageSize).ToList();
Equipments = new ObservableCollection<Equipment>(result);
return BllResultFactory.Success();
}
catch (Exception ex)
{
return BllResultFactory.Error(ex.Message);
}
}
private Expression<Func<Equipment, bool>> GetFilter()
{
var filter = PredicateBuilder.New<Equipment>(true);
if (!string.IsNullOrWhiteSpace(EquipmentCode))
{
filter = filter.And(x => x.Code.Contains(EquipmentCode));
}
if (!string.IsNullOrWhiteSpace(EquipmentName))
{
filter = filter.And(x => x.Name.Contains(EquipmentName));
}
if (EquipmentTypeId != 0)
{
filter = filter.And(x => x.EquipmentTypeId == EquipmentTypeId);
}
if (!string.IsNullOrWhiteSpace(DestinationArea))
{
filter = filter.And(x => x.DestinationArea == DestinationArea);
}
return filter;
}
}
}