using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Primitives; using OneOf; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using static LMS.Common.Enums.PermissionEnum; namespace LMS.DAO.PermissionDAO { public class PermissionBasicDao(ApplicationDbContext context) { private readonly ApplicationDbContext _context = context; /// /// 判断指定的ID的权限是不是存在 /// /// /// public async Task CheckPermissionExistById(string id) { if (string.IsNullOrWhiteSpace(id)) { return false; } return await _context.Permission.AnyAsync(x => x.Id == id); } /// /// 判断权限的PermissionCode是不是存在 /// 通过判断是不是有传递ID , 判断是不是需要排除当前的数据 /// /// /// /// public async Task CheckPermissionCodeExist(string permissionCode, string id = null) { if (string.IsNullOrWhiteSpace(permissionCode)) { throw new ArgumentNullException(nameof(permissionCode)); } if (string.IsNullOrEmpty(id)) { return await _context.Permission.AnyAsync(x => x.PermissionCode == permissionCode); } else { return await _context.Permission.AnyAsync(x => x.PermissionCode == permissionCode && x.Id != id); } } /// /// 检查对应分类的权限是不是存在 /// /// /// /// 再修改的时候必传这个值,用于判断对应的关联ID是不是有其他的值,有的话不能修改 /// /// public async Task CheckPermissionByTypeAndId(PType type, OneOf id, string pid = null) { if (id.IsT0) { if (type != PType.Machine) { throw new Exception("请检查参数的对应关系"); } if (!string.IsNullOrEmpty(pid)) { return await _context.Permission.AnyAsync(x => x.MachineId == id.AsT0 && x.Type == type && x.Id != pid); } else { return await _context.Permission.AnyAsync(x => x.MachineId == id.AsT0 && x.Type == type); } } else if (id.IsT1) { if (type == PType.Machine) { throw new Exception("请检查参数的对应关系"); } if (id.AsT1 == null) { return false; } if (!string.IsNullOrEmpty(pid)) { return type switch { PType.User => await _context.Permission.AnyAsync(x => x.UserId == id.AsT1 && x.Type == type && x.Id != pid), PType.Role => await _context.Permission.AnyAsync(x => x.RoleId == id.AsT1 && x.Type == type && x.Id != pid), PType.Machine => throw new Exception("请检查参数的对应关系"), _ => throw new Exception("参数类型错误"), }; } else { return type switch { PType.User => await _context.Permission.AnyAsync(x => x.UserId == id.AsT1 && x.Type == type), PType.Role => await _context.Permission.AnyAsync(x => x.RoleId == id.AsT1 && x.Type == type), PType.Machine => throw new Exception("请检查参数的对应关系"), _ => throw new Exception("参数类型错误"), }; } } else { throw new Exception("参数类型错误"); } } } }