StableVersion4.3/HL_FristAidPlatform_SYS/Form_RoleMain.cs

635 lines
23 KiB
C#

using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.XtraTreeList.Nodes;
using HL_FristAidPlatform_DTO;
using HL_FristAidPlatform_Public;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
namespace HL_FristAidPlatform_SYS
{
public partial class Form_RoleMain : XtraForm
{
#region 变量
/// <summary>
/// 操作标志 1:新增 2:修改
/// </summary>
public int Flag = 1;
/// <summary>
/// 当前操作的角色ID
/// </summary>
public long Cur_RoleID = 0;
/// <summary>
/// 当前操作的角色名称
/// </summary>
public string Cur_RoleName = string.Empty;
/// <summary>
/// 等待窗体
/// </summary>
private Loading loading = new Loading();
#endregion
public Form_RoleMain()
{
InitializeComponent();
}
/// <summary>
/// 窗体加载
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form_RoleMain_Load(object sender, EventArgs e)
{
BindData();
//详情控件初始化
PublicClass.EnabledControl(this.groupR_Detail, true, false);
}
/// <summary>
/// 查询
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_Select_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
BindData();
}
private void txt_Key_TextChanged(object sender, EventArgs e)
{
BindData();
}
private void cmb_DeleteFlag_SelectedIndexChanged(object sender, EventArgs e)
{
BindData();
}
/// <summary>
/// 单击绑定详情
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void grid_Role_MouseClick(object sender, MouseEventArgs e)
{
GridHitInfo hi = grv_Role.CalcHitInfo(new Point(e.X, e.Y));
//单击数据行
if (hi.InRow)//单击的是列头 hi.InColumn
{
PublicClass.EnabledControl(this.groupR_Detail, true, false);
BindDetail();
}
}
/// <summary>
/// 新增
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_Insert_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
PublicClass.EnabledControl(groupR_Detail, false, true);
btn_Delete.Enabled = false;
ckb_DeleteFlag.Checked = true;
Flag = 1;
}
/// <summary>
/// 修改
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_Update_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
BindDetail();
PublicClass.EnabledControl(groupR_Detail, false, false);
btn_Delete.Enabled = false;
}
/// <summary>
/// 删除
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_Delete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
try
{
#region 删除
if (grv_Role.DataRowCount > 0)
{
int selectRow = grv_Role.GetSelectedRows()[0];
int ID = Convert.ToInt32(grv_Role.GetRowCellValue(selectRow, "ID").ToString());
string RoleName = grv_Role.GetRowCellValue(selectRow, "RoleName").ToString();
if (ID == 0)
{
XtraMessageBox.Show("请先选择要删除的角色!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
if (XtraMessageBox.Show(string.Format("确定要删除角色【{0}】?", RoleName), "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
List<T_SYS_RoleDTO> list = new List<T_SYS_RoleDTO>();
T_SYS_RoleDTO model = new T_SYS_RoleDTO();
string Url = "api/admin/T_SYS_Role/LogicalDelete";
model.ID = ID;
model.DeleteFlag = 1;
list.Add(model);
//初始化两个工厂
ClientFactory<T_SYS_RoleDTO> httpClient = new HttpClientFactory<T_SYS_RoleDTO>();
Client<T_SYS_RoleDTO> client = httpClient.VisitFactory();
//访问
ListEntity<T_SYS_RoleDTO> t = client.Post(Url, list);
if (t.Success)
{
XtraMessageBox.Show("删除角色成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
PublicClass.EnabledControl(this.groupR_Detail, true, false);
Flag = 0;
BindData();
}
else
{
XtraMessageBox.Show("删除角色失败,请稍后重试!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
#endregion
}
catch (Exception ex)
{
PublicClass.WriteErrorLog(this.Text, "删除:\r\n" + ex);
}
}
/// <summary>
/// 保存
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_Save_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
try
{
#region 保存
if (string.IsNullOrEmpty(txt_RoleName.Text.ToString().Trim()))
{
XtraMessageBox.Show("角色名称不能为空!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
txt_RoleName.Focus();
return;
}
List<T_SYS_RoleDTO> list = new List<T_SYS_RoleDTO>();
T_SYS_RoleDTO model = new T_SYS_RoleDTO();
string Url = string.Empty;
if (Flag == 1)
{
Url = "api/admin/T_SYS_Role";
model.GUID = Guid.NewGuid().ToString();
}
else
{
Url = "api/admin/T_SYS_Role/Update";
model.ID = Convert.ToInt32(txt_RoleName.Tag.ToString());
model.GUID = txt_Description.Tag.ToString();
}
model.RoleName = txt_RoleName.Text.ToString().Trim();
model.Description = txt_Description.Text.ToString().Trim();
model.DeleteFlag = ckb_DeleteFlag.Checked == true ? 0 : 1;
model.CreationDate = Convert.ToDateTime(PublicClass.DateTimeNow());
model.CreatorID = Information.User.ID;
model.Creator = Information.User.FullName;
string RoleOperatingValue;
string RoleOperatingText;
PublicClass.GetCheckedListBoxItemValues(checkedListBoxControl_RoleOperating, "#", out RoleOperatingValue, out RoleOperatingText);
model.RoleOperating = RoleOperatingValue;
list.Add(model);
//初始化两个工厂
ClientFactory<T_SYS_RoleDTO> httpClient = new HttpClientFactory<T_SYS_RoleDTO>();
Client<T_SYS_RoleDTO> client = httpClient.VisitFactory();
//访问
ListEntity<T_SYS_RoleDTO> t = client.Post(Url, list);
if (t.Success)
{
XtraMessageBox.Show("保存角色信息成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
PublicClass.EnabledControl(this.groupR_Detail, true, false);
Flag = 0;
//列表分页数据绑定
BindData();
btn_Delete.Enabled = true;
//定位
PublicClass.LocationForGridView(grv_Role, model.GUID, 1);
}
else
{
XtraMessageBox.Show("保存失败,请稍后重试!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
#endregion
}
catch (Exception ex)
{
PublicClass.WriteErrorLog(this.Text, "保存:\r\n" + ex);
}
}
/// <summary>
/// 退出
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_Exit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
Close();
}
/// <summary>
/// 显示隐藏 详情
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void label_Show_Templet_Click(object sender, EventArgs e)
{
if (label_Show_Templet.Text == "︾")
{
label_Show_Templet.Text = "︽";
groupR_Detail.Height = 320;
}
else
{
label_Show_Templet.Text = "︾";
groupR_Detail.Height = 20;
}
}
#region 方法
/// <summary>
/// 查询绑定
/// </summary>
private void BindData()
{
BindRole();
BindMenu();
}
/// <summary>
/// 绑定角色
/// </summary>
private void BindRole()
{
try
{
int DeleteFlag = -1;
if (cmb_DeleteFlag.Text.ToString().Trim() == "启用")
DeleteFlag = 0;
else if (cmb_DeleteFlag.Text.ToString().Trim() == "禁用")
DeleteFlag = 1;
else
DeleteFlag = -1;
DataTable ResultDT = DBHelpClass.Get(string.Format("api/admin/T_SYS_Role?keyWord={0}&deleteFlag={1}", txt_Key.Text.ToString().Trim(), DeleteFlag));
grid_Role.DataSource = ResultDT;//显示结果
}
catch (Exception ex)
{
PublicClass.WriteErrorLog(this.Text, "绑定角色:\r\n" + ex);
}
}
/// <summary>
/// 绑定详情
/// </summary>
private void BindDetail()
{
try
{
if (grv_Role.DataRowCount > 0)
{
int selectRow = this.grv_Role.GetSelectedRows()[0];
Cur_RoleID = Convert.ToInt64(grv_Role.GetRowCellValue(selectRow, "ID").ToString());
Cur_RoleName = grv_Role.GetRowCellValue(selectRow, "RoleName").ToString();
#region 角色基本信息
DataTable DetailDT = DBHelpClass.GetDataRow(string.Format("api/admin/T_SYS_Role/{0}", Cur_RoleID));
if (DetailDT != null && DetailDT.Rows.Count > 0)
{
Flag = 2;//修改标识
txt_RoleName.Tag = DetailDT.Rows[0]["ID"].ToString();
txt_RoleName.Text = DetailDT.Rows[0]["RoleName"].ToString();
txt_Description.Tag = DetailDT.Rows[0]["GUID"].ToString();
txt_Description.Text = DetailDT.Rows[0]["Description"].ToString();
ckb_DeleteFlag.Checked = DetailDT.Rows[0]["DeleteFlag"].ToString().Trim() == "0" ? true : false;
string RoleOperatingItems = DetailDT.Rows[0]["RoleOperating"] + "";
PublicClass.SetItemChecked(checkedListBoxControl_RoleOperating, RoleOperatingItems, '#');
}
#endregion
#region 角色菜单信息
BindMenu();
DataTable RoleMenuDT = DBHelpClass.Get(string.Format("/api/admin/T_SYS_RoleMenu/GetList?roleID={0}", Cur_RoleID));
if (RoleMenuDT != null && RoleMenuDT.Rows.Count > 0)
{
SetCheckMenu(RoleMenuDT);
}
#endregion
}
}
catch (Exception ex)
{
PublicClass.WriteErrorLog(this.Text, "绑定详情:\r\n" + ex);
}
}
public void SetCheckMenu(DataTable RoleMenuDT)
{
for (int i = 0; i < RoleMenuDT.Rows.Count; i++)
{
string MenuID = RoleMenuDT.Rows[i]["MenuID"].ToString().Trim();
SetNodeChecked(MenuID, treeList_Menu.Nodes);
}
}
private void SetNodeChecked(string key, TreeListNodes node)
{
foreach (TreeListNode childNode in node)
{
DataRowView data = treeList_Menu.GetDataRecordByNode(childNode) as DataRowView;
if (data["ID"].ToString() == key)
{
childNode.Checked = true;
}
if (childNode.HasChildren)
{
SetNodeChecked(key, childNode.Nodes);
}
}
}
/// <summary>
/// 绑定树形菜单
/// </summary>
private void BindMenu()
{
try
{
DataTable MenuDT = DBHelpClass.Get("api/admin/T_SYS_Menu/GetList");
if (MenuDT != null && MenuDT.Rows.Count > 0)
{
treeList_Menu.DataSource = MenuDT;
treeList_Menu.KeyFieldName = "ID";//是节点id
treeList_Menu.ParentFieldName = "ParentID";//是父节点id
treeList_Menu.ExpandAll();
}
}
catch (Exception ex)
{
PublicClass.WriteErrorLog(this.Text, "绑定树形菜单:\r\n" + ex);
}
}
#endregion
#region 菜单权限树事件
private void treeList_Menu_AfterCheckNode(object sender, DevExpress.XtraTreeList.NodeEventArgs e)
{
SetCheckedChildNodes(e.Node, e.Node.CheckState);
SetCheckedParentNodes(e.Node, e.Node.CheckState);
}
private void treeList_Menu_BeforeCheckNode(object sender, DevExpress.XtraTreeList.CheckNodeEventArgs e)
{
e.State = (e.PrevState == CheckState.Checked ? CheckState.Unchecked : CheckState.Checked);
}
/// <summary>
/// 设置子节点的状态
/// </summary>
/// <param name="node"></param>
/// <param name="check"></param>
private void SetCheckedChildNodes(DevExpress.XtraTreeList.Nodes.TreeListNode node, CheckState check)
{
for (int i = 0; i < node.Nodes.Count; i++)
{
node.Nodes[i].CheckState = check;
SetCheckedChildNodes(node.Nodes[i], check);
}
}
/// <summary>
/// 设置父节点的状态
/// </summary>
/// <param name="node"></param>
/// <param name="check"></param>
private void SetCheckedParentNodes(DevExpress.XtraTreeList.Nodes.TreeListNode node, CheckState check)
{
if (node.ParentNode != null)
{
bool b = false;
CheckState state;
for (int i = 0; i < node.ParentNode.Nodes.Count; i++)
{
state = (CheckState)node.ParentNode.Nodes[i].CheckState;
if (!check.Equals(state))
{
b = !b;
break;
}
}
node.ParentNode.CheckState = b ? CheckState.Indeterminate : check;
SetCheckedParentNodes(node.ParentNode, check);
}
}
#endregion
#region 保存
/// <summary>
/// 保存权限分配按钮事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_SaveRoleMenu_Click(object sender, EventArgs e)
{
try
{
if (Cur_RoleID > 0)
{
loading.ShowMessage("请稍后", "正在进行数据处理...");
if (DeleteRoleMenu(Cur_RoleID))
{
//然后再保存当前赋予的权限
List<string> list = FindOrigin(treeList_Menu, "ID");
foreach (string item in list)
{
SaveRoleMenu(Cur_RoleID, Convert.ToInt64(item));
}
loading.HideMessage();
XtraMessageBox.Show(string.Format("给角色【{0}】分配权限成功!", Cur_RoleName), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
loading.HideMessage();
XtraMessageBox.Show(string.Format("给角色【{0}】分配权限失败,请稍后重试!", Cur_RoleName), "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else
{
XtraMessageBox.Show("请先在左侧列表中选择需要进行权限分配的角色!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
catch (Exception ex)
{
PublicClass.WriteErrorLog(this.Text, "保存权限分配按钮事件:\r\n" + ex);
}
}
/// <summary>
/// 先删除当前角色已经赋予的所有权限
/// <param name="roleID"></param>
/// </summary>
/// <returns></returns>
public bool DeleteRoleMenu(long roleID)
{
try
{
//先删除当前角色已经赋予的所有权限
List<T_SYS_RoleMenuDTO> list = new List<T_SYS_RoleMenuDTO>();
T_SYS_RoleMenuDTO model = new T_SYS_RoleMenuDTO();
string Url = "api/admin/T_SYS_RoleMenu/PhysicalDelete";
model.GUID = Guid.NewGuid().ToString();
model.RoleID = roleID;
model.DeleteFlag = 0;
list.Add(model);
//初始化两个工厂
ClientFactory<T_SYS_RoleMenuDTO> httpClient = new HttpClientFactory<T_SYS_RoleMenuDTO>();
Client<T_SYS_RoleMenuDTO> client = httpClient.VisitFactory();
//访问
ListEntity<T_SYS_RoleMenuDTO> t = client.Post(Url, list);
return t.Success;
}
catch (Exception ex)
{
PublicClass.WriteErrorLog(this.Text, "先删除当前角色已经赋予的所有权限:\r\n" + ex);
return false;
}
}
/// <summary>
/// 保存权限
/// </summary>
/// <param name="roleID"></param>
/// <param name="menuID"></param>
/// <returns></returns>
private bool SaveRoleMenu(long roleID, long menuID)
{
try
{
List<T_SYS_RoleMenuDTO> list = new List<T_SYS_RoleMenuDTO>();
T_SYS_RoleMenuDTO model = new T_SYS_RoleMenuDTO();
string Url = "api/admin/T_SYS_RoleMenu";
model.GUID = Guid.NewGuid().ToString();
model.RoleID = roleID;
model.MenuID = menuID;
model.DeleteFlag = 0;
list.Add(model);
//初始化两个工厂
ClientFactory<T_SYS_RoleMenuDTO> httpClient = new HttpClientFactory<T_SYS_RoleMenuDTO>();
Client<T_SYS_RoleMenuDTO> client = httpClient.VisitFactory();
//访问
ListEntity<T_SYS_RoleMenuDTO> t = client.Post(Url, list);
return t.Success;
}
catch (Exception ex)
{
PublicClass.WriteErrorLog(this.Text, "保存权限:\r\n" + ex);
return false;
}
}
#endregion
#region 获取树中所有的选择节点值
/// <summary>
/// 获取选择状态的数据主键ID集合
/// </summary>
/// <param name="parentNode">父级节点</param>
/// <param name="columnFiled">绑定的字段</param>
private void GetCheckedKeyID(DevExpress.XtraTreeList.TreeList treeList, TreeListNode parentNode, string columnFiled, ref List<string> lstCheckedKeyID)
{
if (parentNode.Nodes.Count == 0)
{
return;//递归终止
}
if (parentNode.CheckState != CheckState.Unchecked)
{
DataRowView drv = treeList.GetDataRecordByNode(parentNode) as DataRowView;
if (drv != null)
{
string KeyFieldName = (string)drv[columnFiled];
if (!lstCheckedKeyID.Contains(KeyFieldName))
{
lstCheckedKeyID.Add(KeyFieldName);
}
}
}
foreach (TreeListNode node in parentNode.Nodes)
{
if (node.CheckState != CheckState.Unchecked)
{
DataRowView drv = treeList.GetDataRecordByNode(node) as DataRowView;//关键代码,就是不知道是这样获取数据而纠结了很久(鬼知道可以转换为DataRowView啊)
if (drv != null)
{
string KeyFieldName = (string)drv[columnFiled];
lstCheckedKeyID.Add(KeyFieldName);
}
}
GetCheckedKeyID(treeList, node, columnFiled, ref lstCheckedKeyID);
}
}
/// <summary>
/// 获取选中的节点
/// </summary>
/// <param name="tree">树</param>
/// <param name="columnFiled">绑定的字段</param>
private List<string> FindOrigin(DevExpress.XtraTreeList.TreeList treeList, string columnFiled)
{
List<string> lstCheckedKeyID = new List<string>();//选择局ID集合
lstCheckedKeyID.Clear();
if (treeList.Nodes.Count > 0)
{
foreach (TreeListNode nodes in treeList.Nodes)
{
GetCheckedKeyID(treeList, nodes, columnFiled, ref lstCheckedKeyID);
}
}
return lstCheckedKeyID;
}
#endregion
}
}