#region
using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using CSFrameworkV5.Business.BLL_Permission;
using CSFrameworkV5.Common;
using CSFrameworkV5.Core;
using CSFrameworkV5.Library.CommonClass;
using CSFrameworkV5.Models;
using DevExpress.XtraEditors;
using DevExpress.XtraTreeList;
#endregion
namespace CSFrameworkV5.Library.PermissionForms
{
///
/// 组管理及分配权限窗体。
///
public partial class frmGroupPermission : frmBaseDataForm
{
private DataTable _AvaliableRole; //当前组可选的角色
private DataTable _AvaliableUser; //当前组可选的用户
private bllGroup _BLL;
private GroupActionsView _GroupActionsView;
private bllPermission _Permission;
public frmGroupPermission()
{
InitializeComponent();
}
//显示组的权限
private void btnAuthLoad_Click(object sender, EventArgs e)
{
AssertFocusedRow();
try
{
frmWaitingEx.ShowMe(this);
var groupCode =
gvSummary.GetFocusedRowCellDisplayText(colGroupCode);
LoadGroupPermission(groupCode);
if (chkExpandGroupRights.Checked) tlGroup.ExpandAll();
}
finally
{
frmWaitingEx.HideMe(this);
}
}
private void btnEmpty_Click(object sender, EventArgs e)
{
ClearContainerEditorText(pnlSearch);
}
private void btnFindRole_Click(object sender, EventArgs e)
{
if (_AvaliableRole != null)
{
var dt = _AvaliableRole.Copy();
dt.DefaultView.RowFilter =
"RoleID LIKE '%" + txtRoleText.Text +
"%' OR RoleName LIKE '%" +
txtRoleText.Text + "%'";
dt = dt.DefaultView.ToTable();
var groupCode = GetCurrentGroupCode();
DragDropDataSourceBase availableRoles =
new GroupRoleDataSource(dt, groupCode, false);
lbAllRole.DataSource = availableRoles;
lbAllRole.DisplayMember = "Name";
lbAllRole.ValueMember = "Code";
}
}
private void btnFindUser_Click(object sender, EventArgs e)
{
if (_AvaliableUser != null)
{
var dt = _AvaliableUser.Copy();
dt.DefaultView.RowFilter =
"Account LIKE '%" + txtUserText.Text +
"%' OR UserName LIKE '%" +
txtUserText.Text + "%'";
dt = dt.DefaultView.ToTable();
var groupCode = GetCurrentGroupCode();
DragDropDataSourceBase availableUsers =
new GroupUserDataSource(dt, groupCode, false);
lbAllUser.DataSource = availableUsers;
lbAllUser.DisplayMember = "Name";
lbAllUser.ValueMember = "Code";
}
}
private void btnQuery_Click(object sender, EventArgs e)
{
//搜索数据
try
{
frmWaitingEx.ShowMe(this);
var dt = _BLL.Search(
ConvertEx.ToString(txt_GroupCode.EditValue),
ConvertEx.ToString(txt_GroupName.EditValue), "",
ConvertEx.ToString(txt_Owner1.EditValue),
ConvertEx.ToString(txt_OwnerTel1.EditValue));
DoBindingSummaryGrid(dt); //绑定主表的Grid
}
finally
{
frmWaitingEx.HideMe(this);
}
if (gvSummary.RowCount == 0) Msg.Warning("没有找到数据!");
}
public override bool ButtonAuthorized(int authorityValue)
{
if (authorityValue == ButtonAuthority.ADD) return false;
return base.ButtonAuthorized(authorityValue);
}
protected override void ButtonStateChanged(UpdateType currentState)
{
base.ButtonStateChanged(currentState);
SetDetailEditorsAccessable(_DetailGroupControl, DataChanged);
tlGroup.Enabled = IsViewMode;
}
private void chkExpandGroupRights_CheckedChanged(object sender,
EventArgs e)
{
//扩展、收缩组权限树视图
if (chkExpandGroupRights.Checked)
tvRoleActions.ExpandAll();
else
tvRoleActions.CollapseAll();
}
///
/// 新增记录
///
public override void DoAdd(IButtonInfo sender)
{
//
}
public override void DoCancel(IButtonInfo sender)
{
if (Msg.AskQuestion("要取消修改吗?"))
{
InitGroupTreeList(); //初始化树视图
_UpdateType = UpdateType.None;
SetViewMode();
DoViewContent(null);
}
}
///
/// 删除记录
///
///
public override void DoDelete(IButtonInfo sender)
{
AssertFocusedRow(); //检查是否选择一条记录
//调用业务逻辑类删除记录
var groupCode = GetCurrentGroupCode();
var groupName =
gvSummary.GetFocusedRowCellDisplayText(colGroupName);
if (!Msg.AskQuestion("确定要删除当前组 " + groupName + " 的用户及角色资料?"))
return;
var b = _BLL.DeleteGroupRights(groupCode);
AssertEqual(b, true, "删除记录时发生错误!");
if (b)
{
if (_SummaryView.FocusedRowHandle < 0) //删除了最後一条记录. 显示Summary页面.
ShowSummaryPage(true);
else
DoViewContent(
_buttons.GetButtonByName(ButtonNameList.btnView));
Msg.ShowInformation("系统已删除当前组 " + groupName + " 的权限!");
}
}
public override void DoEdit(IButtonInfo sender)
{
AssertFocusedRow();
base.DoEdit(sender);
DoViewContent(sender);
ShowDetailPage(true);
tcBusiness.SelectedTabPage = tpDetail;
}
public virtual void DoInitPermission(IButtonInfo sender)
{
if (Msg.AskQuestion("初始化操作将删除所有系统权限数据,确定要初始化吗?"))
if (_Permission.Init())
InitGroupTreeList(); //预设加载组的资料
}
public override void DoSave(IButtonInfo sender)
{
var success = false;
UpdateLastControl(); //更新最后一个输入控件的数据
var dtGroupUsers =
(lbGroupUser.DataSource as DragDropDataSourceBase).GetChanges();
var dtGroupRoles =
(lbGroupRole.DataSource as DragDropDataSourceBase).GetChanges();
//保存数据
success =
_BLL.Update(_UpdateType, dtGroupUsers,
dtGroupRoles); //调用业务逻辑层的Update方法提交数据
if (success)
{
LoadGroupUser(GetCurrentGroupCode());
LoadGroupRoleData(GetCurrentGroupCode());
base.DoSave(sender);
Msg.ShowInformation("保存成功!");
}
else
{
Msg.Warning("保存失败!");
}
}
public override void DoViewContent(IButtonInfo sender)
{
AssertFocusedRow(); //检查有无记录.
_BLL.CreateDataBinder(
_SummaryView.GetDataRow(_SummaryView.FocusedRowHandle));
//显示编号,名称
txtGroupCode.Text =
gvSummary.GetFocusedRowCellDisplayText(colGroupCode);
txtGroupName.Text =
gvSummary.GetFocusedRowCellDisplayText(colGroupName);
LoadGroupDetails();
base.DoViewContent(sender);
ShowDetailPage(false); //用户点击ViewContent按钮可以显示Summary页
ButtonStateChanged(_UpdateType);
}
private void frmGroupManagement_Load(object sender, EventArgs e)
{
InitializeForm();
SetLanguage();
}
private string GetCurrentGroupCode()
{
var code =
Convert.ToString(
gvSummary.GetFocusedRowCellValue(colGroupCode));
if (string.IsNullOrEmpty(code)) throw new Exception("您没有选择操作的组!");
return code;
}
public override void InitButtons()
{
base.InitButtons();
_buttons.RemoveButton(ButtonNameList.btnAdd); //删除"新增"按钮
//_buttons.GetButtonByName(ButtonNameList.btnAdd).Enable = false;
//管理员,显示“初始化按钮”
if (Loginer.CurrentUser.IsAdmin())
{
var _MyButtons = new IButtonInfo[1];
_MyButtons[0] = ToolbarRegister.CreateButton(
"btnInitPermission", "初始化权限", ToolBarGroup.扩展功能1,
Globals.LoadBitmap("32_InitPermission.png"),
new Size(57, 28), true, true, DoInitPermission);
_buttons.AddRange(_MyButtons);
}
}
private void InitGroupTreeList()
{
var treeData = _BLL.GetSummaryData(true);
tlGroup.LookAndFeel.UseDefaultLookAndFeel = false;
tlGroup.LookAndFeel.UseWindowsXPTheme = true;
tlGroup.OptionsSelection.EnableAppearanceFocusedCell = false;
tlGroup.KeyFieldName = tb_MyGroup.GroupCode; //设置主键
tlGroup.ParentFieldName = tb_MyGroup.ParentGroupCode; //设置父级主键
tlGroup.RootValue = ""; //顶级树结点的值
tlGroup.BeginUpdate();
tlGroup.DataSource = treeData;
DevTreeListView.SetImageIndex(tlGroup, null, 1, 0);
DevStyle.SetTreeListSelectStyle(tlGroup);
tlGroup.EndUpdate();
tlGroup.ExpandAll();
}
///
/// 初始化窗体
///
protected override void InitializeForm()
{
_BLL = new bllGroup();
_Permission = new bllPermission();
_GroupActionsView = new GroupActionsView(tvRoleActions);
_SummaryView = new DevGridView(gvSummary);
frmGridCustomize.RegisterGrid(gvSummary);
gvSummary.DoubleClick += OnGridViewDoubleClick; //主表DoubleClict
BindingSummaryNavigator(controlNavigatorSummary,
gvSummary.GridControl); //Summary导航条.
base.InitializeForm();
PermissionBinder.BoundUser(txt_Owner1);
PermissionBinder.BoundUser(repM_Owner1);
InitGroupTreeList(); //预设加载组的资料
}
private void lbAllRole_DoubleClick(object sender, EventArgs e)
{
if (IsAddOrEditMode)
MoveItem(lbAllRole, lbGroupRole); //将可选角色移动到已选角色
}
private void lbAllUser_DoubleClick(object sender, EventArgs e)
{
if (IsAddOrEditMode)
MoveItem(lbAllUser, lbGroupUser); //将可选用户移动到已选用户
}
private void lbGroupRole_DoubleClick(object sender, EventArgs e)
{
if (IsAddOrEditMode)
MoveItem(lbGroupRole, lbAllRole); //将已选角色移动到可选角色
}
private void lbGroupUser_DoubleClick(object sender, EventArgs e)
{
if (IsAddOrEditMode)
MoveItem(lbGroupUser, lbAllUser); //将已选用户移动到可选用户
}
private void LoadDataByTree()
{
if (tlGroup.FocusedNode != null)
{
var groupCode =
ConvertEx.ToString(
tlGroup.FocusedNode.GetValue(tb_MyGroup.GroupCode));
btnEmpty_Click(btnEmpty, new EventArgs());
txt_GroupCode.EditValue = groupCode;
btnQuery_Click(btnQuery, new EventArgs()); //执行查询
//gcSummary.RefreshDataSource();
if (gvSummary.RowCount > 0)
{
var H = gvSummary.LocateByValue(tb_MyGroup.GroupCode,
groupCode);
if (H >= 0) gvSummary.FocusedRowHandle = H;
}
}
}
///
/// 显示组的明细数据
///
public void LoadGroupDetails()
{
var groupCode = GetCurrentGroupCode();
LoadGroupUser(groupCode);
LoadGroupRoleData(groupCode);
LoadGroupPermission(groupCode);
}
///
/// 移动项目
///
///
///
private void MoveItem(ImageListBoxControl sourceControl,
ImageListBoxControl destControl)
{
var source = sourceControl.DataSource as DragDropDataSourceBase;
var dest = destControl.DataSource as DragDropDataSourceBase;
if (source.Count == 0) return;
var o = sourceControl.SelectedItem as ItemExtend;
if (o != null)
{
dest.AddToListAndTable(o);
source.Remove(o);
destControl.DataSource = null;
destControl.DataSource = dest;
destControl.SelectedIndex =
destControl.ItemCount - 1; //目的地定位到最后一条记录
destControl.Focus();
sourceControl.Update();
}
}
protected override void ShowDetailPage(bool disableSummaryPage)
{
try
{
SuspendLayout();
tpDetail.PageEnabled = true;
tpAllRights.PageEnabled = true;
if (tcBusiness.SelectedTabPage == tpSummary)
tcBusiness.SelectedTabPage = tpDetail;
tpSummary.PageEnabled = !disableSummaryPage;
FocusEditor(); //第一个编辑框获得焦点.
ResumeLayout();
}
catch (Exception ex)
{
ResumeLayout();
Msg.ShowException(ex);
}
}
protected override void ShowSummaryPage(bool disableDetailPage)
{
try
{
SuspendLayout();
tpSummary.PageEnabled = true;
tcBusiness.SelectedTabPage = tpSummary;
tpDetail.PageEnabled = !disableDetailPage;
tpAllRights.PageEnabled = !disableDetailPage;
if (_SummaryView != null) _SummaryView.SetFocus();
ResumeLayout();
}
catch (Exception ex)
{
ResumeLayout();
Msg.ShowException(ex);
}
}
private void tlGroup_FocusedNodeChanged(object sender,
FocusedNodeChangedEventArgs e)
{
try
{
frmWaitingEx.ShowMe(this);
SuspendLayout();
LoadDataByTree();
DoViewContent(null);
}
finally
{
ResumeLayout();
frmWaitingEx.HideMe(this);
}
}
private void tvRoleActions_MouseDown(object sender, MouseEventArgs e)
{
//按下鼠标,设结点为焦点状态
var info = tvRoleActions.HitTest(e.Location);
tvRoleActions.SelectedNode = info.Node;
}
#region DragOver拖放User,Role操作的公共事件
private void OnList_MouseMove(object sender, MouseEventArgs e)
{
//移动鼠标,进入拖拉状态.
var list = sender as ImageListBoxControl;
if (list.AllowDrop == false) return;
if (MouseButtons.Left == e.Button && list.SelectedItem != null)
list.DoDragDrop(list.SelectedItem, DragDropEffects.Move);
if (MouseButtons.Right == e.Button)
list.DoDragDrop(list.SelectedItems, DragDropEffects.Move);
}
private void OnList_MouseDown(object sender, MouseEventArgs e)
{
//点鼠标右键,一次性拖放多个对象.
var list = sender as ImageListBoxControl;
if (list.AllowDrop == false) return;
if (e.Button == MouseButtons.Right && list.SelectedItems.Count > 1)
list.DoDragDrop(list.SelectedItems, DragDropEffects.Move);
}
private void OnList_DragOver(object sender, DragEventArgs e)
{
if (!IsAddOrEditMode) return;
//拖放到目标控件上触发的事件.
var list = sender as ImageListBoxControl;
if (list.AllowDrop == false) return;
var source = list.DataSource as DragDropDataSourceBase;
ItemExtend tag = null;
if (e.Data.GetDataPresent(
typeof(BaseListBoxControl.SelectedItemCollection)))
{
BaseListBoxControl.SelectedItemCollection listSelected;
listSelected =
e.Data.GetData(
typeof(BaseListBoxControl.SelectedItemCollection))
as
BaseListBoxControl.SelectedItemCollection;
tag = listSelected[0] as ItemExtend;
}
if (e.Data.GetDataPresent(typeof(ItemExtend)))
tag = e.Data.GetData(typeof(ItemExtend)) as ItemExtend;
if (tag == null || source.IndexOf(tag) >= 0)
e.Effect = DragDropEffects.None;
else
e.Effect = DragDropEffects.Move;
}
#endregion
#region DragDrop拖放User,Role操作
private void lbGroupRole_DragDrop(object sender, DragEventArgs e)
{
//拖拉可选角色到组的角色.
var dest = lbGroupRole.DataSource as DragDropDataSourceBase;
var source = lbAllRole.DataSource as DragDropDataSourceBase;
if (e.Data.GetDataPresent(typeof(ItemExtend)))
{
var tag = e.Data.GetData(typeof(ItemExtend)) as ItemExtend;
dest.AddToListAndTable(tag);
source.Remove(tag);
}
if (e.Data.GetDataPresent(
typeof(BaseListBoxControl.SelectedItemCollection)))
{
BaseListBoxControl.SelectedItemCollection list;
list =
e.Data.GetData(
typeof(BaseListBoxControl.SelectedItemCollection))
as
BaseListBoxControl.SelectedItemCollection;
foreach (ItemExtend tag in list) dest.AddToListAndTable(tag);
//反向删除
var c = list.Count - 1;
while (c >= 0)
{
source.Remove(list[c]); //删除选择记录
c--;
}
}
//重置数据源
lbGroupRole.DataSource = null;
lbGroupRole.DataSource = dest;
lbGroupRole.SelectedIndex =
lbGroupRole.ItemCount - 1; //目的地定位到最后一条记录
lbGroupRole.Focus();
lbGroupRole.Refresh();
lbAllRole.Refresh();
}
private void lbAllRole_DragDrop(object sender, DragEventArgs e)
{
//拖拉组的角色到可选角色.
var dest = lbAllRole.DataSource as DragDropDataSourceBase;
var source = lbGroupRole.DataSource as DragDropDataSourceBase;
if (e.Data.GetDataPresent(typeof(ItemExtend)))
{
var tag = e.Data.GetData(typeof(ItemExtend)) as ItemExtend;
dest.AddToListAndTable(tag);
source.Remove(tag);
}
if (e.Data.GetDataPresent(
typeof(BaseListBoxControl.SelectedItemCollection)))
{
BaseListBoxControl.SelectedItemCollection list;
list =
e.Data.GetData(
typeof(BaseListBoxControl.SelectedItemCollection))
as
BaseListBoxControl.SelectedItemCollection;
foreach (ItemExtend tag in list) dest.AddToListAndTable(tag);
//反向删除
var c = list.Count - 1;
while (c >= 0)
{
source.Remove(list[c]); //删除选择记录
c--;
}
}
lbAllRole.DataSource = null;
lbAllRole.DataSource = dest;
lbGroupRole.DataSource = null;
lbGroupRole.DataSource = source;
lbAllRole.SelectedIndex = lbAllRole.ItemCount - 1; //目的地定位到最后一条记录
}
private void lbGroupUser_DragDrop(object sender, DragEventArgs e)
{
//拖拉可选用户到组的用户
var dest = lbGroupUser.DataSource as DragDropDataSourceBase;
var source = lbAllUser.DataSource as DragDropDataSourceBase;
if (e.Data.GetDataPresent(typeof(ItemExtend)))
{
var tag = e.Data.GetData(typeof(ItemExtend)) as ItemExtend;
dest.AddToListAndTable(tag);
source.Remove(tag);
}
if (e.Data.GetDataPresent(
typeof(BaseListBoxControl.SelectedItemCollection)))
{
BaseListBoxControl.SelectedItemCollection list;
list =
e.Data.GetData(
typeof(BaseListBoxControl.SelectedItemCollection))
as
BaseListBoxControl.SelectedItemCollection;
foreach (ItemExtend tag in list) dest.AddToListAndTable(tag);
//反向删除
var c = list.Count - 1;
while (c >= 0)
{
source.Remove(list[c]); //删除选择记录
c--;
}
}
//重置数据源
lbGroupUser.DataSource = null;
lbGroupUser.DataSource = dest;
lbGroupUser.SelectedIndex =
lbGroupUser.ItemCount - 1; //目的地定位到最后一条记录
lbGroupUser.Focus();
lbAllUser.Refresh();
lbGroupUser.Refresh();
}
private void lbAllUser_DragDrop(object sender, DragEventArgs e)
{
//拖拉组的用户到可选用户
var dest = lbAllUser.DataSource as DragDropDataSourceBase;
var source = lbGroupUser.DataSource as DragDropDataSourceBase;
if (e.Data.GetDataPresent(typeof(ItemExtend)))
{
var tag = e.Data.GetData(typeof(ItemExtend)) as ItemExtend;
dest.AddToListAndTable(tag); //目的地增加对象
source.Remove(tag); //删除来源对象
}
if (e.Data.GetDataPresent(
typeof(BaseListBoxControl.SelectedItemCollection)))
{
BaseListBoxControl.SelectedItemCollection list;
list =
e.Data.GetData(
typeof(BaseListBoxControl.SelectedItemCollection))
as
BaseListBoxControl.SelectedItemCollection;
foreach (ItemExtend tag in list) dest.AddToListAndTable(tag);
//反向删除
var c = list.Count - 1;
while (c >= 0)
{
source.Remove(list[c]); //删除选择记录
c--;
}
}
lbAllUser.DataSource = null;
lbAllUser.DataSource = dest;
lbGroupUser.DataSource = null;
lbGroupUser.DataSource = source;
lbAllUser.SelectedIndex = lbAllUser.ItemCount - 1; //目的地定位到最后一条记录
}
#endregion
#region Load数据
///
/// 显示组的权限
///
private void LoadGroupPermission(string groupCode)
{
_GroupActionsView.MenuData = _Permission.GetMenuData();
_GroupActionsView.ActionsData = _Permission.GetActionData();
_GroupActionsView.ActionCustomNameData =
_Permission.GetFormTagName();
_GroupActionsView.InitTree("", groupCode);
_GroupActionsView.OnViewMode = true; //设为查看模块,禁止操作树视图
tvRoleActions.Update();
}
//加载组的角色数据.
private void btnRoleLoad_Click(object sender, EventArgs e)
{
try
{
frmWaitingEx.ShowMe(this);
var groupCode = GetCurrentGroupCode();
LoadGroupRoleData(groupCode);
}
finally
{
frmWaitingEx.HideMe(this);
}
}
//加载组的角色数据
private void LoadGroupRoleData(string groupCode)
{
var currentUser = Loginer.CurrentUser.Account;
//string currentUser = "cs";//for test
_AvaliableRole = _BLL.GetGroupRoles4Picker(groupCode, currentUser);
_BLL.GroupRoles = _BLL.GetGroupRoles(groupCode); //组的角色,交易数据
DragDropDataSourceBase availableRoles =
new GroupRoleDataSource(_AvaliableRole, groupCode, false);
DragDropDataSourceBase groupRoles =
new GroupRoleDataSource(_BLL.GroupRoles, groupCode, true);
lbAllRole.DataSource = availableRoles;
lbAllRole.DisplayMember = "Name";
lbAllRole.ValueMember = "Code";
lbGroupRole.DataSource = groupRoles;
lbGroupRole.DisplayMember = "Name";
lbGroupRole.ValueMember = "Code";
}
//加载组的用户数据.
private void btnGroupUserLoad_Click(object sender, EventArgs e)
{
try
{
frmWaitingEx.ShowMe(this);
var groupCode = GetCurrentGroupCode();
LoadGroupUser(groupCode);
}
finally
{
frmWaitingEx.HideMe(this);
}
}
private void LoadGroupUser(string groupCode)
{
_AvaliableUser = _BLL.GetGroupUsers4Picker(groupCode);
_BLL.GroupUsers = _BLL.GetGroupUsers(groupCode); //组的用户,交易数据
DragDropDataSourceBase availableUsers =
new GroupUserDataSource(_AvaliableUser, groupCode, false);
DragDropDataSourceBase groupUsers =
new GroupUserDataSource(_BLL.GroupUsers, groupCode, true);
lbAllUser.DataSource = availableUsers;
lbAllUser.DisplayMember = "Name";
lbAllUser.ValueMember = "Code";
lbGroupUser.DataSource = groupUsers;
lbGroupUser.DisplayMember = "Name";
lbGroupUser.ValueMember = "Code";
}
#endregion
}
}