Please enter the commit message for your changes. Lines starting
with '#' will be ignored, and an empty message aborts the commit. On branch develop Your branch is up to date with 'goflyapp/develop'. Changes to be committed: deleted: zlt-business/user-center/src/main/java/com/central/user/service/impl/SysTenantServiceImpl.java deleted: zlt-business/user-center/src/main/java/com/central/user/service/impl/TenantInitServiceImpl.java
This commit is contained in:
-130
@@ -1,130 +0,0 @@
|
|||||||
package com.central.user.service.impl;
|
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
||||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
||||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
||||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
||||||
import com.central.common.constant.CommonConstant;
|
|
||||||
import com.central.common.model.PageResult;
|
|
||||||
import com.central.common.model.Result;
|
|
||||||
import com.central.user.mapper.SysTenantMapper;
|
|
||||||
import com.central.user.model.SysTenant;
|
|
||||||
import com.central.user.service.ISysTenantService;
|
|
||||||
import lombok.RequiredArgsConstructor;
|
|
||||||
import org.apache.commons.collections4.MapUtils;
|
|
||||||
import org.springframework.data.redis.core.RedisTemplate;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
|
||||||
import com.central.user.config.TenantProperties;
|
|
||||||
import com.central.user.context.TenantContextHolder;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
import cn.hutool.core.util.IdUtil;
|
|
||||||
import cn.hutool.core.util.StrUtil;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
@Slf4j
|
|
||||||
@Service
|
|
||||||
@RequiredArgsConstructor
|
|
||||||
public class SysTenantServiceImpl extends ServiceImpl<SysTenantMapper, SysTenant> implements ISysTenantService {
|
|
||||||
private final RedisTemplate<String, Object> redisTemplate;
|
|
||||||
private final TenantProperties tenantProperties;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public PageResult<SysTenant> findList(Map<String, Object> params) {
|
|
||||||
// 清除租户上下文,查询所有租户
|
|
||||||
TenantContextHolder.clear();
|
|
||||||
try {
|
|
||||||
Page<SysTenant> page = new Page<>(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"));
|
|
||||||
LambdaQueryWrapper<SysTenant> wrapper = new LambdaQueryWrapper<>();
|
|
||||||
// 查询条件
|
|
||||||
if (StrUtil.isNotEmpty(MapUtils.getString(params, "name"))) {
|
|
||||||
wrapper.like(SysTenant::getName, MapUtils.getString(params, "name"));
|
|
||||||
}
|
|
||||||
if (StrUtil.isNotEmpty(MapUtils.getString(params, "code"))) {
|
|
||||||
wrapper.eq(SysTenant::getCode, MapUtils.getString(params, "code"));
|
|
||||||
}
|
|
||||||
if (params.get("status") != null) {
|
|
||||||
wrapper.eq(SysTenant::getStatus, MapUtils.getInteger(params, "status"));
|
|
||||||
}
|
|
||||||
wrapper.orderByDesc(SysTenant::getCreateTime);
|
|
||||||
|
|
||||||
IPage<SysTenant> list = baseMapper.selectPage(page, wrapper);
|
|
||||||
return PageResult.<SysTenant>builder().data(list.getRecords()).count(list.getTotal()).build();
|
|
||||||
} finally {
|
|
||||||
// 恢复租户上下文
|
|
||||||
TenantContextHolder.setTenant(MapUtils.getString(params, "tenantId"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
@Transactional(rollbackFor = Exception.class)
|
|
||||||
public Result saveOrUpdateTenant(SysTenant tenant) {
|
|
||||||
// 清除租户上下文
|
|
||||||
TenantContextHolder.clear();
|
|
||||||
try {
|
|
||||||
if (tenant.getId() == null) {
|
|
||||||
// 新增租户
|
|
||||||
tenant.setCode(generateTenantCode());
|
|
||||||
tenant.setStatus(CommonConstant.STATUS_NORMAL);
|
|
||||||
// 检查租户编码是否重复
|
|
||||||
if (checkTenantCodeExists(tenant.getCode())) {
|
|
||||||
return Result.failed("租户编码已存在");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
boolean result = saveOrUpdate(tenant);
|
|
||||||
if (result) {
|
|
||||||
refreshTenantCache(tenant);
|
|
||||||
return Result.succeed("操作成功");
|
|
||||||
}
|
|
||||||
return Result.failed("操作失败");
|
|
||||||
} finally {
|
|
||||||
// 恢复租户上下文
|
|
||||||
TenantContextHolder.setTenant(tenant.getCode());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
@Transactional(rollbackFor = Exception.class)
|
|
||||||
public Result deleteTenant(String id) {
|
|
||||||
// 清除租户上下文
|
|
||||||
TenantContextHolder.clear();
|
|
||||||
try {
|
|
||||||
SysTenant tenant = getById(id);
|
|
||||||
if (tenant == null) {
|
|
||||||
return Result.failed("租户不存在");
|
|
||||||
}
|
|
||||||
// 逻辑删除租户
|
|
||||||
boolean result = removeById(id);
|
|
||||||
if (result) {
|
|
||||||
clearTenantCache(tenant.getCode());
|
|
||||||
return Result.succeed("删除成功");
|
|
||||||
}
|
|
||||||
return Result.failed("删除失败");
|
|
||||||
} finally {
|
|
||||||
// 恢复租户上下文
|
|
||||||
TenantContextHolder.setTenant(getById(id).getCode());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean checkTenantCodeExists(String code) {
|
|
||||||
LambdaQueryWrapper<SysTenant> wrapper = new LambdaQueryWrapper<>();
|
|
||||||
wrapper.eq(SysTenant::getCode, code);
|
|
||||||
return baseMapper.selectCount(wrapper) > 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
private String generateTenantCode() {
|
|
||||||
return "T" + IdUtil.getSnowflake().nextIdStr();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void refreshTenantCache(SysTenant tenant) {
|
|
||||||
String key = CommonConstant.TENANT_PREFIX + tenant.getCode();
|
|
||||||
redisTemplate.opsForValue().set(key, tenant);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void clearTenantCache(String tenantCode) {
|
|
||||||
String key = CommonConstant.TENANT_PREFIX + tenantCode;
|
|
||||||
redisTemplate.delete(key);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
-49
@@ -1,49 +0,0 @@
|
|||||||
package com.central.user.service.impl;
|
|
||||||
|
|
||||||
import com.central.common.model.Result;
|
|
||||||
import com.central.user.service.ISysTenantService;
|
|
||||||
import lombok.RequiredArgsConstructor;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
import org.springframework.jdbc.datasource.init.DataSourceInitializer;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
|
||||||
|
|
||||||
@Slf4j
|
|
||||||
@Service
|
|
||||||
@RequiredArgsConstructor
|
|
||||||
public class TenantInitServiceImpl {
|
|
||||||
private final ISysTenantService tenantService;
|
|
||||||
private final DataSourceInitializer dataSourceInitializer;
|
|
||||||
|
|
||||||
@Transactional(rollbackFor = Exception.class)
|
|
||||||
public Result initTenantData(String tenantId) {
|
|
||||||
try {
|
|
||||||
// 1. 初始化租户数据源
|
|
||||||
dataSourceInitializer.initTenantDataSource(tenantId);
|
|
||||||
|
|
||||||
// 2. 初始化租户基础数据
|
|
||||||
initBasicData(tenantId);
|
|
||||||
|
|
||||||
// 3. 初始化租户配置
|
|
||||||
initTenantConfig(tenantId);
|
|
||||||
|
|
||||||
return Result.succeed("租户初始化成功");
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("租户初始化失败", e);
|
|
||||||
return Result.failed("租户初始化失败");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void initBasicData(String tenantId) {
|
|
||||||
// 初始化基础数据
|
|
||||||
// 1. 初始化管理员账号
|
|
||||||
// 2. 初始化基础角色
|
|
||||||
// 3. 初始化基础权限
|
|
||||||
}
|
|
||||||
|
|
||||||
private void initTenantConfig(String tenantId) {
|
|
||||||
// 初始化租户配置
|
|
||||||
// 1. 初始化系统配置
|
|
||||||
// 2. 初始化业务配置
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user