唐召明
authored
|
1
2
3
4
5
|
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using HandyControl.Data;
using HHECS.BllModel;
using HHECS.DAQClient.DataAccess;
|
唐召明
authored
|
6
|
using HHECS.DAQClient.View.EquipmentPropView;
|
唐召明
authored
|
7
8
9
|
using HHECS.DAQClient.View.EquipmentView;
using HHECS.EquipmentModel;
using LinqKit;
|
唐召明
authored
|
10
|
using System.Collections;
|
唐召明
authored
|
11
|
using System.Collections.ObjectModel;
|
唐召明
authored
|
12
|
using System.Configuration;
|
唐召明
authored
|
13
|
using System.Linq.Expressions;
|
唐召明
authored
|
14
|
using System.Windows;
|
唐召明
authored
|
15
16
17
18
19
20
21
22
23
24
25
26
27
|
using MessageBox = HandyControl.Controls.MessageBox;
namespace HHECS.DAQClient.ViewModel.EquipmentVM
{
public partial class EquipmentVM : ObservableObject
{
[ObservableProperty]
private string equipmentCode;
[ObservableProperty]
private string equipmentName;
[ObservableProperty]
|
唐召明
authored
|
28
|
private Dictionary<string, int> equipmentTypes = new Dictionary<string, int>();
|
唐召明
authored
|
29
30
|
[ObservableProperty]
|
唐召明
authored
|
31
32
33
|
private string destinationArea;
[ObservableProperty]
|
唐召明
authored
|
34
35
36
|
private int equipmentTypeId = 0;
[ObservableProperty]
|
唐召明
authored
|
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
private int pageIndex = 1;
[ObservableProperty]
private long maxPage;
[ObservableProperty]
private int pageSize = 30;
[ObservableProperty]
private ObservableCollection<Equipment> equipments = new ObservableCollection<Equipment>();
private readonly DataContext _context;
public EquipmentVM(DataContext context)
{
_context = context;
|
唐召明
authored
|
53
|
InitialData();
|
唐召明
authored
|
54
55
56
|
LoadData();
}
|
唐召明
authored
|
57
58
59
60
61
|
private void InitialData()
{
try
{
var result = _context.EquipmentType.Where(x => true).ToList();
|
唐召明
authored
|
62
63
64
65
66
67
68
69
70
|
var keyValuePairs = new Dictionary<string, int>
{
{ "全部",0 }
};
foreach (var item in result)
{
keyValuePairs.Add(item.Name, item.Id);
}
EquipmentTypes = keyValuePairs;
|
唐召明
authored
|
71
|
DestinationArea = ConfigurationManager.AppSettings["Area"];
|
唐召明
authored
|
72
73
74
75
76
77
78
|
}
catch (Exception ex)
{
MessageBox.Error($"加载数据失败.{ex.Message}");
}
}
|
唐召明
authored
|
79
80
81
82
83
84
85
86
87
88
89
|
[RelayCommand]
public void Serach()
{
var result = LoadData();
if (!result.Success)
{
MessageBox.Error($"加载数据失败.{result.Msg}");
}
}
[RelayCommand]
|
唐召明
authored
|
90
|
public void Add()
|
唐召明
authored
|
91
|
{
|
唐召明
authored
|
92
|
var view = new EquipmentAddOrEditView();
|
唐召明
authored
|
93
94
95
96
97
|
var result = view.ShowDialog();
if (result == true)
{
LoadData();
}
|
唐召明
authored
|
98
99
100
|
}
[RelayCommand]
|
唐召明
authored
|
101
|
public void Edit(int equipmentId)
|
唐召明
authored
|
102
|
{
|
唐召明
authored
|
103
104
105
106
107
108
|
var view = new EquipmentAddOrEditView(equipmentId);
var result = view.ShowDialog();
if (result == true)
{
LoadData();
}
|
唐召明
authored
|
109
110
111
|
}
[RelayCommand]
|
唐召明
authored
|
112
113
114
115
|
public void Delete(int equipmentId)
{
try
{
|
唐召明
authored
|
116
117
118
119
120
|
var result = MessageBox.Ask($"确认删除数据?");
if (result != MessageBoxResult.OK)
{
return;
}
|
唐召明
authored
|
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
_context.Equipment.Remove(new Equipment { Id = equipmentId });
var propIds = _context.EquipmentProp.Where(x => x.EquipmentId == equipmentId).ToList(x => x.Id);
var props = propIds.Select(x => new EquipmentProp { Id = x }).ToList();
_context.EquipmentProp.RemoveRange(props);
_context.SaveChanges();
LoadData();
MessageBox.Success("删除数据成功");
}
catch (Exception ex)
{
MessageBox.Error($"删除数据失败.{ex.Message}");
}
}
[RelayCommand]
|
唐召明
authored
|
136
|
public void Detail(Equipment equipment)
|
唐召明
authored
|
137
|
{
|
唐召明
authored
|
138
|
var view = new EquipmentPropMonitor(equipment);
|
唐召明
authored
|
139
140
141
142
|
view.ShowDialog();
}
[RelayCommand]
|
唐召明
authored
|
143
|
public void BatchDelete(IList sender)
|
唐召明
authored
|
144
|
{
|
唐召明
authored
|
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
|
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 equipmentProps = _context.EquipmentProp.Where(x => equipmentIds.Contains(x.EquipmentId)).ToList(x => new EquipmentProp
{
Id = x.Id
});
_context.Equipment.RemoveRange(equipments);
_context.EquipmentProp.RemoveRange(equipmentProps);
_context.SaveChanges();
LoadData();
MessageBox.Success($"成功删除{equipments.Count}条数据");
}
catch (Exception ex)
|
唐召明
authored
|
173
|
{
|
唐召明
authored
|
174
|
MessageBox.Error($"删除失败.{ex.Message}");
|
唐召明
authored
|
175
|
}
|
唐召明
authored
|
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
}
/// <summary>
/// 页码改变
/// </summary>
[RelayCommand]
private void PageUpdated(FunctionEventArgs<int> info)
{
PageIndex = info.Info;
LoadData();
}
private BllResult LoadData()
{
try
{
|
唐召明
authored
|
192
|
var query = _context.Equipment.Where(GetFilter()).Include(x => x.EquipmentType);
|
唐召明
authored
|
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
|
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));
}
|
唐召明
authored
|
216
217
218
219
|
if (EquipmentTypeId != 0)
{
filter = filter.And(x => x.EquipmentTypeId == EquipmentTypeId);
}
|
唐召明
authored
|
220
221
222
223
224
|
if (!string.IsNullOrWhiteSpace(DestinationArea))
{
filter = filter.And(x => x.DestinationArea == DestinationArea);
}
|
唐召明
authored
|
225
226
227
228
|
return filter;
}
}
}
|