DictVM.cs
7.34 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
using HHECS.Application.Enums;
using HHECS.BllModel;
using HHECS.Model.Entities;
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.SystemInfo
{
public class DictVM : VMBase
{
#region 属性
#region Dict
public string Code { get; set; }
public string Name { get; set; }
public List<Dict> Dicts { get; set; }
public PageInfo PageInfo { get; set; } = new PageInfo();
public bool IsTabMainSelected { get; set; } = true;
#endregion
#region DictDetail
public string DetailCode { get; set; }
public string DetailName { get; set; }
public Dict CurrentDict { get; set; }
public List<DictDetail> DictDetails { get; set; }
public bool IsTabDetailSelected { get; set; } = false;
#endregion
#endregion
#region 方法
#region Dict
public void Query()
{
Expression<Func<Dict, 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));
}
var result = App.SystemService.GetDicts(filter, PageInfo.PageIndex, PageInfo.PageSize, out long totalCount);
if (result.Success)
{
PageInfo.TotalCount = totalCount;
Dicts = result.Data;
}
else
{
MessageBox.Show($"查询失败:{result.Msg}");
}
}
public void Delete(List<Dict> dicts)
{
if (dicts == null || dicts.Count == 0)
{
MessageBox.Show("未选中数据");
return;
}
if (MessageBox.Show("是否确认删除?", "警告", MessageBoxButton.YesNo) != MessageBoxResult.Yes)
{
return;
}
BllResult bllResult = App.SystemService.DeleteDicts(dicts);
if (bllResult.Success)
{
App.LogService.LogOperation(Title.DictDelete, ModuleConst.Dict, $"字典删除成功.数据:{JsonConvert.SerializeObject(dicts.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.DictDelete, ModuleConst.Dict, $"字典删除失败.数据:{JsonConvert.SerializeObject(dicts.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 void Edit(Dict dict)
{
if (dict == null)
{
MessageBox.Show("请选中一条数据");
return;
}
WinDictAddOrEdit win = new WinDictAddOrEdit(dict.Id);
win.ShowDialog();
Query();
}
public void New()
{
WinDictAddOrEdit win = new WinDictAddOrEdit(0);
win.ShowDialog();
Query();
}
#endregion
#region DictDetail
public void QueryDetail(Dict dict)
{
if (dict == null)
{
MessageBox.Show("未选中主数据!");
IsTabMainSelected = true;
return;
}
//重新查询一遍主数据,防止同步删除
var result = App.SystemService.GetDicts(t => t.Id == dict.Id);
if (result.Success && result.Data.Count > 0)
{
CurrentDict = result.Data[0];
Expression<Func<DictDetail, bool>> filter = t => t.DictId == dict.Id;
if (!String.IsNullOrWhiteSpace(DetailCode))
{
filter = filter.And(t => t.Code.Contains(DetailCode));
}
if (!String.IsNullOrWhiteSpace(DetailName))
{
filter = filter.And(t => t.Name.Contains(DetailName));
}
BllResult<List<DictDetail>> detailResult = App.SystemService.GetDictDetails(filter);
if (detailResult.Success)
{
DictDetails = detailResult.Data;
}
else
{
DictDetails = null;
MessageBox.Show($"查询当前字典的明细失败:{detailResult.Msg}");
}
IsTabDetailSelected = true;
}
else
{
MessageBox.Show($"未能查询到主数据,请刷新");
IsTabMainSelected = true;
}
}
public void DeleteDetail(List<DictDetail> dictDetails)
{
if (dictDetails == null || dictDetails.Count == 0)
{
MessageBox.Show("未选中数据");
return;
}
if (MessageBox.Show("是否确认删除?", "警告", MessageBoxButton.YesNo) != MessageBoxResult.Yes)
{
return;
}
BllResult bllResult = App.SystemService.DeleteDictDetails(dictDetails);
if (bllResult.Success)
{
App.LogService.LogOperation(Title.DictDetailDelete, ModuleConst.Dict, $"字典明细删除成功.数据:{JsonConvert.SerializeObject(dictDetails.Select(t => new { Id = t.Id, Code = t.Code, Name = t.Name, Value = t.Value }))}", bllResult.Code.ToString(), App.User.UserCode);
MessageBox.Show("删除成功");
}
else
{
App.LogService.LogOperation(Title.DictDetailDelete, ModuleConst.Dict, $"字典明细删除失败.数据:{JsonConvert.SerializeObject(dictDetails.Select(t => new { Id = t.Id, Code = t.Code, Name = t.Name, Value = t.Value }))},详情:{bllResult.Msg}", bllResult.Code.ToString(), App.User.UserCode);
MessageBox.Error($"删除失败:{bllResult.Msg}");
}
QueryDetail(CurrentDict);
}
public void EditDetail(DictDetail dictDetail)
{
if (dictDetail == null)
{
MessageBox.Show("请选中一条数据");
return;
}
WinDictDetailAddOrEdit win = new WinDictDetailAddOrEdit(dictDetail.Id, dictDetail.DictId);
win.ShowDialog();
QueryDetail(CurrentDict);
}
public void NewDetail()
{
if (CurrentDict == null)
{
MessageBox.Show("未选中主数据!请通过选中主数据,双击进入详情界面.");
IsTabMainSelected = true;
return;
}
WinDictDetailAddOrEdit win = new WinDictDetailAddOrEdit(0, CurrentDict.Id);
win.ShowDialog();
QueryDetail(CurrentDict);
}
#endregion
#endregion
}
}