using Infrastructure.Attribute; using Infrastructure.Model; using SqlSugar; using System.Collections.Generic; using System.Linq; using ARW.Model; using ARW.Model.System; namespace ARW.Repository.System { /// /// 角色操作类 /// [AppService(ServiceLifetime = LifeTime.Transient)] public class SysRoleRepository : BaseRepository { /// /// 查询所有角色数据 /// /// public List SelectRoleList() { return Context.Queryable() .Where(role => role.DelFlag == "0") .OrderBy(role => role.RoleSort) .ToList(); } /// /// 根据条件分页查询角色数据 /// /// /// /// public PagedInfo SelectRoleList(SysRole sysRole, PagerInfo pager) { var exp = Expressionable.Create(); exp.And(role => role.DelFlag == "0"); exp.AndIF(!string.IsNullOrEmpty(sysRole.RoleName), role => role.RoleName.Contains(sysRole.RoleName)); exp.AndIF(!string.IsNullOrEmpty(sysRole.Status), role => role.Status == sysRole.Status); exp.AndIF(!string.IsNullOrEmpty(sysRole.RoleKey), role => role.RoleKey == sysRole.RoleKey); var query = Context.Queryable() .Where(exp.ToExpression()) .OrderBy(x => x.RoleSort) .Select((role) => new SysRole { RoleId = role.RoleId.SelectAll(), UserNum = SqlFunc.Subqueryable().Where(f => f.RoleId == role.RoleId).Count() }); return query.ToPage(pager); } /// /// 根据用户查询 /// /// /// public List SelectRolePermissionByUserId(long userId) { return Context.Queryable() .Where(role => role.DelFlag == "0") .Where(it => SqlFunc.Subqueryable().Where(s => s.UserId == userId).Any()) .OrderBy(role => role.RoleSort) .ToList(); } /// /// 通过角色ID查询角色 /// /// 角色编号 /// public SysRole SelectRoleById(long roleId) { return Context.Queryable().InSingle(roleId); } /// /// 通过角色ID删除角色 /// /// /// public int DeleteRoleByRoleIds(long[] roleId) { return Context.Deleteable().In(roleId).ExecuteCommand(); } /// /// 获取用户所有角色信息 /// /// /// public List SelectUserRoleListByUserId(long userId) { return Context.Queryable((ur, r) => new JoinQueryInfos( JoinType.Left, ur.RoleId == r.RoleId )).Where((ur, r) => ur.UserId == userId) .Select((ur, r) => r).ToList(); } #region 用户角色对应菜单 用户N-1角色 /// /// 根据角色获取菜单id /// /// /// public List SelectRoleMenuByRoleId(long roleId) { return Context.Queryable().Where(it => it.Role_id == roleId).ToList(); } /// /// 根据用户所有角色获取菜单 /// /// /// public List SelectRoleMenuByRoleIds(long[] roleIds) { return Context.Queryable().Where(it => roleIds.Contains(it.Role_id)).ToList(); } /// /// 批量插入用户菜单 /// /// /// public int AddRoleMenu(List sysRoleMenus) { return Context.Insertable(sysRoleMenus).ExecuteCommand(); } /// /// 删除角色与菜单关联 /// /// /// public int DeleteRoleMenuByRoleId(long roleId) { return Context.Deleteable().Where(it => it.Role_id == roleId).ExecuteCommand(); } #endregion /// /// 添加角色 /// /// /// public long InsertRole(SysRole sysRole) { sysRole.Create_time = Context.GetDate(); return Context.Insertable(sysRole).ExecuteReturnBigIdentity(); } /// /// 修改用户角色 /// /// /// public int UpdateSysRole(SysRole sysRole) { var db = Context; sysRole.Update_time = db.GetDate(); return db.Updateable() .SetColumns(it => it.Update_time == sysRole.Update_time) .SetColumns(it => it.DataScope == sysRole.DataScope) .SetColumns(it => it.Remark == sysRole.Remark) .SetColumns(it => it.Update_by == sysRole.Update_by) //.SetColumns(it => it.MenuCheckStrictly == sysRole.MenuCheckStrictly) .SetColumns(it => it.DeptCheckStrictly == sysRole.DeptCheckStrictly) .SetColumnsIF(!string.IsNullOrEmpty(sysRole.RoleName), it => it.RoleName == sysRole.RoleName) .SetColumnsIF(!string.IsNullOrEmpty(sysRole.RoleKey), it => it.RoleKey == sysRole.RoleKey) .SetColumnsIF(sysRole.RoleSort >= 0, it => it.RoleSort == sysRole.RoleSort) .Where(it => it.RoleId == sysRole.RoleId) .ExecuteCommand(); } /// /// 更改角色权限状态 /// /// /// /// public int UpdateRoleStatus(SysRole role) { return Context.Updateable(role).UpdateColumns(t => new { t.Status }).ExecuteCommand(); } /// /// 检查角色权限是否存在 /// /// 角色权限 /// public SysRole CheckRoleKeyUnique(string roleKey) { return Context.Queryable().Where(it => it.RoleKey == roleKey).Single(); } /// /// 校验角色名称是否唯一 /// /// 角色名称 /// public SysRole CheckRoleNameUnique(string roleName) { return Context.Queryable().Where(it => it.RoleName == roleName).Single(); } } }