TenantService.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using System.Threading.Tasks;
  2. using Admin.Core.Common.Input;
  3. using Admin.Core.Common.Output;
  4. using Admin.Core.Model.Admin;
  5. using Admin.Core.Repository.Admin;
  6. using Admin.Core.Service.Admin.Tenant.Input;
  7. using Admin.Core.Service.Admin.Tenant.Output;
  8. using Admin.Core.Common.Attributes;
  9. using Admin.Core.Common.Helpers;
  10. namespace Admin.Core.Service.Admin.Tenant
  11. {
  12. public class TenantService : BaseService, ITenantService
  13. {
  14. private readonly ITenantRepository _tenantRepository;
  15. private readonly IRoleRepository _roleRepository;
  16. private readonly IUserRepository _userRepository;
  17. private readonly IUserRoleRepository _userRoleRepository;
  18. private readonly IRolePermissionRepository _rolePermissionRepository;
  19. public TenantService(
  20. ITenantRepository tenantRepository,
  21. IRoleRepository roleRepository,
  22. IUserRepository userRepository,
  23. IUserRoleRepository userRoleRepository,
  24. IRolePermissionRepository rolePermissionRepository
  25. )
  26. {
  27. _tenantRepository = tenantRepository;
  28. _roleRepository = roleRepository;
  29. _userRepository = userRepository;
  30. _userRoleRepository = userRoleRepository;
  31. _rolePermissionRepository = rolePermissionRepository;
  32. }
  33. public async Task<IResponseOutput> GetAsync(long id)
  34. {
  35. var result = await _tenantRepository.GetAsync<TenantGetOutput>(id);
  36. return ResponseOutput.Ok(result);
  37. }
  38. public async Task<IResponseOutput> PageAsync(PageInput<TenantEntity> input)
  39. {
  40. var key = input.Filter?.Name;
  41. var list = await _tenantRepository.Select
  42. .WhereIf(key.NotNull(), a => a.Name.Contains(key))
  43. .Count(out var total)
  44. .OrderByDescending(true, c => c.Id)
  45. .Page(input.CurrentPage, input.PageSize)
  46. .ToListAsync<TenantListOutput>();
  47. var data = new PageOutput<TenantListOutput>()
  48. {
  49. List = list,
  50. Total = total
  51. };
  52. return ResponseOutput.Ok(data);
  53. }
  54. [Transaction]
  55. public async Task<IResponseOutput> AddAsync(TenantAddInput input)
  56. {
  57. var entity = Mapper.Map<TenantEntity>(input);
  58. var tenant = await _tenantRepository.InsertAsync(entity);
  59. var tenantId = tenant.Id;
  60. //添加角色
  61. var role = new RoleEntity { TenantId = tenantId, Code = "plat_admin", Name = "平台管理员", Enabled = true };
  62. await _roleRepository.InsertAsync(role);
  63. //添加用户
  64. var pwd = MD5Encrypt.Encrypt32("111111");
  65. var user = new UserEntity { TenantId = tenantId, UserName = input.Phone, NickName= input.RealName, Password = pwd, Status = 0 };
  66. await _userRepository.InsertAsync(user);
  67. //添加用户角色
  68. var userRole = new UserRoleEntity() { UserId = user.Id, RoleId = role.Id };
  69. await _userRoleRepository.InsertAsync(userRole);
  70. //更新租户用户和角色
  71. tenant.UserId = user.Id;
  72. tenant.RoleId = role.Id;
  73. await _tenantRepository.UpdateAsync(tenant);
  74. return ResponseOutput.Ok();
  75. }
  76. public async Task<IResponseOutput> UpdateAsync(TenantUpdateInput input)
  77. {
  78. if (!(input?.Id > 0))
  79. {
  80. return ResponseOutput.NotOk();
  81. }
  82. var entity = await _tenantRepository.GetAsync(input.Id);
  83. if (!(entity?.Id > 0))
  84. {
  85. return ResponseOutput.NotOk("租户不存在!");
  86. }
  87. Mapper.Map(input, entity);
  88. await _tenantRepository.UpdateAsync(entity);
  89. return ResponseOutput.Ok();
  90. }
  91. public async Task<IResponseOutput> DeleteAsync(long id)
  92. {
  93. var result = false;
  94. if (id > 0)
  95. {
  96. result = (await _tenantRepository.DeleteAsync(m => m.Id == id)) > 0;
  97. }
  98. return ResponseOutput.Result(result);
  99. }
  100. public async Task<IResponseOutput> SoftDeleteAsync(long id)
  101. {
  102. var result = await _tenantRepository.SoftDeleteAsync(id);
  103. return ResponseOutput.Result(result);
  104. }
  105. public async Task<IResponseOutput> BatchSoftDeleteAsync(long[] ids)
  106. {
  107. var result = await _tenantRepository.SoftDeleteAsync(ids);
  108. return ResponseOutput.Result(result);
  109. }
  110. }
  111. }