1
yhj
2024-07-24 5e5d945e91568b973faa27d8ab0bcef99fc4a6c5
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#region
 
using System;
using System.Data;
using System.Windows.Forms;
using CSFrameworkV5.Business;
using CSFrameworkV5.Business.BLL_Permission;
using CSFrameworkV5.Common;
using CSFrameworkV5.Core;
using CSFrameworkV5.Library.CommonClass;
using CSFrameworkV5.Models;
 
#endregion
 
namespace CSFrameworkV5.Library.PermissionForms
{
    public partial class frmRole : frmBaseDataForm
    {
        private bllRole _BLL;
 
        private bllPermission _Permission;
 
        //角色管理业务逻辑
        private RoleActionsView _RoleActionsView;
        private DataTable tbBM; //条码表
 
        public frmRole()
        {
            InitializeComponent();
        }
 
        private void btnEmpty_Click(object sender, EventArgs e)
        {
            txt_RoleType.Text = "";
            txt_Content.Text = "";
        }
 
        private void btnQuery_Click(object sender, EventArgs e)
        {
            try
            {
                frmWaitingEx.ShowMe(this);
                var roleType = txt_RoleType.Text == "<全部>"
                    ? ""
                    : txt_RoleType.Text;
                var dt =
                    _BLL.Query(roleType,
                        txt_Content
                            .Text); //调用业务逻辑类的GetSummaryData()方法获取数据                
                DoBindingSummaryGrid(dt); //绑定主表的Grid
            }
            finally
            {
                frmWaitingEx.HideMe(this);
            }
        }
 
        protected override void ButtonStateChanged(UpdateType currentState)
        {
            base.ButtonStateChanged(currentState);
            SetDetailEditorsAccessable(_DetailGroupControl, DataChanged);
 
            //仅新增状态可修改主键输入框
            if (_KeyEditor != null)
                _KeyEditor.Enabled = UpdateType.Add == currentState;
 
            var editMode = currentState == UpdateType.Add ||
                           currentState == UpdateType.Modify;
            _RoleActionsView.OnViewMode = !editMode; //查看模式禁止操作树视图
            txtRoleID.Enabled = currentState == UpdateType.Add;
        }
 
        public override void DoAdd(IButtonInfo sender)
        {
            _BLL.CreateDataBinder(null);
 
            InitTreeView(); //初始化树视图。
 
            _RoleActionsView.CurrentRoleID = ""; //当前角号编号    
            _RoleActionsView.ShowRoleActions();
 
            DoBindingSummaryEditor(_BLL.DataBinder); //绑定数据输入控件
 
            SetEditorBindingValue(txtRoleType, "普通角色", true);
 
            base.DoAdd(sender);
            ShowDetailPage(true);
        }
 
        /// <summary>
        ///     绑定输入框
        /// </summary>
        /// <param name="summary"></param>
        protected void DoBindingSummaryEditor(DataTable summary)
        {
            DataBinder.BindingEditorPanel(gcDetailEditor, summary);
        }
 
        public override void DoCancel(IButtonInfo sender)
        {
            if (Msg.AskQuestion("要取消修改吗?")) base.DoCancel(sender);
        }
 
        public override void DoDelete(IButtonInfo sender)
        {
            AssertFocusedRow(); //检查是否选择一条记录
 
            //调用业务逻辑类删除记录
            var summary =
                _SummaryView.GetDataRow(_SummaryView.FocusedRowHandle);
            var key = summary[tb_MyRole.RoleID].ToStringEx(); //主键值
            var roleType = summary[tb_MyRole.RoleType].ToStringEx(); //主键值
 
            if (roleType == "系统角色") throw new Exception("不可删除系统角色!");
 
            if (!Msg.AskQuestion("确定要删除?")) return;
 
            var b = _BLL.Delete(key);
            AssertEqual(b, true, "删除记录时发生错误!");
 
            base.DoDelete(sender);
            DeleteSummaryRow(_SummaryView.FocusedRowHandle); //删除Summary资料行
            if (_SummaryView.FocusedRowHandle < 0) //删除了最後一条记录. 显示Summary页面.
                ShowSummaryPage(true);
            else
                DoViewContent(_buttons.GetButtonByName(ButtonNameList.btnView));
        }
 
        public override void DoEdit(IButtonInfo sender)
        {
            AssertFocusedRow();
 
            var roleType = ConvertEx.ToString(
                gvSummary.GetFocusedDataRow()[tb_MyRole.RoleType].ToStringEx());
            if (roleType == "系统角色") throw new Exception("系统角色不可修改!");
 
            {
                base.DoEdit(sender);
                DoViewContent(sender);
                ShowDetailPage(true);
            }
        }
 
        public override void DoSave(IButtonInfo sender)
        {
            UpdateLastControl(); //更新最后一个输入控件的数据
 
            if (!ValidatingData()) return; //检查输入完整性
 
            var changes = _RoleActionsView.GetChanges();
            var roleUsers = gcRoleUsers.DataSource as DataTable;
 
            //调用业务逻辑层的Update方法提交数据
            //bool ret = _BLL.Update(_UpdateType, _RoleActionsView.RoleActionData);
            var ret = _BLL.Update(_UpdateType, changes, roleUsers);
            if (ret)
            {
                UpdateSummaryRow(_BLL.DataBinder.Rows[0]); //刷新表格内的数据.
                base.DoSave(sender);
                Msg.ShowInformation("保存成功!");
            }
            else
            {
                Msg.Warning("保存失败!");
            }
        }
 
