WinEquipment.xaml.cs
9.78 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
using HHECS.Bll;
using HHECS.Model;
using HHECS.Model.BllModel;
using HHECS.Model.Entities;
using HHECS.View.Win;
using HHECS.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace HHECS.View.EquipmentInfo
{
/// <summary>
/// WinEquipment.xaml 的交互逻辑
/// </summary>
public partial class WinEquipment : MyWindow
{
/// <summary>
/// 用于属性页数据绑定
/// </summary>
public Equipment CurrentEquipment { get; set; } = new Equipment();
public PageInfoVM PageInfo { get; set; } = new PageInfoVM();
public WinEquipment()
{
InitializeComponent();
this.GridDetail.DataContext = CurrentEquipment;
this.page.DataContext = PageInfo;
}
private void BtnQuery_Click(object sender, RoutedEventArgs e)
{
Query();
}
private void Query()
{
string sql = " where 1=1 ";
if (!String.IsNullOrWhiteSpace(TxtCode.Text))
{
sql += $" and code like '{TxtCode.Text}%'";
}
if (!String.IsNullOrWhiteSpace(TxtName.Text))
{
sql += $" and name like '{TxtName.Text}%'";
}
BllResult<int> result = AppSession.Bll.GetCommonModelCount<Equipment>(sql);
if (result.Success)
{
PageInfo.TotalCount = result.Data;
var a = AppSession.Bll.GetCommonModeByPageCondition<Equipment>(PageInfo.PageIndex, PageInfo.PageSize, sql, "");
if (a.Success)
{
DGMain.ItemsSource = a.Data;
TIMain.IsSelected = true;
}
else
{
MessageBox.Show($"查询出错:{a.Msg}");
}
}
else
{
MessageBox.Show($"查询失败{result.Msg}");
}
}
private void BtnViewDetail_Click(object sender, RoutedEventArgs e)
{
if (DGMain.SelectedItem == null)
{
MessageBox.Show("请先选中一条数据!");
}
else
{
QueryDetail((Equipment)DGMain.SelectedItem);
}
}
private void QueryDetail(Equipment equipment)
{
//重新查询一遍主数据,防止同步删除
BllResult<List<Equipment>> result = AppSession.Bll.GetCommonModelByCondition<Equipment>($"where id ={equipment.Id}");
if (result.Success)
{
var temp = result.Data[0];
CurrentEquipment.Id = temp.Id;
CurrentEquipment.Code = temp.Code;
CurrentEquipment.Name = temp.Name;
CurrentEquipment.EquipmentType = temp.EquipmentType;
CurrentEquipment.EquipmentTypeId = temp.EquipmentTypeId;
CurrentEquipment.IP = temp.IP;
CurrentEquipment.SelfAddress = temp.SelfAddress;
CurrentEquipment.RoadWay = temp.RoadWay;
CurrentEquipment.StationIndex = temp.StationIndex;
CurrentEquipment.ConnectName = temp.ConnectName;
CurrentEquipment.GroupName = temp.GroupName;
CurrentEquipment.Description = temp.Description;
CurrentEquipment.Created = temp.Created;
CurrentEquipment.CreatedBy = temp.CreatedBy;
CurrentEquipment.Updated = temp.Updated;
CurrentEquipment.UpdatedBy = temp.UpdatedBy;
CurrentEquipment.Disable = temp.Disable;
//查询对应属性
var a = AppSession.Bll.GetCommonModelByCondition<EquipmentProp>($"where equipmentId = {CurrentEquipment.Id}");
if (a.Success)
{
DGDetail.ItemsSource = a.Data;
}
else
{
DGDetail.ItemsSource = null;
MessageBox.Show($"查询当前设备的属性失败:{a.Msg}");
}
TIDetail.IsSelected = true;
}
else
{
MessageBox.Show("查询出错");
}
}
private void BtnAdd_Click(object sender, RoutedEventArgs e)
{
WinEquipmentAddOrEdit win = new WinEquipmentAddOrEdit(null);
win.ShowDialog();
Query();
}
private void BtnEdit_Click(object sender, RoutedEventArgs e)
{
if (DGMain.SelectedItem == null)
{
MessageBox.Show("请先选中一条数据!");
}
else
{
Equipment temp = (Equipment)DGMain.SelectedItem;
WinEquipmentAddOrEdit win = new WinEquipmentAddOrEdit(temp.Id);
win.ShowDialog();
Query();
}
}
private void BtnDelete_Click(object sender, RoutedEventArgs e)
{
if (DGMain.SelectedItems.Count == 0)
{
MessageBox.Show("请至少选中一条数据!");
}
else
{
if (MessageBox.Show("是否确认删除?这可能导致程序异常!", "警告", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
{
List<Equipment> list = new List<Equipment>();
foreach (var item in DGMain.SelectedItems)
{
list.Add((Equipment)item);
}
BllResult result = AppSession.Bll.DeleteEuipmentByIds(list.Select(t => t.Id.Value).ToList());
if (result.Success)
{
MessageBox.Show("删除成功");
}
else
{
MessageBox.Show("删除失败");
}
Query();
}
}
}
private void DataGridRow_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
if (sender != null)
{
DataGridRow dgr = sender as DataGridRow;
if (!dgr.IsNewItem)
{
QueryDetail((Equipment)dgr.Item);
//事件不再向上传递,防止设置其他tabitem的isselected属性失败
e.Handled = true;
DGMain.SelectedItem = dgr.Item;
}
}
}
private void BtnSaveProp_Click(object sender, RoutedEventArgs e)
{
if (DGDetail.ItemsSource != null)
{
foreach (var item in DGDetail.ItemsSource)
{
EquipmentProp equipmentProp = (EquipmentProp)item;
var result = AppSession.Bll.UpdateCommonModel<EquipmentProp>(equipmentProp);
if (!result.Success)
{
MessageBox.Show($"属性{equipmentProp.EquipmentTypePropTemplateCode}更新失败,操作中止");
return;
}
}
MessageBox.Show("更新成功");
}
else
{
MessageBox.Show("无可更新项");
}
}
private void BtnUpdateDetail_Click(object sender, RoutedEventArgs e)
{
QueryDetail(CurrentEquipment);
}
private void BtnSync_Click(object sender, RoutedEventArgs e)
{
if (CurrentEquipment.Id.HasValue)
{
if (MessageBox.Show("同步将会使得当前设备的属性与属性模板同步,可能会导致属性增加或减少。是否继续?", "警告", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
{
BllResult result = AppSession.Bll.SyncEquipmentProp(CurrentEquipment.Id);
MessageBox.Show(result.Success ? "同步成功" : $"同步失败:{result.Msg}");
QueryDetail(CurrentEquipment);
}
}
else
{
MessageBox.Show("无主数据");
}
}
private void BtnDeleteProp_Click(object sender, RoutedEventArgs e)
{
if (DGDetail.SelectedItems == null||DGDetail.SelectedItems.Count==0)
{
MessageBox.Show("未选中数据");
}
else
{
if(MessageBox.Show("删除可能导致程序异常,是否继续?","警告",MessageBoxButton.YesNo)== MessageBoxResult.Yes)
{
List<int> ids = new List<int>();
foreach (var item in DGDetail.SelectedItems)
{
ids.Add(((EquipmentProp)item).Id.Value);
}
var result = AppSession.Bll.DeleteCommonModelByIds<EquipmentProp>(ids);
if (result.Success)
{
MessageBox.Show("删除成功。");
QueryDetail((Equipment)DGMain.SelectedItem);
}
else
{
MessageBox.Show($"删除失败:{result.Msg}");
}
}
}
}
private void page_PageChanged(object sender, Controls.PageChangedEventArgs e)
{
PageInfo.PageIndex = e.CurrentPageIndex;
Query();
}
}
}