TenantService.cs 7.1 KB

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