|
|
@@ -1,8 +1,10 @@
|
|
|
package cn.tr.module.sys.tenant.service.impl;
|
|
|
|
|
|
|
|
|
-import cn.hutool.core.util.ObjectUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
+import cn.tr.core.exception.ServiceException;
|
|
|
+import cn.tr.core.exception.TRExcCode;
|
|
|
+import cn.tr.module.sys.config.SysConfigManager;
|
|
|
import cn.tr.module.sys.tenant.mapper.SysTenantMapper;
|
|
|
import cn.tr.module.sys.tenant.dto.SysTenantAddDTO;
|
|
|
import cn.tr.module.sys.tenant.dto.SysTenantCommonDTO;
|
|
|
@@ -13,13 +15,9 @@ import cn.tr.module.sys.user.enums.CreateEnum;
|
|
|
import cn.tr.module.sys.user.enums.UserStatusEnum;
|
|
|
import cn.tr.module.sys.tenant.po.SysTenantPO;
|
|
|
import cn.tr.module.sys.tenant.repository.SysTenantRepository;
|
|
|
-import cn.tr.module.sys.user.repository.SysMenuRepository;
|
|
|
-import cn.tr.module.sys.user.service.ISysOrgService;
|
|
|
-import cn.tr.module.sys.user.service.ISysRoleService;
|
|
|
+import cn.tr.module.sys.user.service.*;
|
|
|
import cn.tr.module.sys.tenant.service.ISysTenantService;
|
|
|
-import cn.tr.module.sys.user.service.ISysUserService;
|
|
|
-import cn.tr.plugin.biz.tenant.context.TenantContextHolder;
|
|
|
-import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import cn.tr.plugin.biz.tenant.utils.TenantUtils;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.context.annotation.Lazy;
|
|
|
@@ -39,10 +37,18 @@ public class SysTenantServiceImpl implements ISysTenantService {
|
|
|
@Autowired
|
|
|
private SysTenantRepository tenantRepository;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ @Lazy
|
|
|
+ private ISysPortalService portalService;
|
|
|
+
|
|
|
@Autowired
|
|
|
@Lazy
|
|
|
private ISysUserService userService;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ @Lazy
|
|
|
+ private ISysUserPortalService userPortalService;
|
|
|
+
|
|
|
@Autowired
|
|
|
@Lazy
|
|
|
private ISysOrgService orgService;
|
|
|
@@ -52,24 +58,20 @@ public class SysTenantServiceImpl implements ISysTenantService {
|
|
|
private ISysRoleService roleService;
|
|
|
|
|
|
@Autowired
|
|
|
- private SysMenuRepository menuRepository;
|
|
|
+ @Lazy
|
|
|
+ private SysConfigManager configManager;
|
|
|
|
|
|
@Autowired
|
|
|
@Lazy
|
|
|
private ISysTenantPackageMenuService tenantPackageMenuService;
|
|
|
@Override
|
|
|
public List<SysTenantCommonDTO> selectSysTenantList(SysTenantQueryDTO query) {
|
|
|
- return SysTenantMapper.INSTANCE.toCommonDTOList(tenantRepository.selectList(new LambdaQueryWrapper<SysTenantPO>()
|
|
|
- .like(StrUtil.isNotEmpty(query.getName()),SysTenantPO::getName,query.getName())
|
|
|
- .like(StrUtil.isNotEmpty(query.getContactMobile()),SysTenantPO::getContactMobile,query.getContactMobile())
|
|
|
- .like(StrUtil.isNotEmpty(query.getContractUser()),SysTenantPO::getContractUser,query.getContractUser())
|
|
|
- .like(ObjectUtil.isNotNull(query.getDisable()),SysTenantPO::getDisable,query.getDisable())
|
|
|
- ));
|
|
|
+ return tenantRepository.stdSelectList(query);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public SysTenantCommonDTO selectSysTenantById(String id) {
|
|
|
- return SysTenantMapper.INSTANCE.toCommonDTO(tenantRepository.selectById(id));
|
|
|
+ return tenantRepository.stdSelectById(id);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -83,6 +85,7 @@ public class SysTenantServiceImpl implements ISysTenantService {
|
|
|
String orgId= IdWorker.getIdStr();
|
|
|
String userId= IdWorker.getIdStr();
|
|
|
String roleId= IdWorker.getIdStr();
|
|
|
+ String portalId= IdWorker.getIdStr();
|
|
|
SysTenantPO tenant = SysTenantMapper.INSTANCE.toPO(source);
|
|
|
tenant.setTenantUserId(userId);
|
|
|
boolean result = tenantRepository.insert(tenant) != 0;
|
|
|
@@ -90,19 +93,40 @@ public class SysTenantServiceImpl implements ISysTenantService {
|
|
|
return false;
|
|
|
}
|
|
|
String tenantId = tenant.getId();
|
|
|
- TenantContextHolder.setIgnore(false);
|
|
|
- TenantContextHolder.setTenantId(tenantId);
|
|
|
- //创建部门
|
|
|
- orgService.insertSysOrg(buildOrg(orgId,source.getName()));
|
|
|
- //创建角色
|
|
|
- roleService.insertSysRole(buildRole(roleId));
|
|
|
- //创建租户用户
|
|
|
- userService.insertSysUser(buildUser(userId,roleId,orgId,source.getUsername(),source.getPassword(),source.getName()));
|
|
|
+ TenantUtils.execute(tenantId,()->{
|
|
|
+ //创建门面
|
|
|
+ portalService.insertInnerSysPortal(buildPortal(portalId));
|
|
|
+ //创建部门
|
|
|
+ orgService.insertSysOrg(buildOrg(orgId,source.getName()));
|
|
|
+ //创建角色
|
|
|
+ roleService.insertInnerSysRole(buildRole(roleId));
|
|
|
+ //创建租户用户
|
|
|
+ userService.insertSysUser(buildUser(userId,roleId,orgId,source.
|
|
|
+ getUsername(),configManager.getCurrent().getDefaultPsw(),source.getName()));
|
|
|
+ //关联用户和门户
|
|
|
+ userPortalService.assignUserPortal(buildUserPortal(userId,portalId));
|
|
|
+ });
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ private List<SysUserPortalDTO> buildUserPortal(String userId, String portalId) {
|
|
|
+ ArrayList<SysUserPortalDTO> result = new ArrayList<>();
|
|
|
+ SysUserPortalDTO userPortalDTO = new SysUserPortalDTO();
|
|
|
+ userPortalDTO.setIsDefault(true);
|
|
|
+ userPortalDTO.setUserId(userId);
|
|
|
+ userPortalDTO.setPortalId(portalId);
|
|
|
+ result.add(userPortalDTO);
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public int removeSysTenantByIds(Collection<String> ids) {
|
|
|
+ List<SysTenantPO> tenants = tenantRepository.selectBatchIds(ids);
|
|
|
+ for (SysTenantPO role : tenants) {
|
|
|
+ if (StrUtil.equals(role.getType(), CreateEnum.sys.name())) {
|
|
|
+ throw new ServiceException(TRExcCode.SYSTEM_ERROR_B0001,"无法对系统租户进行操作");
|
|
|
+ }
|
|
|
+ }
|
|
|
return tenantRepository.deleteBatchIds(ids);
|
|
|
}
|
|
|
|
|
|
@@ -112,6 +136,16 @@ public class SysTenantServiceImpl implements ISysTenantService {
|
|
|
return tenantPackageMenuService.findMenuIdByPackageId(tenant.getPackageId());
|
|
|
}
|
|
|
|
|
|
+ private SysPortalDTO buildPortal(String portalId){
|
|
|
+ SysPortalDTO org = new SysPortalDTO();
|
|
|
+ org.setId(portalId);
|
|
|
+ org.setCode("admin");
|
|
|
+ org.setName("系统管理员工作台");
|
|
|
+ org.setType(CreateEnum.sys.name());
|
|
|
+ org.setSort(1);
|
|
|
+ return org;
|
|
|
+ }
|
|
|
+
|
|
|
private SysOrgDTO buildOrg(String orgId,String tenantName){
|
|
|
SysOrgDTO org = new SysOrgDTO();
|
|
|
org.setId(orgId);
|