RoleService.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using Admin.Core.Common.Input;
  2. using Admin.Core.Common.Output;
  3. using Admin.Core.Model.Admin;
  4. using Admin.Core.Repository;
  5. using Admin.Core.Repository.Admin;
  6. using Admin.Core.Service.Admin.Role.Input;
  7. using Admin.Core.Service.Admin.Role.Output;
  8. using System.Linq;
  9. using System.Threading.Tasks;
  10. namespace Admin.Core.Service.Admin.Role
  11. {
  12. public class RoleService : BaseService, IRoleService
  13. {
  14. private readonly IRoleRepository _roleRepository;
  15. private readonly IRepositoryBase<RolePermissionEntity> _rolePermissionRepository;
  16. public RoleService(
  17. IRoleRepository roleRepository,
  18. IRepositoryBase<RolePermissionEntity> rolePermissionRepository
  19. )
  20. {
  21. _roleRepository = roleRepository;
  22. _rolePermissionRepository = rolePermissionRepository;
  23. }
  24. public async Task<IResponseOutput> GetAsync(long id)
  25. {
  26. var result = await _roleRepository.GetAsync<RoleGetOutput>(id);
  27. return ResponseOutput.Ok(result);
  28. }
  29. public async Task<IResponseOutput> PageAsync(PageInput<RoleEntity> input)
  30. {
  31. var key = input.Filter?.Name;
  32. var list = await _roleRepository.Select
  33. .WhereIf(key.NotNull(), a => a.Name.Contains(key))
  34. .Count(out var total)
  35. .OrderByDescending(true, c => c.Id)
  36. .Page(input.CurrentPage, input.PageSize)
  37. .ToListAsync<RoleListOutput>();
  38. var data = new PageOutput<RoleListOutput>()
  39. {
  40. List = list,
  41. Total = total
  42. };
  43. return ResponseOutput.Ok(data);
  44. }
  45. public async Task<IResponseOutput> AddAsync(RoleAddInput input)
  46. {
  47. var entity = Mapper.Map<RoleEntity>(input);
  48. var id = (await _roleRepository.InsertAsync(entity)).Id;
  49. return ResponseOutput.Result(id > 0);
  50. }
  51. public async Task<IResponseOutput> UpdateAsync(RoleUpdateInput input)
  52. {
  53. if (!(input?.Id > 0))
  54. {
  55. return ResponseOutput.NotOk();
  56. }
  57. var entity = await _roleRepository.GetAsync(input.Id);
  58. if (!(entity?.Id > 0))
  59. {
  60. return ResponseOutput.NotOk("½ÇÉ«²»´æÔÚ£¡");
  61. }
  62. Mapper.Map(input, entity);
  63. await _roleRepository.UpdateAsync(entity);
  64. return ResponseOutput.Ok();
  65. }
  66. public async Task<IResponseOutput> DeleteAsync(long id)
  67. {
  68. var result = false;
  69. if (id > 0)
  70. {
  71. result = (await _roleRepository.DeleteAsync(m => m.Id == id)) > 0;
  72. }
  73. return ResponseOutput.Result(result);
  74. }
  75. public async Task<IResponseOutput> SoftDeleteAsync(long id)
  76. {
  77. var result = await _roleRepository.SoftDeleteAsync(id);
  78. await _rolePermissionRepository.DeleteAsync(a => a.RoleId == id);
  79. return ResponseOutput.Result(result);
  80. }
  81. public async Task<IResponseOutput> BatchSoftDeleteAsync(long[] ids)
  82. {
  83. var result = await _roleRepository.SoftDeleteAsync(ids);
  84. await _rolePermissionRepository.DeleteAsync(a => ids.Contains(a.RoleId));
  85. return ResponseOutput.Result(result);
  86. }
  87. }
  88. }