BaoKai_202508_Wms_Jingwang_.../WMS.FrmSystems/FrmRoleDet.cs
2025-08-24 21:52:42 +08:00

483 lines
17 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WMS.Frm.Base;
using DevExpress.XtraTreeList.Nodes;
using WMS.Common;
using DevExpress.XtraGrid.Views.Grid;
using WMS.Business;
using WMS.Business.Base;
namespace WMS.FrmSystems
{
public partial class FrmRoleDet : FormBase
{
#region
static FrmRoleDet frmRoleDet = null;
string strUserId = "";
//角色编号
public static string RoleId;
static string roleName;
DataSet dsRole;
//0 新增 1修改
public static int FrmFlag;
public static string RoleName
{
get { return FrmRoleDet.roleName; }
set { FrmRoleDet.roleName = value; }
}
public static FrmRoleDet GetInstance()
{
frmRoleDet = new FrmRoleDet();
return frmRoleDet;
}
#endregion
#region
public FrmRoleDet()
{
InitializeComponent();
}
#endregion
#region
private void FrmRoleDet_Load(object sender, EventArgs e)
{
#region GridView属性设定
GridViewUser.OptionsBehavior.Editable = true;
GridViewUser.OptionsView.NewItemRowPosition = NewItemRowPosition.Bottom;
GridViewUser.OptionsView.ShowAutoFilterRow = false;
//GridViewUser.itemDeleteRow_Click = new EventHandler(MenuDeleteRow);
GridViewPrice.OptionsBehavior.Editable = true;
GridViewPrice.OptionsSelection.MultiSelect = true;
GridViewPrice.OptionsSelection.MultiSelectMode = DevExpress.XtraGrid.Views.Grid.GridMultiSelectMode.RowSelect;
GridViewPrice.OptionsView.ShowAutoFilterRow = false;
#endregion
string strRole;
#region
if (FrmFlag == 0)
{
}
#endregion
#region
if (FrmFlag == 1)
{
//strRole = WebLockConfig.Instance.WebRole.GetRoleRelevanceInfo(RoleId);
////0员工 1菜单 2操作 3价格 4供应商 5客户 6店铺 7仓库 8商品分类
//dsRole = Newtonsoft.Json.JsonConvert.DeserializeObject<DataSet>(strRole);
txtRole.Text = RoleName;
}
//0员工 1菜单 2操作 3价格 4供应商 5客户 6店铺 7仓库 8商品分类
dsRole = IBussFactory<BussRole>.Instance().GetRoleRelevanceInfo("");
//员工
GridUser.DataSource = dsRole.Tables[0];
//价格
GridPrice.DataSource = dsRole.Tables[3];
//供应商
TreeGYS.DataSource = dsRole.Tables[4];
////客户
//TreeKH.DataSource = dsRole.Tables[5];
////店铺
//TreeDP.DataSource = dsRole.Tables[6];
////仓库
//TreeCK.DataSource = dsRole.Tables[7];
////商品分类
//TreeSPFL.DataSource = dsRole.Tables[8];
//菜单操作 动态绑定
TreeMenu.Nodes[0].Nodes.Clear();
GetMenuRole(dsRole.Tables[1], dsRole.Tables[2], "0", TreeMenu.Nodes[0]);
TreeMenu.Nodes[0].Expanded = true;
#endregion
}
#endregion
#region
/// <summary>
/// 鼠标右键删除事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MenuDeleteRow(object sender, EventArgs e)
{
DataRow[] dr = dsRole.Tables[0].Select("USER_ID = '" + strUserId + "'");
if (dr.Length > 0)
dsRole.Tables[0].Rows.Remove(dr[0]);
}
#endregion
#region +
/// <summary>
/// 价格权限赋值
/// </summary>
private void SetPriceCheck()
{
for (int i = 0; i < GridViewPrice.RowCount; i++)
{
string PriceId = GridViewPrice.GetRowCellValue(i, "PRICE_ID").ToString();
if (dsRole != null)
{
if (dsRole.Tables[3].Rows.Count > 0)
{
if (dsRole.Tables[3].Select("PRICE_ID = '" + PriceId + "'").Length > 0)
{
GridViewPrice.SetRowCellValue(i, "TYPE", true);
}
}
}
}
}
#endregion
#region
/// <summary>
/// 树赋值
/// </summary>
/// <param name="nodes"></param>
/// <param name="dt"></param>
private void SetTreeCheck(TreeListNodes nodes, DataTable dt)
{
for (int i = 0; i < nodes.Count; i++)
{
string id = nodes[i].GetValue("ID").ToString();
if (dt != null)
{
if (dt.Rows.Count > 0)
{
if (dt.Select("ID = '" + id + "'").Length > 0)
{
nodes[i].Checked = true;
}
}
}
SetTreeCheck(nodes[i].Nodes, dt);
}
}
#endregion
//菜单
private void GetMenuRole(DataTable dtMenu, DataTable dtFun, string Fid, TreeListNode nodes)
{
if (dtMenu != null && dtMenu.Rows.Count > 0)
{
DataRow[] dr = dtMenu.Select("P_MENU_ID = '" + Fid + "'", "MENU_SORT");
for (int i = 0; i < dr.Length; i++)
{
TreeListNode Node = nodes.TreeList.AppendNode(dr[i]["MENU_ID"].ToString(), nodes);
Node.SetValue(treeListColumn1, dr[i]["MENU_NAME"].ToString());
if (dr[i]["MENU_ID"].ToString().Length == 4)
{
GetFunRole(dtFun, "-1", dr[i]["MENU_ID"].ToString(), Node);
}
GetMenuRole(dtMenu, dtFun, dr[i]["MENU_ID"].ToString(), Node);
}
}
}
//#region 操作
//private void GetFunRole(DataTable dtFun, string Fid, string MenuId, TreeListNode nodes)
//{
// DataRow[] dr = dtFun.Select("P_FUN_ID = '" + Fid + "'", "FUN_ID");
// for (int i = 0; i < dr.Length; i++)
// {
// TreeListNode Node = nodes.TreeList.AppendNode(dr[i]["FUN_ID"].ToString(), nodes);
// Node.SetValue(treeListColumn1, dr[i]["FUN_NAME"].ToString());
// Node.SetValue("FUN_ID", dr[i]["FUN_ID"].ToString());
// Node.SetValue("MENU_ID", MenuId);
// Node.SetValue("P_FUN_ID", dr[i]["P_FUN_ID"].ToString());
// if (FrmFlag == 1)
// {
// if (dsRole != null)
// {
// if (dsRole.Tables[2].Rows.Count > 0)
// {
// if (dsRole.Tables[2].Select("FUN_ID = '" + dr[i]["FUN_ID"].ToString() + "' and MENU_ID = '" + MenuId + "'").Length > 0)
// {
// Node.Checked = true;
// }
// }
// }
// }
// GetFunRole(dtFun, dr[i]["FUN_ID"].ToString(), MenuId, Node);
// }
//}
//#endregion
//操作
private void GetFunRole(DataTable dtFun, string Fid, string MenuId, TreeListNode nodes)
{
if (dtFun != null && dtFun.Rows.Count > 0)
{
//DataRow[] dr = dtFun.Select("P_FUN_ID = '" + Fid + "' and MENU_ID = '" + MenuId + "'", "FUN_ID");
DataRow[] dr = dtFun.Select(" MENU_ID = '" + MenuId + "'", "FUN_ID");
for (int i = 0; i < dr.Length; i++)
{
TreeListNode Node = nodes.TreeList.AppendNode(dr[i]["FUN_ID"].ToString(), nodes);
Node.SetValue(treeListColumn1, dr[i]["fun_dictory"].ToString());
//FUN_NAME
// GetFunRole(dtFun, dr[i]["FUN_ID"].ToString(), MenuId, Node);
}
}
}
#region
private void TreeMenu_AfterCheckNode(object sender, DevExpress.XtraTreeList.NodeEventArgs e)
{
SetCheckedChildNodes(e.Node, e.Node.CheckState);
SetCheckedParentNodes(e.Node, e.Node.CheckState);
}
#endregion
#region
private void SetCheckedChildNodes(TreeListNode node, CheckState check)
{
for (int i = 0; i < node.Nodes.Count; i++)
{
node.Nodes[i].CheckState = check;
SetCheckedChildNodes(node.Nodes[i], check);
}
}
#endregion
#region
private void SetCheckedParentNodes(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;
if (node.ParentNode.CheckState == CheckState.Indeterminate)
{
node.ParentNode.Checked = true;
}
SetCheckedParentNodes(node.ParentNode, check);
}
}
#endregion
private void TreeMenu_BeforeCheckNode(object sender, DevExpress.XtraTreeList.CheckNodeEventArgs e)
{
e.State = (e.PrevState == CheckState.Checked ? CheckState.Unchecked : CheckState.Checked);
}
#region
private void btnAccept_Click(object sender, EventArgs e)
{
#region
if (txtRole.Text == "")
{
txtRole.Focus();
SystemCommon.ShowInfoMessageBox("请输入角色名称!");
return;
}
if (GridViewUser.RowCount < 1)
{
SystemCommon.ShowInfoMessageBox("请设定角色用户!");
return;
}
#endregion
#region
if (FrmFlag == 0)
{
RoleId = IBussFactory<BussRole>.Instance().GetRoleId();
}
#endregion
#region
string sql = "";
List<string> list = new List<string>();
list.Add(deleteRoleSql());
list.Add(InserRoleSql());
list.Add(InsertUserRoleSql());
list.Add(InsertRoleMenuSql(TreeMenu.Nodes[0].Nodes, ref sql));
sql = "";
string msg = "";
int t= IBussFactory<BussRole>.Instance().InsertRole(list, ref msg);
if (msg == "")
{
SystemCommon.ShowInfoMessageBox("操作成功!");
//关闭窗体
btnClose_Click(sender, e);
}
else
{
SystemCommon.ShowInfoMessageBox("操作失败!");
}
#endregion
}
#endregion
#region SQL语句
#region
private string deleteRoleSql()
{
string sql = "delete from t_base_roledictionary where ROLE_ID = '" + RoleId + "';\r\n";
sql = sql + "delete from t_base_role where ROLE_ID = '" + RoleId + "';\r\n";
sql = sql + "delete from t_base_menurole where ROLE_ID = '" + RoleId + "';\r\n";
sql = sql + "delete from t_base_functionrole where ROLE_ID = '" + RoleId + "';\r\n";
sql = sql + "delete from t_base_Price where ROLE_ID = '" + RoleId + "';\r\n";
sql = sql + "delete from t_base_Provider where ROLE_ID = '" + RoleId + "';\r\n";
//sql = sql + "delete from t_base_Customer where ROLE_ID = '" + RoleId + "';\r\n";
//sql = sql + "delete from t_base_shop where ROLE_ID = '" + RoleId + "';\r\n";
//sql = sql + "delete from t_base_Area where ROLE_ID = '" + RoleId + "';\r\n";
//sql = sql + "delete from t_base_GoodsType where ROLE_ID = '" + RoleId + "';\r\n";
return sql;
}
#endregion
#region
private string InserRoleSql()
{
string sql = " insert into t_base_roledictionary (ROLE_ID,ROLE_NAME) values ('" + RoleId + "','" + txtRole.Text + "');\r\n";
return sql;
}
#endregion
#region
private string InsertUserRoleSql()
{
string sql = "";
for (int i = 0; i < GridViewUser.RowCount - 1; i++)
{
sql = sql + " insert into t_base_role (USER_ID,ROLE_ID,ID) values ('" + GridViewUser.GetRowCellValue(i, "USER_ID").ToString() + "','" + RoleId + "',SEQ_ROLE_USER_ID.NEXTVAL);\r\n";
}
return sql;
}
#endregion
#region
private string InsertRoleMenuSql(TreeListNodes Nodes, ref string sql)
{
for (int i = 0; i < Nodes.Count; i++)
{
if (Nodes[i].Checked)
{
string strMenuId = Nodes[i].GetValue("MENU_ID").ToString();
string strFunId = "";
if (Nodes[i].GetValue("FUN_ID") != null)
{
strFunId = Nodes[i].GetValue("FUN_ID").ToString();
}
if (strFunId != "")
{
sql = sql + " insert into t_base_functionrole (FUN_ID,MENU_ID,ROLE_ID) values ('" + strFunId + "','" + strMenuId + "','" + RoleId + "');\r\n";
}
else
{
sql = sql + " insert into t_base_menurole (MENU_ID,ROLE_ID) values ('" + strMenuId + "','" + RoleId + "');\r\n";
}
}
InsertRoleMenuSql(Nodes[i].Nodes, ref sql);
}
return sql;
}
#endregion
#region
private string InsertRolePriceSql()
{
//string value = "";
string sql = "";
for (int i = 0; i < GridViewPrice.RowCount; i++)
{
//value = GridViewPrice.GetDataRow(i)["TYPE"].ToString();
//if (value == "True")
//{
sql = sql + " insert into t_base_Price (PRICE_ID,ROLE_ID) values ('" + GridViewPrice.GetRowCellValue(i, "PRICE_ID").ToString() + "','" + RoleId + "');\r\n";
//}
}
return sql;
}
#endregion
#region
private string InsertTreeSql(TreeListNodes Nodes, string tableName, string colName, ref string sql)
{
for (int i = 0; i < Nodes.Count; i++)
{
if (Nodes[i].Checked)
{
string strId = Nodes[i].GetValue("ID").ToString();
string strPid = Nodes[i].GetValue("PID").ToString();
if (strId != "-1")
{
if (strPid == "-1" && tableName == "t_base_Area")
{
strPid = strId;
strId = "";
}
sql = sql + " insert into " + tableName + " (" + colName + ",ROLE_ID) values ('" + strId + "','" + strPid + "','" + RoleId + "');\r\n";
}
}
InsertTreeSql(Nodes[i].Nodes, tableName, colName, ref sql);
}
return sql;
}
#endregion
#region
private string InsertSpflSql(TreeListNodes Nodes, ref string sql)
{
for (int i = 0; i < Nodes.Count; i++)
{
if (Nodes[i].Checked)
{
string strId = Nodes[i].GetValue("ID").ToString();
string strPid = Nodes[i].GetValue("PID").ToString();
if (strId != "-1")
{
sql = sql + " insert into t_base_GoodsType (GOO_SORT_ID,ROLE_ID) values ('" + strId + "','" + RoleId + "');\r\n";
}
}
InsertSpflSql(Nodes[i].Nodes, ref sql);
}
return sql;
}
#endregion
#endregion
#region
private void btnClose_Click(object sender, EventArgs e)
{
frmRoleDet.Dispose();
frmRoleDet.Close();
}
#endregion
}
}