schangxiang@126.com
2025-11-04 f5ed29dc26c7cd952d56ec5721a2efc43cd25992
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
283
284
285
286
287
288
289
using HxModel;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
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.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using XHandler.Class;
using HxUserManagement.Classes;
using HxUserManagement.HxBLL;
using HxUserManagement.HxDAL;
 
namespace HxUserManagement.Views
{
    /// <summary>
    /// EditAuthority.xaml 的交互逻辑
    /// </summary>
    public partial class EditAuthority : UserControl
    {
        public event EventHandler closeEvent;
        public event EventHandler saveOkEvent;
        string ID;
        RolesModel roleModel;
        RolesBLL rolesBLL = new RolesBLL();
        MenuTree menuTrees = new MenuTree();
        public EditAuthority(string id)
        {
            InitializeComponent();
            ID = id;
 
            roleModel = rolesBLL.GetInfodById(ID);
            this.DataContext = roleModel;
 
            List<string> statusList = DataModel.GetStatusList();
            comboxStatus.ItemsSource= statusList;
 
            GetAuthorityTrees(roleModel);
            tv.ItemsSource = menuTrees.Children;
        }
 
        private List<MenuTree> GetAuthorityTrees(RolesModel role)
        {
            List<MenuTree> treeList = new List<MenuTree>();
 
            List<MenuModel> menuList = new MenuBLL().GetAllMenus();
 
            var parentList = menuList.Where(s => s.Level==0).OrderBy(x=>x.Sequence).ToList();
            var childList = menuList.Where(s => s.Level==1).ToList();
            foreach (var menu in parentList)
            {
                MenuTree m = new MenuTree(menu);
                menuTrees.Children.Add(m);
 
                //if (!string.IsNullOrEmpty(menu.ChildrenId))
                //{
                //    string[] strAry = menu.ChildrenId.Split(',');
                //    foreach (string str in strAry)
                //    {
                //        var sub = childList.Where(s => s.Id == str).FirstOrDefault();
                //        MenuTree menuTree = new MenuTree(sub);
                //        bool isCheck = false;
                //        if (role.MenuId.Contains(sub.Id))
                //        {
                //            isCheck = true;
                //        }
                //        m.CreateTreeWithChildren(menuTree, isCheck);
                //    }
                //}
                int a = 0;
                if (m.Name == "设备管理")
                    a = 0;
                AddSubMenu(m, childList);
            }
 
            return treeList;
        }
 
        private void AddSubMenu(MenuTree parentMenu, List<MenuModel> list)
        {
            if (!string.IsNullOrEmpty(parentMenu.ChildrenId))
            {
                string[] strAry = parentMenu.ChildrenId.Split(',');
                foreach (string str in strAry)
                {
                    var sub = list.Where(s => s.Id == str).FirstOrDefault();
                    MenuTree menuTree = new MenuTree(sub);
                    bool isCheck = false;
                    if (roleModel.MenuId.Contains(sub.Id))
                    {
                        isCheck = true;
                    }
                    parentMenu.CreateTreeWithChildren(menuTree, isCheck);
                    AddSubMenu(menuTree, list);
                }
            }
            else
            {
                if (roleModel.MenuId.Contains(parentMenu.Id))
                    parentMenu.IsChecked = true;
                else
                    parentMenu.IsChecked = false;
            }
        }
 
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            Storyboard storyboard = new Storyboard();
 
            DoubleAnimation doubleAnimation = new DoubleAnimation(0, 600, new Duration(TimeSpan.FromSeconds(0.25)));
            Storyboard.SetTarget(doubleAnimation, border);//Target对象
            Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath("Width"));//Target属性
            storyboard.Children.Add(doubleAnimation);
 
            ObjectAnimationUsingKeyFrames showAnimation = new ObjectAnimationUsingKeyFrames();
            showAnimation.BeginTime = TimeSpan.FromSeconds(0);
            Storyboard.SetTarget(showAnimation, border);
            Storyboard.SetTargetProperty(showAnimation, new PropertyPath("(UIElement.Visibility)"));
            DiscreteObjectKeyFrame closeKeyFrame = new DiscreteObjectKeyFrame(Visibility.Visible, TimeSpan.FromSeconds(0.01));
            showAnimation.KeyFrames.Add(closeKeyFrame);
            storyboard.Children.Add(showAnimation);
 
            storyboard.Begin();
 
            if (roleModel.DataAuthority == "0")
                radioBtnSelf.IsChecked = true;
            else
                radioBtnAll.IsChecked = true;
 
