TenantService.cs 9.2 KB

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