using Edu.Model.ViewModel.System;
using Edu.Repository.System;
using System.Collections.Generic;
using System.Linq;
using VT.FW.DB;
namespace Edu.Module.System
{
///
/// 菜单处理类
///
public class MenuModule
{
///
/// 系统菜单仓储层对象
///
private readonly RB_MenuRepository menuRepository = new RB_MenuRepository();
///
/// 菜单功能权限仓储层对象
///
private readonly RB_Menu_FunctionRepository functionRepository = new RB_Menu_FunctionRepository();
///
/// 获取菜单列表
///
///
///
public List GetMenuListModule(RB_Menu_ViewModel query)
{
return menuRepository.GetMenuListRepository(query);
}
///
/// 获取角色权限菜单列表
///
///
/// 角色编号
///
public List GetRoleMenuListModule(RB_Menu_ViewModel query, int roleId = 0)
{
return menuRepository.GetMenuListRepository(query, isQueryPermission: true, roleId: roleId);
}
///
/// 获取菜单分页列表
///
///
///
///
///
///
public List GetMenuPageListModule(int pageIndex, int pageSize, out long rowsCount, RB_Menu_ViewModel query)
{
return menuRepository.GetMenuPageListRepository(pageIndex, pageSize, out rowsCount, query);
}
///
/// 根据岗位编号获取岗位=>岗位角色列表=>角色列表=>角色菜单列表=>菜单列表
///
///
/// 菜单类型
///
public List GetPostMenuListModule(string postIds,int MenuType)
{
return menuRepository.GetPostMenuListRepository(postIds, MenuType);
}
///
/// 新增修改菜单
///
///
///
public virtual bool SetMenuModule(RB_Menu_ViewModel model)
{
bool flag;
if (model.MenuId > 0)
{
Dictionary fileds = new Dictionary()
{
{nameof(RB_Menu_ViewModel.MenuName),model.MenuName.Trim() },
{nameof(RB_Menu_ViewModel.ParentId),model.ParentId },
{nameof(RB_Menu_ViewModel.MenuUrl),model.MenuUrl },
{nameof(RB_Menu_ViewModel.MenuIcon),model.MenuIcon },
{nameof(RB_Menu_ViewModel.MenuLevel),model.MenuLevel },
{nameof(RB_Menu_ViewModel.MenuType),model.MenuType },
{nameof(RB_Menu_ViewModel.SortNum),model.SortNum },
{nameof(RB_Menu_ViewModel.Status),model.Status },
{nameof(RB_Menu_ViewModel.IsUserCenter),model.IsUserCenter },
};
flag = menuRepository.Update(fileds, new WhereHelper(nameof(RB_Menu_ViewModel.MenuId), model.MenuId));
}
else
{
var newId = menuRepository.Insert(model);
model.MenuId = newId;
flag = newId > 0;
}
return flag;
}
///
/// 根据菜单编号获取菜单实体
///
///
///
public RB_Menu_ViewModel GetMenuModule(object MenuId)
{
return menuRepository.GetEntity(MenuId);
}
///
/// 更新菜单状态
///
///
///
///
public bool SetMenuStatusModule(int MenuId, int Status)
{
Dictionary fileds = new Dictionary()
{
{nameof(RB_Menu_ViewModel.Status),Status },
};
bool flag = menuRepository.Update(fileds, new WhereHelper(nameof(RB_Menu_ViewModel.MenuId), MenuId));
return flag;
}
///
/// 获取岗位菜单树形列表
///
///
///
///
public List GetPostMenuTreeModule(RB_Menu_ViewModel query, out List userCenterList, string postIds = "")
{
List list = new List();
if (!string.IsNullOrEmpty(postIds))
{
list = GetPostMenuListModule(postIds,query.MenuType);
}
userCenterList = menuRepository.GetMenuListRepository(new RB_Menu_ViewModel() { IsUserCenter = 1 });
var newList= list.Where(qitem => qitem.MenuType == query.MenuType && qitem.IsUserCenter ==0).ToList();
return GetMenuTreeModule(newList);
}
///
/// 获取树形菜单
///
///
/// 角色编号
/// 岗位编号
///
public List GetTreeMenuModule(RB_Menu_ViewModel query, int roleId = 0)
{
List list;
if (roleId <= 0)
{
list = GetMenuListModule(query);
}
else
{
list = GetRoleMenuListModule(query, roleId: roleId);
}
return GetMenuTreeModule(list);
}
///
/// 获取菜单树形列表
///
///
///
private List GetMenuTreeModule(List list)
{
List treeList = new List();
if (list != null && list.Count > 0)
{
//一级菜单
var firstLevelList = list.Where(qitem => qitem.MenuLevel == 1).OrderBy(qitem=>qitem.SortNum).ToList();
if (firstLevelList != null && firstLevelList.Count > 0)
{
foreach (var fItem in firstLevelList)
{
MenuTree_ViewModel fModel = new MenuTree_ViewModel()
{
MenuId = fItem.MenuId,
MenuName = fItem.MenuName,
MenuUrl = fItem.MenuUrl,
MenuIcon = fItem.MenuIcon,
ParentId = 0,
MenuType = 1,
SubList = new List(),
};
//二级菜单
var secondLevelList = list.Where(qitem => qitem.MenuLevel == 2 && qitem.ParentId == fItem.MenuId).ToList();
if (secondLevelList != null && secondLevelList.Count > 0)
{
foreach (var sItem in secondLevelList.OrderBy(qitem => qitem.SortNum))
{
MenuTree_ViewModel sModel = new MenuTree_ViewModel()
{
MenuId = sItem.MenuId,
MenuName = sItem.MenuName,
MenuUrl = sItem.MenuUrl,
MenuIcon = sItem.MenuIcon,
ParentId = fItem.MenuId,
MenuType = 1,
SubList = new List()
};
//三级菜单
var thirdLevelList = list.Where(qitem => qitem.MenuLevel == 3 && qitem.ParentId == sItem.MenuId).ToList();
if (thirdLevelList != null && thirdLevelList.Count > 0)
{
foreach (var tItem in thirdLevelList.OrderBy(qitem => qitem.SortNum))
{
sModel.SubList.Add(new MenuTree_ViewModel()
{
MenuId = tItem.MenuId,
ParentId = sItem.MenuId,
MenuName = tItem.MenuName,
MenuUrl = tItem.MenuUrl,
MenuIcon = tItem.MenuIcon,
MenuType = 1,
SubList = new List()
});
}
}
fModel.SubList.Add(sModel);
}
}
treeList.Add(fModel);
}
}
}
return treeList;
}
///
/// 获取菜单功能权限分页列表
///
///
///
///
///
///
public List GetMenuFunctionPageListModule(int pageIndex, int pageSize, out long rowsCount, RB_Menu_Function_ViewModel query)
{
return functionRepository.GetMenuFunctionPageListRepository(pageIndex, pageSize, out rowsCount, query);
}
///
/// 获取菜单功能权限列表
///
///
///
public List GetMenuFunctionListModule(RB_Menu_Function_ViewModel query)
{
return functionRepository.GetMenuFunctionListRepository(query);
}
///
/// 根据岗位编号获取岗位角色菜单功能权限
///
/// 岗位编号
///
public List GetPostMenuFunctionListModule(string postIds)
{
return functionRepository.GetPostMenuFunctionListRepository(postIds);
}
///
/// 判断用户是否有某项功能权限
///
/// 权限编码
/// 岗位编号【可以多个】
///
public bool CheckUserFunctionModule(string actionCode, string postIds)
{
return functionRepository.CheckUserFunctionRepository(actionCode, postIds);
}
///
/// 新增修改菜单功能权限
///
///
///
public bool SetMenuFunctionModule(RB_Menu_Function_ViewModel extModel)
{
bool flag;
if (extModel.Id > 0)
{
Dictionary fileds = new Dictionary()
{
{ nameof(RB_Menu_Function_ViewModel.FunctionName),extModel.FunctionName},
{ nameof(RB_Menu_Function_ViewModel.FunctionCode),extModel.FunctionCode},
{ nameof(RB_Menu_Function_ViewModel.Remarks),extModel.Remarks},
};
flag = functionRepository.Update(fileds, new WhereHelper(nameof(RB_Menu_Function_ViewModel.Id), extModel.Id));
}
else
{
var newId = functionRepository.Insert(extModel);
extModel.Id = newId;
flag = newId > 0;
}
return flag;
}
///
/// 根据编号获取菜单功能权限实体
///
///
///
public RB_Menu_Function_ViewModel GetMenuFunctionModule(object Id)
{
return functionRepository.GetEntity(Id);
}
///
/// 设置菜单功能权限状态
///
/// 主键编号
/// 状态(1-禁用,0-启用)
///
public bool SetMenuFunctionStatusModule(object Id, int Status)
{
Dictionary fileds = new Dictionary()
{
{ nameof(RB_Menu_Function_ViewModel.Status),Status},
};
var flag = functionRepository.Update(fileds, new WhereHelper(nameof(RB_Menu_Function_ViewModel.Id), Id));
return flag;
}
}
}