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.

356 lines
14 KiB

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Shentun.Peis.ItemTypes;
using Shentun.Peis.Models;
using Shentun.Peis.OrganizationUnits;
using Shentun.Peis.RoleMenuInfos;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Identity;
using Volo.Abp.Users;
namespace Shentun.Peis.MenuInfos
{
/// <summary>
/// 后台菜单
/// </summary>
[ApiExplorerSettings(GroupName = "Work")]
[Authorize]
public class MenuInfoAppService : ApplicationService
{
private readonly IRepository<MenuInfo, Guid> _menuInfoRepository;
private readonly IRepository<RoleMenuInfo> _roleMenuInfoRepository;
private readonly ICurrentUser _currentUser;
private readonly IIdentityUserRepository _userRepository;
private readonly IRepository<IdentityUser, Guid> _identityUserRepository;
private readonly IConfiguration _configuration;
private readonly MenuInfoManager _menuInfoManager;
public MenuInfoAppService(
IRepository<MenuInfo, Guid> menuInfoRepository,
IRepository<RoleMenuInfo> roleMenuInfoRepository,
ICurrentUser currentUser,
MenuInfoManager menuInfoManager,
IIdentityUserRepository userRepository,
IRepository<IdentityUser, Guid> identityUserRepository,
IConfiguration configuration)
{
this._menuInfoRepository = menuInfoRepository;
this._menuInfoManager = menuInfoManager;
this._roleMenuInfoRepository = roleMenuInfoRepository;
this._currentUser = currentUser;
this._userRepository = userRepository;
this._identityUserRepository = identityUserRepository;
this._configuration = configuration;
}
/// <summary>
/// 查询菜单数据 根据ID
/// </summary>
/// <param name="MenuInfoId"></param>
/// <returns></returns>
[HttpGet("api/app/menuinfo/getmenuinfoasync")]
public async Task<MenuInfoDto> GetMenuInfoAsync(Guid MenuInfoId)
{
var entity = await _menuInfoRepository.GetAsync(MenuInfoId);
var dto = ObjectMapper.Map<MenuInfo, MenuInfoDto>(entity);
var userList = await _userRepository.GetListAsync();
dto.CreatorName = EntityHelper.GetSurnameNoSql(userList, dto.CreatorId);
dto.LastModifierName = EntityHelper.GetSurnameNoSql(userList, dto.LastModifierId);
return dto;
}
/// <summary>
/// 创建后台菜单
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("api/app/menuinfo/createtmenuinfo")]
public async Task<MenuInfoDto> CreatetMenuInfoAsync(CreateMenuInfoDto input)
{
var createEntity = ObjectMapper.Map<CreateMenuInfoDto, MenuInfo>(input);
var entity = await _menuInfoManager.CreatetMenuInfoAsync(createEntity);
entity = await _menuInfoRepository.InsertAsync(entity);
var dto = ObjectMapper.Map<MenuInfo, MenuInfoDto>(entity);
return dto;
}
/// <summary>
/// 修改后台菜单
/// </summary>
/// <param name="id"></param>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("api/app/menuinfo/updatemenuinfo")]
public async Task<MenuInfoDto> UpdateMenuInfoAsync(Guid id, UpdateMenuInfoDto input)
{
var entity = await _menuInfoRepository.GetAsync(id);
var sourceEntity = ObjectMapper.Map<UpdateMenuInfoDto, MenuInfo>(input);
_menuInfoManager.UpdateMenuInfoAsync(sourceEntity, entity);
entity = await _menuInfoRepository.UpdateAsync(entity);
var dto = ObjectMapper.Map<MenuInfo, MenuInfoDto>(entity);
return dto;
}
/// <summary>
/// 删除
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpPost("api/app/menuinfo/deletemenuinfo")]
public async Task DeleteMenuInfoAsync(Guid id)
{
//判断是否有下级节点
if ((await _menuInfoRepository.GetQueryableAsync()).Where(m => m.ParentId == id).Count() > 0)
throw new UserFriendlyException("当前菜单有下级节点,不能删除");
//删除角色选择的对应的菜单数据
await _roleMenuInfoRepository.DeleteAsync(d => d.MenuInfoId == id);
//删除菜单
await _menuInfoRepository.DeleteAsync(id);
}
/// <summary>
/// 获取当前用户的树型菜单结果
/// </summary>
/// <returns></returns>
[HttpGet("api/app/menuinfo/getmymenuinfotreelist")]
public async Task<List<MenuInfoTreeDto>> GetMyMenuInfoTreeListAsync()
{
var dataList = await _menuInfoRepository.GetQueryableAsync();
string AdminId = _configuration.GetValue<string>("AdminId");
if (_currentUser.Id.Value != Guid.Parse(AdminId))
{
var userRoles = await _userRepository.GetRolesAsync(_currentUser.Id.Value); //获取当前用户的角色
if (!userRoles.Any())
return new List<MenuInfoTreeDto>();
var userMenuInfoList = await _roleMenuInfoRepository.GetListAsync(m => userRoles.Select(s => s.Id).Contains(m.RoleId)); //当前持有的菜单项
if (!userMenuInfoList.Any())
return new List<MenuInfoTreeDto>();
dataList = dataList.Where(m => userMenuInfoList.Select(s => s.MenuInfoId).Distinct().Contains(m.Id));
}
var items = from p in dataList.OrderBy(o => o.DisplayOrder).ThenBy(o => o.DisplayName)
select new MenuInfoTreeDto()
{
Id = p.Id,
ParentId = p.ParentId,
DisplayOrder = p.DisplayOrder,
IconName = p.IconName,
IsActive = p.IsActive,
RouteUrl = p.RouteUrl,
DisplayName = p.DisplayName,
SimpleCode = p.SimpleCode,
MenuType = p.MenuType
};
return GetMenuInfoTree(items.ToList(), null);
}
/// <summary>
/// 获取树型菜单结果
/// </summary>
/// <returns></returns>
[HttpGet("api/app/menuinfo/getmenuinfotreelist")]
public async Task<List<MenuInfoTreeDto>> GetMenuInfoTreeListAsync()
{
var dataList = await _menuInfoRepository.GetQueryableAsync();
var items = from p in dataList.OrderBy(o => o.DisplayOrder).ThenBy(o => o.DisplayName)
select new MenuInfoTreeDto()
{
Id = p.Id,
ParentId = p.ParentId,
DisplayOrder = p.DisplayOrder,
IconName = p.IconName,
IsActive = p.IsActive,
RouteUrl = p.RouteUrl,
DisplayName = p.DisplayName,
SimpleCode = p.SimpleCode,
MenuType = p.MenuType
};
return GetMenuInfoTree(items.ToList(), null);
}
/// <summary>
/// 获取当前用户的菜单列表
/// </summary>
/// <returns></returns>
[HttpGet("api/app/menuinfo/getmymenuinfolist")]
public async Task<List<GetMyMenuInfoListDto>> GetMyMenuInfoListAsync()
{
var query = await _menuInfoRepository.GetQueryableAsync();
query = query.Where(m => m.IsActive == 'Y');
string AdminId = _configuration.GetValue<string>("AdminId");
if (_currentUser.Id.Value != Guid.Parse(AdminId))
{
var userRoles = await _userRepository.GetRolesAsync(_currentUser.Id.Value); //获取当前用户的角色
if (!userRoles.Any())
return new List<GetMyMenuInfoListDto>();
var userMenuInfoList = await _roleMenuInfoRepository.GetListAsync(m => userRoles.Select(s => s.Id).Contains(m.RoleId)); //当前持有的菜单项
if (!userMenuInfoList.Any())
return new List<GetMyMenuInfoListDto>();
query = query.Where(m => userMenuInfoList.Select(s => s.MenuInfoId).Distinct().Contains(m.Id));
}
var entlistdto = query.Select(s => new GetMyMenuInfoListDto
{
DisplayName = s.DisplayName,
DisplayOrder = s.DisplayOrder,
IconName = s.IconName,
Id = s.Id,
MenuType = s.MenuType,
ParentId = s.ParentId,
RouteUrl = s.RouteUrl,
}).OrderBy(o => o.ParentId).ThenBy(o => o.DisplayOrder).ToList();
return entlistdto;
}
/// <summary>
/// 获取指定用户的菜单列表
/// </summary>
/// <returns></returns>
[HttpGet("api/app/menuinfo/getmymenuinfolistinuser")]
public async Task<List<MenuInfoDto>> GetMyMenuInfoListInUserAsync(Guid UserId)
{
var query = from a in await _menuInfoRepository.GetQueryableAsync()
join b in await _identityUserRepository.GetQueryableAsync() on a.CreatorId equals b.Id into bb
from ab in bb.DefaultIfEmpty()
join c in await _identityUserRepository.GetQueryableAsync() on a.LastModifierId equals c.Id into cc
from ac in cc.DefaultIfEmpty()
select new
{
a,
LastModifierName = ac != null ? ac.Surname : "",
CreatorName = ab != null ? ab.Surname : ""
};
var userRoles = await _userRepository.GetRolesAsync(UserId); //获取当前用户的角色
if (!userRoles.Any())
return new List<MenuInfoDto>();
var userMenuInfoList = await _roleMenuInfoRepository.GetListAsync(m => userRoles.Select(s => s.Id).Contains(m.RoleId)); //当前持有的菜单项
if (!userMenuInfoList.Any())
return new List<MenuInfoDto>();
query = query.Where(m => userMenuInfoList.Select(s => s.MenuInfoId).Distinct().Contains(m.a.Id));
var entlistdto = query.Select(s => new MenuInfoDto
{
CreationTime = s.a.CreationTime,
CreatorId = s.a.CreatorId,
DisplayName = s.a.DisplayName,
DisplayOrder = s.a.DisplayOrder,
IconName = s.a.IconName,
Id = s.a.Id,
IsActive = s.a.IsActive,
LastModifierId = s.a.LastModifierId,
LastModificationTime = s.a.LastModificationTime,
MenuType = s.a.MenuType,
ParentId = s.a.ParentId,
RouteUrl = s.a.RouteUrl,
SimpleCode = s.a.SimpleCode,
CreatorName = s.CreatorName,
LastModifierName = s.LastModifierName
}).OrderBy(o => o.DisplayOrder).ThenBy(o => o.DisplayName).ToList();
return entlistdto;
}
/// <summary>
/// 获取菜单列表
/// </summary>
/// <returns></returns>
[HttpGet("api/app/menuinfo/getmenuinfolist")]
public async Task<List<MenuInfoDto>> GetMenuInfoListAsync()
{
var query = from a in await _menuInfoRepository.GetQueryableAsync()
join b in await _identityUserRepository.GetQueryableAsync() on a.CreatorId equals b.Id into bb
from ab in bb.DefaultIfEmpty()
join c in await _identityUserRepository.GetQueryableAsync() on a.LastModifierId equals c.Id into cc
from ac in cc.DefaultIfEmpty()
select new
{
a,
LastModifierName = ac != null ? ac.Surname : "",
CreatorName = ab != null ? ab.Surname : ""
};
var entlistdto = query.Select(s => new MenuInfoDto
{
CreationTime = s.a.CreationTime,
CreatorId = s.a.CreatorId,
DisplayName = s.a.DisplayName,
DisplayOrder = s.a.DisplayOrder,
IconName = s.a.IconName,
Id = s.a.Id,
IsActive = s.a.IsActive,
LastModifierId = s.a.LastModifierId,
LastModificationTime = s.a.LastModificationTime,
MenuType = s.a.MenuType,
ParentId = s.a.ParentId,
RouteUrl = s.a.RouteUrl,
SimpleCode = s.a.SimpleCode,
CreatorName = s.CreatorName,
LastModifierName = s.LastModifierName
}).OrderBy(o => o.DisplayOrder).ThenBy(o => o.DisplayName).ToList();
return entlistdto;
}
/// <summary>
///
/// </summary>
/// <param name="items"></param>
/// <param name="ParentId"></param>
/// <returns></returns>
private List<MenuInfoTreeDto> GetMenuInfoTree(List<MenuInfoTreeDto> items, Guid? ParentId)
{
return (from p in items
where p.ParentId == ParentId
let subs = GetMenuInfoTree(items, p.Id)
select new MenuInfoTreeDto()
{
Id = p.Id,
ParentId = p.ParentId,
DisplayOrder = p.DisplayOrder,
IconName = p.IconName,
IsActive = p.IsActive,
RouteUrl = p.RouteUrl,
DisplayName = p.DisplayName,
SimpleCode = p.SimpleCode,
MenuType = p.MenuType,
TreeChildren = subs.ToList()
}
).ToList();
}
}
}