From d66edbeffee5e308504e48372a5bb4d04aa4e843 Mon Sep 17 00:00:00 2001 From: houzhongjian <houzhongyi@126.com> Date: 星期一, 06 一月 2025 13:23:06 +0800 Subject: [PATCH] 租户套餐名称重复判定 --- iailab-framework/iailab-common/src/main/java/com/iailab/framework/common/pojo/CommonResult.java | 22 ++++++++++++---------- iailab-module-system/iailab-module-system-api/src/main/java/com/iailab/module/system/enums/ErrorCodeConstants.java | 1 + iailab-module-system/iailab-module-system-biz/src/main/java/com/iailab/module/system/dal/mysql/tenant/TenantPackageMapper.java | 5 +++++ iailab-module-system/iailab-module-system-biz/src/main/java/com/iailab/module/system/service/tenant/TenantPackageServiceImpl.java | 22 ++++++++++++++++++++++ 4 files changed, 40 insertions(+), 10 deletions(-) diff --git a/iailab-framework/iailab-common/src/main/java/com/iailab/framework/common/pojo/CommonResult.java b/iailab-framework/iailab-common/src/main/java/com/iailab/framework/common/pojo/CommonResult.java index 2f39029..063d0a6 100644 --- a/iailab-framework/iailab-common/src/main/java/com/iailab/framework/common/pojo/CommonResult.java +++ b/iailab-framework/iailab-common/src/main/java/com/iailab/framework/common/pojo/CommonResult.java @@ -1,11 +1,12 @@ package com.iailab.framework.common.pojo; +import cn.hutool.core.lang.Assert; import com.iailab.framework.common.exception.ErrorCode; import com.iailab.framework.common.exception.ServiceException; import com.iailab.framework.common.exception.enums.GlobalErrorCodeConstants; import com.fasterxml.jackson.annotation.JsonIgnore; +import com.iailab.framework.common.exception.util.ServiceExceptionUtil; import lombok.Data; -import org.springframework.util.Assert; import java.io.Serializable; import java.util.Objects; @@ -41,7 +42,7 @@ * 因为 A 方法返回的 CommonResult 对象,不满足调用其的 B 方法的返回,所以需要进行转换。 * * @param result 传入的 result 对象 - * @param <T> 返回的泛型 + * @param <T> 返回的泛型 * @return 新的 CommonResult 对象 */ public static <T> CommonResult<T> error(CommonResult<?> result) { @@ -49,10 +50,18 @@ } public static <T> CommonResult<T> error(Integer code, String message) { - Assert.isTrue(!GlobalErrorCodeConstants.SUCCESS.getCode().equals(code), "code 必须是错误的!"); + cn.hutool.core.lang.Assert.notEquals(GlobalErrorCodeConstants.SUCCESS.getCode(), code, "code 必须是错误的!"); CommonResult<T> result = new CommonResult<>(); result.code = code; result.msg = message; + return result; + } + + public static <T> CommonResult<T> error(ErrorCode errorCode, Object... params) { + Assert.notEquals(GlobalErrorCodeConstants.SUCCESS.getCode(), errorCode.getCode(), "code 必须是错误的!"); + CommonResult<T> result = new CommonResult<>(); + result.code = errorCode.getCode(); + result.msg = ServiceExceptionUtil.doFormat(errorCode.getCode(), errorCode.getMsg(), params); return result; } @@ -65,13 +74,6 @@ result.code = GlobalErrorCodeConstants.SUCCESS.getCode(); result.data = data; result.msg = ""; - return result; - } - - public static CommonResult<String> success() { - CommonResult<String> result = new CommonResult<>(); - result.code = GlobalErrorCodeConstants.SUCCESS.getCode(); - result.msg = "success"; return result; } diff --git a/iailab-module-system/iailab-module-system-api/src/main/java/com/iailab/module/system/enums/ErrorCodeConstants.java b/iailab-module-system/iailab-module-system-api/src/main/java/com/iailab/module/system/enums/ErrorCodeConstants.java index aa3103d..5a968ac 100644 --- a/iailab-module-system/iailab-module-system-api/src/main/java/com/iailab/module/system/enums/ErrorCodeConstants.java +++ b/iailab-module-system/iailab-module-system-api/src/main/java/com/iailab/module/system/enums/ErrorCodeConstants.java @@ -110,6 +110,7 @@ ErrorCode TENANT_PACKAGE_NOT_EXISTS = new ErrorCode(1_002_016_000, "租户套餐不存在"); ErrorCode TENANT_PACKAGE_USED = new ErrorCode(1_002_016_001, "租户正在使用该套餐,请给租户重新设置套餐后再尝试删除"); ErrorCode TENANT_PACKAGE_DISABLE = new ErrorCode(1_002_016_002, "名字为【{}】的租户套餐已被禁用"); + ErrorCode TENANT_PACKAGE_NAME_DUPLICATE = new ErrorCode(1_002_016_003, "已经存在该名字的租户套餐"); // ========== 社交用户 1-002-018-000 ========== ErrorCode SOCIAL_USER_AUTH_FAILURE = new ErrorCode(1_002_018_000, "社交授权失败,原因是:{}"); diff --git a/iailab-module-system/iailab-module-system-biz/src/main/java/com/iailab/module/system/dal/mysql/tenant/TenantPackageMapper.java b/iailab-module-system/iailab-module-system-biz/src/main/java/com/iailab/module/system/dal/mysql/tenant/TenantPackageMapper.java index 031c4e3..84dd53b 100644 --- a/iailab-module-system/iailab-module-system-biz/src/main/java/com/iailab/module/system/dal/mysql/tenant/TenantPackageMapper.java +++ b/iailab-module-system/iailab-module-system-biz/src/main/java/com/iailab/module/system/dal/mysql/tenant/TenantPackageMapper.java @@ -29,4 +29,9 @@ default List<TenantPackageDO> selectListByStatus(Integer status) { return selectList(TenantPackageDO::getStatus, status); } + + + default TenantPackageDO selectByName(String name) { + return selectOne(TenantPackageDO::getName, name); + } } diff --git a/iailab-module-system/iailab-module-system-biz/src/main/java/com/iailab/module/system/service/tenant/TenantPackageServiceImpl.java b/iailab-module-system/iailab-module-system-biz/src/main/java/com/iailab/module/system/service/tenant/TenantPackageServiceImpl.java index 06371fa..e5898b6 100644 --- a/iailab-module-system/iailab-module-system-biz/src/main/java/com/iailab/module/system/service/tenant/TenantPackageServiceImpl.java +++ b/iailab-module-system/iailab-module-system-biz/src/main/java/com/iailab/module/system/service/tenant/TenantPackageServiceImpl.java @@ -1,6 +1,8 @@ package com.iailab.module.system.service.tenant; import cn.hutool.core.collection.CollUtil; +import cn.hutool.core.util.StrUtil; +import com.google.common.annotations.VisibleForTesting; import com.iailab.framework.common.enums.CommonStatusEnum; import com.iailab.framework.common.pojo.PageResult; import com.iailab.framework.common.util.object.BeanUtils; @@ -38,6 +40,8 @@ @Override public Long createTenantPackage(TenantPackageSaveReqVO createReqVO) { + // 校验套餐名是否重复 + validateTenantPackageNameUnique(null, createReqVO.getName()); // 插入 TenantPackageDO tenantPackage = BeanUtils.toBean(createReqVO, TenantPackageDO.class); tenantPackageMapper.insert(tenantPackage); @@ -111,4 +115,22 @@ return tenantPackageMapper.selectListByStatus(status); } + @VisibleForTesting + void validateTenantPackageNameUnique(Long id, String name) { + if (StrUtil.isBlank(name)) { + return; + } + TenantPackageDO tenantPackage = tenantPackageMapper.selectByName(name); + if (tenantPackage == null) { + return; + } + // 如果 id 为空,说明不用比较是否为相同 id 的用户 + if (id == null) { + throw exception(TENANT_PACKAGE_NAME_DUPLICATE); + } + if (!tenantPackage.getId().equals(id)) { + throw exception(TENANT_PACKAGE_NAME_DUPLICATE); + } + } + } -- Gitblit v1.9.3