using LFlow.Base.Interfaces; using LFlow.Base.Utils; using LFlow.Cache.Interface; using LFlow.Permission.Model; using Mapster; using SqlSugar; namespace LFlow.Permission.Service { /// /// 角色服务 /// public class PermissionService(IRepo repo, ISelfCache cacher) : IPermissionService { /// /// 添加权限项 /// /// /// public Task AddPermissionAsync(PermissionDto model) { var savedModel = repo.SaveOrUpdate(model.Adapt(), isUpdate: false); var result = savedModel?.MapTo() ?? throw new InvalidOperationException("Failed to add the permission model."); return Task.FromResult(result); } /// /// 删除权限项并清理缓存 /// /// /// public Task DeletePermissionAsync(string id) { cacher.RemoveAsync(id); return Task.FromResult(repo.DeleteById(id)); } /// /// 根据ID获取权限 /// /// /// public async Task GetPermissionAsync(string id) { var cachedPermission = await cacher.GetAsync(id); if (cachedPermission != null) { return cachedPermission!.MapTo()!; } var permission = await Task.FromResult(repo.Get(id)); if (permission != null) { await cacher.SetAsync(id, permission); } return permission?.MapTo() ?? new PermissionDto(); } /// /// 获取权限列表 /// /// /// /// /// public Task> GetPermissionListAsync(int pageIndex, int pageSize, ref int total) { return Task.FromResult(repo.GetAll(pageIndex, pageSize, ref total).Adapt>()); } /// /// 获取程序权限列表 /// /// /// public Task> GetProgPerminssionListAsync(string progID) { return Task.FromResult(repo.Search(new PermissionModel { ProgID = progID }).Adapt>()); } /// /// 更新权限项 /// /// /// public Task UpdatePermissionAsync(PermissionDto model) { if (model != null && !string.IsNullOrEmpty(model.ID)) { cacher.RemoveAsync(model.ID); } if (model != null) { var permissionModel = model?.Adapt() ?? throw new ArgumentNullException(nameof(model)); var savedModel = repo.SaveOrUpdate(permissionModel, isUpdate: true); return Task.FromResult(savedModel?.MapTo() ?? throw new InvalidOperationException("Failed to update the permission model.")); } throw new ArgumentNullException(nameof(model)); } } }