123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- 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
- {
- /// <summary>
- /// 角色服务
- /// </summary>
- [DynamicApi(Area = "admin")]
- public class RoleService : BaseService, IRoleService, IDynamicApi
- {
- private readonly IRoleRepository _roleRepository;
- private readonly IRepositoryBase<RolePermissionEntity> _rolePermissionRepository;
- public RoleService(
- IRoleRepository roleRepository,
- IRepositoryBase<RolePermissionEntity> rolePermissionRepository
- )
- {
- _roleRepository = roleRepository;
- _rolePermissionRepository = rolePermissionRepository;
- }
- /// <summary>
- /// 查询角色
- /// </summary>
- /// <param name="id"></param>
- /// <returns></returns>
- public async Task<IResultOutput> GetAsync(long id)
- {
- var result = await _roleRepository.GetAsync<RoleGetOutput>(id);
- return ResultOutput.Ok(result);
- }
- /// <summary>
- /// 查询分页
- /// </summary>
- /// <param name="input"></param>
- /// <returns></returns>
- public async Task<IResultOutput> GetPageAsync(PageInput<RoleGetPageDto> 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<RoleListOutput>();
- var data = new PageOutput<RoleListOutput>()
- {
- List = list,
- Total = total
- };
- return ResultOutput.Ok(data);
- }
- /// <summary>
- /// 新增
- /// </summary>
- /// <param name="input"></param>
- /// <returns></returns>
- public async Task<IResultOutput> AddAsync(RoleAddInput input)
- {
- var entity = Mapper.Map<RoleEntity>(input);
- var id = (await _roleRepository.InsertAsync(entity)).Id;
- return ResultOutput.Result(id > 0);
- }
- /// <summary>
- /// 修改
- /// </summary>
- /// <param name="input"></param>
- /// <returns></returns>
- public async Task<IResultOutput> 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();
- }
- /// <summary>
- /// 删除
- /// </summary>
- /// <param name="id"></param>
- /// <returns></returns>
- public async Task<IResultOutput> DeleteAsync(long id)
- {
- var result = false;
- if (id > 0)
- {
- result = (await _roleRepository.DeleteAsync(m => m.Id == id)) > 0;
- }
- return ResultOutput.Result(result);
- }
- /// <summary>
- /// 软删除
- /// </summary>
- /// <param name="id"></param>
- /// <returns></returns>
- public async Task<IResultOutput> SoftDeleteAsync(long id)
- {
- var result = await _roleRepository.SoftDeleteAsync(id);
- await _rolePermissionRepository.DeleteAsync(a => a.RoleId == id);
- return ResultOutput.Result(result);
- }
- /// <summary>
- /// 批量软删除
- /// </summary>
- /// <param name="ids"></param>
- /// <returns></returns>
- public async Task<IResultOutput> BatchSoftDeleteAsync(long[] ids)
- {
- var result = await _roleRepository.SoftDeleteAsync(ids);
- await _rolePermissionRepository.DeleteAsync(a => ids.Contains(a.RoleId));
- return ResultOutput.Result(result);
- }
- }
- }
|