TenantInfoServiceImpl.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package com.keao.edu.user.service.impl;
  2. import com.keao.edu.auth.api.entity.SysUser;
  3. import com.keao.edu.common.dal.BaseDAO;
  4. import com.keao.edu.common.exception.BizException;
  5. import com.keao.edu.common.page.PageInfo;
  6. import com.keao.edu.common.page.QueryInfo;
  7. import com.keao.edu.common.service.impl.BaseServiceImpl;
  8. import com.keao.edu.im.api.client.ImFeignService;
  9. import com.keao.edu.im.api.entity.ImResult;
  10. import com.keao.edu.im.api.entity.ImUserModel;
  11. import com.keao.edu.user.dao.EmployeeDao;
  12. import com.keao.edu.user.dao.OrganizationDao;
  13. import com.keao.edu.user.dao.SysUserDao;
  14. import com.keao.edu.user.dao.TenantInfoDao;
  15. import com.keao.edu.user.dto.TenantInfoDto;
  16. import com.keao.edu.user.entity.Employee;
  17. import com.keao.edu.user.entity.Organization;
  18. import com.keao.edu.user.entity.TenantInfo;
  19. import com.keao.edu.user.service.TenantInfoService;
  20. import com.keao.edu.util.collection.MapUtil;
  21. import org.apache.commons.lang3.StringUtils;
  22. import org.springframework.beans.factory.annotation.Autowired;
  23. import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
  24. import org.springframework.stereotype.Service;
  25. import org.springframework.transaction.annotation.Transactional;
  26. import java.util.*;
  27. @Service
  28. public class TenantInfoServiceImpl extends BaseServiceImpl<Integer, TenantInfo> implements TenantInfoService {
  29. @Autowired
  30. private TenantInfoDao tenantInfoDao;
  31. @Autowired
  32. private SysUserDao sysUserDao;
  33. @Autowired
  34. private OrganizationDao organizationDao;
  35. @Autowired
  36. private ImFeignService imFeignService;
  37. @Autowired
  38. private EmployeeDao employeeDao;
  39. @Override
  40. public BaseDAO<Integer, TenantInfo> getDAO() {
  41. return tenantInfoDao;
  42. }
  43. @Override
  44. @Transactional(rollbackFor = Exception.class)
  45. public void addTenant(TenantInfoDto tenantInfo) {
  46. if(StringUtils.isBlank(tenantInfo.getContactPhone())){
  47. throw new BizException("请填写手机号码");
  48. }
  49. SysUser userWithPhone = sysUserDao.queryByPhone(tenantInfo.getContactPhone());
  50. if(Objects.nonNull(userWithPhone)){
  51. throw new BizException("手机号已被占用");
  52. }
  53. tenantInfoDao.insert(tenantInfo);
  54. SysUser sysUser = new SysUser();
  55. // sysUser.setTenantId(tenantInfo.getId().toString());
  56. sysUser.setPassword(new BCryptPasswordEncoder().encode("123456"));
  57. sysUser.setUserType("SYSTEM");
  58. sysUser.setRealName(tenantInfo.getContactName());
  59. sysUser.setAvatar(tenantInfo.getLogoUrl());
  60. sysUser.setPhone(tenantInfo.getContactPhone());
  61. sysUserDao.insert(sysUser);
  62. if(tenantInfo.getRoleIds() != null){
  63. // Set<Integer> roleIds = Arrays.stream(tenantInfo.getRoleIds().split(",")).map(e -> Integer.valueOf(e)).collect(Collectors.toSet());
  64. sysUserDao.batchAddEmployeeRole(sysUser.getId(), tenantInfo.getRoleIds());
  65. }
  66. Organization organ=new Organization();
  67. organ.setUserId(sysUser.getId());
  68. organ.setTenantId(tenantInfo.getId().toString());
  69. organ.setParentOrganId(sysUser.getId());
  70. organ.setLevel(0);
  71. organ.setParentOrganIdTag(sysUser.getId().toString());
  72. organ.setId(sysUser.getId());
  73. organ.setName(tenantInfo.getName());
  74. organ.setContactName(tenantInfo.getContactName());
  75. organ.setContactPhone(tenantInfo.getContactPhone());
  76. organizationDao.insert(organ);
  77. Employee employee = new Employee();
  78. employee.setOrganId(organ.getId());
  79. employee.setUserId(sysUser.getId());
  80. employee.setTenantId(tenantInfo.getId().toString());
  81. employee.setEmployeeType("ORGAN");
  82. employeeDao.insert(employee);
  83. ImResult imResult = imFeignService.register(new ImUserModel(sysUser.getId().toString(), sysUser.getRealName(),null));
  84. sysUser.setImToken(imResult.getToken());
  85. sysUserDao.update(sysUser);
  86. }
  87. @Override
  88. @Transactional(rollbackFor = Exception.class)
  89. public void updateTenant(TenantInfoDto newTenantInfo) {
  90. TenantInfo existTenantInfo = tenantInfoDao.get(newTenantInfo.getId());
  91. if(Objects.isNull(existTenantInfo)){
  92. throw new BizException("机构不存在");
  93. }
  94. SysUser sysUser = sysUserDao.queryByPhone(existTenantInfo.getContactPhone());
  95. if(sysUser == null){
  96. throw new BizException("用户信息异常");
  97. }
  98. if(!newTenantInfo.getContactPhone().equals(sysUser.getPhone())){
  99. if (sysUserDao.queryByPhone(newTenantInfo.getContactPhone()) != null) {
  100. throw new BizException("手机号已被占用");
  101. }
  102. }
  103. Organization organization = organizationDao.findByUserId(sysUser.getId());
  104. if(newTenantInfo.getRoleIds() != null){
  105. //删除当前用户角色
  106. sysUserDao.delEmployeeRole(sysUser.getId());
  107. //新增用户角色
  108. sysUserDao.batchAddEmployeeRole(sysUser.getId(),newTenantInfo.getRoleIds());
  109. }
  110. if(StringUtils.isNotBlank(newTenantInfo.getContactName())){
  111. sysUser.setRealName(newTenantInfo.getContactName());
  112. organization.setContactName(newTenantInfo.getContactName());
  113. organization.setName(newTenantInfo.getName());
  114. }
  115. if(StringUtils.isNotBlank(newTenantInfo.getContactPhone())){
  116. sysUser.setPhone(newTenantInfo.getContactPhone());
  117. organization.setContactPhone(newTenantInfo.getContactPhone());
  118. }
  119. organizationDao.update(organization);
  120. sysUserDao.update(sysUser);
  121. imFeignService.update(new ImUserModel(sysUser.getId().toString(),sysUser.getRealName(),sysUser.getAvatar()));
  122. tenantInfoDao.update(newTenantInfo);
  123. }
  124. @Override
  125. public PageInfo<TenantInfoDto> queryTenants(QueryInfo queryInfo) {
  126. PageInfo<TenantInfoDto> pageInfo = new PageInfo<>(queryInfo.getPage(), queryInfo.getRows());
  127. Map<String, Object> params = new HashMap<String, Object>();
  128. MapUtil.populateMap(params, queryInfo);
  129. List<TenantInfoDto> dataList = new ArrayList<>();
  130. int count = tenantInfoDao.countTenants(params);
  131. if (count > 0) {
  132. pageInfo.setTotal(count);
  133. params.put("offset", pageInfo.getOffset());
  134. dataList = tenantInfoDao.queryTenants(params);
  135. }
  136. pageInfo.setRows(dataList);
  137. return pageInfo;
  138. }
  139. }