LMS.service/LMS.DAO/PermissionDAO/PermissionBasicDao.cs
2024-10-13 17:04:47 +08:00

118 lines
4.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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;
/// <summary>
/// 判断指定的ID的权限是不是存在
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public async Task<bool> CheckPermissionExistById(string id)
{
if (string.IsNullOrWhiteSpace(id))
{
return false;
}
return await _context.Permission.AnyAsync(x => x.Id == id);
}
/// <summary>
/// 判断权限的PermissionCode是不是存在
/// 通过判断是不是有传递ID , 判断是不是需要排除当前的数据
/// </summary>
/// <param name="permissionCode"></param>
/// <param name="id"></param>
/// <returns></returns>
public async Task<bool> 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);
}
}
/// <summary>
/// 检查对应分类的权限是不是存在
/// </summary>
/// <param name="type"></param>
/// <param name="id"></param>
/// <param name="pid">再修改的时候必传这个值用于判断对应的关联ID是不是有其他的值有的话不能修改</param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public async Task<bool> CheckPermissionByTypeAndId(PType type, OneOf<string, long?> 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("参数类型错误");
}
}
}
}