WinRoleAddOrEdit.xaml.cs
5.48 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
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.UserOperation
{
/// <summary>
/// RoleAddOrEdit.xaml 的交互逻辑
/// </summary>
public partial class WinRoleAddOrEdit : BaseWindow
{
public int? Id { get; set; }
public Role CurrentRole { get; set; } = new Role()
{
RoleName = "",
Remark = ""
};
public List<MenuOperation> AllMenuOperations { get; set; }
public List<MenuOperation> HasMenuOperations { get; set; }
public WinRoleAddOrEdit(int? id)
{
this.Id = id;
InitializeComponent();
if (id != null)
{
this.Title = "编辑";
}
else
{
this.Title = "新增";
}
Init();
}
public void Init()
{
if (Id.HasValue)
{
BllResult<Role> result = AppSession.Bll.GetRoleById(Id.Value);
if (result.Success)
{
CurrentRole.Id = result.Data.Id;
CurrentRole.RoleName = result.Data.RoleName;
CurrentRole.Remark = result.Data.Remark;
}
else
{
MessageBox.Show("未能获取指定角色");
}
}
this.GridMain.DataContext = CurrentRole;
BllResult<List<MenuOperation>> result2 = AppSession.Bll.GetAllMenuOperation();
if (!result2.Success)
{
MessageBox.Show("未能获取权限值");
}
else
{
AllMenuOperations = result2.Data;
}
if (CurrentRole.Id != null)
{
result2 = AppSession.Bll.FindMenuOperation(new List<Role>() { CurrentRole });
if (!result2.Success)
{
MessageBox.Show("未能获取拥有的权限值");
}
else
{
HasMenuOperations = result2.Data;
AllMenuOperations.ForEach(t => { t.HasPerm = HasMenuOperations.Count(i => i.Id == t.Id) > 0; });
}
}
AppSession.Bll.Combine(AllMenuOperations.FindAll(t => t.ParentId == null).ToList(), AllMenuOperations);
treeMain.ItemsSource = AllMenuOperations.FindAll(t => t.ParentId == null).ToList();
}
private void treeMain_Selected(object sender, RoutedEventArgs e)
{
//MenuOperation o = (MenuOperation)treeMain.SelectedItem;
//if (o != null)
//{
// o.HasPerm = !o.HasPerm;
//}
}
private void BtnSave_Click(object sender, RoutedEventArgs e)
{
if (String.IsNullOrWhiteSpace(CurrentRole.RoleName))
{
MessageBox.Show("角色名不能为空");
return;
}
if (Id == null)
{
//说明是新增
BllResult<Role> result = AppSession.Bll.InsertRoleAndMenuOperations(CurrentRole, AllMenuOperations.FindAll(t => t.HasPerm == true));
if (result.Success)
{
CurrentRole.Id = result.Data.Id;
Id = result.Data.Id;
MessageBox.Show("新增成功");
}
else
{
MessageBox.Show("新增失败:" + result.Msg);
}
}
else
{
//说明是更新
BllResult<Role> result = AppSession.Bll.UpdateRoleAndMenuOperations(CurrentRole, AllMenuOperations.FindAll(t => t.HasPerm == true));
if (result.Success)
{
CurrentRole.Id = result.Data.Id;
Id = result.Data.Id;
MessageBox.Show("更新成功");
}
else
{
MessageBox.Show("更新失败:" + result.Msg);
}
}
Init();
}
private void BtnCancel_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
private void CheckBox_Click(object sender, RoutedEventArgs e)
{
CheckBox cb = (CheckBox)sender;
int? id = (int?)cb.Tag;
var temp = AllMenuOperations.Find(t => t.Id == id);
List<int> ids = new List<int>();
AppSession.Bll.GetMenuOperationIds(new List<MenuOperation> { temp }, AllMenuOperations, ids);
AllMenuOperations.ForEach(t =>
{
if (ids.Count(i => i == t.Id) > 0)
{
t.HasPerm = !t.HasPerm;
}
});
}
private void BtnUpdate_Click(object sender, RoutedEventArgs e)
{
Init();
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
}
}
}