From 16f1d653c349b54b653fec60f77eac3a3495efb2 Mon Sep 17 00:00:00 2001 From: ziin Date: Thu, 25 Dec 2025 19:17:50 +0800 Subject: [PATCH] =?UTF-8?q?feat(tenant):=20=E6=96=B0=E5=A2=9E=E4=BB=A3?= =?UTF-8?q?=E7=90=86=E7=A7=9F=E6=88=B7=E5=88=9B=E5=BB=BA=E4=B8=8E=E5=88=86?= =?UTF-8?q?=E6=88=90=E6=AF=94=E4=BE=8B=E6=A0=A1=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../system/enums/ErrorCodeConstants.java | 2 ++ .../service/tenant/TenantServiceImpl.java | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/yolo-module-system/src/main/java/com/yolo/keyboard/module/system/enums/ErrorCodeConstants.java b/yolo-module-system/src/main/java/com/yolo/keyboard/module/system/enums/ErrorCodeConstants.java index 7aa77bd..76465eb 100644 --- a/yolo-module-system/src/main/java/com/yolo/keyboard/module/system/enums/ErrorCodeConstants.java +++ b/yolo-module-system/src/main/java/com/yolo/keyboard/module/system/enums/ErrorCodeConstants.java @@ -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, "租户套餐不存在"); diff --git a/yolo-module-system/src/main/java/com/yolo/keyboard/module/system/service/tenant/TenantServiceImpl.java b/yolo-module-system/src/main/java/com/yolo/keyboard/module/system/service/tenant/TenantServiceImpl.java index ce310e1..1f3250e 100644 --- a/yolo-module-system/src/main/java/com/yolo/keyboard/module/system/service/tenant/TenantServiceImpl.java +++ b/yolo-module-system/src/main/java/com/yolo/keyboard/module/system/service/tenant/TenantServiceImpl.java @@ -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(), () -> {