You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

211 lines
6.9 KiB

using SqlSugar;
using Wood.Cache;
using Wood.Data.Repository;
using Wood.Entity;
using Wood.Entity.SystemManage;
using Wood.Service.BaseService;
using Wood.Service.SystemManage.Dto;
using Wood.Service.SystemManage.Param;
using Wood.Util;
using Wood.Util.JwtAuthorization;
namespace Wood.Service.SystemManage
{
/// <summary>
/// 菜单管理
/// </summary>
public class MenuService : ApiCRUDService<MenuEntity, MenuEntity, MenuAddParam, MenuUpdateParam>
{
public readonly SqlSugarRepository<MenuEntity> _menuRepository;
public readonly SqlSugarRepository<MenuAuthorizeEntity> _menuAuthorizeRepository;
private readonly ICache _cache;
public MenuService(SqlSugarRepository<MenuEntity> menuRepository, SqlSugarRepository<MenuAuthorizeEntity> menuAuthorizeEntity, ICache cache) : base(menuRepository)
{
_menuRepository = menuRepository;
_menuAuthorizeRepository = menuAuthorizeEntity;
_cache = cache;
}
#region 获取数据
/// <summary>
/// 获取所有菜单
/// </summary>
/// <returns>树形结构</returns>
public async Task<List<MenuTreeListDto>> TreeList(MenuTreeListParam param)
{
var user = this.UserInfo();
List<long>? menuIds = null;
//非超级管理员只能查看自己有权限的菜单
if (!user!.IsSuperAdmin)
{
var userCache = _cache.GetCache<UserCache>(user!.CacheKey);
menuIds = await _menuAuthorizeRepository.AsQueryable()
.Where(it => userCache!.Roles.Contains(it.RoleId))
.Select(it => it.MenuId)
.Distinct().ToListAsync();
}
var query = _menuRepository.AsQueryable()
.WhereIF(menuIds != null, it => menuIds!.Contains(it.Id))
.WhereIF(!string.IsNullOrEmpty(param.Name), it => it.MenuName.Contains(param.Name!))
.WhereIF(param.TreeType == 1, it => it.MenuType == MenuTypeEnum.Directory)
.WhereIF(param.TreeType == 2, it => it.MenuType == MenuTypeEnum.Directory || it.MenuType == MenuTypeEnum.Menu)
.OrderBy(it => it.Sort)
.Select(it => new MenuTreeListDto()
{
Id = it.Id.SelectAll()
});
if (param.HasCondition())
return await query.ToListAsync();
else
return await query.ToTreeAsync(it => it.Children, it => it.ParentId, 0);
}
/// <summary>
/// 获取所有菜单
/// 用于El树形结构
/// </summary>
/// <returns>用于El树形结构</returns>
public async Task<List<ElTreeExtraDto>> ElTreeList(MenuTreeListParam param)
{
var user = this.UserInfo();
List<long>? menuIds = null;
//非超级管理员只能查看自己有权限的菜单
if (!user!.IsSuperAdmin)
{
var userCache = _cache.GetCache<UserCache>(user!.CacheKey);
menuIds = await _menuAuthorizeRepository.AsQueryable()
.Where(it => userCache!.Roles.Contains(it.RoleId))
.Select(it => it.MenuId)
.Distinct().ToListAsync();
}
var query = _menuRepository.AsQueryable()
.WhereIF(menuIds != null, it => menuIds!.Contains(it.Id))
.WhereIF(!string.IsNullOrEmpty(param.Name), it => it.MenuName.Contains(param.Name!))
.WhereIF(param.TreeType == 1, it => it.MenuType == MenuTypeEnum.Directory)
.WhereIF(param.TreeType == 2, it => it.MenuType == MenuTypeEnum.Directory || it.MenuType == MenuTypeEnum.Menu)
.OrderBy(it => it.Sort)
.Select(it => new ElTreeExtraDto()
{
Id = it.Id,
Label = it.MenuName,
ParentId = it.ParentId,
Disabled = (it.Status == 0),
Type = (int)it.MenuType
});
if (param.HasCondition())
return await query.ToListAsync();
else
return await query.ToTreeAsync(it => it.Children, it => it.ParentId, 0);
}
/// <summary>
/// 获取一个最大的排序号码
/// </summary>
/// <returns></returns>
public async Task<int> GetMaxSort()
{
var result = await this._menuRepository.AsQueryable().MaxAsync(it => it.Sort);
return result + 10;
}
#endregion
#region 提交数据
/// <summary>
/// 新增菜单
/// </summary>
/// <param name="entity"></param>
/// <returns></returns>
public override async Task Add(MenuAddParam entity)
{
if (await _menuRepository.IsAnyAsync(it => it.ParentId == entity.ParentId && it.MenuName == entity.MenuName))
throw Oops.Oh("名称重复!无法添加。");
MenuEntity? parentEntity = null;
if (entity.ParentId != 0) parentEntity = await _menuRepository.GetByIdAsync(entity.ParentId);
if (entity.MenuType == MenuTypeEnum.Directory)
{
if (parentEntity != null && parentEntity.MenuType != MenuTypeEnum.Directory)
throw Oops.Oh("错误!目录的父级必须是目录!");
}
else if (entity.MenuType == MenuTypeEnum.Menu)
{
if (parentEntity == null)
throw Oops.Oh("错误!菜单必须有所属目录!");
else if (parentEntity.MenuType != MenuTypeEnum.Directory)
throw Oops.Oh("错误!菜单的父级必须是目录!");
}
else
{
if (parentEntity == null)
throw Oops.Oh("错误!按钮必须有所属菜单!");
else if (parentEntity.MenuType != MenuTypeEnum.Menu)
throw Oops.Oh("错误!按钮的父级必须是菜单!");
}
await base.Add(entity);
}
/// <summary>
/// 更新菜单
/// </summary>
/// <param name="entity"></param>
/// <returns></returns>
public override async Task Update(MenuUpdateParam entity)
{
if (await _menuRepository.IsAnyAsync(it => it.ParentId == entity.ParentId && it.MenuName == entity.MenuName && it.Id != entity.Id))
throw Oops.Oh("名称重复!无法更新。");
MenuEntity? parentEntity = null;
if (entity.ParentId != 0) parentEntity = await _menuRepository.GetByIdAsync(entity.ParentId);
if (entity.MenuType == MenuTypeEnum.Directory)
{
if (parentEntity != null && parentEntity.MenuType != MenuTypeEnum.Directory)
throw Oops.Oh("错误!目录的父级必须是目录!");
}
else if (entity.MenuType == MenuTypeEnum.Menu)
{
if (parentEntity == null)
throw Oops.Oh("错误!菜单必须有所属目录!");
else if (parentEntity.MenuType != MenuTypeEnum.Directory)
throw Oops.Oh("错误!菜单的父级必须是目录!");
}
else
{
if (parentEntity == null)
throw Oops.Oh("错误!按钮必须有所属菜单!");
else if (parentEntity.MenuType != MenuTypeEnum.Menu)
throw Oops.Oh("错误!按钮的父级必须是菜单!");
}
await base.Update(entity);
}
/// <summary>
/// 删除菜单 会联动删除菜单角色授权信息
/// 也会删除子集菜单
/// </summary>
/// <param name="param"></param>
/// <returns></returns>
[UnitOfWork]
public override async Task Delete(BaseIdListParam param)
{
if (param.Ids.Any())
{
foreach (var item in param.Ids)
{
var childs = await _menuRepository.AsQueryable().ToChildListAsync(it => it.ParentId, item);
var childIds = childs.Select(it => it.Id).ToList();
await _menuRepository.DeleteAsync(it => childIds.Contains(it.Id));
await _menuAuthorizeRepository.DeleteAsync(it => childIds.Contains(it.MenuId));
}
}
}
#endregion
}
}