0
0

TenantService.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. using System.Linq;
  2. using System.Threading.Tasks;
  3. using Microsoft.AspNetCore.Mvc;
  4. using ZhonTai.Admin.Core.Attributes;
  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 ZhonTai.Admin.Core.Consts;
  17. using ZhonTai.Admin.Core.Configs;
  18. using ZhonTai.Admin.Domain.Org;
  19. using ZhonTai.Admin.Domain.UserStaff;
  20. using ZhonTai.Admin.Domain.UserOrg;
  21. namespace ZhonTai.Admin.Services.Tenant;
  22. /// <summary>
  23. /// 租户服务
  24. /// </summary>
  25. [DynamicApi(Area = AdminConsts.AreaName)]
  26. public class TenantService : BaseService, ITenantService, IDynamicApi
  27. {
  28. private AppConfig _appConfig => LazyGetRequiredService<AppConfig>();
  29. private ITenantRepository _tenantRepository => LazyGetRequiredService<ITenantRepository>();
  30. private IRoleRepository _roleRepository => LazyGetRequiredService<IRoleRepository>();
  31. private IUserRepository _userRepository => LazyGetRequiredService<IUserRepository>();
  32. private IOrgRepository _orgRepository => LazyGetRequiredService<IOrgRepository>();
  33. private IUserRoleRepository _userRoleRepository => LazyGetRequiredService<IUserRoleRepository>();
  34. private IRolePermissionRepository _rolePermissionRepository => LazyGetRequiredService<IRolePermissionRepository>();
  35. private IUserStaffRepository _userStaffRepository => LazyGetRequiredService<IUserStaffRepository>();
  36. private IUserOrgRepository _userOrgRepository => LazyGetRequiredService<IUserOrgRepository>();
  37. public TenantService()
  38. {
  39. }
  40. /// <summary>
  41. /// 查询租户
  42. /// </summary>
  43. /// <param name="id"></param>
  44. /// <returns></returns>
  45. public async Task<TenantGetOutput> GetAsync(long id)
  46. {
  47. var result = await _tenantRepository.GetAsync<TenantGetOutput>(id);
  48. return result;
  49. }
  50. /// <summary>
  51. /// 查询租户列表
  52. /// </summary>
  53. /// <param name="input"></param>
  54. /// <returns></returns>
  55. [HttpPost]
  56. public async Task<PageOutput<TenantListOutput>> GetPageAsync(PageInput<TenantGetPageDto> input)
  57. {
  58. var key = input.Filter?.Name;
  59. var list = await _tenantRepository.Select
  60. .WhereDynamicFilter(input.DynamicFilter)
  61. .WhereIf(key.NotNull(), a => a.Name.Contains(key))
  62. .Count(out var total)
  63. .OrderByDescending(true, c => c.Id)
  64. .Page(input.CurrentPage, input.PageSize)
  65. .ToListAsync<TenantListOutput>();
  66. var data = new PageOutput<TenantListOutput>()
  67. {
  68. List = list,
  69. Total = total
  70. };
  71. return data;
  72. }
  73. /// <summary>
  74. /// 新增
  75. /// </summary>
  76. /// <param name="input"></param>
  77. /// <returns></returns>
  78. [AdminTransaction]
  79. public virtual async Task<long> AddAsync(TenantAddInput input)
  80. {
  81. if (await _tenantRepository.Select.AnyAsync(a => a.Name == input.Name))
  82. {
  83. throw ResultOutput.Exception($"企业名称已存在");
  84. }
  85. if (await _tenantRepository.Select.AnyAsync(a => a.Code == input.Code))
  86. {
  87. throw ResultOutput.Exception($"企业编码已存在");
  88. }
  89. //添加租户
  90. TenantEntity entity = Mapper.Map<TenantEntity>(input);
  91. TenantEntity tenant = await _tenantRepository.InsertAsync(entity);
  92. long tenantId = tenant.Id;
  93. //添加部门
  94. var org = new OrgEntity
  95. {
  96. TenantId = tenantId,
  97. Name = input.Name,
  98. Code = input.Code,
  99. ParentId = 0,
  100. MemberCount = 1
  101. };
  102. await _orgRepository.InsertAsync(org);
  103. //添加主管理员
  104. string pwd = MD5Encrypt.Encrypt32(_appConfig.DefaultPassword);
  105. var user = new UserEntity
  106. {
  107. TenantId = tenantId,
  108. UserName = input.Phone,
  109. Password = pwd,
  110. Name = input.RealName,
  111. Mobile = input.Phone,
  112. Email = input.Email,
  113. Status = UserStatus.Enabled,
  114. Type = UserType.TenantAdmin,
  115. OrgId = org.Id
  116. };
  117. await _userRepository.InsertAsync(user);
  118. long userId = user.Id;
  119. //添加员工
  120. var emp = new UserStaffEntity
  121. {
  122. Id = userId,
  123. TenantId = tenantId
  124. };
  125. await _userStaffRepository.InsertAsync(emp);
  126. //添加用户部门
  127. var userOrg = new UserOrgEntity
  128. {
  129. UserId = userId,
  130. OrgId = org.Id
  131. };
  132. await _userOrgRepository.InsertAsync(userOrg);
  133. //添加角色分组
  134. var roleGroup = new RoleEntity
  135. {
  136. ParentId = 0,
  137. TenantId = tenantId,
  138. Name = "系统默认"
  139. };
  140. await _roleRepository.InsertAsync(roleGroup);
  141. //添加角色
  142. var role = new RoleEntity
  143. {
  144. TenantId = tenantId,
  145. Name = "主管理员",
  146. Code = "main-admin",
  147. ParentId = roleGroup.Id,
  148. DataScope = DataScope.All
  149. };
  150. await _roleRepository.InsertAsync(role);
  151. //添加用户角色
  152. var userRole = new UserRoleEntity()
  153. {
  154. UserId = userId,
  155. RoleId = role.Id
  156. };
  157. await _userRoleRepository.InsertAsync(userRole);
  158. //更新租户的用户
  159. tenant.UserId = userId;
  160. await _tenantRepository.UpdateAsync(tenant);
  161. return tenant.Id;
  162. }
  163. /// <summary>
  164. /// 修改
  165. /// </summary>
  166. /// <param name="input"></param>
  167. /// <returns></returns>
  168. public async Task UpdateAsync(TenantUpdateInput input)
  169. {
  170. var entity = await _tenantRepository.GetAsync(input.Id);
  171. if (!(entity?.Id > 0))
  172. {
  173. throw ResultOutput.Exception("租户不存在!");
  174. }
  175. Mapper.Map(input, entity);
  176. await _tenantRepository.UpdateAsync(entity);
  177. }
  178. /// <summary>
  179. /// 彻底删除
  180. /// </summary>
  181. /// <param name="id"></param>
  182. /// <returns></returns>
  183. [AdminTransaction]
  184. public virtual async Task DeleteAsync(long id)
  185. {
  186. //删除角色权限
  187. await _rolePermissionRepository.Where(a => a.Role.TenantId == id).DisableGlobalFilter(FilterNames.Tenant).ToDelete().ExecuteAffrowsAsync();
  188. //删除用户角色
  189. await _userRoleRepository.Where(a => a.User.TenantId == id).DisableGlobalFilter(FilterNames.Tenant).ToDelete().ExecuteAffrowsAsync();
  190. //删除员工
  191. await _userStaffRepository.Where(a => a.TenantId == id).DisableGlobalFilter(FilterNames.Tenant).ToDelete().ExecuteAffrowsAsync();
  192. //删除用户部门
  193. await _userOrgRepository.Where(a => a.User.TenantId == id).DisableGlobalFilter(FilterNames.Tenant).ToDelete().ExecuteAffrowsAsync();
  194. //删除用户
  195. await _userRepository.Where(a => a.TenantId == id && a.Type != UserType.Member).DisableGlobalFilter(FilterNames.Tenant).ToDelete().ExecuteAffrowsAsync();
  196. //删除角色
  197. await _roleRepository.Where(a => a.TenantId == id).DisableGlobalFilter(FilterNames.Tenant).ToDelete().ExecuteAffrowsAsync();
  198. //删除租户
  199. await _tenantRepository.DeleteAsync(id);
  200. }
  201. /// <summary>
  202. /// 删除
  203. /// </summary>
  204. /// <param name="id"></param>
  205. /// <returns></returns>
  206. [AdminTransaction]
  207. public virtual async Task SoftDeleteAsync(long id)
  208. {
  209. //删除用户
  210. await _userRepository.SoftDeleteAsync(a => a.TenantId == id && a.Type != UserType.Member, FilterNames.Tenant);
  211. //删除角色
  212. await _roleRepository.SoftDeleteAsync(a => a.TenantId == id, FilterNames.Tenant);
  213. //删除租户
  214. var result = await _tenantRepository.SoftDeleteAsync(id);
  215. }
  216. /// <summary>
  217. /// 批量删除
  218. /// </summary>
  219. /// <param name="ids"></param>
  220. /// <returns></returns>
  221. [AdminTransaction]
  222. public virtual async Task BatchSoftDeleteAsync(long[] ids)
  223. {
  224. //删除用户
  225. await _userRepository.SoftDeleteAsync(a => ids.Contains(a.TenantId.Value) && a.Type != UserType.Member, FilterNames.Tenant);
  226. //删除角色
  227. await _roleRepository.SoftDeleteAsync(a => ids.Contains(a.TenantId.Value), FilterNames.Tenant);
  228. //删除租户
  229. var result = await _tenantRepository.SoftDeleteAsync(ids);
  230. }
  231. }