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
{
///
/// 后台菜单
///
[ApiExplorerSettings(GroupName = "Work")]
[Authorize]
public class MenuInfoAppService : ApplicationService
{
private readonly IRepository _menuInfoRepository;
private readonly IRepository _roleMenuInfoRepository;
private readonly ICurrentUser _currentUser;
private readonly IIdentityUserRepository _userRepository;
private readonly IRepository _identityUserRepository;
private readonly IConfiguration _configuration;
private readonly MenuInfoManager _menuInfoManager;
public MenuInfoAppService(
IRepository menuInfoRepository,
IRepository roleMenuInfoRepository,
ICurrentUser currentUser,
MenuInfoManager menuInfoManager,
IIdentityUserRepository userRepository,
IRepository identityUserRepository,
IConfiguration configuration)
{
this._menuInfoRepository = menuInfoRepository;
this._menuInfoManager = menuInfoManager;
this._roleMenuInfoRepository = roleMenuInfoRepository;
this._currentUser = currentUser;
this._userRepository = userRepository;
this._identityUserRepository = identityUserRepository;
this._configuration = configuration;
}
///
/// 查询菜单数据 根据ID
///
///
///
[HttpGet("api/app/menuinfo/getmenuinfoasync")]
public async Task GetMenuInfoAsync(Guid MenuInfoId)
{
var entity = await _menuInfoRepository.GetAsync(MenuInfoId);
var dto = ObjectMapper.Map(entity);
var userList = await _userRepository.GetListAsync();
dto.CreatorName = EntityHelper.GetSurnameNoSql(userList, dto.CreatorId);
dto.LastModifierName = EntityHelper.GetSurnameNoSql(userList, dto.LastModifierId);
return dto;
}
///
/// 创建后台菜单
///
///
///
[HttpPost("api/app/menuinfo/createtmenuinfo")]
public async Task CreatetMenuInfoAsync(CreateMenuInfoDto input)
{
var createEntity = ObjectMapper.Map(input);
var entity = await _menuInfoManager.CreatetMenuInfoAsync(createEntity);
entity = await _menuInfoRepository.InsertAsync(entity);
var dto = ObjectMapper.Map(entity);
return dto;
}
///
/// 修改后台菜单
///
///
///
///
[HttpPost("api/app/menuinfo/updatemenuinfo")]
public async Task UpdateMenuInfoAsync(Guid id, UpdateMenuInfoDto input)
{
var entity = await _menuInfoRepository.GetAsync(id);
var sourceEntity = ObjectMapper.Map(input);
_menuInfoManager.UpdateMenuInfoAsync(sourceEntity, entity);
entity = await _menuInfoRepository.UpdateAsync(entity);
var dto = ObjectMapper.Map(entity);
return dto;
}
///
/// 删除
///
///
///
[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);
}
///
/// 获取当前用户的树型菜单结果
///
///
[HttpGet("api/app/menuinfo/getmymenuinfotreelist")]
public async Task> GetMyMenuInfoTreeListAsync()
{
var dataList = await _menuInfoRepository.GetQueryableAsync();
string AdminId = _configuration.GetValue("AdminId");
if (_currentUser.Id.Value != Guid.Parse(AdminId))
{
var userRoles = await _userRepository.GetRolesAsync(_currentUser.Id.Value); //获取当前用户的角色
if (!userRoles.Any())
return new List();
var userMenuInfoList = await _roleMenuInfoRepository.GetListAsync(m => userRoles.Select(s => s.Id).Contains(m.RoleId)); //当前持有的菜单项
if (!userMenuInfoList.Any())
return new List();
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);
}
///
/// 获取树型菜单结果
///
///
[HttpGet("api/app/menuinfo/getmenuinfotreelist")]
public async Task> 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);
}
///
/// 获取当前用户的菜单列表
///
///
[HttpGet("api/app/menuinfo/getmymenuinfolist")]
public async Task> GetMyMenuInfoListAsync()
{
var query = await _menuInfoRepository.GetQueryableAsync();
query = query.Where(m => m.IsActive == 'Y');
string AdminId = _configuration.GetValue("AdminId");
if (_currentUser.Id.Value != Guid.Parse(AdminId))
{
var userRoles = await _userRepository.GetRolesAsync(_currentUser.Id.Value); //获取当前用户的角色
if (!userRoles.Any())
return new List();
var userMenuInfoList = await _roleMenuInfoRepository.GetListAsync(m => userRoles.Select(s => s.Id).Contains(m.RoleId)); //当前持有的菜单项
if (!userMenuInfoList.Any())
return new List();
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;
}
///
/// 获取指定用户的菜单列表
///
///
[HttpGet("api/app/menuinfo/getmymenuinfolistinuser")]
public async Task> 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();
var userMenuInfoList = await _roleMenuInfoRepository.GetListAsync(m => userRoles.Select(s => s.Id).Contains(m.RoleId)); //当前持有的菜单项
if (!userMenuInfoList.Any())
return new List();
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;
}
///
/// 获取菜单列表
///
///
[HttpGet("api/app/menuinfo/getmenuinfolist")]
public async Task> 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;
}
///
///
///
///
///
///
private List GetMenuInfoTree(List 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();
}
}
}