feat(tenant): 新增代理租户创建与分成比例校验
This commit is contained in:
@@ -109,6 +109,8 @@ public interface ErrorCodeConstants {
|
||||
ErrorCode TENANT_CAN_NOT_UPDATE_SYSTEM = new ErrorCode(1_002_015_003, "系统租户不能进行修改、删除等操作!");
|
||||
ErrorCode TENANT_NAME_DUPLICATE = new ErrorCode(1_002_015_004, "名字为【{}】的租户已存在");
|
||||
ErrorCode TENANT_WEBSITE_DUPLICATE = new ErrorCode(1_002_015_005, "域名为【{}】的租户已存在");
|
||||
ErrorCode TENANT_LEVEL_MAX = new ErrorCode(1_002_015_006, "当前租户级别已达到最大值,不允许创建代理租户");
|
||||
ErrorCode TENANT_PROFIT_SHARE_RATIO_INVALID = new ErrorCode(1_002_015_007, "分成比例必须在 0 到 1 之间");
|
||||
|
||||
// ========== 租户套餐 1-002-016-000 ==========
|
||||
ErrorCode TENANT_PACKAGE_NOT_EXISTS = new ErrorCode(1_002_016_000, "租户套餐不存在");
|
||||
|
||||
@@ -37,6 +37,7 @@ import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
@@ -104,9 +105,27 @@ public class TenantServiceImpl implements TenantService {
|
||||
validTenantWebsiteDuplicate(createReqVO.getWebsites(), null);
|
||||
// 校验套餐被禁用
|
||||
TenantPackageDO tenantPackage = tenantPackageService.validTenantPackage(createReqVO.getPackageId());
|
||||
// 校验分成比例
|
||||
if (createReqVO.getProfitShareRatio() != null &&
|
||||
(createReqVO.getProfitShareRatio().compareTo(BigDecimal.ZERO) < 0 ||
|
||||
createReqVO.getProfitShareRatio().compareTo(BigDecimal.ONE) > 0)) {
|
||||
throw exception(TENANT_PROFIT_SHARE_RATIO_INVALID);
|
||||
}
|
||||
|
||||
// 创建租户
|
||||
TenantDO tenant = BeanUtils.toBean(createReqVO, TenantDO.class);
|
||||
// 处理代理租户逻辑
|
||||
if ("代理".equals(createReqVO.getTenantType())) {
|
||||
Long currentTenantId = TenantContextHolder.getTenantId();
|
||||
if (currentTenantId != null) {
|
||||
TenantDO parentTenant = tenantMapper.selectById(currentTenantId);
|
||||
if (parentTenant != null && parentTenant.getTenantLevel() != null && parentTenant.getTenantLevel() == 2) {
|
||||
throw exception(TENANT_LEVEL_MAX);
|
||||
}
|
||||
tenant.setParentId(currentTenantId);
|
||||
tenant.setTenantLevel(parentTenant != null && parentTenant.getTenantLevel() != null ? parentTenant.getTenantLevel() + 1 : 1);
|
||||
}
|
||||
}
|
||||
tenantMapper.insert(tenant);
|
||||
// 创建租户的管理员
|
||||
TenantUtils.execute(tenant.getId(), () -> {
|
||||
|
||||
Reference in New Issue
Block a user