using FineUIPro;
using FineUIPro.iWareWms;
using iWareCommon.Common.Entity;
using iWareDataCore.RBAC.Entity;
using iWareDataCore.RBAC.EnumType;
using iWareDataCore.RBAC.Service;
using System;
using System.Collections.Generic;
using System.Linq;
namespace iWareWms.View.RBAC.RoleContent
{
public partial class RoleContent : PageBase
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadData();
}
}
private void LoadData()
{
BindGridRoles();
if (GridRole.Rows.Count > 0)
{
GridRole.SelectedRowIndex = 0;
}
BindGridMenu();
}
///
/// 查询所有角色
///
private void BindGridRoles()
{
string msg;
var roles = RoleService.GetInstance().QueryByParam(new QueryParam(),out msg);
GridRole.DataSource = roles;
GridRole.DataBind();
}
///
/// 查询所有菜单
///
private void BindGridMenu()
{
try
{
string msg;
var contents = ContentService.GetInstance().QueryByParam(new QueryParam {
Filter = new Dictionary { { "Type", EContentType.用于BS端的菜单 } },
Order = new Dictionary { { "Type", "ASC" }, { "ContentIndex", "ASC" } } }, out msg);
if (!string.IsNullOrEmpty(msg))
{
Alert.ShowInTop(msg);
return;
}
GridMenu.DataSource = contents;
GridMenu.DataBind();
int userRoleId = GetSelectedDataKeyID(GridRole);
List selectArray = new List();
if (userRoleId > -1)
{
var res = RoleContentService.GetInstance().QueryByParam(new QueryParam { Filter = new Dictionary { { "RoleId", userRoleId } } }, out msg);
for (var i = 0; i < res.Count; i++)
{
selectArray.Add(res[i].ContentId.ToString());
}
GridMenu.SelectedRowIDArray = selectArray.ToArray();
}
WriteLog("角色权限更新" + msg, "权限分配");
}
catch (Exception ex)
{
Alert.ShowInTop(ex.Message);
}
}
protected void GridRole_RowClick(object sender, EventArgs e)
{
BindGridMenu();
}
protected void btnSaveClose_Click(object sender, EventArgs e)
{
if (GridMenu.SelectedRowIndexArray.Length < 1)
{
Alert.ShowInTop("请至少选择一项");
return;
}
int roleId = GetSelectedDataKeyID(GridRole);
List ids = new List();
var menuList = new List();
for (int i = 0; i < GridMenu.SelectedRowIndexArray.Length; i++)
{
int index = GridMenu.SelectedRowIndexArray[i];
menuList.Add(new RoleContentEntity { RoleId = roleId, ContentId = Convert.ToInt32(GridMenu.Rows[index].DataKeys[0]), Value = 5 });
}
string msg;
var res = RoleContentService.GetInstance().Save( roleId, menuList, out msg);
Alert.ShowInTop(string.IsNullOrEmpty(msg) ? "保存成功" : msg);
}
protected void GridMenu_RowClick(object sender, GridRowClickEventArgs e)
{
string msg;
var contentDict = ContentService.GetInstance().ToDictionary(new QueryParam(), out msg);
List ids = GetSelectedDataKeyIDs(GridMenu);
var content = contentDict.Values.ToList();
object[] keys = GridMenu.DataKeys[e.RowIndex];
var id = Convert.ToInt32(keys[0]);
var parentId = Convert.ToInt32(keys[2]);
if (!ids.Contains(id) && parentId == -1)
{
var contents = contentDict.Values.Where(x => x.ParentId == Convert.ToInt32(keys[0])).ToList();
if (contents.Count > 0)
{
contents.ForEach(x => ids.Remove(x.Id));
}
}
else
{
if(Convert.ToInt32(keys[2])!=-1){
var contents = contentDict.Values.Where(x => x.Id == Convert.ToInt32(keys[2])).ToList();
ids.Add(contents[0].Id);
}else{
var contents = contentDict.Values.Where(x => x.ParentId == Convert.ToInt32(keys[0])).ToList();
if (contents.Count > 0)
{
contents.ForEach(x => ids.Add(x.Id));
}
}
}
GridMenu.SelectedRowIDArray = ids.ConvertAll(x => x.ToString()).ToArray();
}
///
/// 改变contentType显示信息
///
///
///
protected string GetContentType(object contentType)
{
return ((EContentType)contentType).ToString();
}
}
}