#region
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
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.XtraEditors;
#endregion
namespace CSFrameworkV5.Library.PermissionForms
{
public partial class frmUserRolePermission : frmBaseChild
{
private bllUserRole _BLL;
private bllUser _BLLUser;
private bool _DataChanged;
public frmUserRolePermission()
{
InitializeComponent();
}
private void btnCancelRole_Click(object sender, EventArgs e)
{
if (Msg.AskQuestion("确定要取消操作吗?"))
try
{
frmWaitingEx.ShowMe(this);
DoLoadData(
ConvertEx.ToString(txt_CurrentUser
.EditValue)); //重新加载数据
EnterEditMode(false);
}
finally
{
frmWaitingEx.HideMe(this);
}
}
private void btnEmpty_Click(object sender, EventArgs e)
{
txt_Account.EditValue = null;
txt_Role.EditValue = null;
txt_ExpireFrom.Text = "";
txt_ExpireTo.Text = "";
}
private void btnFindRole_Click(object sender, EventArgs e)
{
var currentUser = ConvertEx.ToString(txt_CurrentUser.EditValue);
LoadRole(currentUser, txtRoleText.Text);
}
private void btnQuery_Click(object sender, EventArgs e)
{
DoQuery();
ShowSummaryPage(false); //显示Summary页面.
if (gvSummary.RowCount == 0) Msg.Warning("没有查到数据!");
}
private void btnSaveRole_Click(object sender, EventArgs e)
{
try
{
frmWaitingEx.ShowMe(this);
var source = gcUserRoles.DataSource as DragDropDataSourceBase;
var dt = source.GetChanges();
if (dt != null)
{
var success = _BLL.Update(dt);
if (success)
DoLoadData(
ConvertEx.ToString(txt_CurrentUser
.EditValue)); //重新加载数据
}
EnterEditMode(false);
Msg.ShowInformation("保存成功!");
}
finally
{
frmWaitingEx.HideMe(this);
}
}
private void btnViewUserRole_Click(object sender, EventArgs e)
{
try
{
frmWaitingEx.ShowMe(this);
//显示用户名称
var account = ConvertEx.ToString(txt_CurrentUser.EditValue);
lcAccount.Text = "ID:" + account;
DoLoadData(account);
}
finally
{
frmWaitingEx.HideMe(this);
}
}
///
/// 删除记录
///
///
public void DoDelete(IButtonInfo sender)
{
//删除查询表格的数据
if (tcBusiness.SelectedTabPage == tpSummary)
{
if (gvSummary.RowCount == 0)
{
Msg.Warning("表格内没有资料!");
return;
}
if (!Msg.AskQuestion("确定要删除吗?")) return;
var account =
ConvertEx.ToString(
gvSummary.GetFocusedRowCellValue(col_Account));
var roleID =
ConvertEx.ToString(
gvSummary.GetFocusedRowCellValue(col_RoleID));
var success = _BLL.DeleteUserRole(account, roleID);
if (success)
{
gvSummary.DeleteRow(gvSummary.FocusedRowHandle);
gcSummary.RefreshDataSource();
//刷新明细表
account = ConvertEx.ToString(txt_CurrentUser.EditValue);
DoLoadData(account);
Msg.ShowInformation("删除成功!");
}
}
//删除明细表格的数据
if (tcBusiness.SelectedTabPage == tpRole)
menuDeleteUserRole.PerformClick();
}
private void DoLoadData(string currentUser)
{
var dtAllRoles = _BLLUser.GetUserRoles4Picker(currentUser, "");
var dtUserRoles = _BLLUser.GetUserRoles(currentUser);
DragDropDataSourceBase availableRoles =
new RoleDataSource(dtAllRoles);
DragDropDataSourceBase userRoles =
new UserRoleDataSource(dtUserRoles, currentUser, true);
//观察数据变动情况
userRoles.OnDataSourceChanged += userRoles_OnDataSourceChanged;
lbAllRole.DisplayMember = "Name";
lbAllRole.ValueMember = "Code";
lbAllRole.DataSource = availableRoles;
gcUserRoles.DataSource = userRoles;
}
private void DoQuery()
{
try
{
frmWaitingEx.ShowMe(this);
//搜索数据
var dt = _BLL.SearchUserRole(
ConvertEx.ToString(txt_Account.EditValue),
ConvertEx.ToString(txt_Role.EditValue),
txt_ExpireFrom.DateTime, txt_ExpireTo.DateTime);
gcSummary.DataSource = null; //绑定主表的Grid
gcSummary.DataSource = dt;
}
finally
{
frmWaitingEx.HideMe(this);
}
}
///
/// 查看选中记录的数据
///
///
public void DoViewContent(IButtonInfo sender)
{
if (tcBusiness.SelectedTabPage == tpSummary)
{
if (gvSummary.RowCount == 0)
{
Msg.Warning("表格内没有资料,请给用户分配角色。");
ShowDetailPage(false);
}
else
{
var currentUser =
ConvertEx.ToString(
gvSummary.GetFocusedRowCellValue(col_Account));
if (!string.IsNullOrEmpty(currentUser))
{
LoadRole(currentUser, "");
txt_CurrentUser.EditValue = currentUser;
ShowDetailPage(false);
btnViewUserRole
.PerformClick(); //显示当前用户的内容
}
}
}
else
{
btnViewUserRole.PerformClick();
}
}
private void EnterEditMode(bool yes)
{
_DataChanged = yes;
btnSaveRole.Enabled = yes;
btnCancelRole.Enabled = yes;
btnViewUserRole.Enabled = !yes;
txt_CurrentUser.Enabled = !yes;
_buttons.GetButtonByName(ButtonNameList.btnClose).Enable = !yes;
_buttons.GetButtonByName(ButtonNameList.btnView).Enable = !yes;
}
private void frmUserRoleManagement_FormClosing(object sender,
FormClosingEventArgs e)
{
if (_DataChanged) e.Cancel = !Msg.AskQuestion("您修改了数据没有保存,确定要退出吗?");
}
private void frmUserRoleManagement_Load(object sender, EventArgs e)
{
_BLL = new bllUserRole();
_BLLUser = new bllUser();
InitButtons();
gvSummary.DoubleClick += gvSummary_DoubleClick;
PermissionBinder.BoundUser(txt_Account);
DataBinder.BindingLookupEditDataSource(txt_Role,
new bllRole().GetLookupData(), tb_MyRole.RoleName,
tb_MyRole.RoleID);
txt_CurrentUser.Properties.PopupControl =
ucCodeNamePicker1.PopupContainer;
txt_CurrentUser.QueryPopUp += txt_CurrentUser_QueryPopUp;
ucCodeNamePicker1.DoSetCaption("帐号", "姓名");
ucCodeNamePicker1.DoSetField(tb_MyUser.Account, tb_MyUser.UserName);
ucCodeNamePicker1.OnShowFiltredData +=
ucCodeNamePicker1_OnShowFiltredData;
ucCodeNamePicker1.OnRowClick += ucCodeNamePicker1_OnRowClick;
ucCodeNamePicker1.DoSetSize(500);
ucCodeNamePicker1.DoShowDefaultData(DataDictCache.Cache.User);
lbAllRole.Items.Clear();
DoQuery(); //查询
EnterEditMode(false);
}
private void gvSummary_DoubleClick(object sender, EventArgs e)
{
if (gvSummary.RowCount > 0) DoViewContent(null);
}
private void gvUserRoles_DoubleClick(object sender, EventArgs e)
{
//双击表格,打开修改窗体
menuEditUserRole.PerformClick();
}
///
/// 初始化数据窗体的按钮
///
public override void InitButtons()
{
base.InitButtons();
var list = new ArrayList();
list.Add(ToolbarRegister.CreateButton(ButtonNameList.btnView, "查看",
ToolBarGroup.数据操作,
Globals.LoadBitmap("32_View.png"), new Size(57, 28), true, true,
DoViewContent));
list.Add(ToolbarRegister.CreateButton(ButtonNameList.btnDelete,
"删除", ToolBarGroup.数据操作,
Globals.LoadBitmap("32_Del.png"), new Size(57, 28), true, true,
DoDelete));
var ii = (IButtonInfo[])list.ToArray(typeof(IButtonInfo));
Buttons.AddRange(ii);
}
private void lbAllRole_DoubleClick(object sender, EventArgs e)
{
//双击移动项目到表格中
Assertion.AssertEditorEmpty(txt_CurrentUser, "请选择用户!", true);
var source = lbAllRole.DataSource as DragDropDataSourceBase;
var dest = gcUserRoles.DataSource as DragDropDataSourceBase;
if (source.Count > 0)
{
var currentUser = ConvertEx.ToString(txt_CurrentUser.EditValue);
var tag = lbAllRole.SelectedItem as ItemExtend;
UserRoleItem U;
if (frmUserRoleEditor.ExecuteAdd(currentUser, tag, out U))
{
dest.AddToListAndTable(U);
source.Remove(tag);
}
}
gcUserRoles.DataSource = null; //刷新表格
gcUserRoles.DataSource = dest;
lbAllRole.DataSource = null;
lbAllRole.DataSource = source;
}
private void LoadRole(string currentUser, string searchText)
{
var dtAllRoles =
_BLLUser.GetUserRoles4Picker(currentUser, searchText);
DragDropDataSourceBase availableRoles =
new RoleDataSource(dtAllRoles);
lbAllRole.DisplayMember = "Name";
lbAllRole.ValueMember = "Code";
lbAllRole.DataSource = availableRoles;
}
private void menuDeleteUserRole_Click(object sender, EventArgs e)
{
//将表格的数据移动到可选角色
if (gvUserRoles.RowCount == 0) return;
var source = gcUserRoles.DataSource as DragDropDataSourceBase;
var dest = lbAllRole.DataSource as DragDropDataSourceBase;
if (source.Count > 0)
{
var o = gvUserRoles.GetRow(gvUserRoles.FocusedRowHandle);
var tag = o as ItemExtend;
dest.AddToListAndTable(tag);
source.Remove(tag);
}
gcUserRoles.RefreshDataSource(); //刷新表格
lbAllRole.DataSource = null;
lbAllRole.DataSource = dest;
}
private void menuEditUserRole_Click(object sender, EventArgs e)
{
//弹出菜单,打开修改窗体
if (gvUserRoles.FocusedRowHandle >= 0)
{
var R =
gvUserRoles.GetRow(gvUserRoles.FocusedRowHandle) as
UserRoleItem;
if (frmUserRoleEditor.ExecuteEdit(R))
{
EnterEditMode(true);
gvUserRoles.UpdateCurrentRow();
}
}
}
private void ShowDetailPage(bool disableSummaryPage)
{
tpRole.PageEnabled = true;
tcBusiness.SelectedTabPage = tpRole;
tpSummary.PageEnabled = !disableSummaryPage;
}
private void ShowSummaryPage(bool disableDetailPage)
{
tpSummary.PageEnabled = true;
tcBusiness.SelectedTabPage = tpSummary;
tpRole.PageEnabled = !disableDetailPage;
}
private void txt_CurrentUser_EditValueChanged(object sender,
EventArgs e)
{
btnViewUserRole.PerformClick();
}
private void txt_CurrentUser_QueryPopUp(object sender,
CancelEventArgs e)
{
ucCodeNamePicker1.DoShowDefaultData(DataDictCache.Cache.User);
}
private void ucCodeNamePicker1_OnRowClick(DataRow clickedRow)
{
txt_CurrentUser.EditValue = clickedRow[tb_MyUser.Account];
ucCodeNamePicker1.Hide();
txt_CurrentUser.ClosePopup();
btnFindRole.PerformClick();
}
private DataTable ucCodeNamePicker1_OnShowFiltredData(string code,
string name)
{
var dt = DataDictCache.Cache.User.Copy();
var filter = "";
if (!string.IsNullOrEmpty(code))
{
if (filter != "") filter = filter + " OR ";
filter = " Account LIKE '%" + code + "%'";
}
if (!string.IsNullOrEmpty(name))
{
if (filter != "") filter = filter + " OR ";
filter = filter + " UserName LIKE '%" + name + "%'";
}
dt.DefaultView.RowFilter = filter;
dt = dt.DefaultView.ToTable();
return dt;
}
private void userRoles_OnDataSourceChanged(DataTable sourceTable,
ItemExtend item, DataRowState state)
{
if (state == DataRowState.Added || state == DataRowState.Deleted)
EnterEditMode(true);
}
#region DragDrop拖放操作
private void lbAllRole_DragDrop(object sender, DragEventArgs e)
{
//拖拉用户的角色到可选角色.
var dest = lbAllRole.DataSource as DragDropDataSourceBase;
var source = gcUserRoles.DataSource as DragDropDataSourceBase;
if (e.Data.GetDataPresent(typeof(UserRoleItem)))
{
var tag = e.Data.GetData(typeof(UserRoleItem)) as UserRoleItem;
dest.AddToListAndTable(tag);
source.Remove(tag);
}
lbAllRole.DataSource = null;
lbAllRole.DataSource = dest;
lbAllRole.SelectedIndex = lbAllRole.ItemCount - 1; //目的地定位到最后一条记录
lbAllRole.Focus();
lbAllRole.Refresh();
gcUserRoles.RefreshDataSource();
}
private void OnList_DragOver(object sender, DragEventArgs e)
{
//拖放到目标控件上触发的事件.
var list = sender as ImageListBoxControl;
var source = list.DataSource as DragDropDataSourceBase;
UserRoleItem tag = null;
if (e.Data.GetDataPresent(typeof(UserRoleItem)))
tag = e.Data.GetData(typeof(UserRoleItem)) as UserRoleItem;
if (e.Data.GetDataPresent(typeof(ItemExtend)))
tag = e.Data.GetData(typeof(ItemExtend)) as UserRoleItem;
if (tag == null || source.IndexOf(tag) >= 0)
e.Effect = DragDropEffects.None;
else
e.Effect = DragDropEffects.Move;
}
private void OnList_MouseDown(object sender, MouseEventArgs e)
{
//点鼠标右键,一次性拖放多个对象.
var list = sender as ImageListBoxControl;
if (e.Button == MouseButtons.Right && list.SelectedItems.Count > 1)
list.DoDragDrop(list.SelectedItems, DragDropEffects.Move);
}
private void OnList_MouseMove(object sender, MouseEventArgs e)
{
//移动鼠标,进入拖拉状态.
var list = sender as ImageListBoxControl;
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 gvUserRoles_MouseDown(object sender, MouseEventArgs e)
{
//点鼠标右键,一次性拖放多个对象.
if (e.Button == MouseButtons.Right &&
gvUserRoles.SelectedRowsCount > 1)
{
var iRows = gvUserRoles.GetSelectedRows();
var rows = new List();
foreach (var i in iRows) rows.Add(gvUserRoles.GetDataRow(i));
gcUserRoles.DoDragDrop(rows, DragDropEffects.Move);
}
}
private void gvUserRoles_MouseMove(object sender, MouseEventArgs e)
{
//移动鼠标,进入拖拉状态.
if (MouseButtons.Left == e.Button &&
gvUserRoles.FocusedRowHandle >= 0)
{
var o = gvUserRoles.GetRow(gvUserRoles.FocusedRowHandle);
gcUserRoles.DoDragDrop(o, DragDropEffects.Move);
}
if (e.Button == MouseButtons.Right &&
gvUserRoles.SelectedRowsCount >= 1)
{
var iRows = gvUserRoles.GetSelectedRows();
var rows = new List();
foreach (var i in iRows) rows.Add(gvUserRoles.GetDataRow(i));
gcUserRoles.DoDragDrop(rows, DragDropEffects.Move);
}
}
private void gcUserRoles_DragOver(object sender, DragEventArgs e)
{
//拖放到目标控件上触发的事件.
//没有用户,不支持拖放
if (txt_CurrentUser.Text == "") return;
var source = gcUserRoles.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 (e.Data.GetDataPresent(typeof(UserRoleItem)))
tag = e.Data.GetData(typeof(UserRoleItem)) as ItemExtend;
if (tag == null || source.IndexOf(tag) >= 0)
e.Effect = DragDropEffects.None;
else
e.Effect = DragDropEffects.Move;
}
private void gcUserRoles_DragDrop(object sender, DragEventArgs e)
{
//拖拉可选角色到组的角色.
var dest = gcUserRoles.DataSource as DragDropDataSourceBase;
var source = lbAllRole.DataSource as DragDropDataSourceBase;
ItemExtend tag = null;
UserRoleItem U = null;
if (e.Data.GetDataPresent(typeof(ItemExtend)))
tag = e.Data.GetData(typeof(ItemExtend)) as ItemExtend;
if (e.Data.GetDataPresent(typeof(UserRoleItem)))
tag = e.Data.GetData(typeof(UserRoleItem)) as UserRoleItem;
if (tag != null)
{
var currentUser = ConvertEx.ToString(txt_CurrentUser.EditValue);
if (frmUserRoleEditor.ExecuteAdd(currentUser, tag, out U))
{
dest.AddToListAndTable(U);
source.Remove(tag);
}
gcUserRoles.DataSource = null;
gcUserRoles.DataSource = dest;
gvUserRoles.MoveLast(); //目的地定位到最后一条记录
lbAllRole.Refresh();
}
}
#endregion
}
}