mirror of
https://github.com/thousmile/molly-multi-tenant.git
synced 2025-12-30 04:32:26 +00:00
1.修复 随机新增租户的测试类。2.删除租户时,删除租户关联的用户!
This commit is contained in:
@@ -6,10 +6,10 @@ import com.xaaef.molly.common.util.JsonUtils;
|
||||
import com.xaaef.molly.corems.entity.CmsProject;
|
||||
import com.xaaef.molly.corems.mapper.CmsProjectMapper;
|
||||
import com.xaaef.molly.tenant.props.MultiTenantProperties;
|
||||
import jakarta.servlet.ServletContextEvent;
|
||||
import jakarta.servlet.ServletContextListener;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.ApplicationArguments;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@@ -30,14 +30,14 @@ import static com.xaaef.molly.common.consts.MbpConst.PROJECT_ID;
|
||||
@Component
|
||||
@Order(Integer.MIN_VALUE)
|
||||
@AllArgsConstructor
|
||||
public class ProjectTableRunner implements ServletContextListener {
|
||||
public class ProjectTableRunner implements ApplicationRunner {
|
||||
|
||||
private final CmsProjectMapper projectMapper;
|
||||
|
||||
private final MultiTenantProperties multiTenantProperties;
|
||||
|
||||
@Override
|
||||
public void contextInitialized(ServletContextEvent sce) {
|
||||
public void run(ApplicationArguments args) throws Exception {
|
||||
log.info("ProjectTableRunner Initialized .....");
|
||||
if (multiTenantProperties.getEnableProject()) {
|
||||
// 查询 所有的 不包含 project_id 的表
|
||||
|
||||
@@ -20,7 +20,9 @@ import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import static com.xaaef.molly.system.cron.TestCronAsync.randomChinese;
|
||||
@@ -63,18 +65,21 @@ public class MollyApplicationTests {
|
||||
*/
|
||||
@Test
|
||||
public void test1() throws Exception {
|
||||
for (int i = 0; i < 1; i++) {
|
||||
var areaCodeList = List.of(440301000000L, 440303000000L, 440304000000L, 440305000000L,
|
||||
440306000000L, 440307000000L, 440308000000L, 440309000000L, 440310000000L, 440311000000L);
|
||||
for (int i = 0; i < 3; i++) {
|
||||
var start = System.currentTimeMillis();
|
||||
var email = String.format("%s@qq.com", RandomUtil.randomString(10));
|
||||
var contactNumber = String.format("0755-%s", RandomUtil.randomNumbers(7));
|
||||
var expired = LocalDateTime.now().plusDays(RandomUtil.randomInt(30, 3650));
|
||||
var ac = areaCodeList.get(RandomUtil.randomInt(0, (areaCodeList.size() - 1)));
|
||||
var po = new CreateTenantPO()
|
||||
.setName(randomChinese(5))
|
||||
.setEmail(email)
|
||||
.setLinkman(randomChinese(3))
|
||||
.setContactNumber(contactNumber)
|
||||
.setLogo("https://images.xaaef.com/molly_master_logo.png")
|
||||
.setAreaCode(440307000000L)
|
||||
.setAreaCode(ac)
|
||||
.setAddress(randomChinese(10))
|
||||
.setTemplates(Set.of(new SysTemplate().setId(10001L)))
|
||||
.setExpired(expired);
|
||||
@@ -82,12 +87,14 @@ public class MollyApplicationTests {
|
||||
System.out.println(JsonUtils.toFormatJson(success));
|
||||
var end = System.currentTimeMillis() - start;
|
||||
System.out.printf("耗时: %d ms\n", end);
|
||||
// 异步初始化,需要等创建数据库表结构完成
|
||||
Thread.sleep(Duration.ofSeconds(20));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 随机生成租户
|
||||
* 获取项目
|
||||
*
|
||||
* @author WangChenChen
|
||||
* @version 2.0
|
||||
|
||||
@@ -20,6 +20,11 @@ public interface SysUserMapper {
|
||||
int deleteHaveTenants(Long userId);
|
||||
|
||||
|
||||
// 租户 删除关联的 系统用户
|
||||
@Delete("delete from sys_user_tenant WHERE tenant_id = #{tenantId}")
|
||||
int deleteHaveSysUser(String tenantId);
|
||||
|
||||
|
||||
// 系统用户 关联租户
|
||||
int insertByTenants(@Param("userId") Long userId,
|
||||
@Param("tenantIds") Set<String> tenantIds);
|
||||
|
||||
@@ -10,10 +10,11 @@ import com.xaaef.molly.system.entity.SysTenant;
|
||||
import com.xaaef.molly.system.mapper.SysTenantMapper;
|
||||
import com.xaaef.molly.tenant.DatabaseManager;
|
||||
import com.xaaef.molly.tenant.service.MultiTenantManager;
|
||||
import jakarta.servlet.ServletContextEvent;
|
||||
import jakarta.servlet.ServletContextListener;
|
||||
import jakarta.annotation.PreDestroy;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.ApplicationArguments;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
import org.springframework.context.annotation.DependsOn;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.stereotype.Component;
|
||||
@@ -42,7 +43,7 @@ import java.util.stream.Collectors;
|
||||
@DependsOn(value = "projectTableRunner")
|
||||
@Order(Integer.MIN_VALUE + 1)
|
||||
@AllArgsConstructor
|
||||
public class TenantTableRunner implements ServletContextListener {
|
||||
public class TenantTableRunner implements ApplicationRunner {
|
||||
|
||||
private final SysTenantMapper tenantMapper;
|
||||
|
||||
@@ -52,9 +53,8 @@ public class TenantTableRunner implements ServletContextListener {
|
||||
|
||||
private final ApiCmsProjectService projectService;
|
||||
|
||||
|
||||
@Override
|
||||
public void contextInitialized(ServletContextEvent sce) {
|
||||
public void run(ApplicationArguments args) throws Exception {
|
||||
log.info("TenantTableRunner Initialized .....");
|
||||
var props = databaseManager.getMultiTenantProperties();
|
||||
if (props.getEnable()) {
|
||||
@@ -98,9 +98,8 @@ public class TenantTableRunner implements ServletContextListener {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void contextDestroyed(ServletContextEvent sce) {
|
||||
@PreDestroy
|
||||
public void destroy() {
|
||||
tenantManager.removeAll();
|
||||
log.info("delete the tenantId in redis ...");
|
||||
}
|
||||
|
||||
@@ -50,4 +50,14 @@ public interface SysUserService {
|
||||
*/
|
||||
boolean updateTenant(Long userId, Set<String> tenantIds);
|
||||
|
||||
|
||||
/**
|
||||
* 租户 删除关联的 系统用户
|
||||
*
|
||||
* @param tenantId
|
||||
* @author Wang Chen Chen
|
||||
* @date 2023/11/23 17:31
|
||||
*/
|
||||
boolean deleteHaveSysUser(String tenantId);
|
||||
|
||||
}
|
||||
|
||||
@@ -466,6 +466,8 @@ public class SysTenantServiceImpl extends BaseServiceImpl<SysTenantMapper, SysTe
|
||||
dataSourceManager.deleteTable(tenantId);
|
||||
// 删除 redis中租户
|
||||
tenantManager.removeTenantId(tenantId);
|
||||
// 删除 关联此租户的 系统用户
|
||||
sysUserService.deleteHaveSysUser(tenantId);
|
||||
return flag;
|
||||
}
|
||||
|
||||
|
||||
@@ -103,4 +103,11 @@ public class SysUserServiceImpl implements SysUserService {
|
||||
}
|
||||
|
||||
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
@Override
|
||||
public boolean deleteHaveSysUser(String tenantId) {
|
||||
return baseMapper.deleteHaveSysUser(tenantId) > 0;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user