using System.Linq;
using System.Threading.Tasks;
using ZhonTai.Common.Domain.Repositories;
using ZhonTai.Common.Domain.Dto;
using ZhonTai.Plate.Admin.Domain.Role;
using ZhonTai.Plate.Admin.Domain.RolePermission;
using ZhonTai.Plate.Admin.Service.Role.Dto;
using ZhonTai.Plate.Admin.Domain.Role.Dto;
using ZhonTai.Tools.DynamicApi;
using ZhonTai.Tools.DynamicApi.Attributes;
namespace ZhonTai.Plate.Admin.Service.Role
{
///
/// 角色服务
///
[DynamicApi(Area = "admin")]
public class RoleService : BaseService, IRoleService, IDynamicApi
{
private readonly IRoleRepository _roleRepository;
private readonly IRepositoryBase _rolePermissionRepository;
public RoleService(
IRoleRepository roleRepository,
IRepositoryBase rolePermissionRepository
)
{
_roleRepository = roleRepository;
_rolePermissionRepository = rolePermissionRepository;
}
///
/// 查询角色
///
///
///
public async Task GetAsync(long id)
{
var result = await _roleRepository.GetAsync(id);
return ResultOutput.Ok(result);
}
///
/// 查询分页
///
///
///
public async Task GetPageAsync(PageInput input)
{
var key = input.Filter?.Name;
var list = await _roleRepository.Select
.WhereDynamicFilter(input.DynamicFilter)
.WhereIf(key.NotNull(), a => a.Name.Contains(key))
.Count(out var total)
.OrderByDescending(true, c => c.Id)
.Page(input.CurrentPage, input.PageSize)
.ToListAsync();
var data = new PageOutput()
{
List = list,
Total = total
};
return ResultOutput.Ok(data);
}
///
/// 新增
///
///
///
public async Task AddAsync(RoleAddInput input)
{
var entity = Mapper.Map(input);
var id = (await _roleRepository.InsertAsync(entity)).Id;
return ResultOutput.Result(id > 0);
}
///
/// 修改
///
///
///
public async Task UpdateAsync(RoleUpdateInput input)
{
if (!(input?.Id > 0))
{
return ResultOutput.NotOk();
}
var entity = await _roleRepository.GetAsync(input.Id);
if (!(entity?.Id > 0))
{
return ResultOutput.NotOk("角色不存在!");
}
Mapper.Map(input, entity);
await _roleRepository.UpdateAsync(entity);
return ResultOutput.Ok();
}
///
/// 删除
///
///
///
public async Task DeleteAsync(long id)
{
var result = false;
if (id > 0)
{
result = (await _roleRepository.DeleteAsync(m => m.Id == id)) > 0;
}
return ResultOutput.Result(result);
}
///
/// 软删除
///
///
///
public async Task SoftDeleteAsync(long id)
{
var result = await _roleRepository.SoftDeleteAsync(id);
await _rolePermissionRepository.DeleteAsync(a => a.RoleId == id);
return ResultOutput.Result(result);
}
///
/// 批量软删除
///
///
///
public async Task BatchSoftDeleteAsync(long[] ids)
{
var result = await _roleRepository.SoftDeleteAsync(ids);
await _rolePermissionRepository.DeleteAsync(a => ids.Contains(a.RoleId));
return ResultOutput.Result(result);
}
}
}