            comboxStatus.SelectedIndex = Convert.ToInt16(roleModel.Type);
        }
 
        private void root_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.OriginalSource is Grid)
            {
                Grid grid = e.OriginalSource as Grid;
                if (grid.Name == "root")
                    Button_Click(this, null);
            }
        }
 
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (EventResponseController.Instance.CanExecute() == false)
                return;
 
            Storyboard storyboard = new Storyboard();
 
            DoubleAnimation doubleAnimation = new DoubleAnimation(600, 0, new Duration(TimeSpan.FromSeconds(0.25)));
            Storyboard.SetTarget(doubleAnimation, border);//Target对象
            Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath("Width"));//Target属性
            storyboard.Children.Add(doubleAnimation);
 
            ObjectAnimationUsingKeyFrames showAnimation = new ObjectAnimationUsingKeyFrames();
            showAnimation.BeginTime = TimeSpan.FromSeconds(0);
            Storyboard.SetTarget(showAnimation, border);
            Storyboard.SetTargetProperty(showAnimation, new PropertyPath("(UIElement.Visibility)"));
            DiscreteObjectKeyFrame closeKeyFrame = new DiscreteObjectKeyFrame(Visibility.Collapsed, TimeSpan.FromSeconds(0.26));
            showAnimation.KeyFrames.Add(closeKeyFrame);
            storyboard.Children.Add(showAnimation);
            storyboard.Completed += delegate { closeEvent?.Invoke(this, EventArgs.Empty); };
            storyboard.Begin();
        }
 
        private void ButtonOK_Click(object sender, RoutedEventArgs e)
        {
            if (EventResponseController.Instance.CanExecute() == false)
                return;
 
            Storyboard storyboard = new Storyboard();
 
            DoubleAnimation doubleAnimation = new DoubleAnimation(600, 0, new Duration(TimeSpan.FromSeconds(0.25)));
            Storyboard.SetTarget(doubleAnimation, border);//Target对象
            Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath("Width"));//Target属性
            storyboard.Children.Add(doubleAnimation);
 
            ObjectAnimationUsingKeyFrames showAnimation = new ObjectAnimationUsingKeyFrames();
            showAnimation.BeginTime = TimeSpan.FromSeconds(0);
            Storyboard.SetTarget(showAnimation, border);
            Storyboard.SetTargetProperty(showAnimation, new PropertyPath("(UIElement.Visibility)"));
            DiscreteObjectKeyFrame closeKeyFrame = new DiscreteObjectKeyFrame(Visibility.Collapsed, TimeSpan.FromSeconds(0.26));
            showAnimation.KeyFrames.Add(closeKeyFrame);
            storyboard.Children.Add(showAnimation);
            storyboard.Completed += delegate { saveOkEvent?.Invoke(this, EventArgs.Empty); };
            storyboard.Begin();
        }
 
        private void radioBtnSelf_Checked(object sender, RoutedEventArgs e)
        {
            if (radioBtnSelf.IsChecked == true)
                roleModel.DataAuthority = "0";
        }
 
        private void radioBtnAll_Checked(object sender, RoutedEventArgs e)
        {
            if (radioBtnAll.IsChecked == true)
                roleModel.DataAuthority = "1";
        }
 
        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            if (EventResponseController.Instance.CanExecute() == false)
                return;
            string ids = "";
            foreach (var item in menuTrees.Children)
            {
                string id = GetSelectedAuthorityIDs(item);
                if (!string.IsNullOrEmpty(id))
                {
                    if (string.IsNullOrEmpty(ids))
                        ids = id;
                    else
                        ids += "," + id;
                }
            }
            roleModel.MenuId = ids;
            roleModel.Remarks = textboxRemark.Text.Trim();
            roleModel.ModifyName = UserManagement.currentUser.UserName;
            roleModel.ModifyTime = DateTime.Now;
            RolesModel model = rolesBLL.Update(roleModel);
            if(model!=null)
                MessageBox.Show(Properties.Resources.strModifyRoleSuccess, "Tips");
            else
                MessageBox.Show(Properties.Resources.strModifyRoleFailed, "Tips");
 
            ButtonOK_Click(this, null);
        }
 
        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            if (EventResponseController.Instance.CanExecute() == false)
                return;
            Button_Click(this, null);
        }
 
        private string GetSelectedAuthorityIDs(MenuTree tree)
        {
            string ret = "";
            if (tree.IsChecked == false)
                return ret;
            else
            {
                if(tree.Children.Count==0)
                {
                    if(tree.IsChecked == true)
                    {
                        ret = tree.Id;
                        return ret;
                    }
                }
                else
                {
                    foreach(var item in tree.Children)
                    {
                        string id = GetSelectedAuthorityIDs(item);
                        if (!string.IsNullOrEmpty(id))
                        {
                            if (string.IsNullOrEmpty(ret))
                                ret = id;
                            else
                                ret += "," + id;
                        }
 
                    }
                    return ret;
                }
            }
            return ret;
        }
 
        private void comboxStatus_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (comboxStatus.SelectedIndex < 0)
                return;
            roleModel.Type = comboxStatus.SelectedIndex.ToString();
        }
    }
}