using GxPress.Repository.Interface.SystemRoleMenus;
using GxPress.Service.Interface.Role;
using GxPress.Repository.Interface.SystemRole;
using GxPress.Repository.Interface.Menus;
using System.Threading.Tasks;
using GxPress.Request.Role;
using System.Collections.Generic;
using System.Linq;
using System.Transactions;
using AutoMapper;
namespace GxPress.Service.Implement.Role
{
public class RoleService : IRoleService
{
private readonly ISystemRoleMenusRepository systemRoleMenusRepository;
private readonly IMenusRepository menusRepository;
private readonly ISystemRoleRepository systemRoleRepository;
private readonly IMapper _mapper;
public RoleService(ISystemRoleMenusRepository systemRoleMenusRepository, IMenusRepository menusRepository, ISystemRoleRepository systemRoleRepository,IMapper _mapper)
{
this.systemRoleRepository = systemRoleRepository;
this.systemRoleMenusRepository = systemRoleMenusRepository;
this.menusRepository = menusRepository;
this._mapper=_mapper;
}
///
/// 添加权限菜单关联
///
///
///
public async Task InsertSystemRoleMenusAsync(SystemRoleMenusInRequest models)
{
return await systemRoleMenusRepository.InsertAsync(models);
}
///
/// 添加权限
///
///
///
public async Task InsertSystemRoleAsync(Entity.SystemRole.SystemRole model)
{
return await systemRoleRepository.InsertAsync(model) > 0;
}
///
/// 修改权限
///
///
///
public async Task UpdateSystemRoleAsync(Entity.SystemRole.SystemRole model)
{
return await systemRoleRepository.UpdateAsync(model);
}
///
/// 删除权限
///
///
///
public async Task DeleteSystemRoleAsync(int id)
{
return await systemRoleRepository.DeleteAsync(id);
}
///
/// 添加菜单
///
///
///
public async Task InsertMenusAsync(Entity.Menus.Menus model)
{
return await menusRepository.InsertAsync(model) > 0;
}
///
/// 添加菜单
///
///
///
public async Task InsertMenusAsync(List models)
{
try
{
using (var transactionScope = new TransactionScope())
{
foreach (var item in models)
{
var id = await menusRepository.InsertAsync(item);
if (item.Children == null)
continue;
foreach (var menu in item.Children)
{
await menusRepository.InsertAsync(_mapper.Map(menu));
}
}
transactionScope.Complete();
}
}
catch (System.Exception ex)
{
throw new Common.Exceptions.BusinessException(ex.Message);
}
return true;
}
///
/// 修改菜单
///
///
///
public async Task UpdateMenusAsync(Entity.Menus.Menus model)
{
return await menusRepository.UpdateAsync(model);
}
///
/// 删除菜单
///
///
///
public async Task DeleteMenusAsync(int id)
{
return await menusRepository.DeleteAsync(id);
}
///
/// 根据权限ID获取菜单
///
///
///
public async Task> GetMenusAllAsync(int roleId)
{
//获取
var models = await systemRoleMenusRepository.GetAllAsync(roleId);
return await menusRepository.GetAllAsync(models.Select(n => n.Id).ToList());
}
///
/// 获取所有菜单
///
///
///
public async Task> GetMenusAllAsync()
{
return await menusRepository.GetAllAsync();
}
}
}