唐召明
authored
|
1
2
3
4
5
|
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using HandyControl.Data;
using HHECS.BllModel;
using HHECS.DAQClient.Model;
|
唐召明
authored
|
6
|
using HHECS.DAQClient.View.CommunicationView;
|
唐召明
authored
|
7
|
using LinqKit;
|
唐召明
authored
|
8
|
using System.Collections;
|
唐召明
authored
|
9
10
|
using System.Collections.ObjectModel;
using System.Linq.Expressions;
|
唐召明
authored
|
11
|
using System.Windows;
|
唐召明
authored
|
12
13
14
15
16
17
18
19
20
21
22
23
24
|
using MessageBox = HandyControl.Controls.MessageBox;
namespace HHECS.DAQClient.ViewModel.CommunicationVM
{
public partial class CommunicationVM : ObservableObject
{
[ObservableProperty]
private string code;
[ObservableProperty]
private string name;
[ObservableProperty]
|
唐召明
authored
|
25
26
27
28
29
30
|
private Dictionary<string, CommunicationTypeConst?> communicationTypes = new Dictionary<string, CommunicationTypeConst?>();
[ObservableProperty]
private CommunicationTypeConst? communicationType;
[ObservableProperty]
|
唐召明
authored
|
31
32
33
34
35
36
37
38
39
40
41
|
private int pageIndex = 1;
[ObservableProperty]
private long maxPage;
[ObservableProperty]
private int pageSize = 30;
[ObservableProperty]
private ObservableCollection<CommunicationConfig> communicationConfigs = new ObservableCollection<CommunicationConfig>();
|
唐召明
authored
|
42
|
private readonly IFreeSql _freeSql;
|
唐召明
authored
|
43
|
|
唐召明
authored
|
44
|
public CommunicationVM(IFreeSql freeSql)
|
唐召明
authored
|
45
|
{
|
唐召明
authored
|
46
|
_freeSql = freeSql;
|
唐召明
authored
|
47
|
InitialData();
|
唐召明
authored
|
48
49
50
|
LoadData();
}
|
唐召明
authored
|
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
private void InitialData()
{
var keyValuePairs = new Dictionary<string, CommunicationTypeConst?>()
{
{ "全部", null },
};
foreach (var item in Enum.GetNames<CommunicationTypeConst>())
{
keyValuePairs.Add(item, Enum.Parse<CommunicationTypeConst>(item));
}
CommunicationTypes = keyValuePairs;
}
|
唐召明
authored
|
65
66
67
68
69
70
|
[RelayCommand]
public void Serach()
{
var result = LoadData();
if (!result.Success)
{
|
唐召明
authored
|
71
|
MessageBox.Error($"[{nameof(CommunicationVM)}]加载数据失败.{result.Msg}");
|
唐召明
authored
|
72
73
74
75
|
}
}
[RelayCommand]
|
唐召明
authored
|
76
|
public void Add()
|
唐召明
authored
|
77
|
{
|
唐召明
authored
|
78
|
var view = new CommunicationAddOrEditView();
|
唐召明
authored
|
79
80
81
82
83
|
var result = view.ShowDialog();
if (result == true)
{
LoadData();
}
|
唐召明
authored
|
84
85
86
|
}
[RelayCommand]
|
唐召明
authored
|
87
|
public void Edit(int communicationId)
|
唐召明
authored
|
88
|
{
|
唐召明
authored
|
89
90
91
92
93
94
|
var vm = new CommunicationAddOrEditView(communicationId);
var result = vm.ShowDialog();
if (result == true)
{
LoadData();
}
|
唐召明
authored
|
95
96
97
|
}
[RelayCommand]
|
唐召明
authored
|
98
|
public void Delete(int communicationId)
|
唐召明
authored
|
99
|
{
|
唐召明
authored
|
100
101
|
try
{
|
唐召明
authored
|
102
103
104
105
106
|
var result = MessageBox.Ask($"确认删除数据?");
if (result != MessageBoxResult.OK)
{
return;
}
|
唐召明
authored
|
107
|
_freeSql.Delete<CommunicationConfig>().Where(x => x.Id == communicationId).ExecuteAffrows();
|
唐召明
authored
|
108
109
110
111
112
113
114
|
LoadData();
MessageBox.Success("删除数据成功");
}
catch (Exception ex)
{
MessageBox.Error($"删除数据失败.{ex.Message}");
}
|
唐召明
authored
|
115
116
|
}
|
唐召明
authored
|
117
|
[RelayCommand]
|
唐召明
authored
|
118
|
public void BatchDelete(IList sender)
|
唐召明
authored
|
119
|
{
|
唐召明
authored
|
120
121
122
123
124
125
126
127
128
129
130
131
132
|
try
{
var temps = sender.Cast<CommunicationConfig>().ToList();
if (temps.Count == 0)
{
MessageBox.Warning("未选中任何数据!");
return;
}
var result = MessageBox.Ask($"确认删除选中数据?");
if (result == MessageBoxResult.OK)
{
return;
}
|
唐召明
authored
|
133
134
|
var communicationIds = temps.Select(x => x.Id).ToList();
_freeSql.Delete<CommunicationConfig>().Where(x => communicationIds.Contains(x.Id)).ExecuteAffrows();
|
唐召明
authored
|
135
136
137
138
|
LoadData();
MessageBox.Success($"成功删除{temps.Count}条数据");
}
catch (Exception ex)
|
唐召明
authored
|
139
|
{
|
唐召明
authored
|
140
|
MessageBox.Error($"删除失败.{ex.Message}");
|
唐召明
authored
|
141
142
143
|
}
}
|
唐召明
authored
|
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
/// <summary>
/// 页码改变
/// </summary>
[RelayCommand]
private void PageUpdated(FunctionEventArgs<int> info)
{
PageIndex = info.Info;
LoadData();
}
private BllResult LoadData()
{
try
{
|
唐召明
authored
|
158
|
var query = _freeSql.Queryable<CommunicationConfig>().Where(GetFilter());
|
唐召明
authored
|
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
var total = query.Count();
MaxPage = total / PageSize + Convert.ToInt32(total % PageSize != 0);
var result = query.Page(PageIndex, PageSize).ToList();
CommunicationConfigs = new ObservableCollection<CommunicationConfig>(result);
return BllResultFactory.Success();
}
catch (Exception ex)
{
return BllResultFactory.Error(ex.Message);
}
}
private Expression<Func<CommunicationConfig, bool>> GetFilter()
{
var filter = PredicateBuilder.New<CommunicationConfig>(true);
if (!string.IsNullOrWhiteSpace(Code))
{
filter = filter.And(x => x.Code.Contains(Code));
}
if (!string.IsNullOrWhiteSpace(Name))
{
filter = filter.And(x => x.Name.Contains(Name));
}
|
唐召明
authored
|
182
183
184
185
|
if (CommunicationType != null)
{
filter = filter.And(x => x.CommunicationType.Equals(CommunicationType));
}
|
唐召明
authored
|
186
187
188
189
|
return filter;
}
}
}
|