RoleService.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using System.Linq;
  2. using System.Threading.Tasks;
  3. using ZhonTai.Common.Domain.Repositories;
  4. using ZhonTai.Common.Domain.Dto;
  5. using ZhonTai.Plate.Admin.Domain.Role;
  6. using ZhonTai.Plate.Admin.Domain.RolePermission;
  7. using ZhonTai.Plate.Admin.Service.Role.Dto;
  8. using ZhonTai.Plate.Admin.Domain.Role.Dto;
  9. using ZhonTai.Tools.DynamicApi;
  10. using ZhonTai.Tools.DynamicApi.Attributes;
  11. namespace ZhonTai.Plate.Admin.Service.Role
  12. {
  13. /// <summary>
  14. /// 角色服务
  15. /// </summary>
  16. [DynamicApi(Area = "admin")]
  17. public class RoleService : BaseService, IRoleService, IDynamicApi
  18. {
  19. private readonly IRoleRepository _roleRepository;
  20. private readonly IRepositoryBase<RolePermissionEntity> _rolePermissionRepository;
  21. public RoleService(
  22. IRoleRepository roleRepository,
  23. IRepositoryBase<RolePermissionEntity> rolePermissionRepository
  24. )
  25. {
  26. _roleRepository = roleRepository;
  27. _rolePermissionRepository = rolePermissionRepository;
  28. }
  29. /// <summary>
  30. /// 查询角色
  31. /// </summary>
  32. /// <param name="id"></param>
  33. /// <returns></returns>
  34. public async Task<IResultOutput> GetAsync(long id)
  35. {
  36. var result = await _roleRepository.GetAsync<RoleGetOutput>(id);
  37. return ResultOutput.Ok(result);
  38. }
  39. /// <summary>
  40. /// 查询分页
  41. /// </summary>
  42. /// <param name="input"></param>
  43. /// <returns></returns>
  44. public async Task<IResultOutput> GetPageAsync(PageInput<RoleGetPageDto> input)
  45. {
  46. var key = input.Filter?.Name;
  47. var list = await _roleRepository.Select
  48. .WhereDynamicFilter(input.DynamicFilter)
  49. .WhereIf(key.NotNull(), a => a.Name.Contains(key))
  50. .Count(out var total)
  51. .OrderByDescending(true, c => c.Id)
  52. .Page(input.CurrentPage, input.PageSize)
  53. .ToListAsync<RoleListOutput>();
  54. var data = new PageOutput<RoleListOutput>()
  55. {
  56. List = list,
  57. Total = total
  58. };
  59. return ResultOutput.Ok(data);
  60. }
  61. /// <summary>
  62. /// 新增
  63. /// </summary>
  64. /// <param name="input"></param>
  65. /// <returns></returns>
  66. public async Task<IResultOutput> AddAsync(RoleAddInput input)
  67. {
  68. var entity = Mapper.Map<RoleEntity>(input);
  69. var id = (await _roleRepository.InsertAsync(entity)).Id;
  70. return ResultOutput.Result(id > 0);
  71. }
  72. /// <summary>
  73. /// 修改
  74. /// </summary>
  75. /// <param name="input"></param>
  76. /// <returns></returns>
  77. public async Task<IResultOutput> UpdateAsync(RoleUpdateInput input)
  78. {
  79. if (!(input?.Id > 0))
  80. {
  81. return ResultOutput.NotOk();
  82. }
  83. var entity = await _roleRepository.GetAsync(input.Id);
  84. if (!(entity?.Id > 0))
  85. {
  86. return ResultOutput.NotOk("角色不存在!");
  87. }
  88. Mapper.Map(input, entity);
  89. await _roleRepository.UpdateAsync(entity);
  90. return ResultOutput.Ok();
  91. }
  92. /// <summary>
  93. /// 删除
  94. /// </summary>
  95. /// <param name="id"></param>
  96. /// <returns></returns>
  97. public async Task<IResultOutput> DeleteAsync(long id)
  98. {
  99. var result = false;
  100. if (id > 0)
  101. {
  102. result = (await _roleRepository.DeleteAsync(m => m.Id == id)) > 0;
  103. }
  104. return ResultOutput.Result(result);
  105. }
  106. /// <summary>
  107. /// 软删除
  108. /// </summary>
  109. /// <param name="id"></param>
  110. /// <returns></returns>
  111. public async Task<IResultOutput> SoftDeleteAsync(long id)
  112. {
  113. var result = await _roleRepository.SoftDeleteAsync(id);
  114. await _rolePermissionRepository.DeleteAsync(a => a.RoleId == id);
  115. return ResultOutput.Result(result);
  116. }
  117. /// <summary>
  118. /// 批量软删除
  119. /// </summary>
  120. /// <param name="ids"></param>
  121. /// <returns></returns>
  122. public async Task<IResultOutput> BatchSoftDeleteAsync(long[] ids)
  123. {
  124. var result = await _roleRepository.SoftDeleteAsync(ids);
  125. await _rolePermissionRepository.DeleteAsync(a => ids.Contains(a.RoleId));
  126. return ResultOutput.Result(result);
  127. }
  128. }
  129. }