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
{
///
/// 角色权限仓储实践
///
public class RoleRightRepository : BaseRepository, IRoleRightRepository
{
private static readonly SqlSugarScope Db = DataContext.Db;
public RoleRightRepository() : base(Db)
{
}
public List GetRoleMenuList(int userId)
{
try
{
var user = Db.Queryable().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(str);
return roleRightDtos.OrderBy(m => m.Ord).ToList();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}
///
/// 获取角色权限信息列表
/// 多表:角色权限、角色、菜单
///
///
public List 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 roleRightDtos = Db.Ado.SqlQuery(str, new
{
isdel = "0", //角色是否删除
});
return roleRightDtos;
}
///
/// 根据角色id获取当前角色所拥有权限
///
/// 角色id
///
public List 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 rolerightlist = Db.Ado.SqlQuery(str, new
{
isdel = "0", //角色权限是否删除
id //id
});
return rolerightlist;
}
///
/// 获取角色权限信息列表(单表)
///
///
public List 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 roleRights = Db.Ado.SqlQuery(str, new
{
roleisdel = "0", //角色是否删除
isdel = "0", //角色权限是否删除
id //id
});
return roleRights;
}
///
/// 根据角色号获取角色权限信息列表(单表)
///
/// 角色号
///
public List GetRoleRightOneListByNo(string RoleNo)
{
string str = "select * from SysRoleRight where RoleNo = @roleno";
List roleRights = Db.Ado.SqlQuery(str, new
{
roleno = RoleNo //角色号
});
return roleRights;
}
///
/// 新增角色权限信息
///
/// 角色权限实体模型
///
public async Task 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;
}
///
/// 删除角色权限信息
///
/// 角色号
///
public async Task DeleteRoleRight(string RoleNo)
{
string str = "delete from SysRoleRight where RoleNo = @roleno";
//删除
int i = await Db.Ado.ExecuteCommandAsync(str, new
{
roleno = RoleNo //角色号
});
return i;
}
///
/// 删除子级角色权限信息
///
/// 角色号
///
public async Task DeleteChildRoleRight(string RoleNo, string MenuNo)
{
try
{
//将菜单号进行分割 存储数组中
var arr = MenuNo.Split(',');
List roleList = Db.Ado.SqlQuery($"select * from SysRoles where CreateUser in (select Id from SysUserInfor where RoleNo='{RoleNo}') ");
foreach (var item in roleList)
{
List roleRightList = Db.Ado.SqlQuery($"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);
}
}
///
/// 分配权限显示
///
///
///
public List GetRoleRightRBAC(string str)
{
List rolerightdto = Db.Ado.SqlQuery(str);
return rolerightdto;
}
}
}