diff --git a/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/exception/util/ServiceExceptionUtil.java b/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/exception/util/ServiceExceptionUtil.java index 966109fbf..03c085940 100644 --- a/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/exception/util/ServiceExceptionUtil.java +++ b/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/exception/util/ServiceExceptionUtil.java @@ -6,74 +6,24 @@ import cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstant import com.google.common.annotations.VisibleForTesting; import lombok.extern.slf4j.Slf4j; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentMap; - /** * {@link ServiceException} 工具类 * * 目的在于,格式化异常信息提示。 * 考虑到 String.format 在参数不正确时会报错,因此使用 {} 作为占位符,并使用 {@link #doFormat(int, String, Object...)} 方法来格式化 * - * 因为 {@link #MESSAGES} 里面默认是没有异常信息提示的模板的,所以需要使用方自己初始化进去。目前想到的有几种方式: - * - * 1. 异常提示信息,写在枚举类中,例如说,cn.iocoder.oceans.user.api.constants.ErrorCodeEnum 类 + ServiceExceptionConfiguration - * 2. 异常提示信息,写在 .properties 等等配置文件 - * 3. 异常提示信息,写在 Apollo 等等配置中心中,从而实现可动态刷新 - * 4. 异常提示信息,存储在 db 等等数据库中,从而实现可动态刷新 */ @Slf4j public class ServiceExceptionUtil { - /** - * 错误码提示模板 - */ - private static final ConcurrentMap MESSAGES = new ConcurrentHashMap<>(); - - public static void putAll(Map messages) { - ServiceExceptionUtil.MESSAGES.putAll(messages); - } - - public static void put(Integer code, String message) { - ServiceExceptionUtil.MESSAGES.put(code, message); - } - - public static void delete(Integer code, String message) { - ServiceExceptionUtil.MESSAGES.remove(code, message); - } - // ========== 和 ServiceException 的集成 ========== public static ServiceException exception(ErrorCode errorCode) { - String messagePattern = MESSAGES.getOrDefault(errorCode.getCode(), errorCode.getMsg()); - return exception0(errorCode.getCode(), messagePattern); + return exception0(errorCode.getCode(), errorCode.getMsg()); } public static ServiceException exception(ErrorCode errorCode, Object... params) { - String messagePattern = MESSAGES.getOrDefault(errorCode.getCode(), errorCode.getMsg()); - return exception0(errorCode.getCode(), messagePattern, params); - } - - /** - * 创建指定编号的 ServiceException 的异常 - * - * @param code 编号 - * @return 异常 - */ - public static ServiceException exception(Integer code) { - return exception0(code, MESSAGES.get(code)); - } - - /** - * 创建指定编号的 ServiceException 的异常 - * - * @param code 编号 - * @param params 消息提示的占位符对应的参数 - * @return 异常 - */ - public static ServiceException exception(Integer code, Object... params) { - return exception0(code, MESSAGES.get(code), params); + return exception0(errorCode.getCode(), errorCode.getMsg(), params); } public static ServiceException exception0(Integer code, String messagePattern, Object... params) { diff --git a/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/errorcode/config/ErrorCodeProperties.java b/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/errorcode/config/ErrorCodeProperties.java deleted file mode 100644 index 5e6aab49a..000000000 --- a/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/errorcode/config/ErrorCodeProperties.java +++ /dev/null @@ -1,30 +0,0 @@ -package cn.iocoder.yudao.framework.errorcode.config; - -import lombok.Data; -import org.springframework.boot.context.properties.ConfigurationProperties; -import org.springframework.validation.annotation.Validated; - -import jakarta.validation.constraints.NotNull; -import java.util.List; - -/** - * 错误码的配置属性类 - * - * @author dlyan - */ -@ConfigurationProperties("yudao.error-code") -@Data -@Validated -public class ErrorCodeProperties { - - /** - * 是否开启 - */ - private Boolean enable = true; - /** - * 错误码枚举类 - */ - @NotNull(message = "错误码枚举类不能为空") - private List constantsClassList; - -} diff --git a/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/errorcode/config/YudaoErrorCodeAutoConfiguration.java b/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/errorcode/config/YudaoErrorCodeAutoConfiguration.java deleted file mode 100644 index ed2c92fc2..000000000 --- a/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/errorcode/config/YudaoErrorCodeAutoConfiguration.java +++ /dev/null @@ -1,39 +0,0 @@ -package cn.iocoder.yudao.framework.errorcode.config; - -import cn.iocoder.yudao.framework.errorcode.core.generator.ErrorCodeAutoGenerator; -import cn.iocoder.yudao.framework.errorcode.core.generator.ErrorCodeAutoGeneratorImpl; -import cn.iocoder.yudao.framework.errorcode.core.loader.ErrorCodeLoader; -import cn.iocoder.yudao.framework.errorcode.core.loader.ErrorCodeLoaderImpl; -import cn.iocoder.yudao.module.system.api.errorcode.ErrorCodeApi; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.boot.autoconfigure.AutoConfiguration; -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; -import org.springframework.boot.context.properties.EnableConfigurationProperties; -import org.springframework.context.annotation.Bean; -import org.springframework.scheduling.annotation.EnableScheduling; - -/** - * 错误码配置类 - * - * @author 芋道源码 - */ -@AutoConfiguration -@ConditionalOnProperty(prefix = "yudao.error-code", value = "enable", matchIfMissing = true) // 允许使用 yudao.error-code.enable=false 禁用访问日志 -@EnableConfigurationProperties(ErrorCodeProperties.class) -@EnableScheduling // 开启调度任务的功能,因为 ErrorCodeRemoteLoader 通过定时刷新错误码 -public class YudaoErrorCodeAutoConfiguration { - - @Bean - public ErrorCodeAutoGenerator errorCodeAutoGenerator(@Value("${spring.application.name}") String applicationName, - ErrorCodeProperties errorCodeProperties, - ErrorCodeApi errorCodeApi) { - return new ErrorCodeAutoGeneratorImpl(applicationName, errorCodeProperties.getConstantsClassList(), errorCodeApi); - } - - @Bean - public ErrorCodeLoader errorCodeLoader(@Value("${spring.application.name}") String applicationName, - ErrorCodeApi errorCodeApi) { - return new ErrorCodeLoaderImpl(applicationName, errorCodeApi); - } - -} diff --git a/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/errorcode/core/generator/ErrorCodeAutoGenerator.java b/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/errorcode/core/generator/ErrorCodeAutoGenerator.java deleted file mode 100644 index b13cacaa2..000000000 --- a/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/errorcode/core/generator/ErrorCodeAutoGenerator.java +++ /dev/null @@ -1,15 +0,0 @@ -package cn.iocoder.yudao.framework.errorcode.core.generator; - -/** - * 错误码的自动生成器 - * - * @author dylan - */ -public interface ErrorCodeAutoGenerator { - - /** - * 将配置类到错误码写入数据库 - */ - void execute(); - -} diff --git a/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/errorcode/core/generator/ErrorCodeAutoGeneratorImpl.java b/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/errorcode/core/generator/ErrorCodeAutoGeneratorImpl.java deleted file mode 100644 index 00fbc03ca..000000000 --- a/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/errorcode/core/generator/ErrorCodeAutoGeneratorImpl.java +++ /dev/null @@ -1,108 +0,0 @@ -package cn.iocoder.yudao.framework.errorcode.core.generator; - -import cn.hutool.core.collection.CollUtil; -import cn.hutool.core.exceptions.ExceptionUtil; -import cn.hutool.core.util.ClassUtil; -import cn.hutool.core.util.ReflectUtil; -import cn.iocoder.yudao.framework.common.exception.ErrorCode; -import cn.iocoder.yudao.module.system.api.errorcode.ErrorCodeApi; -import cn.iocoder.yudao.module.system.api.errorcode.dto.ErrorCodeAutoGenerateReqDTO; -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.springframework.boot.context.event.ApplicationReadyEvent; -import org.springframework.context.event.EventListener; -import org.springframework.scheduling.annotation.Async; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -/** - * ErrorCodeAutoGenerator 的实现类 - * 目的是,扫描指定的 {@link #constantsClassList} 类,写入到 system 服务中 - * - * @author dylan - */ -@RequiredArgsConstructor -@Slf4j -public class ErrorCodeAutoGeneratorImpl implements ErrorCodeAutoGenerator { - - /** - * 应用分组 - */ - private final String applicationName; - /** - * 错误码枚举类 - */ - private final List constantsClassList; - /** - * 错误码 Api - */ - private final ErrorCodeApi errorCodeApi; - - @Override - @EventListener(ApplicationReadyEvent.class) - @Async // 异步,保证项目的启动过程,毕竟非关键流程 - public void execute() { - // 第一步,解析错误码 - List autoGenerateDTOs = parseErrorCode(); - log.info("[execute][解析到错误码数量为 ({}) 个]", autoGenerateDTOs.size()); - - // 第二步,写入到 system 服务 - try { - errorCodeApi.autoGenerateErrorCodeList(autoGenerateDTOs); - log.info("[execute][写入到 system 组件完成]"); - } catch (Exception ex) { - log.error("[execute][写入到 system 组件失败({})]", ExceptionUtil.getRootCauseMessage(ex)); - } - } - - /** - * 解析 constantsClassList 变量,转换成错误码数组 - * - * @return 错误码数组 - */ - private List parseErrorCode() { - // 校验 errorCodeConstantsClass 参数 - if (CollUtil.isEmpty(constantsClassList)) { - log.info("[execute][未配置 yudao.error-code.constants-class-list 配置项,不进行自动写入到 system 服务中]"); - return new ArrayList<>(); - } - - // 解析错误码 - List autoGenerateDTOs = new ArrayList<>(); - constantsClassList.forEach(constantsClass -> { - try { - // 解析错误码枚举类 - Class errorCodeConstantsClazz = ClassUtil.loadClass(constantsClass); - // 解析错误码 - autoGenerateDTOs.addAll(parseErrorCode(errorCodeConstantsClazz)); - } catch (Exception ex) { - log.warn("[parseErrorCode][constantsClass({}) 加载失败({})]", constantsClass, - ExceptionUtil.getRootCauseMessage(ex)); - } - }); - return autoGenerateDTOs; - } - - /** - * 解析错误码类,获得错误码数组 - * - * @return 错误码数组 - */ - private List parseErrorCode(Class constantsClass) { - List autoGenerateDTOs = new ArrayList<>(); - Arrays.stream(constantsClass.getFields()).forEach(field -> { - if (field.getType() != ErrorCode.class) { - return; - } - // 转换成 ErrorCodeAutoGenerateReqDTO 对象 - ErrorCode errorCode = (ErrorCode) ReflectUtil.getFieldValue(constantsClass, field); - autoGenerateDTOs.add(new ErrorCodeAutoGenerateReqDTO().setApplicationName(applicationName) - .setCode(errorCode.getCode()).setMessage(errorCode.getMsg())); - }); - return autoGenerateDTOs; - } - -} - diff --git a/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/errorcode/core/loader/ErrorCodeLoader.java b/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/errorcode/core/loader/ErrorCodeLoader.java deleted file mode 100644 index 0bf70211f..000000000 --- a/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/errorcode/core/loader/ErrorCodeLoader.java +++ /dev/null @@ -1,34 +0,0 @@ -package cn.iocoder.yudao.framework.errorcode.core.loader; - -import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil; - -/** - * 错误码加载器 - * - * 注意,错误码最终加载到 {@link ServiceExceptionUtil} 的 MESSAGES 变量中! - * - * @author dlyan - */ -public interface ErrorCodeLoader { - - /** - * 添加错误码 - * - * @param code 错误码的编号 - * @param msg 错误码的提示 - */ - default void putErrorCode(Integer code, String msg) { - ServiceExceptionUtil.put(code, msg); - } - - /** - * 刷新错误码 - */ - void refreshErrorCodes(); - - /** - * 加载错误码 - */ - void loadErrorCodes(); - -} diff --git a/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/errorcode/core/loader/ErrorCodeLoaderImpl.java b/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/errorcode/core/loader/ErrorCodeLoaderImpl.java deleted file mode 100644 index 4d1febfd7..000000000 --- a/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/errorcode/core/loader/ErrorCodeLoaderImpl.java +++ /dev/null @@ -1,82 +0,0 @@ -package cn.iocoder.yudao.framework.errorcode.core.loader; - -import cn.hutool.core.collection.CollUtil; -import cn.hutool.core.exceptions.ExceptionUtil; -import cn.iocoder.yudao.framework.common.util.date.DateUtils; -import cn.iocoder.yudao.module.system.api.errorcode.ErrorCodeApi; -import cn.iocoder.yudao.module.system.api.errorcode.dto.ErrorCodeRespDTO; -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.springframework.boot.context.event.ApplicationReadyEvent; -import org.springframework.context.event.EventListener; -import org.springframework.scheduling.annotation.Async; -import org.springframework.scheduling.annotation.Scheduled; - -import java.time.LocalDateTime; -import java.util.List; - -/** - * ErrorCodeLoader 的实现类,从 infra 的数据库中,加载错误码。 - * - * 考虑到错误码会刷新,所以按照 {@link #REFRESH_ERROR_CODE_PERIOD} 频率,增量加载错误码。 - * - * @author dlyan - */ -@RequiredArgsConstructor -@Slf4j -public class ErrorCodeLoaderImpl implements ErrorCodeLoader { - - /** - * 刷新错误码的频率,单位:毫秒 - */ - private static final int REFRESH_ERROR_CODE_PERIOD = 60 * 1000; - - /** - * 应用分组 - */ - private final String applicationName; - /** - * 错误码 Api - */ - private final ErrorCodeApi errorCodeApi; - - /** - * 缓存错误码的最大更新时间,用于后续的增量轮询,判断是否有更新 - */ - private LocalDateTime maxUpdateTime; - - @Override - @EventListener(ApplicationReadyEvent.class) - @Async // 异步,保证项目的启动过程,毕竟非关键流程 - public void loadErrorCodes() { - loadErrorCodes0(); - } - - @Override - @Scheduled(fixedDelay = REFRESH_ERROR_CODE_PERIOD, initialDelay = REFRESH_ERROR_CODE_PERIOD) - public void refreshErrorCodes() { - loadErrorCodes0(); - } - - private void loadErrorCodes0() { - try { - // 加载错误码 - List errorCodeRespDTOs = errorCodeApi.getErrorCodeList(applicationName, maxUpdateTime); - if (CollUtil.isEmpty(errorCodeRespDTOs)) { - return; - } - log.info("[loadErrorCodes0][加载到 ({}) 个错误码]", errorCodeRespDTOs.size()); - - // 刷新错误码的缓存 - errorCodeRespDTOs.forEach(errorCodeRespDTO -> { - // 写入到错误码的缓存 - putErrorCode(errorCodeRespDTO.getCode(), errorCodeRespDTO.getMessage()); - // 记录下更新时间,方便增量更新 - maxUpdateTime = DateUtils.max(maxUpdateTime, errorCodeRespDTO.getUpdateTime()); - }); - } catch (Exception ex) { - log.error("[loadErrorCodes0][加载错误码失败({})]", ExceptionUtil.getRootCauseMessage(ex)); - } - } - -} diff --git a/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/errorcode/package-info.java b/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/errorcode/package-info.java deleted file mode 100644 index ddba4f78a..000000000 --- a/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/errorcode/package-info.java +++ /dev/null @@ -1,10 +0,0 @@ -/** - * 错误码 ErrorCode 的自动配置功能,提供如下功能: - * - * 1. 远程读取:项目启动时,从 system-service 服务,读取数据库中的 ErrorCode 错误码,实现错误码的提水可配置; - * 2. 自动更新:管理员在管理后台修数据库中的 ErrorCode 错误码时,项目自动从 system-service 服务加载最新的 ErrorCode 错误码; - * 3. 自动写入:项目启动时,将项目本地的错误码写到 system-server 服务中,方便管理员在管理后台编辑; - * - * @author 芋道源码 - */ -package cn.iocoder.yudao.framework.errorcode; diff --git a/yudao-framework/yudao-spring-boot-starter-web/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/yudao-framework/yudao-spring-boot-starter-web/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports index 36ba94cbb..9cdcd09c4 100644 --- a/yudao-framework/yudao-spring-boot-starter-web/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports +++ b/yudao-framework/yudao-spring-boot-starter-web/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -3,5 +3,4 @@ cn.iocoder.yudao.framework.jackson.config.YudaoJacksonAutoConfiguration cn.iocoder.yudao.framework.swagger.config.YudaoSwaggerAutoConfiguration cn.iocoder.yudao.framework.web.config.YudaoWebAutoConfiguration cn.iocoder.yudao.framework.xss.config.YudaoXssAutoConfiguration -cn.iocoder.yudao.framework.banner.config.YudaoBannerAutoConfiguration -cn.iocoder.yudao.framework.errorcode.config.YudaoErrorCodeAutoConfiguration \ No newline at end of file +cn.iocoder.yudao.framework.banner.config.YudaoBannerAutoConfiguration \ No newline at end of file diff --git a/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/api/errorcode/ErrorCodeApi.java b/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/api/errorcode/ErrorCodeApi.java deleted file mode 100644 index a86a999af..000000000 --- a/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/api/errorcode/ErrorCodeApi.java +++ /dev/null @@ -1,35 +0,0 @@ -package cn.iocoder.yudao.module.system.api.errorcode; - -import cn.iocoder.yudao.module.system.api.errorcode.dto.ErrorCodeAutoGenerateReqDTO; -import cn.iocoder.yudao.module.system.api.errorcode.dto.ErrorCodeRespDTO; - -import jakarta.validation.Valid; -import java.time.LocalDateTime; -import java.util.List; - -/** - * 错误码 Api 接口 - * - * @author 芋道源码 - */ -public interface ErrorCodeApi { - - /** - * 自动创建错误码 - * - * @param autoGenerateDTOs 错误码信息 - */ - void autoGenerateErrorCodeList(@Valid List autoGenerateDTOs); - - /** - * 增量获得错误码数组 - * - * 如果 minUpdateTime 为空时,则获取所有错误码 - * - * @param applicationName 应用名 - * @param minUpdateTime 最小更新时间 - * @return 错误码数组 - */ - List getErrorCodeList(String applicationName, LocalDateTime minUpdateTime); - -} diff --git a/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/api/errorcode/dto/ErrorCodeAutoGenerateReqDTO.java b/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/api/errorcode/dto/ErrorCodeAutoGenerateReqDTO.java deleted file mode 100644 index cb8935a0a..000000000 --- a/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/api/errorcode/dto/ErrorCodeAutoGenerateReqDTO.java +++ /dev/null @@ -1,34 +0,0 @@ -package cn.iocoder.yudao.module.system.api.errorcode.dto; - -import lombok.Data; -import lombok.experimental.Accessors; - -import jakarta.validation.constraints.NotEmpty; -import jakarta.validation.constraints.NotNull; - -/** - * 错误码自动生成 DTO - * - * @author dylan - */ -@Data -@Accessors(chain = true) -public class ErrorCodeAutoGenerateReqDTO { - - /** - * 应用名 - */ - @NotNull(message = "应用名不能为空") - private String applicationName; - /** - * 错误码编码 - */ - @NotNull(message = "错误码编码不能为空") - private Integer code; - /** - * 错误码错误提示 - */ - @NotEmpty(message = "错误码错误提示不能为空") - private String message; - -} diff --git a/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/api/errorcode/dto/ErrorCodeRespDTO.java b/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/api/errorcode/dto/ErrorCodeRespDTO.java deleted file mode 100644 index 99372300b..000000000 --- a/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/api/errorcode/dto/ErrorCodeRespDTO.java +++ /dev/null @@ -1,28 +0,0 @@ -package cn.iocoder.yudao.module.system.api.errorcode.dto; - -import lombok.Data; - -import java.time.LocalDateTime; - -/** - * 错误码的 Response DTO - * - * @author 芋道源码 - */ -@Data -public class ErrorCodeRespDTO { - - /** - * 错误码编码 - */ - private Integer code; - /** - * 错误码错误提示 - */ - private String message; - /** - * 更新时间 - */ - private LocalDateTime updateTime; - -} diff --git a/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/enums/errorcode/ErrorCodeTypeEnum.java b/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/enums/errorcode/ErrorCodeTypeEnum.java deleted file mode 100644 index 97349e763..000000000 --- a/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/enums/errorcode/ErrorCodeTypeEnum.java +++ /dev/null @@ -1,39 +0,0 @@ -package cn.iocoder.yudao.module.system.enums.errorcode; - -import cn.iocoder.yudao.framework.common.core.IntArrayValuable; -import lombok.AllArgsConstructor; -import lombok.Getter; - -import java.util.Arrays; - -/** - * 错误码的类型枚举 - * - * @author dylan - */ -@AllArgsConstructor -@Getter -public enum ErrorCodeTypeEnum implements IntArrayValuable { - - /** - * 自动生成 - */ - AUTO_GENERATION(1), - /** - * 手动编辑 - */ - MANUAL_OPERATION(2); - - public static final int[] ARRAYS = Arrays.stream(values()).mapToInt(ErrorCodeTypeEnum::getType).toArray(); - - /** - * 类型 - */ - private final Integer type; - - @Override - public int[] array() { - return ARRAYS; - } - -} diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/api/errorcode/ErrorCodeApiImpl.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/api/errorcode/ErrorCodeApiImpl.java deleted file mode 100644 index dd630205c..000000000 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/api/errorcode/ErrorCodeApiImpl.java +++ /dev/null @@ -1,33 +0,0 @@ -package cn.iocoder.yudao.module.system.api.errorcode; - -import cn.iocoder.yudao.module.system.api.errorcode.dto.ErrorCodeAutoGenerateReqDTO; -import cn.iocoder.yudao.module.system.api.errorcode.dto.ErrorCodeRespDTO; -import cn.iocoder.yudao.module.system.service.errorcode.ErrorCodeService; -import org.springframework.stereotype.Service; - -import jakarta.annotation.Resource; -import java.time.LocalDateTime; -import java.util.List; - -/** - * 错误码 Api 实现类 - * - * @author 芋道源码 - */ -@Service -public class ErrorCodeApiImpl implements ErrorCodeApi { - - @Resource - private ErrorCodeService errorCodeService; - - @Override - public void autoGenerateErrorCodeList(List autoGenerateDTOs) { - errorCodeService.autoGenerateErrorCodes(autoGenerateDTOs); - } - - @Override - public List getErrorCodeList(String applicationName, LocalDateTime minUpdateTime) { - return errorCodeService.getErrorCodeList(applicationName, minUpdateTime); - } - -} diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/errorcode/ErrorCodeController.http b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/errorcode/ErrorCodeController.http deleted file mode 100644 index 06b872318..000000000 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/errorcode/ErrorCodeController.http +++ /dev/null @@ -1,13 +0,0 @@ -### 创建错误码 -POST {{baseUrl}}/inra/error-code/create -Authorization: Bearer {{token}} -Content-Type: application/json -tenant-id: {{adminTenentId}} - -{ - "code": 200, - "message": "成功", - "group": "test", - "type": 1 -} - diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/errorcode/ErrorCodeController.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/errorcode/ErrorCodeController.java deleted file mode 100644 index a0d14c557..000000000 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/errorcode/ErrorCodeController.java +++ /dev/null @@ -1,93 +0,0 @@ -package cn.iocoder.yudao.module.system.controller.admin.errorcode; - -import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog; -import cn.iocoder.yudao.framework.common.pojo.CommonResult; -import cn.iocoder.yudao.framework.common.pojo.PageParam; -import cn.iocoder.yudao.framework.common.pojo.PageResult; -import cn.iocoder.yudao.framework.common.util.object.BeanUtils; -import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils; -import cn.iocoder.yudao.module.system.controller.admin.errorcode.vo.ErrorCodePageReqVO; -import cn.iocoder.yudao.module.system.controller.admin.errorcode.vo.ErrorCodeRespVO; -import cn.iocoder.yudao.module.system.controller.admin.errorcode.vo.ErrorCodeSaveReqVO; -import cn.iocoder.yudao.module.system.dal.dataobject.errorcode.ErrorCodeDO; -import cn.iocoder.yudao.module.system.service.errorcode.ErrorCodeService; -import io.swagger.v3.oas.annotations.Operation; -import io.swagger.v3.oas.annotations.Parameter; -import io.swagger.v3.oas.annotations.tags.Tag; -import jakarta.annotation.Resource; -import jakarta.servlet.http.HttpServletResponse; -import jakarta.validation.Valid; -import org.springframework.security.access.prepost.PreAuthorize; -import org.springframework.validation.annotation.Validated; -import org.springframework.web.bind.annotation.*; - -import java.io.IOException; -import java.util.List; - -import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT; -import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; - -@Tag(name = "管理后台 - 错误码") -@RestController -@RequestMapping("/system/error-code") -@Validated -public class ErrorCodeController { - - @Resource - private ErrorCodeService errorCodeService; - - @PostMapping("/create") - @Operation(summary = "创建错误码") - @PreAuthorize("@ss.hasPermission('system:error-code:create')") - public CommonResult createErrorCode(@Valid @RequestBody ErrorCodeSaveReqVO createReqVO) { - return success(errorCodeService.createErrorCode(createReqVO)); - } - - @PutMapping("/update") - @Operation(summary = "更新错误码") - @PreAuthorize("@ss.hasPermission('system:error-code:update')") - public CommonResult updateErrorCode(@Valid @RequestBody ErrorCodeSaveReqVO updateReqVO) { - errorCodeService.updateErrorCode(updateReqVO); - return success(true); - } - - @DeleteMapping("/delete") - @Operation(summary = "删除错误码") - @Parameter(name = "id", description = "编号", required = true) - @PreAuthorize("@ss.hasPermission('system:error-code:delete')") - public CommonResult deleteErrorCode(@RequestParam("id") Long id) { - errorCodeService.deleteErrorCode(id); - return success(true); - } - - @GetMapping("/get") - @Operation(summary = "获得错误码") - @Parameter(name = "id", description = "编号", required = true, example = "1024") - @PreAuthorize("@ss.hasPermission('system:error-code:query')") - public CommonResult getErrorCode(@RequestParam("id") Long id) { - ErrorCodeDO errorCode = errorCodeService.getErrorCode(id); - return success(BeanUtils.toBean(errorCode, ErrorCodeRespVO.class)); - } - - @GetMapping("/page") - @Operation(summary = "获得错误码分页") - @PreAuthorize("@ss.hasPermission('system:error-code:query')") - public CommonResult> getErrorCodePage(@Valid ErrorCodePageReqVO pageVO) { - PageResult pageResult = errorCodeService.getErrorCodePage(pageVO); - return success(BeanUtils.toBean(pageResult, ErrorCodeRespVO.class)); - } - - @GetMapping("/export-excel") - @Operation(summary = "导出错误码 Excel") - @PreAuthorize("@ss.hasPermission('system:error-code:export')") - @ApiAccessLog(operateType = EXPORT) - public void exportErrorCodeExcel(@Valid ErrorCodePageReqVO exportReqVO, - HttpServletResponse response) throws IOException { - exportReqVO.setPageSize(PageParam.PAGE_SIZE_NONE); - List list = errorCodeService.getErrorCodePage(exportReqVO).getList(); - // 导出 Excel - ExcelUtils.write(response, "错误码.xls", "数据", ErrorCodeRespVO.class, - BeanUtils.toBean(list, ErrorCodeRespVO.class)); - } - -} diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/errorcode/vo/ErrorCodePageReqVO.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/errorcode/vo/ErrorCodePageReqVO.java deleted file mode 100644 index ba565d94c..000000000 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/errorcode/vo/ErrorCodePageReqVO.java +++ /dev/null @@ -1,36 +0,0 @@ -package cn.iocoder.yudao.module.system.controller.admin.errorcode.vo; - -import cn.iocoder.yudao.framework.common.pojo.PageParam; -import io.swagger.v3.oas.annotations.media.Schema; -import lombok.Data; -import lombok.EqualsAndHashCode; -import lombok.ToString; -import org.springframework.format.annotation.DateTimeFormat; - -import java.time.LocalDateTime; - -import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND; - -@Schema(description = "管理后台 - 错误码分页 Request VO") -@Data -@EqualsAndHashCode(callSuper = true) -@ToString(callSuper = true) -public class ErrorCodePageReqVO extends PageParam { - - @Schema(description = "错误码类型,参见 ErrorCodeTypeEnum 枚举类", example = "1") - private Integer type; - - @Schema(description = "应用名", example = "dashboard") - private String applicationName; - - @Schema(description = "错误码编码", example = "1234") - private Integer code; - - @Schema(description = "错误码错误提示", example = "帅气") - private String message; - - @DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND) - @Schema(description = "创建时间") - private LocalDateTime[] createTime; - -} diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/errorcode/vo/ErrorCodeRespVO.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/errorcode/vo/ErrorCodeRespVO.java deleted file mode 100644 index 31f72bf93..000000000 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/errorcode/vo/ErrorCodeRespVO.java +++ /dev/null @@ -1,47 +0,0 @@ -package cn.iocoder.yudao.module.system.controller.admin.errorcode.vo; - -import cn.iocoder.yudao.framework.excel.core.annotations.DictFormat; -import cn.iocoder.yudao.framework.excel.core.convert.DictConvert; -import cn.iocoder.yudao.module.system.enums.DictTypeConstants; -import com.alibaba.excel.annotation.ExcelIgnoreUnannotated; -import com.alibaba.excel.annotation.ExcelProperty; -import io.swagger.v3.oas.annotations.media.Schema; -import lombok.Data; - -import java.time.LocalDateTime; - -@Schema(description = "管理后台 - 错误码 Response VO") -@Data -@ExcelIgnoreUnannotated -public class ErrorCodeRespVO { - - @Schema(description = "错误码编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1024") - @ExcelProperty("错误码编号") - private Long id; - - @Schema(description = "错误码类型,参见 ErrorCodeTypeEnum 枚举类", requiredMode = Schema.RequiredMode.REQUIRED, example = "1") - @ExcelProperty(value = "错误码类型", converter = DictConvert.class) - @DictFormat(DictTypeConstants.ERROR_CODE_TYPE) - private Integer type; - - @Schema(description = "应用名", requiredMode = Schema.RequiredMode.REQUIRED, example = "dashboard") - @ExcelProperty("应用名") - private String applicationName; - - @Schema(description = "错误码编码", requiredMode = Schema.RequiredMode.REQUIRED, example = "1234") - @ExcelProperty("错误码编码") - private Integer code; - - @Schema(description = "错误码错误提示", requiredMode = Schema.RequiredMode.REQUIRED, example = "帅气") - @ExcelProperty("错误码错误提示") - private String message; - - @Schema(description = "备注", example = "哈哈哈") - @ExcelProperty("备注") - private String memo; - - @Schema(description = "创建时间", requiredMode = Schema.RequiredMode.REQUIRED) - @ExcelProperty("创建时间") - private LocalDateTime createTime; - -} diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/errorcode/vo/ErrorCodeSaveReqVO.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/errorcode/vo/ErrorCodeSaveReqVO.java deleted file mode 100644 index 1fd2e0cc9..000000000 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/errorcode/vo/ErrorCodeSaveReqVO.java +++ /dev/null @@ -1,30 +0,0 @@ -package cn.iocoder.yudao.module.system.controller.admin.errorcode.vo; - -import io.swagger.v3.oas.annotations.media.Schema; -import lombok.Data; - -import jakarta.validation.constraints.NotNull; - -@Schema(description = "管理后台 - 错误码创建/修改 Request VO") -@Data -public class ErrorCodeSaveReqVO { - - @Schema(description = "错误码编号", example = "1024") - private Long id; - - @Schema(description = "应用名", requiredMode = Schema.RequiredMode.REQUIRED, example = "dashboard") - @NotNull(message = "应用名不能为空") - private String applicationName; - - @Schema(description = "错误码编码", requiredMode = Schema.RequiredMode.REQUIRED, example = "1234") - @NotNull(message = "错误码编码不能为空") - private Integer code; - - @Schema(description = "错误码错误提示", requiredMode = Schema.RequiredMode.REQUIRED, example = "帅气") - @NotNull(message = "错误码错误提示不能为空") - private String message; - - @Schema(description = "备注", example = "哈哈哈") - private String memo; - -} diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/dataobject/errorcode/ErrorCodeDO.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/dataobject/errorcode/ErrorCodeDO.java deleted file mode 100644 index 9ad563395..000000000 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/dataobject/errorcode/ErrorCodeDO.java +++ /dev/null @@ -1,52 +0,0 @@ -package cn.iocoder.yudao.module.system.dal.dataobject.errorcode; - -import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO; -import cn.iocoder.yudao.module.system.enums.errorcode.ErrorCodeTypeEnum; -import com.baomidou.mybatisplus.annotation.KeySequence; -import com.baomidou.mybatisplus.annotation.TableId; -import com.baomidou.mybatisplus.annotation.TableName; -import lombok.Data; -import lombok.EqualsAndHashCode; -import lombok.ToString; - -/** - * 错误码表 - * - * @author 芋道源码 - */ -@TableName(value = "system_error_code") -@KeySequence("system_error_code_seq") // 用于 Oracle、PostgreSQL、Kingbase、DB2、H2 数据库的主键自增。如果是 MySQL 等数据库,可不写。 -@Data -@EqualsAndHashCode(callSuper = true) -@ToString(callSuper = true) -public class ErrorCodeDO extends BaseDO { - - /** - * 错误码编号,自增 - */ - @TableId - private Long id; - /** - * 错误码类型 - * - * 枚举 {@link ErrorCodeTypeEnum} - */ - private Integer type; - /** - * 应用名 - */ - private String applicationName; - /** - * 错误码编码 - */ - private Integer code; - /** - * 错误码错误提示 - */ - private String message; - /** - * 错误码备注 - */ - private String memo; - -} diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/errorcode/ErrorCodeMapper.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/errorcode/ErrorCodeMapper.java deleted file mode 100644 index 2e345e3ad..000000000 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/errorcode/ErrorCodeMapper.java +++ /dev/null @@ -1,40 +0,0 @@ -package cn.iocoder.yudao.module.system.dal.mysql.errorcode; - -import cn.iocoder.yudao.framework.common.pojo.PageResult; -import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; -import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; -import cn.iocoder.yudao.module.system.controller.admin.errorcode.vo.ErrorCodePageReqVO; -import cn.iocoder.yudao.module.system.dal.dataobject.errorcode.ErrorCodeDO; -import org.apache.ibatis.annotations.Mapper; - -import java.time.LocalDateTime; -import java.util.Collection; -import java.util.List; - -@Mapper -public interface ErrorCodeMapper extends BaseMapperX { - - default PageResult selectPage(ErrorCodePageReqVO reqVO) { - return selectPage(reqVO, new LambdaQueryWrapperX() - .eqIfPresent(ErrorCodeDO::getType, reqVO.getType()) - .likeIfPresent(ErrorCodeDO::getApplicationName, reqVO.getApplicationName()) - .eqIfPresent(ErrorCodeDO::getCode, reqVO.getCode()) - .likeIfPresent(ErrorCodeDO::getMessage, reqVO.getMessage()) - .betweenIfPresent(ErrorCodeDO::getCreateTime, reqVO.getCreateTime()) - .orderByDesc(ErrorCodeDO::getCode)); - } - - default List selectListByCodes(Collection codes) { - return selectList(ErrorCodeDO::getCode, codes); - } - - default ErrorCodeDO selectByCode(Integer code) { - return selectOne(ErrorCodeDO::getCode, code); - } - - default List selectListByApplicationNameAndUpdateTimeGt(String applicationName, LocalDateTime minUpdateTime) { - return selectList(new LambdaQueryWrapperX().eq(ErrorCodeDO::getApplicationName, applicationName) - .gtIfPresent(ErrorCodeDO::getUpdateTime, minUpdateTime)); - } - -} diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/errorcode/ErrorCodeService.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/errorcode/ErrorCodeService.java deleted file mode 100644 index 1c7db3e31..000000000 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/errorcode/ErrorCodeService.java +++ /dev/null @@ -1,77 +0,0 @@ -package cn.iocoder.yudao.module.system.service.errorcode; - -import cn.iocoder.yudao.framework.common.pojo.PageResult; -import cn.iocoder.yudao.module.system.api.errorcode.dto.ErrorCodeAutoGenerateReqDTO; -import cn.iocoder.yudao.module.system.api.errorcode.dto.ErrorCodeRespDTO; -import cn.iocoder.yudao.module.system.controller.admin.errorcode.vo.ErrorCodePageReqVO; -import cn.iocoder.yudao.module.system.controller.admin.errorcode.vo.ErrorCodeSaveReqVO; -import cn.iocoder.yudao.module.system.dal.dataobject.errorcode.ErrorCodeDO; - -import jakarta.validation.Valid; -import java.time.LocalDateTime; -import java.util.List; - -/** - * 错误码 Service 接口 - * - * @author 芋道源码 - */ -public interface ErrorCodeService { - - /** - * 自动创建错误码 - * - * @param autoGenerateDTOs 错误码信息 - */ - void autoGenerateErrorCodes(@Valid List autoGenerateDTOs); - - /** - * 增量获得错误码数组 - * - * 如果 minUpdateTime 为空时,则获取所有错误码 - * - * @param applicationName 应用名 - * @param minUpdateTime 最小更新时间 - * @return 错误码数组 - */ - List getErrorCodeList(String applicationName, LocalDateTime minUpdateTime); - - /** - * 创建错误码 - * - * @param createReqVO 创建信息 - * @return 编号 - */ - Long createErrorCode(@Valid ErrorCodeSaveReqVO createReqVO); - - /** - * 更新错误码 - * - * @param updateReqVO 更新信息 - */ - void updateErrorCode(@Valid ErrorCodeSaveReqVO updateReqVO); - - /** - * 删除错误码 - * - * @param id 编号 - */ - void deleteErrorCode(Long id); - - /** - * 获得错误码 - * - * @param id 编号 - * @return 错误码 - */ - ErrorCodeDO getErrorCode(Long id); - - /** - * 获得错误码分页 - * - * @param pageReqVO 分页查询 - * @return 错误码分页 - */ - PageResult getErrorCodePage(ErrorCodePageReqVO pageReqVO); - -} diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/errorcode/ErrorCodeServiceImpl.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/errorcode/ErrorCodeServiceImpl.java deleted file mode 100644 index 2664f2871..000000000 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/errorcode/ErrorCodeServiceImpl.java +++ /dev/null @@ -1,167 +0,0 @@ -package cn.iocoder.yudao.module.system.service.errorcode; - -import cn.hutool.core.collection.CollUtil; -import cn.iocoder.yudao.framework.common.pojo.PageResult; -import cn.iocoder.yudao.framework.common.util.object.BeanUtils; -import cn.iocoder.yudao.module.system.api.errorcode.dto.ErrorCodeAutoGenerateReqDTO; -import cn.iocoder.yudao.module.system.api.errorcode.dto.ErrorCodeRespDTO; -import cn.iocoder.yudao.module.system.controller.admin.errorcode.vo.ErrorCodePageReqVO; -import cn.iocoder.yudao.module.system.controller.admin.errorcode.vo.ErrorCodeSaveReqVO; -import cn.iocoder.yudao.module.system.dal.dataobject.errorcode.ErrorCodeDO; -import cn.iocoder.yudao.module.system.dal.mysql.errorcode.ErrorCodeMapper; -import cn.iocoder.yudao.module.system.enums.errorcode.ErrorCodeTypeEnum; -import com.google.common.annotations.VisibleForTesting; -import lombok.extern.slf4j.Slf4j; -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; -import org.springframework.validation.annotation.Validated; - -import jakarta.annotation.Resource; -import java.time.LocalDateTime; -import java.util.List; -import java.util.Map; - -import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; -import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap; -import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet; -import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.ERROR_CODE_DUPLICATE; -import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.ERROR_CODE_NOT_EXISTS; - -/** - * 错误码 Service 实现类 - * - * @author dlyan - */ -@Service -@Validated -@Slf4j -public class ErrorCodeServiceImpl implements ErrorCodeService { - - @Resource - private ErrorCodeMapper errorCodeMapper; - - @Override - public Long createErrorCode(ErrorCodeSaveReqVO createReqVO) { - // 校验 code 重复 - validateCodeDuplicate(createReqVO.getCode(), null); - - // 插入 - ErrorCodeDO errorCode = BeanUtils.toBean(createReqVO, ErrorCodeDO.class) - .setType(ErrorCodeTypeEnum.MANUAL_OPERATION.getType()); - errorCodeMapper.insert(errorCode); - // 返回 - return errorCode.getId(); - } - - @Override - public void updateErrorCode(ErrorCodeSaveReqVO updateReqVO) { - // 校验存在 - validateErrorCodeExists(updateReqVO.getId()); - // 校验 code 重复 - validateCodeDuplicate(updateReqVO.getCode(), updateReqVO.getId()); - - // 更新 - ErrorCodeDO updateObj = BeanUtils.toBean(updateReqVO, ErrorCodeDO.class) - .setType(ErrorCodeTypeEnum.MANUAL_OPERATION.getType()); - errorCodeMapper.updateById(updateObj); - } - - @Override - public void deleteErrorCode(Long id) { - // 校验存在 - validateErrorCodeExists(id); - // 删除 - errorCodeMapper.deleteById(id); - } - - /** - * 校验错误码的唯一字段是否重复 - * - * 是否存在相同编码的错误码 - * - * @param code 错误码编码 - * @param id 错误码编号 - */ - @VisibleForTesting - public void validateCodeDuplicate(Integer code, Long id) { - ErrorCodeDO errorCodeDO = errorCodeMapper.selectByCode(code); - if (errorCodeDO == null) { - return; - } - // 如果 id 为空,说明不用比较是否为相同 id 的错误码 - if (id == null) { - throw exception(ERROR_CODE_DUPLICATE); - } - if (!errorCodeDO.getId().equals(id)) { - throw exception(ERROR_CODE_DUPLICATE); - } - } - - @VisibleForTesting - void validateErrorCodeExists(Long id) { - if (errorCodeMapper.selectById(id) == null) { - throw exception(ERROR_CODE_NOT_EXISTS); - } - } - - @Override - public ErrorCodeDO getErrorCode(Long id) { - return errorCodeMapper.selectById(id); - } - - @Override - public PageResult getErrorCodePage(ErrorCodePageReqVO pageReqVO) { - return errorCodeMapper.selectPage(pageReqVO); - } - - @Override - @Transactional - public void autoGenerateErrorCodes(List autoGenerateDTOs) { - if (CollUtil.isEmpty(autoGenerateDTOs)) { - return; - } - // 获得错误码 - List errorCodeDOs = errorCodeMapper.selectListByCodes( - convertSet(autoGenerateDTOs, ErrorCodeAutoGenerateReqDTO::getCode)); - Map errorCodeDOMap = convertMap(errorCodeDOs, ErrorCodeDO::getCode); - - // 遍历 autoGenerateBOs 数组,逐个插入或更新。考虑到每次量级不大,就不走批量了 - autoGenerateDTOs.forEach(autoGenerateDTO -> { - ErrorCodeDO errorCode = errorCodeDOMap.get(autoGenerateDTO.getCode()); - // 不存在,则进行新增 - if (errorCode == null) { - errorCode = BeanUtils.toBean(autoGenerateDTO, ErrorCodeDO.class) - .setType(ErrorCodeTypeEnum.AUTO_GENERATION.getType()); - errorCodeMapper.insert(errorCode); - return; - } - // 存在,则进行更新。更新有三个前置条件: - // 条件 1. 只更新自动生成的错误码,即 Type 为 ErrorCodeTypeEnum.AUTO_GENERATION - if (!ErrorCodeTypeEnum.AUTO_GENERATION.getType().equals(errorCode.getType())) { - return; - } - // 条件 2. 分组 applicationName 必须匹配,避免存在错误码冲突的情况 - if (!autoGenerateDTO.getApplicationName().equals(errorCode.getApplicationName())) { - log.error("[autoGenerateErrorCodes][自动创建({}/{}) 错误码失败,数据库中已经存在({}/{})]", - autoGenerateDTO.getCode(), autoGenerateDTO.getApplicationName(), - errorCode.getCode(), errorCode.getApplicationName()); - return; - } - // 条件 3. 错误提示语存在差异 - if (autoGenerateDTO.getMessage().equals(errorCode.getMessage())) { - return; - } - // 最终匹配,进行更新 - errorCodeMapper.updateById(new ErrorCodeDO().setId(errorCode.getId()).setMessage(autoGenerateDTO.getMessage())); - }); - } - - @Override - public List getErrorCodeList(String applicationName, LocalDateTime minUpdateTime) { - List list = errorCodeMapper.selectListByApplicationNameAndUpdateTimeGt( - applicationName, minUpdateTime); - return BeanUtils.toBean(list, ErrorCodeRespDTO.class); - } - -} - diff --git a/yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/errorcode/ErrorCodeServiceTest.java b/yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/errorcode/ErrorCodeServiceTest.java deleted file mode 100644 index b337e037a..000000000 --- a/yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/errorcode/ErrorCodeServiceTest.java +++ /dev/null @@ -1,308 +0,0 @@ -package cn.iocoder.yudao.module.system.service.errorcode; - -import cn.iocoder.yudao.framework.common.pojo.PageResult; -import cn.iocoder.yudao.framework.common.util.collection.ArrayUtils; -import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest; -import cn.iocoder.yudao.module.system.api.errorcode.dto.ErrorCodeAutoGenerateReqDTO; -import cn.iocoder.yudao.module.system.api.errorcode.dto.ErrorCodeRespDTO; -import cn.iocoder.yudao.module.system.controller.admin.errorcode.vo.ErrorCodePageReqVO; -import cn.iocoder.yudao.module.system.controller.admin.errorcode.vo.ErrorCodeSaveReqVO; -import cn.iocoder.yudao.module.system.dal.dataobject.errorcode.ErrorCodeDO; -import cn.iocoder.yudao.module.system.dal.mysql.errorcode.ErrorCodeMapper; -import cn.iocoder.yudao.module.system.enums.errorcode.ErrorCodeTypeEnum; -import org.assertj.core.util.Lists; -import org.junit.jupiter.api.Test; -import org.springframework.context.annotation.Import; - -import jakarta.annotation.Resource; -import java.time.LocalDateTime; -import java.util.List; -import java.util.function.Consumer; - -import static cn.hutool.core.util.RandomUtil.randomEle; -import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildBetweenTime; -import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildTime; -import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId; -import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals; -import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException; -import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*; -import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.ERROR_CODE_DUPLICATE; -import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.ERROR_CODE_NOT_EXISTS; -import static org.junit.jupiter.api.Assertions.*; - -@Import(ErrorCodeServiceImpl.class) -public class ErrorCodeServiceTest extends BaseDbUnitTest { - - @Resource - private ErrorCodeServiceImpl errorCodeService; - - @Resource - private ErrorCodeMapper errorCodeMapper; - - @Test - public void testCreateErrorCode_success() { - // 准备参数 - ErrorCodeSaveReqVO reqVO = randomPojo(ErrorCodeSaveReqVO.class) - .setId(null); // 防止 id 被赋值 - - // 调用 - Long errorCodeId = errorCodeService.createErrorCode(reqVO); - // 断言 - assertNotNull(errorCodeId); - // 校验记录的属性是否正确 - ErrorCodeDO errorCode = errorCodeMapper.selectById(errorCodeId); - assertPojoEquals(reqVO, errorCode, "id"); - assertEquals(ErrorCodeTypeEnum.MANUAL_OPERATION.getType(), errorCode.getType()); - } - - @Test - public void testUpdateErrorCode_success() { - // mock 数据 - ErrorCodeDO dbErrorCode = randomErrorCodeDO(); - errorCodeMapper.insert(dbErrorCode);// @Sql: 先插入出一条存在的数据 - // 准备参数 - ErrorCodeSaveReqVO reqVO = randomPojo(ErrorCodeSaveReqVO.class, o -> { - o.setId(dbErrorCode.getId()); // 设置更新的 ID - }); - - // 调用 - errorCodeService.updateErrorCode(reqVO); - // 校验是否更新正确 - ErrorCodeDO errorCode = errorCodeMapper.selectById(reqVO.getId()); // 获取最新的 - assertPojoEquals(reqVO, errorCode); - assertEquals(ErrorCodeTypeEnum.MANUAL_OPERATION.getType(), errorCode.getType()); - } - - @Test - public void testDeleteErrorCode_success() { - // mock 数据 - ErrorCodeDO dbErrorCode = randomErrorCodeDO(); - errorCodeMapper.insert(dbErrorCode);// @Sql: 先插入出一条存在的数据 - // 准备参数 - Long id = dbErrorCode.getId(); - - // 调用 - errorCodeService.deleteErrorCode(id); - // 校验数据不存在了 - assertNull(errorCodeMapper.selectById(id)); - } - - @Test - public void testGetErrorCodePage() { - // mock 数据 - ErrorCodeDO dbErrorCode = initGetErrorCodePage(); - // 准备参数 - ErrorCodePageReqVO reqVO = new ErrorCodePageReqVO(); - reqVO.setType(ErrorCodeTypeEnum.AUTO_GENERATION.getType()); - reqVO.setApplicationName("tu"); - reqVO.setCode(1); - reqVO.setMessage("ma"); - reqVO.setCreateTime(buildBetweenTime(2020, 11, 1, 2020, 11, 30)); - - // 调用 - PageResult pageResult = errorCodeService.getErrorCodePage(reqVO); - // 断言 - assertEquals(1, pageResult.getTotal()); - assertEquals(1, pageResult.getList().size()); - assertPojoEquals(dbErrorCode, pageResult.getList().get(0)); - } - - /** - * 初始化 getErrorCodePage 方法的测试数据 - */ - private ErrorCodeDO initGetErrorCodePage() { - ErrorCodeDO dbErrorCode = randomErrorCodeDO(o -> { // 等会查询到 - o.setType(ErrorCodeTypeEnum.AUTO_GENERATION.getType()); - o.setApplicationName("tudou"); - o.setCode(1); - o.setMessage("yuanma"); - o.setCreateTime(buildTime(2020, 11, 11)); - }); - errorCodeMapper.insert(dbErrorCode); - // 测试 type 不匹配 - errorCodeMapper.insert(cloneIgnoreId(dbErrorCode, o -> o.setType(ErrorCodeTypeEnum.MANUAL_OPERATION.getType()))); - // 测试 applicationName 不匹配 - errorCodeMapper.insert(cloneIgnoreId(dbErrorCode, o -> o.setApplicationName("yuan"))); - // 测试 code 不匹配 - errorCodeMapper.insert(cloneIgnoreId(dbErrorCode, o -> o.setCode(2))); - // 测试 message 不匹配 - errorCodeMapper.insert(cloneIgnoreId(dbErrorCode, o -> o.setMessage("nai"))); - // 测试 createTime 不匹配 - errorCodeMapper.insert(cloneIgnoreId(dbErrorCode, o -> o.setCreateTime(buildTime(2020, 12, 12)))); - return dbErrorCode; - } - - @Test - public void testValidateCodeDuplicate_codeDuplicateForCreate() { - // 准备参数 - Integer code = randomInteger(); - // mock 数据 - errorCodeMapper.insert(randomErrorCodeDO(o -> o.setCode(code))); - - // 调用,校验异常 - assertServiceException(() -> errorCodeService.validateCodeDuplicate(code, null), - ERROR_CODE_DUPLICATE); - } - - @Test - public void testValidateCodeDuplicate_codeDuplicateForUpdate() { - // 准备参数 - Long id = randomLongId(); - Integer code = randomInteger(); - // mock 数据 - errorCodeMapper.insert(randomErrorCodeDO(o -> o.setCode(code))); - - // 调用,校验异常 - assertServiceException(() -> errorCodeService.validateCodeDuplicate(code, id), - ERROR_CODE_DUPLICATE); - } - - @Test - public void testValidateErrorCodeExists_notExists() { - assertServiceException(() -> errorCodeService.validateErrorCodeExists(null), - ERROR_CODE_NOT_EXISTS); - } - - /** - * 情况 1,错误码不存在的情况 - */ - @Test - public void testAutoGenerateErrorCodes_01() { - // 准备参数 - ErrorCodeAutoGenerateReqDTO generateReqDTO = randomPojo(ErrorCodeAutoGenerateReqDTO.class); - // mock 方法 - - // 调用 - errorCodeService.autoGenerateErrorCodes(Lists.newArrayList(generateReqDTO)); - // 断言 - ErrorCodeDO errorCode = errorCodeMapper.selectOne(null); - assertPojoEquals(generateReqDTO, errorCode); - assertEquals(ErrorCodeTypeEnum.AUTO_GENERATION.getType(), errorCode.getType()); - } - - /** - * 情况 2.1,错误码存在,但是是 ErrorCodeTypeEnum.MANUAL_OPERATION 类型 - */ - @Test - public void testAutoGenerateErrorCodes_021() { - // mock 数据 - ErrorCodeDO dbErrorCode = randomErrorCodeDO(o -> o.setType(ErrorCodeTypeEnum.MANUAL_OPERATION.getType())); - errorCodeMapper.insert(dbErrorCode); - // 准备参数 - ErrorCodeAutoGenerateReqDTO generateReqDTO = randomPojo(ErrorCodeAutoGenerateReqDTO.class, - o -> o.setCode(dbErrorCode.getCode())); - // mock 方法 - - // 调用 - errorCodeService.autoGenerateErrorCodes(Lists.newArrayList(generateReqDTO)); - // 断言,相等,说明不会更新 - ErrorCodeDO errorCode = errorCodeMapper.selectById(dbErrorCode.getId()); - assertPojoEquals(dbErrorCode, errorCode); - } - - /** - * 情况 2.2,错误码存在,但是是 applicationName 不匹配 - */ - @Test - public void testAutoGenerateErrorCodes_022() { - // mock 数据 - ErrorCodeDO dbErrorCode = randomErrorCodeDO(o -> o.setType(ErrorCodeTypeEnum.AUTO_GENERATION.getType())); - errorCodeMapper.insert(dbErrorCode); - // 准备参数 - ErrorCodeAutoGenerateReqDTO generateReqDTO = randomPojo(ErrorCodeAutoGenerateReqDTO.class, - o -> o.setCode(dbErrorCode.getCode()).setApplicationName(randomString())); - // mock 方法 - - // 调用 - errorCodeService.autoGenerateErrorCodes(Lists.newArrayList(generateReqDTO)); - // 断言,相等,说明不会更新 - ErrorCodeDO errorCode = errorCodeMapper.selectById(dbErrorCode.getId()); - assertPojoEquals(dbErrorCode, errorCode); - } - - /** - * 情况 2.3,错误码存在,但是是 message 相同 - */ - @Test - public void testAutoGenerateErrorCodes_023() { - // mock 数据 - ErrorCodeDO dbErrorCode = randomErrorCodeDO(o -> o.setType(ErrorCodeTypeEnum.AUTO_GENERATION.getType())); - errorCodeMapper.insert(dbErrorCode); - // 准备参数 - ErrorCodeAutoGenerateReqDTO generateReqDTO = randomPojo(ErrorCodeAutoGenerateReqDTO.class, - o -> o.setCode(dbErrorCode.getCode()).setApplicationName(dbErrorCode.getApplicationName()) - .setMessage(dbErrorCode.getMessage())); - // mock 方法 - - // 调用 - errorCodeService.autoGenerateErrorCodes(Lists.newArrayList(generateReqDTO)); - // 断言,相等,说明不会更新 - ErrorCodeDO errorCode = errorCodeMapper.selectById(dbErrorCode.getId()); - assertPojoEquals(dbErrorCode, errorCode); - } - - /** - * 情况 2.3,错误码存在,但是是 message 不同,则进行更新 - */ - @Test - public void testAutoGenerateErrorCodes_024() { - // mock 数据 - ErrorCodeDO dbErrorCode = randomErrorCodeDO(o -> o.setType(ErrorCodeTypeEnum.AUTO_GENERATION.getType())); - errorCodeMapper.insert(dbErrorCode); - // 准备参数 - ErrorCodeAutoGenerateReqDTO generateReqDTO = randomPojo(ErrorCodeAutoGenerateReqDTO.class, - o -> o.setCode(dbErrorCode.getCode()).setApplicationName(dbErrorCode.getApplicationName())); - // mock 方法 - - // 调用 - errorCodeService.autoGenerateErrorCodes(Lists.newArrayList(generateReqDTO)); - // 断言,匹配 - ErrorCodeDO errorCode = errorCodeMapper.selectById(dbErrorCode.getId()); - assertPojoEquals(generateReqDTO, errorCode); - } - - @Test - public void testGetErrorCode() { - // 准备参数 - ErrorCodeDO errorCodeDO = randomErrorCodeDO(); - errorCodeMapper.insert(errorCodeDO); - // mock 方法 - Long id = errorCodeDO.getId(); - - // 调用 - ErrorCodeDO dbErrorCode = errorCodeService.getErrorCode(id); - // 断言 - assertPojoEquals(errorCodeDO, dbErrorCode); - } - - @Test - public void testGetErrorCodeList() { - // 准备参数 - ErrorCodeDO errorCodeDO01 = randomErrorCodeDO( - o -> o.setApplicationName("yunai_server").setUpdateTime(buildTime(2022, 1, 10))); - errorCodeMapper.insert(errorCodeDO01); - ErrorCodeDO errorCodeDO02 = randomErrorCodeDO( - o -> o.setApplicationName("yunai_server").setUpdateTime(buildTime(2022, 1, 12))); - errorCodeMapper.insert(errorCodeDO02); - // mock 方法 - String applicationName = "yunai_server"; - LocalDateTime minUpdateTime = buildTime(2022, 1, 11); - - // 调用 - List errorCodeList = errorCodeService.getErrorCodeList(applicationName, minUpdateTime); - // 断言 - assertEquals(1, errorCodeList.size()); - assertPojoEquals(errorCodeDO02, errorCodeList.get(0)); - } - - // ========== 随机对象 ========== - - @SafeVarargs - private static ErrorCodeDO randomErrorCodeDO(Consumer... consumers) { - Consumer consumer = (o) -> { - o.setType(randomEle(ErrorCodeTypeEnum.values()).getType()); // 保证 key 的范围 - }; - return randomPojo(ErrorCodeDO.class, ArrayUtils.append(consumer, consumers)); - } - -} diff --git a/yudao-module-system/yudao-module-system-biz/src/test/resources/sql/clean.sql b/yudao-module-system/yudao-module-system-biz/src/test/resources/sql/clean.sql index 55778022e..8b516869f 100644 --- a/yudao-module-system/yudao-module-system-biz/src/test/resources/sql/clean.sql +++ b/yudao-module-system/yudao-module-system-biz/src/test/resources/sql/clean.sql @@ -16,7 +16,6 @@ DELETE FROM "system_sms_channel"; DELETE FROM "system_sms_template"; DELETE FROM "system_sms_log"; DELETE FROM "system_sms_code"; -DELETE FROM "system_error_code"; DELETE FROM "system_social_client"; DELETE FROM "system_social_user"; DELETE FROM "system_social_user_bind"; diff --git a/yudao-module-system/yudao-module-system-biz/src/test/resources/sql/create_tables.sql b/yudao-module-system/yudao-module-system-biz/src/test/resources/sql/create_tables.sql index 731b2850e..9a213ef12 100644 --- a/yudao-module-system/yudao-module-system-biz/src/test/resources/sql/create_tables.sql +++ b/yudao-module-system/yudao-module-system-biz/src/test/resources/sql/create_tables.sql @@ -332,21 +332,6 @@ CREATE TABLE IF NOT EXISTS "system_sms_code" ( PRIMARY KEY ("id") ) COMMENT '短信日志'; -CREATE TABLE IF NOT EXISTS "system_error_code" ( - "id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY, - "type" tinyint NOT NULL DEFAULT '0', - "application_name" varchar(50) NOT NULL, - "code" int NOT NULL DEFAULT '0', - "message" varchar(512) NOT NULL DEFAULT '', - "memo" varchar(512) DEFAULT '', - "creator" varchar(64) DEFAULT '', - "create_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, - "updater" varchar(64) DEFAULT '', - "update_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, - "deleted" bit NOT NULL DEFAULT FALSE, - PRIMARY KEY ("id") -) COMMENT '错误码表'; - CREATE TABLE IF NOT EXISTS "system_social_client" ( "id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY, "name" varchar(255) NOT NULL, diff --git a/yudao-server/src/main/resources/application-local.yaml b/yudao-server/src/main/resources/application-local.yaml index 555030b57..9917ac9e7 100644 --- a/yudao-server/src/main/resources/application-local.yaml +++ b/yudao-server/src/main/resources/application-local.yaml @@ -227,8 +227,6 @@ yudao: refund-notify-url: http://yunai.natapp1.cc/admin-api/pay/notify/refund # 支付渠道的【退款】回调地址 access-log: # 访问日志的配置项 enable: false - error-code: # 错误码相关配置项 - enable: false demo: false # 关闭演示模式 tencent-lbs-key: TVDBZ-TDILD-4ON4B-PFDZA-RNLKH-VVF6E # QQ 地图的密钥 https://lbs.qq.com/service/staticV2/staticGuide/staticDoc diff --git a/yudao-server/src/main/resources/application.yaml b/yudao-server/src/main/resources/application.yaml index d8d6c493d..ae336f429 100644 --- a/yudao-server/src/main/resources/application.yaml +++ b/yudao-server/src/main/resources/application.yaml @@ -181,14 +181,6 @@ yudao: base-package: ${yudao.info.base-package} db-schemas: ${spring.datasource.dynamic.datasource.master.name} front-type: 10 # 前端模版的类型,参见 CodegenFrontTypeEnum 枚举类 - error-code: # 错误码相关配置项 - constants-class-list: - - cn.iocoder.yudao.module.bpm.enums.ErrorCodeConstants - - cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants - - cn.iocoder.yudao.module.member.enums.ErrorCodeConstants - - cn.iocoder.yudao.module.pay.enums.ErrorCodeConstants - - cn.iocoder.yudao.module.system.enums.ErrorCodeConstants - - cn.iocoder.yudao.module.mp.enums.ErrorCodeConstants tenant: # 多租户相关配置项 enable: true ignore-urls: