#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;
using DevExpress.XtraBars;
using DevExpress.XtraTab;
using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.Nodes;
#endregion
namespace CSFrameworkV5.Library.PermissionForms
{
///
/// 角色、菜单、功能点、模块管理
///
public partial class frmMenuActions : frmBaseDataForm
{
//权限管理业务逻辑层
private bllPermission _BLL;
//角色管理业务逻辑
private RoleActionsView _RoleActionsView;
private ISummaryView _ViewActions;
private ISummaryView _ViewMenu;
public frmMenuActions()
{
InitializeComponent();
}
///
/// 初始化功能权限控制面板
///
private void ActionInitPanel()
{
frmEditorMenu.ActionInitPanel(pcActions,
_RoleActionsView.ActionsData);
}
///
/// 显示菜单的功能点
///
///
private void ActionShowMenuFuns(TreeListNode node)
{
var row = (node.TreeList.GetDataRecordByNode(node) as DataRowView)
.Row; //取记录
frmEditorMenu.ActionShowMenuFuns(pcActions, row,
_RoleActionsView.ActionCustomNameData);
}
private void btnActionLoad_Click(object sender, EventArgs e)
{
LoadActionData();
}
//弹出菜单-新增功能
private void btnCommonAdd_ItemClick(object sender, ItemClickEventArgs e)
{
DoAdd(null);
}
//弹出菜单-删除功能
private void btnCommonDelete_ItemClick(object sender,
ItemClickEventArgs e)
{
DoDelete(null);
}
//弹出菜单-修改功能
private void btnCommonEdit_ItemClick(object sender,
ItemClickEventArgs e)
{
DoEdit(null);
}
private void btnDelAll_Click(object sender, EventArgs e)
{
if (Msg.AskQuestion("确定要删除菜单数据吗?"))
try
{
frmWaitingEx.ShowMe(this);
var i = _BLL.DeleteAllMenu();
LoadMenuData();
Msg.ShowInformation($"共删除{i}个菜单!");
}
finally
{
frmWaitingEx.HideMe(this);
}
}
private void btnImportMenu_Click(object sender, EventArgs e)
{
if (Msg.AskQuestion("确定要导入或更新菜单的权限数据吗?"))
try
{
frmWaitingEx.ShowMe(this);
DoImportMenu(null);
LoadMenuData();
}
finally
{
frmWaitingEx.HideMe(this);
}
}
private void btnMenuLoad_Click(object sender, EventArgs e)
{
try
{
frmWaitingEx.ShowMe(this);
LoadMenuData();
if (chkExpand.Checked) tlMenu.ExpandAll();
}
finally
{
frmWaitingEx.HideMe(this);
}
}
private void btnSearchByMenu_Click(object sender, EventArgs e)
{
if (txtMenuID.Text != "")
try
{
frmWaitingEx.ShowMe(this);
var ds =
_BLL.GetOwnersByMenu(txtMenuID.EditValue.ToStringEx());
gcGroup.DataSource = ds.Tables[tb_MyGroup.__TableName];
gcRole.DataSource = ds.Tables[tb_MyRole.__TableName];
gcUser.DataSource = ds.Tables[tb_MyUser.__TableName];
}
finally
{
frmWaitingEx.HideMe(this);
}
else
txtMenuID.Focus();
}
protected override void ButtonStateChanged(UpdateType currentState)
{
base.ButtonStateChanged(currentState);
}
private void chkExpand_CheckedChanged(object sender, EventArgs e)
{
if (chkExpand.Checked)
tlMenu.ExpandAll();
else
tlMenu.CollapseAll();
}
public override void DoAdd(IButtonInfo sender)
{
if (tcBusiness.SelectedTabPage == tpSummary) //新增菜单
Msg.Warning("在模块主窗体定义菜单项,然后使用右上方 [导入菜单] 功能!");
if (tcBusiness.SelectedTabPage == tpDetail) //新增功能点
{
base.DoAdd(sender); //进入新增状态
if (frmEditorAction.Execute(_RoleActionsView.ActionsData,
UpdateType.Add, gvAction))
{
DoSave(null); //保存资料
gcAction.RefreshDataSource();
}
else
{
DoCancelSub();
}
}
}
public override void DoCancel(IButtonInfo sender)
{
if (Msg.AskQuestion("要取消修改吗?")) DoCancelSub();
}
private void DoCancelSub()
{
_RoleActionsView.CancelOperation(); //取消所有修改
_UpdateType = UpdateType.None;
SetViewMode();
ButtonStateChanged(_UpdateType);
//刷新数据源
tlMenu.RefreshDataSource();
gcAction.RefreshDataSource();
}
public override void DoEdit(IButtonInfo sender)
{
base.DoEdit(sender); //进入修改状态
if (tcBusiness.SelectedTabPage == tpSummary) //修改菜单
{
if (frmEditorMenu.Execute(_RoleActionsView, UpdateType.Modify,
tlMenu.FocusedNode))
{
DoSave(null); //保存资料
LoadMenuData();
}
else
{
DoCancelSub();
}
DevTreeListView.SetImageIndex(tlMenu, null, 1, 0);
}
if (tcBusiness.SelectedTabPage == tpDetail) //修改功能点
{
if (frmEditorAction.Execute(_RoleActionsView.ActionsData,
UpdateType.Modify, gvAction))
{
DoSave(null); //保存资料
LoadActionData();
gcAction.RefreshDataSource();
}
else
{
DoCancelSub();
}
}
}
public void DoImportMenu(IButtonInfo sender)
{
var bll = new bllMenu();
_RoleActionsView.ActionCustomNameData =
_BLL.GetFormTagCustomName(); //数据源
_RoleActionsView.MenuData = _BLL.GetMenuData(); //菜单数据源
//导入菜单数据
var success = bll.ImportMenu((MdiParent as IMdiForm).MainMenu,
_RoleActionsView.MenuData,
_RoleActionsView.ActionCustomNameData);
if (success)
{
_RoleActionsView.MenuData = _BLL.GetMenuData(); //菜单数据源
_RoleActionsView.ModuleData.AcceptChanges();
_RoleActionsView.ActionCustomNameData =
_BLL.GetFormTagCustomName();
var msg =
$"操作完成!共更新菜单 {bll.LastUpdated} 个,导入新菜单 {bll.LastInserted} 个,更新自定义权限 {bll.LastInsertCustomAction} 个。";
DevTreeListView.SetImageIndex(tlMenu, null, 1, 0);
Msg.ShowInformation(msg);
}
else
{
Msg.Warning("导入菜单数据失败!");
}
}
public override void DoSave(IButtonInfo sender)
{
try
{
var success = _RoleActionsView.PostChanges();
if (success)
{
_UpdateType = UpdateType.None;
SetViewMode();
ButtonStateChanged(_UpdateType);
Msg.ShowInformation("保存资料成功!");
}
}
catch (Exception ex)
{
Msg.ShowError("保存数据失败!请先【删除全部菜单】,然后【导入菜单数据】。\r\n" + ex.Message);
}
}
private void frmRoleManagement_Load(object sender, EventArgs e)
{
_RoleActionsView = new RoleActionsView(null);
_BLL = new bllPermission();
_ViewMenu = new DevTreeListView(tlMenu);
_ViewActions = new DevGridView(gvAction);
_SummaryView = _ViewMenu; //预设显示菜单的表格
LoadActionData(); //加载功能点资料
LoadModuleData(); //加载模块资料
LoadMenuData();
; //加载菜单资料
LoadActionCustomNameData();
ActionInitPanel();
BindingSummaryNavigator(controlNavigatorSummary, tlMenu);
base.InitializeForm();
SetLanguage();
DevStyle.SetTreeListSelectStyle(tlMenu);
DevTreeListView.SetImageIndex(tlMenu, null, 1, 0);
var dt = _BLL.GetLookupMenu();
DataBinder.AddEmptyRow(dt);
DataBinder.BindingLookupEditDataSource(txtMenuID, dt,
tb_MyMenu.MenuCaption, tb_MyMenu.MenuName);
}
private void LoadActionCustomNameData()
{
_RoleActionsView.ActionCustomNameData =
_BLL.GetFormTagCustomName(); //数据源
}
//加载功能点数据
private void LoadActionData()
{
_RoleActionsView.ActionsData =
_BLL.GetActionData(); //数据源
gcAction.DataSource = _RoleActionsView.ActionsData;
}
//加载菜单数据
private void LoadMenuData()
{
_RoleActionsView.MenuData = _BLL.GetMenuData(); //菜单数据源
tlMenu.KeyFieldName = "MenuName"; //设置主键
tlMenu.ParentFieldName = "ParentMenuName"; //设置父级主键
tlMenu.RootValue = ""; //顶级树结点的值
tlMenu.DataSource = _RoleActionsView.MenuData; //数据源
DevTreeListView.SetImageIndex(tlMenu, null, 1, 0);
}
//加载模块数据
private void LoadModuleData()
{
_RoleActionsView.ModuleData = _BLL.GetModuleData(); //数据源
}
private void tcBusiness_SelectedPageChanged(object sender,
TabPageChangedEventArgs e)
{
if (e.Page == tpSummary)
{
_SummaryView = _ViewMenu;
controlNavigatorSummary.NavigatableControl = tlMenu;
}
if (e.Page == tpDetail)
{
_SummaryView = _ViewActions;
controlNavigatorSummary.NavigatableControl = gcAction;
}
}
private void tlMenu_DoubleClick(object sender, EventArgs e)
{
if (IsAddOrEditMode == false)
{
if (tlMenu.FocusedNode != null)
DoViewContent(null); //双击查看
else
DoAdd(null);
}
}
private void tlMenu_FocusedNodeChanged(object sender,
FocusedNodeChangedEventArgs e)
{
if (e.Node != null) ActionShowMenuFuns(e.Node); // 显示菜单的功能
}
private void tlMenu_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
var hit = tlMenu.CalcHitInfo(e.Location);
if (hit.Node != null) tlMenu.FocusedNode = hit.Node;
//菜单管理显示弹出菜单
popupMenu.ShowPopup(tlMenu.PointToScreen(e.Location));
}
}
#region Summary数据导航功能
protected override void DoMoveFirst(IButtonInfo sender)
{
if (_SummaryView != null) _SummaryView.MoveFirst();
}
protected override void DoMovePrior(IButtonInfo sender)
{
if (_SummaryView != null) _SummaryView.MovePrior();
}
protected override void DoMoveNext(IButtonInfo sender)
{
if (_SummaryView != null) _SummaryView.MoveNext();
}
protected override void DoMoveLast(IButtonInfo sender)
{
if (_SummaryView != null) _SummaryView.MoveLast();
}
#endregion
#region DoViewContent 相关方法
public override void DoViewContent(IButtonInfo sender)
{
if (tcBusiness.SelectedTabPage == tpSummary) ViewMenuData();
if (tcBusiness.SelectedTabPage == tpDetail) ViewActionData();
}
private void ViewActionData()
{
if (gvAction.RowCount == 0) return;
frmEditorAction.Execute(_RoleActionsView.ActionsData,
UpdateType.None, gvAction);
}
private void ViewMenuData()
{
if (tlMenu.FocusedNode == null) return;
frmEditorMenu.Execute(_RoleActionsView, UpdateType.None,
tlMenu.FocusedNode);
}
#endregion
#region DoDelete相关方法
public override void DoDelete(IButtonInfo sender)
{
if (tcBusiness.SelectedTabPage == tpSummary)
{
Msg.Warning("不能删除菜单项,请使用 [导入菜单] 同步菜单数据!");
return;
}
if (Msg.AskQuestion("确定要删除吗?"))
{
if (tcBusiness.SelectedTabPage ==
tpSummary) DeleteMenuData(); //删除菜单资料
if (tcBusiness.SelectedTabPage ==
tpDetail) DeleteActionData(); //删除角色资料
}
}
private void DeleteActionData()
{
if (gvAction.RowCount == 0) return;
var dr = gvAction.GetDataRow(gvAction.FocusedRowHandle);
var key = ConvertEx.ToString(dr[tb_MyActions.__KeyName]);
var success = new bllActions().Delete(key);
if (success)
{
dr.Delete(); //取出当前资料行打上删除标记。
if (dr.RowState == DataRowState.Added) //如果是新增的资料行,永久删除
dr.Table.Rows.Remove(dr);
dr.Table.AcceptChanges();
gcAction.RefreshDataSource();
}
}
private void DeleteMenuData()
{
if (tlMenu.FocusedNode == null) return;
var rv =
(DataRowView)tlMenu.GetDataRecordByNode(tlMenu.FocusedNode);
var key = ConvertEx.ToString(rv.Row[tb_MyMenu.MenuName]); //根据菜单名删除
var success = new bllMenu().Delete(key);
if (success)
{
rv.Row.Delete(); //取出当前资料行打上删除标记。
if (rv.Row.RowState == DataRowState.Added) //如果是新增的资料行,永久删除
rv.DataView.Table.Rows.Remove(rv.Row);
rv.DataView.Table.AcceptChanges();
tlMenu.RefreshDataSource();
}
}
#endregion
}
}