using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
using System.Threading.Tasks;
|
using Model.ModelDto.SysDto;
|
using Model.ModelVm.SysVm;
|
using SqlSugar;
|
using WMS.Entity.Context;
|
using WMS.Entity.SysEntity;
|
using WMS.IDAL.ISysInterface;
|
|
namespace WMS.DAL.SysInfrastructure
|
{
|
/// <summary>
|
/// 角色权限仓储实践
|
/// </summary>
|
public class RoleRightRepository : BaseRepository<SysRoleRight>, IRoleRightRepository
|
{
|
private static readonly SqlSugarScope Db = DataContext.Db;
|
public RoleRightRepository() : base(Db)
|
{
|
}
|
|
|
public List<RoleRightDto> GetRoleMenuList(int userId)
|
{
|
try
|
{
|
var user = Db.Queryable<SysUserInfor>().First(m => m.Id == userId && m.IsDel == "0");
|
if (user == null)
|
{
|
throw new Exception("未查询到登录人信息");
|
}
|
if (string.IsNullOrWhiteSpace(user.RoleNo))
|
{
|
throw new Exception("未查询到登录人的所属角色信息");
|
}
|
|
string str = $@"select role.Id RoleId, menu.Id MenuId,roleright.RoleNo,roleright.MenuNo,role.RoleName,
|
menu.MenuName,menu.ParentNo,menu.Ord,menu.Url,menu.level,menu.IsEnable,role.Demo RoleDemo,menu.Demo MenuDemo
|
from SysRoleRight roleright
|
join SysFunctionMenu menu on roleright.MenuNo = menu.MenuNo
|
join SysRoles role on roleright.RoleNo = role.RoleNo
|
where role.IsDel = '0' and menu.IsDel = '0'
|
and roleright.IsDel = '0' and roleright.RoleNo = '{user.RoleNo}' ";
|
|
var roleRightDtos = Db.Ado.SqlQuery<RoleRightDto>(str);
|
return roleRightDtos.OrderBy(m => m.Ord).ToList();
|
|
|
}
|
catch (Exception e)
|
{
|
throw new Exception(e.Message);
|
}
|
}
|
|
/// <summary>
|
/// 获取角色权限信息列表
|
/// 多表:角色权限、角色、菜单
|
/// </summary>
|
/// <returns></returns>
|
public List<RoleRightDto> GetRoleRightList()
|
{
|
string str = "select role.Id RoleId,menu.Id MenuId,roleright.RoleNo,roleright.MenuNo,role.RoleName,menu.MenuName,menu.ParentNo,menu.Ord,menu.Url,menu.level,menu.IsEnable,role.Demo RoleDemo,menu.Demo MenuDemo from SysRoleRight roleright join SysFunctionMenu menu on roleright.MenuNo = menu.MenuNo join SysRoles role on roleright.RoleNo = role.RoleNo where role.IsDel = @isdel";
|
|
List<RoleRightDto> roleRightDtos = Db.Ado.SqlQuery<RoleRightDto>(str, new
|
{
|
isdel = "0", //角色是否删除
|
});
|
return roleRightDtos;
|
}
|
|
/// <summary>
|
/// 根据角色id获取当前角色所拥有权限
|
/// </summary>
|
/// <param name="id">角色id</param>
|
/// <returns></returns>
|
public List<RoleRightDto> GetRoleRightListById(int id)
|
{
|
string str = "select * from SysRoleRight roleright left join SysRoles role on roleright.RoleNo = role.RoleNo left join SysFunctionMenu menu1 on menu1.MenuNo = roleright.MenuNo where role.Id = @id and roleright.IsDel = @isdel";
|
List<RoleRightDto> rolerightlist = Db.Ado.SqlQuery<RoleRightDto>(str, new
|
{
|
isdel = "0", //角色权限是否删除
|
id //id
|
});
|
return rolerightlist;
|
}
|
|
/// <summary>
|
/// 获取角色权限信息列表(单表)
|
/// </summary>
|
/// <returns></returns>
|
public List<SysRoleRight> GetRoleRightOneListById(int id)
|
{
|
string str = "select RoleNo,MenuNo from SysRoleRight where Isdel = @isdel and RoleNo = (select RoleNo from SysRoles where IsDel = @roleisdel and Id = @id)";
|
List<SysRoleRight> roleRights = Db.Ado.SqlQuery<SysRoleRight>(str, new
|
{
|
roleisdel = "0", //角色是否删除
|
isdel = "0", //角色权限是否删除
|
id //id
|
});
|
return roleRights;
|
}
|
|
/// <summary>
|
/// 根据角色号获取角色权限信息列表(单表)
|
/// </summary>
|
/// <param name="RoleNo">角色号</param>
|
/// <returns></returns>
|
public List<SysRoleRight> GetRoleRightOneListByNo(string RoleNo)
|
{
|
string str = "select * from SysRoleRight where RoleNo = @roleno";
|
List<SysRoleRight> roleRights = Db.Ado.SqlQuery<SysRoleRight>(str, new
|
{
|
roleno = RoleNo //角色号
|
});
|
return roleRights;
|
}
|
|
/// <summary>
|
/// 新增角色权限信息
|
/// </summary>
|
/// <param name="roleright">角色权限实体模型</param>
|
/// <returns></returns>
|
public async Task<int> InsertRoleRight(SysRoleRight roleright)
|
{
|
string str = "insert into SysRoleRight values(@roleno, @menuno, @isdel, @createtime, @createuser, null, null)";
|
int i = await Db.Ado.ExecuteCommandAsync(str, new
|
{
|
roleno = roleright.RoleNo, //角色号
|
menuno = roleright.MenuNo, //菜单号
|
isdel = "0", //是否删除
|
createtime = Db.GetDate(), //创建时间
|
createuser = roleright.CreateUser //创建人
|
});
|
return i;
|
}
|
|
/// <summary>
|
/// 删除角色权限信息
|
/// </summary>
|
/// <param name="RoleNo">角色号</param>
|
/// <returns></returns>
|
public async Task<int> DeleteRoleRight(string RoleNo)
|
{
|
string str = "delete from SysRoleRight where RoleNo = @roleno";
|
//删除
|
int i = await Db.Ado.ExecuteCommandAsync(str, new
|
{
|
roleno = RoleNo //角色号
|
});
|
return i;
|
}
|
|
/// <summary>
|
/// 删除子级角色权限信息
|
/// </summary>
|
/// <param name="RoleNo">角色号</param>
|
/// <returns></returns>
|
public async Task<int> DeleteChildRoleRight(string RoleNo, string MenuNo)
|
{
|
try
|
{
|
//将菜单号进行分割 存储数组中
|
var arr = MenuNo.Split(',');
|
|
List<SysRoles> roleList = Db.Ado.SqlQuery<SysRoles>($"select * from SysRoles where CreateUser in (select Id from SysUserInfor where RoleNo='{RoleNo}') ");
|
foreach (var item in roleList)
|
{
|
List<SysRoleRight> roleRightList = Db.Ado.SqlQuery<SysRoleRight>($"select * from SysRoleRight where RoleNo='{item.RoleNo}'");
|
foreach (var right in roleRightList)
|
{
|
int first1 = Array.IndexOf(arr, right.MenuNo);
|
if (first1 <= -1)
|
{
|
await Db.Ado.ExecuteCommandAsync($"delete from SysRoleRight where RoleNo = '{item.RoleNo}' and MenuNo='{right.MenuNo}' ");
|
}
|
}
|
}
|
return 1;
|
}
|
catch(Exception ex)
|
{
|
throw new Exception("编辑角色权限信息异常", ex);
|
}
|
}
|
|
/// <summary>
|
/// 分配权限显示
|
/// </summary>
|
/// <param name="str"></param>
|
/// <returns></returns>
|
public List<RoleRightDto> GetRoleRightRBAC(string str)
|
{
|
List<RoleRightDto> rolerightdto = Db.Ado.SqlQuery<RoleRightDto>(str);
|
return rolerightdto;
|
}
|
}
|
}
|