TenantService.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. using System.Linq;
  2. using System.Threading.Tasks;
  3. using ZhonTai.Admin.Core.Attributes;
  4. using ZhonTai.Admin.Core.Repositories;
  5. using ZhonTai.Common.Helpers;
  6. using ZhonTai.Admin.Core.Dto;
  7. using ZhonTai.Admin.Domain.Role;
  8. using ZhonTai.Admin.Domain.RolePermission;
  9. using ZhonTai.Admin.Domain.Tenant;
  10. using ZhonTai.Admin.Domain.User;
  11. using ZhonTai.Admin.Domain.UserRole;
  12. using ZhonTai.Admin.Services.Tenant.Dto;
  13. using ZhonTai.Admin.Domain.Tenant.Dto;
  14. using ZhonTai.DynamicApi;
  15. using ZhonTai.DynamicApi.Attributes;
  16. using Microsoft.AspNetCore.Mvc;
  17. namespace ZhonTai.Admin.Services.Tenant
  18. {
  19. /// <summary>
  20. /// 租户服务
  21. /// </summary>
  22. [DynamicApi(Area = "admin")]
  23. public class TenantService : BaseService, ITenantService, IDynamicApi
  24. {
  25. private readonly ITenantRepository _tenantRepository;
  26. private readonly IRoleRepository _roleRepository;
  27. private readonly IUserRepository _userRepository;
  28. private readonly IRepositoryBase<UserRoleEntity> _userRoleRepository;
  29. private readonly IRepositoryBase<RolePermissionEntity> _rolePermissionRepository;
  30. public TenantService(
  31. ITenantRepository tenantRepository,
  32. IRoleRepository roleRepository,
  33. IUserRepository userRepository,
  34. IRepositoryBase<UserRoleEntity> userRoleRepository,
  35. IRepositoryBase<RolePermissionEntity> rolePermissionRepository
  36. )
  37. {
  38. _tenantRepository = tenantRepository;
  39. _roleRepository = roleRepository;
  40. _userRepository = userRepository;
  41. _userRoleRepository = userRoleRepository;
  42. _rolePermissionRepository = rolePermissionRepository;
  43. }
  44. /// <summary>
  45. /// 查询租户
  46. /// </summary>
  47. /// <param name="id"></param>
  48. /// <returns></returns>
  49. public async Task<IResultOutput> GetAsync(long id)
  50. {
  51. var result = await _tenantRepository.GetAsync<TenantGetOutput>(id);
  52. return ResultOutput.Ok(result);
  53. }
  54. /// <summary>
  55. /// 查询租户列表
  56. /// </summary>
  57. /// <param name="input"></param>
  58. /// <returns></returns>
  59. [HttpPost]
  60. public async Task<IResultOutput> GetPageAsync(PageInput<TenantGetPageDto> input)
  61. {
  62. var key = input.Filter?.Name;
  63. var list = await _tenantRepository.Select
  64. .WhereDynamicFilter(input.DynamicFilter)
  65. .WhereIf(key.NotNull(), a => a.Name.Contains(key))
  66. .Count(out var total)
  67. .OrderByDescending(true, c => c.Id)
  68. .Page(input.CurrentPage, input.PageSize)
  69. .ToListAsync<TenantListOutput>();
  70. var data = new PageOutput<TenantListOutput>()
  71. {
  72. List = list,
  73. Total = total
  74. };
  75. return ResultOutput.Ok(data);
  76. }
  77. /// <summary>
  78. /// 新增
  79. /// </summary>
  80. /// <param name="input"></param>
  81. /// <returns></returns>
  82. [Transaction]
  83. public async Task<IResultOutput> AddAsync(TenantAddInput input)
  84. {
  85. var entity = Mapper.Map<TenantEntity>(input);
  86. var tenant = await _tenantRepository.InsertAsync(entity);
  87. var tenantId = tenant.Id;
  88. //添加用户
  89. var pwd = MD5Encrypt.Encrypt32("111111");
  90. var user = new UserEntity { TenantId = tenantId, UserName = input.Phone, NickName = input.RealName, Password = pwd, Status = 0 };
  91. await _userRepository.InsertAsync(user);
  92. //添加角色
  93. var role = new RoleEntity { TenantId = tenantId, Code = "plat_admin", Name = "平台管理员", Enabled = true };
  94. await _roleRepository.InsertAsync(role);
  95. //添加用户角色
  96. var userRole = new UserRoleEntity() { UserId = user.Id, RoleId = role.Id };
  97. await _userRoleRepository.InsertAsync(userRole);
  98. //更新租户用户和角色
  99. tenant.UserId = user.Id;
  100. tenant.RoleId = role.Id;
  101. await _tenantRepository.UpdateAsync(tenant);
  102. return ResultOutput.Ok();
  103. }
  104. /// <summary>
  105. /// 修改
  106. /// </summary>
  107. /// <param name="input"></param>
  108. /// <returns></returns>
  109. public async Task<IResultOutput> UpdateAsync(TenantUpdateInput input)
  110. {
  111. if (!(input?.Id > 0))
  112. {
  113. return ResultOutput.NotOk();
  114. }
  115. var entity = await _tenantRepository.GetAsync(input.Id);
  116. if (!(entity?.Id > 0))
  117. {
  118. return ResultOutput.NotOk("租户不存在!");
  119. }
  120. Mapper.Map(input, entity);
  121. await _tenantRepository.UpdateAsync(entity);
  122. return ResultOutput.Ok();
  123. }
  124. /// <summary>
  125. /// 彻底删除
  126. /// </summary>
  127. /// <param name="id"></param>
  128. /// <returns></returns>
  129. [Transaction]
  130. public async Task<IResultOutput> DeleteAsync(long id)
  131. {
  132. //删除角色权限
  133. await _rolePermissionRepository.Where(a => a.Role.TenantId == id).DisableGlobalFilter("Tenant").ToDelete().ExecuteAffrowsAsync();
  134. //删除用户角色
  135. await _userRoleRepository.Where(a => a.User.TenantId == id).DisableGlobalFilter("Tenant").ToDelete().ExecuteAffrowsAsync();
  136. //删除用户
  137. await _userRepository.Where(a => a.TenantId == id).DisableGlobalFilter("Tenant").ToDelete().ExecuteAffrowsAsync();
  138. //删除角色
  139. await _roleRepository.Where(a => a.TenantId == id).DisableGlobalFilter("Tenant").ToDelete().ExecuteAffrowsAsync();
  140. //删除租户
  141. await _tenantRepository.DeleteAsync(id);
  142. return ResultOutput.Ok();
  143. }
  144. /// <summary>
  145. /// 删除
  146. /// </summary>
  147. /// <param name="id"></param>
  148. /// <returns></returns>
  149. [Transaction]
  150. public async Task<IResultOutput> SoftDeleteAsync(long id)
  151. {
  152. //删除用户
  153. await _userRepository.SoftDeleteAsync(a => a.TenantId == id, "Tenant");
  154. //删除角色
  155. await _roleRepository.SoftDeleteAsync(a => a.TenantId == id, "Tenant");
  156. //删除租户
  157. var result = await _tenantRepository.SoftDeleteAsync(id);
  158. return ResultOutput.Result(result);
  159. }
  160. /// <summary>
  161. /// 批量删除
  162. /// </summary>
  163. /// <param name="ids"></param>
  164. /// <returns></returns>
  165. [Transaction]
  166. public async Task<IResultOutput> BatchSoftDeleteAsync(long[] ids)
  167. {
  168. //删除用户
  169. await _userRepository.SoftDeleteAsync(a => ids.Contains(a.TenantId.Value), "Tenant");
  170. //删除角色
  171. await _roleRepository.SoftDeleteAsync(a => ids.Contains(a.TenantId.Value), "Tenant");
  172. //删除租户
  173. var result = await _tenantRepository.SoftDeleteAsync(ids);
  174. return ResultOutput.Result(result);
  175. }
  176. }
  177. }