TenantService.cs 4.5 KB

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