        public override void DoViewContent(IButtonInfo sender)
        {
            AssertFocusedRow(); //检查有无记录.                
 
            var page = tcBusiness.SelectedTabPage;
 
            var roleID =
                ConvertEx.ToString(gvSummary.GetFocusedRowCellValue(colRoleID));
            InitTreeView(); //初始化树视图。
            _BLL.CreateDataBinder(
                _SummaryView.GetDataRow(_SummaryView.FocusedRowHandle));
            _RoleActionsView.CurrentRoleID = roleID; //当前角号编号    
            _RoleActionsView.ShowRoleActions(); //显示角色的权限
 
            base.DoViewContent(sender);
            DoBindingSummaryEditor(_BLL.DataBinder); //绑定数据输入控件
 
            var ds = _BLL.GetRoleOwners(roleID);
            gcMyGroups.DataSource = ds.Tables[tb_MyGroup.__TableName];
            gcMyUser.DataSource = ds.Tables[tb_MyUser.__TableName];
            gcRoleUsers.DataSource = ds.Tables[tb_MyUserRoles.__TableName];
 
            ShowDetailPage(false); //用户点击ViewContent按钮可以显示Summary页      
 
            xtraTabControl1.SelectedTabPageIndex = 0;
        }
 
        private void frmRole_Load(object sender, EventArgs e)
        {
            InitializeForm();
            SetLanguage();
        }
 
        protected override void InitializeForm()
        {
            try
            {
                base.InitializeForm();
 
                _SummaryView = new DevGridView(gvSummary);
                var sqlstr = "SELECT BM002 部门编号,BM003 部门名称 FROM GS_BM ";
 
                _ActiveEditor = txtRoleID;
                _DetailGroupControl = gcDetailEditor;
                gvSummary.DoubleClick += OnGridViewDoubleClick; //主表DoubleClict
 
                frmGridCustomize.RegisterGrid(gvSummary);
                DevStyle.SetGridControlLayout(gcSummary, false); //表格设置   
                DevStyle.SetSummaryGridViewLayout(gvSummary);
 
                _BLL = new bllRole();
                _Permission = new bllPermission();
                _RoleActionsView = new RoleActionsView(tvRoleActions);
                tbBM = _BLL.GETDAT(sqlstr);
                txtRoleID.Properties.ValueMember = "部门名称";
                txtRoleID.Properties.DisplayMember = "部门名称";
                //txtRoleID.Properties.SH = false;
                txtRoleID.Properties.DataSource = tbBM;
                ShowSummary(); //下载显示数据.
                DataBinder.BindingLookupEditDataSource(lookUser,
                    DataDictCache.Cache.User, tb_MyUser.UserName,
                    tb_MyUser.Account);
 
                BindingSummaryNavigator(controlNavigatorSummary,
                    gcSummary); //Summary导航条.
                ShowSummaryPage(true); //一切初始化完毕後显示SummaryPage        
            }
            catch (Exception ex)
            {
                Msg.ShowException(ex);
            }
        }
 
        private void InitTreeView()
        {
            if (_RoleActionsView.IsCreatedTree == false)
            {
                //下载生成树相关的基础资料
                _RoleActionsView.RoleData = _Permission.GetRoleData();
                _RoleActionsView.ActionsData = _Permission.GetActionData();
                _RoleActionsView.ActionCustomNameData =
                    _Permission.GetFormTagCustomName();
 
                //管理员取全部菜单数据
                if (Loginer.CurrentUser.IsAdmin())
                    _RoleActionsView.MenuData = _Permission.GetMenuData();
                else //取当前用户的菜单数据
                    _RoleActionsView.MenuData =
                        _Permission.GetMenuByUser(Loginer.CurrentUser.Account,
                            Loginer.CurrentUser.DBID);
 
                _RoleActionsView.InitTree(""); //创建树视图
            }
 
            _RoleActionsView.OnViewMode = false;
        }
 
        private void menuCollase_Click(object sender, EventArgs e)
        {
            if (tvRoleActions.SelectedNode != null)
                tvRoleActions.SelectedNode.Collapse();
        }
 
        private void menuExpand_Click(object sender, EventArgs e)
        {
            if (tvRoleActions.SelectedNode != null)
                tvRoleActions.SelectedNode.ExpandAll();
        }
 
        private void ShowSummary()
        {
            try
            {
                _BLL.GetSummaryData(
                    true); //调用业务逻辑类的GetSummaryData()方法获取数据                
                DoBindingSummaryGrid(_BLL.SummaryTable); //绑定主表的Grid
                ShowSummaryPage(true); //显示Summary页面. 
            }
            catch (Exception ex)
            {
                Msg.ShowException(ex);
            }
        }
 
        private void tvRoleActions_MouseDown(object sender, MouseEventArgs e)
        {
            //在角色的权限树按下鼠标键,选择结点,设置高亮状态                    
            var info = tvRoleActions.HitTest(e.Location);
            tvRoleActions.SelectedNode = info.Node;
        }
 
        private bool ValidatingData()
        {
            if (txtRoleID.Text == string.Empty)
            {
                Msg.Warning("编号不能为空!");
                txtRoleID.Focus();
                return false;
            }
 
            if (txtRoleName.Text == string.Empty)
            {
                Msg.Warning("名称不能为空!");
                txtRoleName.Focus();
                return false;
            }
 
            if (_UpdateType == UpdateType.Add)
                if (_BLL.IsExists(txtRoleID.Text))
                {
                    Msg.Warning("编号已存在!");
                    txtRoleID.Focus();
                    return false;
                }
 
            return true;
        }
    }
}