Commit 8093ef3b968d566c9b42e9442a100932540683a7
1 parent
ff527699
【移除】错误码的管理,简化项目的复杂度
Showing
28 changed files
with
3 additions
and
1430 deletions
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 |
| 6 | 6 | import com.google.common.annotations.VisibleForTesting; |
| 7 | 7 | import lombok.extern.slf4j.Slf4j; |
| 8 | 8 | |
| 9 | -import java.util.Map; | |
| 10 | -import java.util.concurrent.ConcurrentHashMap; | |
| 11 | -import java.util.concurrent.ConcurrentMap; | |
| 12 | - | |
| 13 | 9 | /** |
| 14 | 10 | * {@link ServiceException} 工具类 |
| 15 | 11 | * |
| 16 | 12 | * 目的在于,格式化异常信息提示。 |
| 17 | 13 | * 考虑到 String.format 在参数不正确时会报错,因此使用 {} 作为占位符,并使用 {@link #doFormat(int, String, Object...)} 方法来格式化 |
| 18 | 14 | * |
| 19 | - * 因为 {@link #MESSAGES} 里面默认是没有异常信息提示的模板的,所以需要使用方自己初始化进去。目前想到的有几种方式: | |
| 20 | - * | |
| 21 | - * 1. 异常提示信息,写在枚举类中,例如说,cn.iocoder.oceans.user.api.constants.ErrorCodeEnum 类 + ServiceExceptionConfiguration | |
| 22 | - * 2. 异常提示信息,写在 .properties 等等配置文件 | |
| 23 | - * 3. 异常提示信息,写在 Apollo 等等配置中心中,从而实现可动态刷新 | |
| 24 | - * 4. 异常提示信息,存储在 db 等等数据库中,从而实现可动态刷新 | |
| 25 | 15 | */ |
| 26 | 16 | @Slf4j |
| 27 | 17 | public class ServiceExceptionUtil { |
| 28 | 18 | |
| 29 | - /** | |
| 30 | - * 错误码提示模板 | |
| 31 | - */ | |
| 32 | - private static final ConcurrentMap<Integer, String> MESSAGES = new ConcurrentHashMap<>(); | |
| 33 | - | |
| 34 | - public static void putAll(Map<Integer, String> messages) { | |
| 35 | - ServiceExceptionUtil.MESSAGES.putAll(messages); | |
| 36 | - } | |
| 37 | - | |
| 38 | - public static void put(Integer code, String message) { | |
| 39 | - ServiceExceptionUtil.MESSAGES.put(code, message); | |
| 40 | - } | |
| 41 | - | |
| 42 | - public static void delete(Integer code, String message) { | |
| 43 | - ServiceExceptionUtil.MESSAGES.remove(code, message); | |
| 44 | - } | |
| 45 | - | |
| 46 | 19 | // ========== 和 ServiceException 的集成 ========== |
| 47 | 20 | |
| 48 | 21 | public static ServiceException exception(ErrorCode errorCode) { |
| 49 | - String messagePattern = MESSAGES.getOrDefault(errorCode.getCode(), errorCode.getMsg()); | |
| 50 | - return exception0(errorCode.getCode(), messagePattern); | |
| 22 | + return exception0(errorCode.getCode(), errorCode.getMsg()); | |
| 51 | 23 | } |
| 52 | 24 | |
| 53 | 25 | public static ServiceException exception(ErrorCode errorCode, Object... params) { |
| 54 | - String messagePattern = MESSAGES.getOrDefault(errorCode.getCode(), errorCode.getMsg()); | |
| 55 | - return exception0(errorCode.getCode(), messagePattern, params); | |
| 56 | - } | |
| 57 | - | |
| 58 | - /** | |
| 59 | - * 创建指定编号的 ServiceException 的异常 | |
| 60 | - * | |
| 61 | - * @param code 编号 | |
| 62 | - * @return 异常 | |
| 63 | - */ | |
| 64 | - public static ServiceException exception(Integer code) { | |
| 65 | - return exception0(code, MESSAGES.get(code)); | |
| 66 | - } | |
| 67 | - | |
| 68 | - /** | |
| 69 | - * 创建指定编号的 ServiceException 的异常 | |
| 70 | - * | |
| 71 | - * @param code 编号 | |
| 72 | - * @param params 消息提示的占位符对应的参数 | |
| 73 | - * @return 异常 | |
| 74 | - */ | |
| 75 | - public static ServiceException exception(Integer code, Object... params) { | |
| 76 | - return exception0(code, MESSAGES.get(code), params); | |
| 26 | + return exception0(errorCode.getCode(), errorCode.getMsg(), params); | |
| 77 | 27 | } |
| 78 | 28 | |
| 79 | 29 | public static ServiceException exception0(Integer code, String messagePattern, Object... params) { | ... | ... |
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/errorcode/config/ErrorCodeProperties.java deleted
| 1 | -package cn.iocoder.yudao.framework.errorcode.config; | |
| 2 | - | |
| 3 | -import lombok.Data; | |
| 4 | -import org.springframework.boot.context.properties.ConfigurationProperties; | |
| 5 | -import org.springframework.validation.annotation.Validated; | |
| 6 | - | |
| 7 | -import jakarta.validation.constraints.NotNull; | |
| 8 | -import java.util.List; | |
| 9 | - | |
| 10 | -/** | |
| 11 | - * 错误码的配置属性类 | |
| 12 | - * | |
| 13 | - * @author dlyan | |
| 14 | - */ | |
| 15 | -@ConfigurationProperties("yudao.error-code") | |
| 16 | -@Data | |
| 17 | -@Validated | |
| 18 | -public class ErrorCodeProperties { | |
| 19 | - | |
| 20 | - /** | |
| 21 | - * 是否开启 | |
| 22 | - */ | |
| 23 | - private Boolean enable = true; | |
| 24 | - /** | |
| 25 | - * 错误码枚举类 | |
| 26 | - */ | |
| 27 | - @NotNull(message = "错误码枚举类不能为空") | |
| 28 | - private List<String> constantsClassList; | |
| 29 | - | |
| 30 | -} |
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/errorcode/config/YudaoErrorCodeAutoConfiguration.java deleted
| 1 | -package cn.iocoder.yudao.framework.errorcode.config; | |
| 2 | - | |
| 3 | -import cn.iocoder.yudao.framework.errorcode.core.generator.ErrorCodeAutoGenerator; | |
| 4 | -import cn.iocoder.yudao.framework.errorcode.core.generator.ErrorCodeAutoGeneratorImpl; | |
| 5 | -import cn.iocoder.yudao.framework.errorcode.core.loader.ErrorCodeLoader; | |
| 6 | -import cn.iocoder.yudao.framework.errorcode.core.loader.ErrorCodeLoaderImpl; | |
| 7 | -import cn.iocoder.yudao.module.system.api.errorcode.ErrorCodeApi; | |
| 8 | -import org.springframework.beans.factory.annotation.Value; | |
| 9 | -import org.springframework.boot.autoconfigure.AutoConfiguration; | |
| 10 | -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | |
| 11 | -import org.springframework.boot.context.properties.EnableConfigurationProperties; | |
| 12 | -import org.springframework.context.annotation.Bean; | |
| 13 | -import org.springframework.scheduling.annotation.EnableScheduling; | |
| 14 | - | |
| 15 | -/** | |
| 16 | - * 错误码配置类 | |
| 17 | - * | |
| 18 | - * @author 芋道源码 | |
| 19 | - */ | |
| 20 | -@AutoConfiguration | |
| 21 | -@ConditionalOnProperty(prefix = "yudao.error-code", value = "enable", matchIfMissing = true) // 允许使用 yudao.error-code.enable=false 禁用访问日志 | |
| 22 | -@EnableConfigurationProperties(ErrorCodeProperties.class) | |
| 23 | -@EnableScheduling // 开启调度任务的功能,因为 ErrorCodeRemoteLoader 通过定时刷新错误码 | |
| 24 | -public class YudaoErrorCodeAutoConfiguration { | |
| 25 | - | |
| 26 | - @Bean | |
| 27 | - public ErrorCodeAutoGenerator errorCodeAutoGenerator(@Value("${spring.application.name}") String applicationName, | |
| 28 | - ErrorCodeProperties errorCodeProperties, | |
| 29 | - ErrorCodeApi errorCodeApi) { | |
| 30 | - return new ErrorCodeAutoGeneratorImpl(applicationName, errorCodeProperties.getConstantsClassList(), errorCodeApi); | |
| 31 | - } | |
| 32 | - | |
| 33 | - @Bean | |
| 34 | - public ErrorCodeLoader errorCodeLoader(@Value("${spring.application.name}") String applicationName, | |
| 35 | - ErrorCodeApi errorCodeApi) { | |
| 36 | - return new ErrorCodeLoaderImpl(applicationName, errorCodeApi); | |
| 37 | - } | |
| 38 | - | |
| 39 | -} |
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/errorcode/core/generator/ErrorCodeAutoGenerator.java deleted
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/errorcode/core/generator/ErrorCodeAutoGeneratorImpl.java deleted
| 1 | -package cn.iocoder.yudao.framework.errorcode.core.generator; | |
| 2 | - | |
| 3 | -import cn.hutool.core.collection.CollUtil; | |
| 4 | -import cn.hutool.core.exceptions.ExceptionUtil; | |
| 5 | -import cn.hutool.core.util.ClassUtil; | |
| 6 | -import cn.hutool.core.util.ReflectUtil; | |
| 7 | -import cn.iocoder.yudao.framework.common.exception.ErrorCode; | |
| 8 | -import cn.iocoder.yudao.module.system.api.errorcode.ErrorCodeApi; | |
| 9 | -import cn.iocoder.yudao.module.system.api.errorcode.dto.ErrorCodeAutoGenerateReqDTO; | |
| 10 | -import lombok.RequiredArgsConstructor; | |
| 11 | -import lombok.extern.slf4j.Slf4j; | |
| 12 | -import org.springframework.boot.context.event.ApplicationReadyEvent; | |
| 13 | -import org.springframework.context.event.EventListener; | |
| 14 | -import org.springframework.scheduling.annotation.Async; | |
| 15 | - | |
| 16 | -import java.util.ArrayList; | |
| 17 | -import java.util.Arrays; | |
| 18 | -import java.util.List; | |
| 19 | - | |
| 20 | -/** | |
| 21 | - * ErrorCodeAutoGenerator 的实现类 | |
| 22 | - * 目的是,扫描指定的 {@link #constantsClassList} 类,写入到 system 服务中 | |
| 23 | - * | |
| 24 | - * @author dylan | |
| 25 | - */ | |
| 26 | -@RequiredArgsConstructor | |
| 27 | -@Slf4j | |
| 28 | -public class ErrorCodeAutoGeneratorImpl implements ErrorCodeAutoGenerator { | |
| 29 | - | |
| 30 | - /** | |
| 31 | - * 应用分组 | |
| 32 | - */ | |
| 33 | - private final String applicationName; | |
| 34 | - /** | |
| 35 | - * 错误码枚举类 | |
| 36 | - */ | |
| 37 | - private final List<String> constantsClassList; | |
| 38 | - /** | |
| 39 | - * 错误码 Api | |
| 40 | - */ | |
| 41 | - private final ErrorCodeApi errorCodeApi; | |
| 42 | - | |
| 43 | - @Override | |
| 44 | - @EventListener(ApplicationReadyEvent.class) | |
| 45 | - @Async // 异步,保证项目的启动过程,毕竟非关键流程 | |
| 46 | - public void execute() { | |
| 47 | - // 第一步,解析错误码 | |
| 48 | - List<ErrorCodeAutoGenerateReqDTO> autoGenerateDTOs = parseErrorCode(); | |
| 49 | - log.info("[execute][解析到错误码数量为 ({}) 个]", autoGenerateDTOs.size()); | |
| 50 | - | |
| 51 | - // 第二步,写入到 system 服务 | |
| 52 | - try { | |
| 53 | - errorCodeApi.autoGenerateErrorCodeList(autoGenerateDTOs); | |
| 54 | - log.info("[execute][写入到 system 组件完成]"); | |
| 55 | - } catch (Exception ex) { | |
| 56 | - log.error("[execute][写入到 system 组件失败({})]", ExceptionUtil.getRootCauseMessage(ex)); | |
| 57 | - } | |
| 58 | - } | |
| 59 | - | |
| 60 | - /** | |
| 61 | - * 解析 constantsClassList 变量,转换成错误码数组 | |
| 62 | - * | |
| 63 | - * @return 错误码数组 | |
| 64 | - */ | |
| 65 | - private List<ErrorCodeAutoGenerateReqDTO> parseErrorCode() { | |
| 66 | - // 校验 errorCodeConstantsClass 参数 | |
| 67 | - if (CollUtil.isEmpty(constantsClassList)) { | |
| 68 | - log.info("[execute][未配置 yudao.error-code.constants-class-list 配置项,不进行自动写入到 system 服务中]"); | |
| 69 | - return new ArrayList<>(); | |
| 70 | - } | |
| 71 | - | |
| 72 | - // 解析错误码 | |
| 73 | - List<ErrorCodeAutoGenerateReqDTO> autoGenerateDTOs = new ArrayList<>(); | |
| 74 | - constantsClassList.forEach(constantsClass -> { | |
| 75 | - try { | |
| 76 | - // 解析错误码枚举类 | |
| 77 | - Class<?> errorCodeConstantsClazz = ClassUtil.loadClass(constantsClass); | |
| 78 | - // 解析错误码 | |
| 79 | - autoGenerateDTOs.addAll(parseErrorCode(errorCodeConstantsClazz)); | |
| 80 | - } catch (Exception ex) { | |
| 81 | - log.warn("[parseErrorCode][constantsClass({}) 加载失败({})]", constantsClass, | |
| 82 | - ExceptionUtil.getRootCauseMessage(ex)); | |
| 83 | - } | |
| 84 | - }); | |
| 85 | - return autoGenerateDTOs; | |
| 86 | - } | |
| 87 | - | |
| 88 | - /** | |
| 89 | - * 解析错误码类,获得错误码数组 | |
| 90 | - * | |
| 91 | - * @return 错误码数组 | |
| 92 | - */ | |
| 93 | - private List<ErrorCodeAutoGenerateReqDTO> parseErrorCode(Class<?> constantsClass) { | |
| 94 | - List<ErrorCodeAutoGenerateReqDTO> autoGenerateDTOs = new ArrayList<>(); | |
| 95 | - Arrays.stream(constantsClass.getFields()).forEach(field -> { | |
| 96 | - if (field.getType() != ErrorCode.class) { | |
| 97 | - return; | |
| 98 | - } | |
| 99 | - // 转换成 ErrorCodeAutoGenerateReqDTO 对象 | |
| 100 | - ErrorCode errorCode = (ErrorCode) ReflectUtil.getFieldValue(constantsClass, field); | |
| 101 | - autoGenerateDTOs.add(new ErrorCodeAutoGenerateReqDTO().setApplicationName(applicationName) | |
| 102 | - .setCode(errorCode.getCode()).setMessage(errorCode.getMsg())); | |
| 103 | - }); | |
| 104 | - return autoGenerateDTOs; | |
| 105 | - } | |
| 106 | - | |
| 107 | -} | |
| 108 | - |
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/errorcode/core/loader/ErrorCodeLoader.java deleted
| 1 | -package cn.iocoder.yudao.framework.errorcode.core.loader; | |
| 2 | - | |
| 3 | -import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil; | |
| 4 | - | |
| 5 | -/** | |
| 6 | - * 错误码加载器 | |
| 7 | - * | |
| 8 | - * 注意,错误码最终加载到 {@link ServiceExceptionUtil} 的 MESSAGES 变量中! | |
| 9 | - * | |
| 10 | - * @author dlyan | |
| 11 | - */ | |
| 12 | -public interface ErrorCodeLoader { | |
| 13 | - | |
| 14 | - /** | |
| 15 | - * 添加错误码 | |
| 16 | - * | |
| 17 | - * @param code 错误码的编号 | |
| 18 | - * @param msg 错误码的提示 | |
| 19 | - */ | |
| 20 | - default void putErrorCode(Integer code, String msg) { | |
| 21 | - ServiceExceptionUtil.put(code, msg); | |
| 22 | - } | |
| 23 | - | |
| 24 | - /** | |
| 25 | - * 刷新错误码 | |
| 26 | - */ | |
| 27 | - void refreshErrorCodes(); | |
| 28 | - | |
| 29 | - /** | |
| 30 | - * 加载错误码 | |
| 31 | - */ | |
| 32 | - void loadErrorCodes(); | |
| 33 | - | |
| 34 | -} |
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/errorcode/core/loader/ErrorCodeLoaderImpl.java deleted
| 1 | -package cn.iocoder.yudao.framework.errorcode.core.loader; | |
| 2 | - | |
| 3 | -import cn.hutool.core.collection.CollUtil; | |
| 4 | -import cn.hutool.core.exceptions.ExceptionUtil; | |
| 5 | -import cn.iocoder.yudao.framework.common.util.date.DateUtils; | |
| 6 | -import cn.iocoder.yudao.module.system.api.errorcode.ErrorCodeApi; | |
| 7 | -import cn.iocoder.yudao.module.system.api.errorcode.dto.ErrorCodeRespDTO; | |
| 8 | -import lombok.RequiredArgsConstructor; | |
| 9 | -import lombok.extern.slf4j.Slf4j; | |
| 10 | -import org.springframework.boot.context.event.ApplicationReadyEvent; | |
| 11 | -import org.springframework.context.event.EventListener; | |
| 12 | -import org.springframework.scheduling.annotation.Async; | |
| 13 | -import org.springframework.scheduling.annotation.Scheduled; | |
| 14 | - | |
| 15 | -import java.time.LocalDateTime; | |
| 16 | -import java.util.List; | |
| 17 | - | |
| 18 | -/** | |
| 19 | - * ErrorCodeLoader 的实现类,从 infra 的数据库中,加载错误码。 | |
| 20 | - * | |
| 21 | - * 考虑到错误码会刷新,所以按照 {@link #REFRESH_ERROR_CODE_PERIOD} 频率,增量加载错误码。 | |
| 22 | - * | |
| 23 | - * @author dlyan | |
| 24 | - */ | |
| 25 | -@RequiredArgsConstructor | |
| 26 | -@Slf4j | |
| 27 | -public class ErrorCodeLoaderImpl implements ErrorCodeLoader { | |
| 28 | - | |
| 29 | - /** | |
| 30 | - * 刷新错误码的频率,单位:毫秒 | |
| 31 | - */ | |
| 32 | - private static final int REFRESH_ERROR_CODE_PERIOD = 60 * 1000; | |
| 33 | - | |
| 34 | - /** | |
| 35 | - * 应用分组 | |
| 36 | - */ | |
| 37 | - private final String applicationName; | |
| 38 | - /** | |
| 39 | - * 错误码 Api | |
| 40 | - */ | |
| 41 | - private final ErrorCodeApi errorCodeApi; | |
| 42 | - | |
| 43 | - /** | |
| 44 | - * 缓存错误码的最大更新时间,用于后续的增量轮询,判断是否有更新 | |
| 45 | - */ | |
| 46 | - private LocalDateTime maxUpdateTime; | |
| 47 | - | |
| 48 | - @Override | |
| 49 | - @EventListener(ApplicationReadyEvent.class) | |
| 50 | - @Async // 异步,保证项目的启动过程,毕竟非关键流程 | |
| 51 | - public void loadErrorCodes() { | |
| 52 | - loadErrorCodes0(); | |
| 53 | - } | |
| 54 | - | |
| 55 | - @Override | |
| 56 | - @Scheduled(fixedDelay = REFRESH_ERROR_CODE_PERIOD, initialDelay = REFRESH_ERROR_CODE_PERIOD) | |
| 57 | - public void refreshErrorCodes() { | |
| 58 | - loadErrorCodes0(); | |
| 59 | - } | |
| 60 | - | |
| 61 | - private void loadErrorCodes0() { | |
| 62 | - try { | |
| 63 | - // 加载错误码 | |
| 64 | - List<ErrorCodeRespDTO> errorCodeRespDTOs = errorCodeApi.getErrorCodeList(applicationName, maxUpdateTime); | |
| 65 | - if (CollUtil.isEmpty(errorCodeRespDTOs)) { | |
| 66 | - return; | |
| 67 | - } | |
| 68 | - log.info("[loadErrorCodes0][加载到 ({}) 个错误码]", errorCodeRespDTOs.size()); | |
| 69 | - | |
| 70 | - // 刷新错误码的缓存 | |
| 71 | - errorCodeRespDTOs.forEach(errorCodeRespDTO -> { | |
| 72 | - // 写入到错误码的缓存 | |
| 73 | - putErrorCode(errorCodeRespDTO.getCode(), errorCodeRespDTO.getMessage()); | |
| 74 | - // 记录下更新时间,方便增量更新 | |
| 75 | - maxUpdateTime = DateUtils.max(maxUpdateTime, errorCodeRespDTO.getUpdateTime()); | |
| 76 | - }); | |
| 77 | - } catch (Exception ex) { | |
| 78 | - log.error("[loadErrorCodes0][加载错误码失败({})]", ExceptionUtil.getRootCauseMessage(ex)); | |
| 79 | - } | |
| 80 | - } | |
| 81 | - | |
| 82 | -} |
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/errorcode/package-info.java deleted
| 1 | -/** | |
| 2 | - * 错误码 ErrorCode 的自动配置功能,提供如下功能: | |
| 3 | - * | |
| 4 | - * 1. 远程读取:项目启动时,从 system-service 服务,读取数据库中的 ErrorCode 错误码,实现错误码的提水可配置; | |
| 5 | - * 2. 自动更新:管理员在管理后台修数据库中的 ErrorCode 错误码时,项目自动从 system-service 服务加载最新的 ErrorCode 错误码; | |
| 6 | - * 3. 自动写入:项目启动时,将项目本地的错误码写到 system-server 服务中,方便管理员在管理后台编辑; | |
| 7 | - * | |
| 8 | - * @author 芋道源码 | |
| 9 | - */ | |
| 10 | -package cn.iocoder.yudao.framework.errorcode; |
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 |
| 3 | 3 | cn.iocoder.yudao.framework.swagger.config.YudaoSwaggerAutoConfiguration |
| 4 | 4 | cn.iocoder.yudao.framework.web.config.YudaoWebAutoConfiguration |
| 5 | 5 | cn.iocoder.yudao.framework.xss.config.YudaoXssAutoConfiguration |
| 6 | -cn.iocoder.yudao.framework.banner.config.YudaoBannerAutoConfiguration | |
| 7 | -cn.iocoder.yudao.framework.errorcode.config.YudaoErrorCodeAutoConfiguration | |
| 8 | 6 | \ No newline at end of file |
| 7 | +cn.iocoder.yudao.framework.banner.config.YudaoBannerAutoConfiguration | |
| 9 | 8 | \ No newline at end of file | ... | ... |
yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/api/errorcode/ErrorCodeApi.java deleted
| 1 | -package cn.iocoder.yudao.module.system.api.errorcode; | |
| 2 | - | |
| 3 | -import cn.iocoder.yudao.module.system.api.errorcode.dto.ErrorCodeAutoGenerateReqDTO; | |
| 4 | -import cn.iocoder.yudao.module.system.api.errorcode.dto.ErrorCodeRespDTO; | |
| 5 | - | |
| 6 | -import jakarta.validation.Valid; | |
| 7 | -import java.time.LocalDateTime; | |
| 8 | -import java.util.List; | |
| 9 | - | |
| 10 | -/** | |
| 11 | - * 错误码 Api 接口 | |
| 12 | - * | |
| 13 | - * @author 芋道源码 | |
| 14 | - */ | |
| 15 | -public interface ErrorCodeApi { | |
| 16 | - | |
| 17 | - /** | |
| 18 | - * 自动创建错误码 | |
| 19 | - * | |
| 20 | - * @param autoGenerateDTOs 错误码信息 | |
| 21 | - */ | |
| 22 | - void autoGenerateErrorCodeList(@Valid List<ErrorCodeAutoGenerateReqDTO> autoGenerateDTOs); | |
| 23 | - | |
| 24 | - /** | |
| 25 | - * 增量获得错误码数组 | |
| 26 | - * | |
| 27 | - * 如果 minUpdateTime 为空时,则获取所有错误码 | |
| 28 | - * | |
| 29 | - * @param applicationName 应用名 | |
| 30 | - * @param minUpdateTime 最小更新时间 | |
| 31 | - * @return 错误码数组 | |
| 32 | - */ | |
| 33 | - List<ErrorCodeRespDTO> getErrorCodeList(String applicationName, LocalDateTime minUpdateTime); | |
| 34 | - | |
| 35 | -} |
yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/api/errorcode/dto/ErrorCodeAutoGenerateReqDTO.java deleted
| 1 | -package cn.iocoder.yudao.module.system.api.errorcode.dto; | |
| 2 | - | |
| 3 | -import lombok.Data; | |
| 4 | -import lombok.experimental.Accessors; | |
| 5 | - | |
| 6 | -import jakarta.validation.constraints.NotEmpty; | |
| 7 | -import jakarta.validation.constraints.NotNull; | |
| 8 | - | |
| 9 | -/** | |
| 10 | - * 错误码自动生成 DTO | |
| 11 | - * | |
| 12 | - * @author dylan | |
| 13 | - */ | |
| 14 | -@Data | |
| 15 | -@Accessors(chain = true) | |
| 16 | -public class ErrorCodeAutoGenerateReqDTO { | |
| 17 | - | |
| 18 | - /** | |
| 19 | - * 应用名 | |
| 20 | - */ | |
| 21 | - @NotNull(message = "应用名不能为空") | |
| 22 | - private String applicationName; | |
| 23 | - /** | |
| 24 | - * 错误码编码 | |
| 25 | - */ | |
| 26 | - @NotNull(message = "错误码编码不能为空") | |
| 27 | - private Integer code; | |
| 28 | - /** | |
| 29 | - * 错误码错误提示 | |
| 30 | - */ | |
| 31 | - @NotEmpty(message = "错误码错误提示不能为空") | |
| 32 | - private String message; | |
| 33 | - | |
| 34 | -} |
yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/api/errorcode/dto/ErrorCodeRespDTO.java deleted
| 1 | -package cn.iocoder.yudao.module.system.api.errorcode.dto; | |
| 2 | - | |
| 3 | -import lombok.Data; | |
| 4 | - | |
| 5 | -import java.time.LocalDateTime; | |
| 6 | - | |
| 7 | -/** | |
| 8 | - * 错误码的 Response DTO | |
| 9 | - * | |
| 10 | - * @author 芋道源码 | |
| 11 | - */ | |
| 12 | -@Data | |
| 13 | -public class ErrorCodeRespDTO { | |
| 14 | - | |
| 15 | - /** | |
| 16 | - * 错误码编码 | |
| 17 | - */ | |
| 18 | - private Integer code; | |
| 19 | - /** | |
| 20 | - * 错误码错误提示 | |
| 21 | - */ | |
| 22 | - private String message; | |
| 23 | - /** | |
| 24 | - * 更新时间 | |
| 25 | - */ | |
| 26 | - private LocalDateTime updateTime; | |
| 27 | - | |
| 28 | -} |
yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/enums/errorcode/ErrorCodeTypeEnum.java deleted
| 1 | -package cn.iocoder.yudao.module.system.enums.errorcode; | |
| 2 | - | |
| 3 | -import cn.iocoder.yudao.framework.common.core.IntArrayValuable; | |
| 4 | -import lombok.AllArgsConstructor; | |
| 5 | -import lombok.Getter; | |
| 6 | - | |
| 7 | -import java.util.Arrays; | |
| 8 | - | |
| 9 | -/** | |
| 10 | - * 错误码的类型枚举 | |
| 11 | - * | |
| 12 | - * @author dylan | |
| 13 | - */ | |
| 14 | -@AllArgsConstructor | |
| 15 | -@Getter | |
| 16 | -public enum ErrorCodeTypeEnum implements IntArrayValuable { | |
| 17 | - | |
| 18 | - /** | |
| 19 | - * 自动生成 | |
| 20 | - */ | |
| 21 | - AUTO_GENERATION(1), | |
| 22 | - /** | |
| 23 | - * 手动编辑 | |
| 24 | - */ | |
| 25 | - MANUAL_OPERATION(2); | |
| 26 | - | |
| 27 | - public static final int[] ARRAYS = Arrays.stream(values()).mapToInt(ErrorCodeTypeEnum::getType).toArray(); | |
| 28 | - | |
| 29 | - /** | |
| 30 | - * 类型 | |
| 31 | - */ | |
| 32 | - private final Integer type; | |
| 33 | - | |
| 34 | - @Override | |
| 35 | - public int[] array() { | |
| 36 | - return ARRAYS; | |
| 37 | - } | |
| 38 | - | |
| 39 | -} |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/api/errorcode/ErrorCodeApiImpl.java deleted
| 1 | -package cn.iocoder.yudao.module.system.api.errorcode; | |
| 2 | - | |
| 3 | -import cn.iocoder.yudao.module.system.api.errorcode.dto.ErrorCodeAutoGenerateReqDTO; | |
| 4 | -import cn.iocoder.yudao.module.system.api.errorcode.dto.ErrorCodeRespDTO; | |
| 5 | -import cn.iocoder.yudao.module.system.service.errorcode.ErrorCodeService; | |
| 6 | -import org.springframework.stereotype.Service; | |
| 7 | - | |
| 8 | -import jakarta.annotation.Resource; | |
| 9 | -import java.time.LocalDateTime; | |
| 10 | -import java.util.List; | |
| 11 | - | |
| 12 | -/** | |
| 13 | - * 错误码 Api 实现类 | |
| 14 | - * | |
| 15 | - * @author 芋道源码 | |
| 16 | - */ | |
| 17 | -@Service | |
| 18 | -public class ErrorCodeApiImpl implements ErrorCodeApi { | |
| 19 | - | |
| 20 | - @Resource | |
| 21 | - private ErrorCodeService errorCodeService; | |
| 22 | - | |
| 23 | - @Override | |
| 24 | - public void autoGenerateErrorCodeList(List<ErrorCodeAutoGenerateReqDTO> autoGenerateDTOs) { | |
| 25 | - errorCodeService.autoGenerateErrorCodes(autoGenerateDTOs); | |
| 26 | - } | |
| 27 | - | |
| 28 | - @Override | |
| 29 | - public List<ErrorCodeRespDTO> getErrorCodeList(String applicationName, LocalDateTime minUpdateTime) { | |
| 30 | - return errorCodeService.getErrorCodeList(applicationName, minUpdateTime); | |
| 31 | - } | |
| 32 | - | |
| 33 | -} |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/errorcode/ErrorCodeController.http deleted
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/errorcode/ErrorCodeController.java deleted
| 1 | -package cn.iocoder.yudao.module.system.controller.admin.errorcode; | |
| 2 | - | |
| 3 | -import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog; | |
| 4 | -import cn.iocoder.yudao.framework.common.pojo.CommonResult; | |
| 5 | -import cn.iocoder.yudao.framework.common.pojo.PageParam; | |
| 6 | -import cn.iocoder.yudao.framework.common.pojo.PageResult; | |
| 7 | -import cn.iocoder.yudao.framework.common.util.object.BeanUtils; | |
| 8 | -import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils; | |
| 9 | -import cn.iocoder.yudao.module.system.controller.admin.errorcode.vo.ErrorCodePageReqVO; | |
| 10 | -import cn.iocoder.yudao.module.system.controller.admin.errorcode.vo.ErrorCodeRespVO; | |
| 11 | -import cn.iocoder.yudao.module.system.controller.admin.errorcode.vo.ErrorCodeSaveReqVO; | |
| 12 | -import cn.iocoder.yudao.module.system.dal.dataobject.errorcode.ErrorCodeDO; | |
| 13 | -import cn.iocoder.yudao.module.system.service.errorcode.ErrorCodeService; | |
| 14 | -import io.swagger.v3.oas.annotations.Operation; | |
| 15 | -import io.swagger.v3.oas.annotations.Parameter; | |
| 16 | -import io.swagger.v3.oas.annotations.tags.Tag; | |
| 17 | -import jakarta.annotation.Resource; | |
| 18 | -import jakarta.servlet.http.HttpServletResponse; | |
| 19 | -import jakarta.validation.Valid; | |
| 20 | -import org.springframework.security.access.prepost.PreAuthorize; | |
| 21 | -import org.springframework.validation.annotation.Validated; | |
| 22 | -import org.springframework.web.bind.annotation.*; | |
| 23 | - | |
| 24 | -import java.io.IOException; | |
| 25 | -import java.util.List; | |
| 26 | - | |
| 27 | -import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT; | |
| 28 | -import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; | |
| 29 | - | |
| 30 | -@Tag(name = "管理后台 - 错误码") | |
| 31 | -@RestController | |
| 32 | -@RequestMapping("/system/error-code") | |
| 33 | -@Validated | |
| 34 | -public class ErrorCodeController { | |
| 35 | - | |
| 36 | - @Resource | |
| 37 | - private ErrorCodeService errorCodeService; | |
| 38 | - | |
| 39 | - @PostMapping("/create") | |
| 40 | - @Operation(summary = "创建错误码") | |
| 41 | - @PreAuthorize("@ss.hasPermission('system:error-code:create')") | |
| 42 | - public CommonResult<Long> createErrorCode(@Valid @RequestBody ErrorCodeSaveReqVO createReqVO) { | |
| 43 | - return success(errorCodeService.createErrorCode(createReqVO)); | |
| 44 | - } | |
| 45 | - | |
| 46 | - @PutMapping("/update") | |
| 47 | - @Operation(summary = "更新错误码") | |
| 48 | - @PreAuthorize("@ss.hasPermission('system:error-code:update')") | |
| 49 | - public CommonResult<Boolean> updateErrorCode(@Valid @RequestBody ErrorCodeSaveReqVO updateReqVO) { | |
| 50 | - errorCodeService.updateErrorCode(updateReqVO); | |
| 51 | - return success(true); | |
| 52 | - } | |
| 53 | - | |
| 54 | - @DeleteMapping("/delete") | |
| 55 | - @Operation(summary = "删除错误码") | |
| 56 | - @Parameter(name = "id", description = "编号", required = true) | |
| 57 | - @PreAuthorize("@ss.hasPermission('system:error-code:delete')") | |
| 58 | - public CommonResult<Boolean> deleteErrorCode(@RequestParam("id") Long id) { | |
| 59 | - errorCodeService.deleteErrorCode(id); | |
| 60 | - return success(true); | |
| 61 | - } | |
| 62 | - | |
| 63 | - @GetMapping("/get") | |
| 64 | - @Operation(summary = "获得错误码") | |
| 65 | - @Parameter(name = "id", description = "编号", required = true, example = "1024") | |
| 66 | - @PreAuthorize("@ss.hasPermission('system:error-code:query')") | |
| 67 | - public CommonResult<ErrorCodeRespVO> getErrorCode(@RequestParam("id") Long id) { | |
| 68 | - ErrorCodeDO errorCode = errorCodeService.getErrorCode(id); | |
| 69 | - return success(BeanUtils.toBean(errorCode, ErrorCodeRespVO.class)); | |
| 70 | - } | |
| 71 | - | |
| 72 | - @GetMapping("/page") | |
| 73 | - @Operation(summary = "获得错误码分页") | |
| 74 | - @PreAuthorize("@ss.hasPermission('system:error-code:query')") | |
| 75 | - public CommonResult<PageResult<ErrorCodeRespVO>> getErrorCodePage(@Valid ErrorCodePageReqVO pageVO) { | |
| 76 | - PageResult<ErrorCodeDO> pageResult = errorCodeService.getErrorCodePage(pageVO); | |
| 77 | - return success(BeanUtils.toBean(pageResult, ErrorCodeRespVO.class)); | |
| 78 | - } | |
| 79 | - | |
| 80 | - @GetMapping("/export-excel") | |
| 81 | - @Operation(summary = "导出错误码 Excel") | |
| 82 | - @PreAuthorize("@ss.hasPermission('system:error-code:export')") | |
| 83 | - @ApiAccessLog(operateType = EXPORT) | |
| 84 | - public void exportErrorCodeExcel(@Valid ErrorCodePageReqVO exportReqVO, | |
| 85 | - HttpServletResponse response) throws IOException { | |
| 86 | - exportReqVO.setPageSize(PageParam.PAGE_SIZE_NONE); | |
| 87 | - List<ErrorCodeDO> list = errorCodeService.getErrorCodePage(exportReqVO).getList(); | |
| 88 | - // 导出 Excel | |
| 89 | - ExcelUtils.write(response, "错误码.xls", "数据", ErrorCodeRespVO.class, | |
| 90 | - BeanUtils.toBean(list, ErrorCodeRespVO.class)); | |
| 91 | - } | |
| 92 | - | |
| 93 | -} |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/errorcode/vo/ErrorCodePageReqVO.java deleted
| 1 | -package cn.iocoder.yudao.module.system.controller.admin.errorcode.vo; | |
| 2 | - | |
| 3 | -import cn.iocoder.yudao.framework.common.pojo.PageParam; | |
| 4 | -import io.swagger.v3.oas.annotations.media.Schema; | |
| 5 | -import lombok.Data; | |
| 6 | -import lombok.EqualsAndHashCode; | |
| 7 | -import lombok.ToString; | |
| 8 | -import org.springframework.format.annotation.DateTimeFormat; | |
| 9 | - | |
| 10 | -import java.time.LocalDateTime; | |
| 11 | - | |
| 12 | -import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND; | |
| 13 | - | |
| 14 | -@Schema(description = "管理后台 - 错误码分页 Request VO") | |
| 15 | -@Data | |
| 16 | -@EqualsAndHashCode(callSuper = true) | |
| 17 | -@ToString(callSuper = true) | |
| 18 | -public class ErrorCodePageReqVO extends PageParam { | |
| 19 | - | |
| 20 | - @Schema(description = "错误码类型,参见 ErrorCodeTypeEnum 枚举类", example = "1") | |
| 21 | - private Integer type; | |
| 22 | - | |
| 23 | - @Schema(description = "应用名", example = "dashboard") | |
| 24 | - private String applicationName; | |
| 25 | - | |
| 26 | - @Schema(description = "错误码编码", example = "1234") | |
| 27 | - private Integer code; | |
| 28 | - | |
| 29 | - @Schema(description = "错误码错误提示", example = "帅气") | |
| 30 | - private String message; | |
| 31 | - | |
| 32 | - @DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND) | |
| 33 | - @Schema(description = "创建时间") | |
| 34 | - private LocalDateTime[] createTime; | |
| 35 | - | |
| 36 | -} |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/errorcode/vo/ErrorCodeRespVO.java deleted
| 1 | -package cn.iocoder.yudao.module.system.controller.admin.errorcode.vo; | |
| 2 | - | |
| 3 | -import cn.iocoder.yudao.framework.excel.core.annotations.DictFormat; | |
| 4 | -import cn.iocoder.yudao.framework.excel.core.convert.DictConvert; | |
| 5 | -import cn.iocoder.yudao.module.system.enums.DictTypeConstants; | |
| 6 | -import com.alibaba.excel.annotation.ExcelIgnoreUnannotated; | |
| 7 | -import com.alibaba.excel.annotation.ExcelProperty; | |
| 8 | -import io.swagger.v3.oas.annotations.media.Schema; | |
| 9 | -import lombok.Data; | |
| 10 | - | |
| 11 | -import java.time.LocalDateTime; | |
| 12 | - | |
| 13 | -@Schema(description = "管理后台 - 错误码 Response VO") | |
| 14 | -@Data | |
| 15 | -@ExcelIgnoreUnannotated | |
| 16 | -public class ErrorCodeRespVO { | |
| 17 | - | |
| 18 | - @Schema(description = "错误码编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1024") | |
| 19 | - @ExcelProperty("错误码编号") | |
| 20 | - private Long id; | |
| 21 | - | |
| 22 | - @Schema(description = "错误码类型,参见 ErrorCodeTypeEnum 枚举类", requiredMode = Schema.RequiredMode.REQUIRED, example = "1") | |
| 23 | - @ExcelProperty(value = "错误码类型", converter = DictConvert.class) | |
| 24 | - @DictFormat(DictTypeConstants.ERROR_CODE_TYPE) | |
| 25 | - private Integer type; | |
| 26 | - | |
| 27 | - @Schema(description = "应用名", requiredMode = Schema.RequiredMode.REQUIRED, example = "dashboard") | |
| 28 | - @ExcelProperty("应用名") | |
| 29 | - private String applicationName; | |
| 30 | - | |
| 31 | - @Schema(description = "错误码编码", requiredMode = Schema.RequiredMode.REQUIRED, example = "1234") | |
| 32 | - @ExcelProperty("错误码编码") | |
| 33 | - private Integer code; | |
| 34 | - | |
| 35 | - @Schema(description = "错误码错误提示", requiredMode = Schema.RequiredMode.REQUIRED, example = "帅气") | |
| 36 | - @ExcelProperty("错误码错误提示") | |
| 37 | - private String message; | |
| 38 | - | |
| 39 | - @Schema(description = "备注", example = "哈哈哈") | |
| 40 | - @ExcelProperty("备注") | |
| 41 | - private String memo; | |
| 42 | - | |
| 43 | - @Schema(description = "创建时间", requiredMode = Schema.RequiredMode.REQUIRED) | |
| 44 | - @ExcelProperty("创建时间") | |
| 45 | - private LocalDateTime createTime; | |
| 46 | - | |
| 47 | -} |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/errorcode/vo/ErrorCodeSaveReqVO.java deleted
| 1 | -package cn.iocoder.yudao.module.system.controller.admin.errorcode.vo; | |
| 2 | - | |
| 3 | -import io.swagger.v3.oas.annotations.media.Schema; | |
| 4 | -import lombok.Data; | |
| 5 | - | |
| 6 | -import jakarta.validation.constraints.NotNull; | |
| 7 | - | |
| 8 | -@Schema(description = "管理后台 - 错误码创建/修改 Request VO") | |
| 9 | -@Data | |
| 10 | -public class ErrorCodeSaveReqVO { | |
| 11 | - | |
| 12 | - @Schema(description = "错误码编号", example = "1024") | |
| 13 | - private Long id; | |
| 14 | - | |
| 15 | - @Schema(description = "应用名", requiredMode = Schema.RequiredMode.REQUIRED, example = "dashboard") | |
| 16 | - @NotNull(message = "应用名不能为空") | |
| 17 | - private String applicationName; | |
| 18 | - | |
| 19 | - @Schema(description = "错误码编码", requiredMode = Schema.RequiredMode.REQUIRED, example = "1234") | |
| 20 | - @NotNull(message = "错误码编码不能为空") | |
| 21 | - private Integer code; | |
| 22 | - | |
| 23 | - @Schema(description = "错误码错误提示", requiredMode = Schema.RequiredMode.REQUIRED, example = "帅气") | |
| 24 | - @NotNull(message = "错误码错误提示不能为空") | |
| 25 | - private String message; | |
| 26 | - | |
| 27 | - @Schema(description = "备注", example = "哈哈哈") | |
| 28 | - private String memo; | |
| 29 | - | |
| 30 | -} |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/dataobject/errorcode/ErrorCodeDO.java deleted
| 1 | -package cn.iocoder.yudao.module.system.dal.dataobject.errorcode; | |
| 2 | - | |
| 3 | -import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO; | |
| 4 | -import cn.iocoder.yudao.module.system.enums.errorcode.ErrorCodeTypeEnum; | |
| 5 | -import com.baomidou.mybatisplus.annotation.KeySequence; | |
| 6 | -import com.baomidou.mybatisplus.annotation.TableId; | |
| 7 | -import com.baomidou.mybatisplus.annotation.TableName; | |
| 8 | -import lombok.Data; | |
| 9 | -import lombok.EqualsAndHashCode; | |
| 10 | -import lombok.ToString; | |
| 11 | - | |
| 12 | -/** | |
| 13 | - * 错误码表 | |
| 14 | - * | |
| 15 | - * @author 芋道源码 | |
| 16 | - */ | |
| 17 | -@TableName(value = "system_error_code") | |
| 18 | -@KeySequence("system_error_code_seq") // 用于 Oracle、PostgreSQL、Kingbase、DB2、H2 数据库的主键自增。如果是 MySQL 等数据库,可不写。 | |
| 19 | -@Data | |
| 20 | -@EqualsAndHashCode(callSuper = true) | |
| 21 | -@ToString(callSuper = true) | |
| 22 | -public class ErrorCodeDO extends BaseDO { | |
| 23 | - | |
| 24 | - /** | |
| 25 | - * 错误码编号,自增 | |
| 26 | - */ | |
| 27 | - @TableId | |
| 28 | - private Long id; | |
| 29 | - /** | |
| 30 | - * 错误码类型 | |
| 31 | - * | |
| 32 | - * 枚举 {@link ErrorCodeTypeEnum} | |
| 33 | - */ | |
| 34 | - private Integer type; | |
| 35 | - /** | |
| 36 | - * 应用名 | |
| 37 | - */ | |
| 38 | - private String applicationName; | |
| 39 | - /** | |
| 40 | - * 错误码编码 | |
| 41 | - */ | |
| 42 | - private Integer code; | |
| 43 | - /** | |
| 44 | - * 错误码错误提示 | |
| 45 | - */ | |
| 46 | - private String message; | |
| 47 | - /** | |
| 48 | - * 错误码备注 | |
| 49 | - */ | |
| 50 | - private String memo; | |
| 51 | - | |
| 52 | -} |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/errorcode/ErrorCodeMapper.java deleted
| 1 | -package cn.iocoder.yudao.module.system.dal.mysql.errorcode; | |
| 2 | - | |
| 3 | -import cn.iocoder.yudao.framework.common.pojo.PageResult; | |
| 4 | -import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; | |
| 5 | -import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; | |
| 6 | -import cn.iocoder.yudao.module.system.controller.admin.errorcode.vo.ErrorCodePageReqVO; | |
| 7 | -import cn.iocoder.yudao.module.system.dal.dataobject.errorcode.ErrorCodeDO; | |
| 8 | -import org.apache.ibatis.annotations.Mapper; | |
| 9 | - | |
| 10 | -import java.time.LocalDateTime; | |
| 11 | -import java.util.Collection; | |
| 12 | -import java.util.List; | |
| 13 | - | |
| 14 | -@Mapper | |
| 15 | -public interface ErrorCodeMapper extends BaseMapperX<ErrorCodeDO> { | |
| 16 | - | |
| 17 | - default PageResult<ErrorCodeDO> selectPage(ErrorCodePageReqVO reqVO) { | |
| 18 | - return selectPage(reqVO, new LambdaQueryWrapperX<ErrorCodeDO>() | |
| 19 | - .eqIfPresent(ErrorCodeDO::getType, reqVO.getType()) | |
| 20 | - .likeIfPresent(ErrorCodeDO::getApplicationName, reqVO.getApplicationName()) | |
| 21 | - .eqIfPresent(ErrorCodeDO::getCode, reqVO.getCode()) | |
| 22 | - .likeIfPresent(ErrorCodeDO::getMessage, reqVO.getMessage()) | |
| 23 | - .betweenIfPresent(ErrorCodeDO::getCreateTime, reqVO.getCreateTime()) | |
| 24 | - .orderByDesc(ErrorCodeDO::getCode)); | |
| 25 | - } | |
| 26 | - | |
| 27 | - default List<ErrorCodeDO> selectListByCodes(Collection<Integer> codes) { | |
| 28 | - return selectList(ErrorCodeDO::getCode, codes); | |
| 29 | - } | |
| 30 | - | |
| 31 | - default ErrorCodeDO selectByCode(Integer code) { | |
| 32 | - return selectOne(ErrorCodeDO::getCode, code); | |
| 33 | - } | |
| 34 | - | |
| 35 | - default List<ErrorCodeDO> selectListByApplicationNameAndUpdateTimeGt(String applicationName, LocalDateTime minUpdateTime) { | |
| 36 | - return selectList(new LambdaQueryWrapperX<ErrorCodeDO>().eq(ErrorCodeDO::getApplicationName, applicationName) | |
| 37 | - .gtIfPresent(ErrorCodeDO::getUpdateTime, minUpdateTime)); | |
| 38 | - } | |
| 39 | - | |
| 40 | -} |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/errorcode/ErrorCodeService.java deleted
| 1 | -package cn.iocoder.yudao.module.system.service.errorcode; | |
| 2 | - | |
| 3 | -import cn.iocoder.yudao.framework.common.pojo.PageResult; | |
| 4 | -import cn.iocoder.yudao.module.system.api.errorcode.dto.ErrorCodeAutoGenerateReqDTO; | |
| 5 | -import cn.iocoder.yudao.module.system.api.errorcode.dto.ErrorCodeRespDTO; | |
| 6 | -import cn.iocoder.yudao.module.system.controller.admin.errorcode.vo.ErrorCodePageReqVO; | |
| 7 | -import cn.iocoder.yudao.module.system.controller.admin.errorcode.vo.ErrorCodeSaveReqVO; | |
| 8 | -import cn.iocoder.yudao.module.system.dal.dataobject.errorcode.ErrorCodeDO; | |
| 9 | - | |
| 10 | -import jakarta.validation.Valid; | |
| 11 | -import java.time.LocalDateTime; | |
| 12 | -import java.util.List; | |
| 13 | - | |
| 14 | -/** | |
| 15 | - * 错误码 Service 接口 | |
| 16 | - * | |
| 17 | - * @author 芋道源码 | |
| 18 | - */ | |
| 19 | -public interface ErrorCodeService { | |
| 20 | - | |
| 21 | - /** | |
| 22 | - * 自动创建错误码 | |
| 23 | - * | |
| 24 | - * @param autoGenerateDTOs 错误码信息 | |
| 25 | - */ | |
| 26 | - void autoGenerateErrorCodes(@Valid List<ErrorCodeAutoGenerateReqDTO> autoGenerateDTOs); | |
| 27 | - | |
| 28 | - /** | |
| 29 | - * 增量获得错误码数组 | |
| 30 | - * | |
| 31 | - * 如果 minUpdateTime 为空时,则获取所有错误码 | |
| 32 | - * | |
| 33 | - * @param applicationName 应用名 | |
| 34 | - * @param minUpdateTime 最小更新时间 | |
| 35 | - * @return 错误码数组 | |
| 36 | - */ | |
| 37 | - List<ErrorCodeRespDTO> getErrorCodeList(String applicationName, LocalDateTime minUpdateTime); | |
| 38 | - | |
| 39 | - /** | |
| 40 | - * 创建错误码 | |
| 41 | - * | |
| 42 | - * @param createReqVO 创建信息 | |
| 43 | - * @return 编号 | |
| 44 | - */ | |
| 45 | - Long createErrorCode(@Valid ErrorCodeSaveReqVO createReqVO); | |
| 46 | - | |
| 47 | - /** | |
| 48 | - * 更新错误码 | |
| 49 | - * | |
| 50 | - * @param updateReqVO 更新信息 | |
| 51 | - */ | |
| 52 | - void updateErrorCode(@Valid ErrorCodeSaveReqVO updateReqVO); | |
| 53 | - | |
| 54 | - /** | |
| 55 | - * 删除错误码 | |
| 56 | - * | |
| 57 | - * @param id 编号 | |
| 58 | - */ | |
| 59 | - void deleteErrorCode(Long id); | |
| 60 | - | |
| 61 | - /** | |
| 62 | - * 获得错误码 | |
| 63 | - * | |
| 64 | - * @param id 编号 | |
| 65 | - * @return 错误码 | |
| 66 | - */ | |
| 67 | - ErrorCodeDO getErrorCode(Long id); | |
| 68 | - | |
| 69 | - /** | |
| 70 | - * 获得错误码分页 | |
| 71 | - * | |
| 72 | - * @param pageReqVO 分页查询 | |
| 73 | - * @return 错误码分页 | |
| 74 | - */ | |
| 75 | - PageResult<ErrorCodeDO> getErrorCodePage(ErrorCodePageReqVO pageReqVO); | |
| 76 | - | |
| 77 | -} |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/errorcode/ErrorCodeServiceImpl.java deleted
| 1 | -package cn.iocoder.yudao.module.system.service.errorcode; | |
| 2 | - | |
| 3 | -import cn.hutool.core.collection.CollUtil; | |
| 4 | -import cn.iocoder.yudao.framework.common.pojo.PageResult; | |
| 5 | -import cn.iocoder.yudao.framework.common.util.object.BeanUtils; | |
| 6 | -import cn.iocoder.yudao.module.system.api.errorcode.dto.ErrorCodeAutoGenerateReqDTO; | |
| 7 | -import cn.iocoder.yudao.module.system.api.errorcode.dto.ErrorCodeRespDTO; | |
| 8 | -import cn.iocoder.yudao.module.system.controller.admin.errorcode.vo.ErrorCodePageReqVO; | |
| 9 | -import cn.iocoder.yudao.module.system.controller.admin.errorcode.vo.ErrorCodeSaveReqVO; | |
| 10 | -import cn.iocoder.yudao.module.system.dal.dataobject.errorcode.ErrorCodeDO; | |
| 11 | -import cn.iocoder.yudao.module.system.dal.mysql.errorcode.ErrorCodeMapper; | |
| 12 | -import cn.iocoder.yudao.module.system.enums.errorcode.ErrorCodeTypeEnum; | |
| 13 | -import com.google.common.annotations.VisibleForTesting; | |
| 14 | -import lombok.extern.slf4j.Slf4j; | |
| 15 | -import org.springframework.stereotype.Service; | |
| 16 | -import org.springframework.transaction.annotation.Transactional; | |
| 17 | -import org.springframework.validation.annotation.Validated; | |
| 18 | - | |
| 19 | -import jakarta.annotation.Resource; | |
| 20 | -import java.time.LocalDateTime; | |
| 21 | -import java.util.List; | |
| 22 | -import java.util.Map; | |
| 23 | - | |
| 24 | -import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; | |
| 25 | -import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap; | |
| 26 | -import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet; | |
| 27 | -import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.ERROR_CODE_DUPLICATE; | |
| 28 | -import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.ERROR_CODE_NOT_EXISTS; | |
| 29 | - | |
| 30 | -/** | |
| 31 | - * 错误码 Service 实现类 | |
| 32 | - * | |
| 33 | - * @author dlyan | |
| 34 | - */ | |
| 35 | -@Service | |
| 36 | -@Validated | |
| 37 | -@Slf4j | |
| 38 | -public class ErrorCodeServiceImpl implements ErrorCodeService { | |
| 39 | - | |
| 40 | - @Resource | |
| 41 | - private ErrorCodeMapper errorCodeMapper; | |
| 42 | - | |
| 43 | - @Override | |
| 44 | - public Long createErrorCode(ErrorCodeSaveReqVO createReqVO) { | |
| 45 | - // 校验 code 重复 | |
| 46 | - validateCodeDuplicate(createReqVO.getCode(), null); | |
| 47 | - | |
| 48 | - // 插入 | |
| 49 | - ErrorCodeDO errorCode = BeanUtils.toBean(createReqVO, ErrorCodeDO.class) | |
| 50 | - .setType(ErrorCodeTypeEnum.MANUAL_OPERATION.getType()); | |
| 51 | - errorCodeMapper.insert(errorCode); | |
| 52 | - // 返回 | |
| 53 | - return errorCode.getId(); | |
| 54 | - } | |
| 55 | - | |
| 56 | - @Override | |
| 57 | - public void updateErrorCode(ErrorCodeSaveReqVO updateReqVO) { | |
| 58 | - // 校验存在 | |
| 59 | - validateErrorCodeExists(updateReqVO.getId()); | |
| 60 | - // 校验 code 重复 | |
| 61 | - validateCodeDuplicate(updateReqVO.getCode(), updateReqVO.getId()); | |
| 62 | - | |
| 63 | - // 更新 | |
| 64 | - ErrorCodeDO updateObj = BeanUtils.toBean(updateReqVO, ErrorCodeDO.class) | |
| 65 | - .setType(ErrorCodeTypeEnum.MANUAL_OPERATION.getType()); | |
| 66 | - errorCodeMapper.updateById(updateObj); | |
| 67 | - } | |
| 68 | - | |
| 69 | - @Override | |
| 70 | - public void deleteErrorCode(Long id) { | |
| 71 | - // 校验存在 | |
| 72 | - validateErrorCodeExists(id); | |
| 73 | - // 删除 | |
| 74 | - errorCodeMapper.deleteById(id); | |
| 75 | - } | |
| 76 | - | |
| 77 | - /** | |
| 78 | - * 校验错误码的唯一字段是否重复 | |
| 79 | - * | |
| 80 | - * 是否存在相同编码的错误码 | |
| 81 | - * | |
| 82 | - * @param code 错误码编码 | |
| 83 | - * @param id 错误码编号 | |
| 84 | - */ | |
| 85 | - @VisibleForTesting | |
| 86 | - public void validateCodeDuplicate(Integer code, Long id) { | |
| 87 | - ErrorCodeDO errorCodeDO = errorCodeMapper.selectByCode(code); | |
| 88 | - if (errorCodeDO == null) { | |
| 89 | - return; | |
| 90 | - } | |
| 91 | - // 如果 id 为空,说明不用比较是否为相同 id 的错误码 | |
| 92 | - if (id == null) { | |
| 93 | - throw exception(ERROR_CODE_DUPLICATE); | |
| 94 | - } | |
| 95 | - if (!errorCodeDO.getId().equals(id)) { | |
| 96 | - throw exception(ERROR_CODE_DUPLICATE); | |
| 97 | - } | |
| 98 | - } | |
| 99 | - | |
| 100 | - @VisibleForTesting | |
| 101 | - void validateErrorCodeExists(Long id) { | |
| 102 | - if (errorCodeMapper.selectById(id) == null) { | |
| 103 | - throw exception(ERROR_CODE_NOT_EXISTS); | |
| 104 | - } | |
| 105 | - } | |
| 106 | - | |
| 107 | - @Override | |
| 108 | - public ErrorCodeDO getErrorCode(Long id) { | |
| 109 | - return errorCodeMapper.selectById(id); | |
| 110 | - } | |
| 111 | - | |
| 112 | - @Override | |
| 113 | - public PageResult<ErrorCodeDO> getErrorCodePage(ErrorCodePageReqVO pageReqVO) { | |
| 114 | - return errorCodeMapper.selectPage(pageReqVO); | |
| 115 | - } | |
| 116 | - | |
| 117 | - @Override | |
| 118 | - @Transactional | |
| 119 | - public void autoGenerateErrorCodes(List<ErrorCodeAutoGenerateReqDTO> autoGenerateDTOs) { | |
| 120 | - if (CollUtil.isEmpty(autoGenerateDTOs)) { | |
| 121 | - return; | |
| 122 | - } | |
| 123 | - // 获得错误码 | |
| 124 | - List<ErrorCodeDO> errorCodeDOs = errorCodeMapper.selectListByCodes( | |
| 125 | - convertSet(autoGenerateDTOs, ErrorCodeAutoGenerateReqDTO::getCode)); | |
| 126 | - Map<Integer, ErrorCodeDO> errorCodeDOMap = convertMap(errorCodeDOs, ErrorCodeDO::getCode); | |
| 127 | - | |
| 128 | - // 遍历 autoGenerateBOs 数组,逐个插入或更新。考虑到每次量级不大,就不走批量了 | |
| 129 | - autoGenerateDTOs.forEach(autoGenerateDTO -> { | |
| 130 | - ErrorCodeDO errorCode = errorCodeDOMap.get(autoGenerateDTO.getCode()); | |
| 131 | - // 不存在,则进行新增 | |
| 132 | - if (errorCode == null) { | |
| 133 | - errorCode = BeanUtils.toBean(autoGenerateDTO, ErrorCodeDO.class) | |
| 134 | - .setType(ErrorCodeTypeEnum.AUTO_GENERATION.getType()); | |
| 135 | - errorCodeMapper.insert(errorCode); | |
| 136 | - return; | |
| 137 | - } | |
| 138 | - // 存在,则进行更新。更新有三个前置条件: | |
| 139 | - // 条件 1. 只更新自动生成的错误码,即 Type 为 ErrorCodeTypeEnum.AUTO_GENERATION | |
| 140 | - if (!ErrorCodeTypeEnum.AUTO_GENERATION.getType().equals(errorCode.getType())) { | |
| 141 | - return; | |
| 142 | - } | |
| 143 | - // 条件 2. 分组 applicationName 必须匹配,避免存在错误码冲突的情况 | |
| 144 | - if (!autoGenerateDTO.getApplicationName().equals(errorCode.getApplicationName())) { | |
| 145 | - log.error("[autoGenerateErrorCodes][自动创建({}/{}) 错误码失败,数据库中已经存在({}/{})]", | |
| 146 | - autoGenerateDTO.getCode(), autoGenerateDTO.getApplicationName(), | |
| 147 | - errorCode.getCode(), errorCode.getApplicationName()); | |
| 148 | - return; | |
| 149 | - } | |
| 150 | - // 条件 3. 错误提示语存在差异 | |
| 151 | - if (autoGenerateDTO.getMessage().equals(errorCode.getMessage())) { | |
| 152 | - return; | |
| 153 | - } | |
| 154 | - // 最终匹配,进行更新 | |
| 155 | - errorCodeMapper.updateById(new ErrorCodeDO().setId(errorCode.getId()).setMessage(autoGenerateDTO.getMessage())); | |
| 156 | - }); | |
| 157 | - } | |
| 158 | - | |
| 159 | - @Override | |
| 160 | - public List<ErrorCodeRespDTO> getErrorCodeList(String applicationName, LocalDateTime minUpdateTime) { | |
| 161 | - List<ErrorCodeDO> list = errorCodeMapper.selectListByApplicationNameAndUpdateTimeGt( | |
| 162 | - applicationName, minUpdateTime); | |
| 163 | - return BeanUtils.toBean(list, ErrorCodeRespDTO.class); | |
| 164 | - } | |
| 165 | - | |
| 166 | -} | |
| 167 | - |
yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/errorcode/ErrorCodeServiceTest.java deleted
| 1 | -package cn.iocoder.yudao.module.system.service.errorcode; | |
| 2 | - | |
| 3 | -import cn.iocoder.yudao.framework.common.pojo.PageResult; | |
| 4 | -import cn.iocoder.yudao.framework.common.util.collection.ArrayUtils; | |
| 5 | -import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest; | |
| 6 | -import cn.iocoder.yudao.module.system.api.errorcode.dto.ErrorCodeAutoGenerateReqDTO; | |
| 7 | -import cn.iocoder.yudao.module.system.api.errorcode.dto.ErrorCodeRespDTO; | |
| 8 | -import cn.iocoder.yudao.module.system.controller.admin.errorcode.vo.ErrorCodePageReqVO; | |
| 9 | -import cn.iocoder.yudao.module.system.controller.admin.errorcode.vo.ErrorCodeSaveReqVO; | |
| 10 | -import cn.iocoder.yudao.module.system.dal.dataobject.errorcode.ErrorCodeDO; | |
| 11 | -import cn.iocoder.yudao.module.system.dal.mysql.errorcode.ErrorCodeMapper; | |
| 12 | -import cn.iocoder.yudao.module.system.enums.errorcode.ErrorCodeTypeEnum; | |
| 13 | -import org.assertj.core.util.Lists; | |
| 14 | -import org.junit.jupiter.api.Test; | |
| 15 | -import org.springframework.context.annotation.Import; | |
| 16 | - | |
| 17 | -import jakarta.annotation.Resource; | |
| 18 | -import java.time.LocalDateTime; | |
| 19 | -import java.util.List; | |
| 20 | -import java.util.function.Consumer; | |
| 21 | - | |
| 22 | -import static cn.hutool.core.util.RandomUtil.randomEle; | |
| 23 | -import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildBetweenTime; | |
| 24 | -import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildTime; | |
| 25 | -import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId; | |
| 26 | -import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals; | |
| 27 | -import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException; | |
| 28 | -import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*; | |
| 29 | -import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.ERROR_CODE_DUPLICATE; | |
| 30 | -import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.ERROR_CODE_NOT_EXISTS; | |
| 31 | -import static org.junit.jupiter.api.Assertions.*; | |
| 32 | - | |
| 33 | -@Import(ErrorCodeServiceImpl.class) | |
| 34 | -public class ErrorCodeServiceTest extends BaseDbUnitTest { | |
| 35 | - | |
| 36 | - @Resource | |
| 37 | - private ErrorCodeServiceImpl errorCodeService; | |
| 38 | - | |
| 39 | - @Resource | |
| 40 | - private ErrorCodeMapper errorCodeMapper; | |
| 41 | - | |
| 42 | - @Test | |
| 43 | - public void testCreateErrorCode_success() { | |
| 44 | - // 准备参数 | |
| 45 | - ErrorCodeSaveReqVO reqVO = randomPojo(ErrorCodeSaveReqVO.class) | |
| 46 | - .setId(null); // 防止 id 被赋值 | |
| 47 | - | |
| 48 | - // 调用 | |
| 49 | - Long errorCodeId = errorCodeService.createErrorCode(reqVO); | |
| 50 | - // 断言 | |
| 51 | - assertNotNull(errorCodeId); | |
| 52 | - // 校验记录的属性是否正确 | |
| 53 | - ErrorCodeDO errorCode = errorCodeMapper.selectById(errorCodeId); | |
| 54 | - assertPojoEquals(reqVO, errorCode, "id"); | |
| 55 | - assertEquals(ErrorCodeTypeEnum.MANUAL_OPERATION.getType(), errorCode.getType()); | |
| 56 | - } | |
| 57 | - | |
| 58 | - @Test | |
| 59 | - public void testUpdateErrorCode_success() { | |
| 60 | - // mock 数据 | |
| 61 | - ErrorCodeDO dbErrorCode = randomErrorCodeDO(); | |
| 62 | - errorCodeMapper.insert(dbErrorCode);// @Sql: 先插入出一条存在的数据 | |
| 63 | - // 准备参数 | |
| 64 | - ErrorCodeSaveReqVO reqVO = randomPojo(ErrorCodeSaveReqVO.class, o -> { | |
| 65 | - o.setId(dbErrorCode.getId()); // 设置更新的 ID | |
| 66 | - }); | |
| 67 | - | |
| 68 | - // 调用 | |
| 69 | - errorCodeService.updateErrorCode(reqVO); | |
| 70 | - // 校验是否更新正确 | |
| 71 | - ErrorCodeDO errorCode = errorCodeMapper.selectById(reqVO.getId()); // 获取最新的 | |
| 72 | - assertPojoEquals(reqVO, errorCode); | |
| 73 | - assertEquals(ErrorCodeTypeEnum.MANUAL_OPERATION.getType(), errorCode.getType()); | |
| 74 | - } | |
| 75 | - | |
| 76 | - @Test | |
| 77 | - public void testDeleteErrorCode_success() { | |
| 78 | - // mock 数据 | |
| 79 | - ErrorCodeDO dbErrorCode = randomErrorCodeDO(); | |
| 80 | - errorCodeMapper.insert(dbErrorCode);// @Sql: 先插入出一条存在的数据 | |
| 81 | - // 准备参数 | |
| 82 | - Long id = dbErrorCode.getId(); | |
| 83 | - | |
| 84 | - // 调用 | |
| 85 | - errorCodeService.deleteErrorCode(id); | |
| 86 | - // 校验数据不存在了 | |
| 87 | - assertNull(errorCodeMapper.selectById(id)); | |
| 88 | - } | |
| 89 | - | |
| 90 | - @Test | |
| 91 | - public void testGetErrorCodePage() { | |
| 92 | - // mock 数据 | |
| 93 | - ErrorCodeDO dbErrorCode = initGetErrorCodePage(); | |
| 94 | - // 准备参数 | |
| 95 | - ErrorCodePageReqVO reqVO = new ErrorCodePageReqVO(); | |
| 96 | - reqVO.setType(ErrorCodeTypeEnum.AUTO_GENERATION.getType()); | |
| 97 | - reqVO.setApplicationName("tu"); | |
| 98 | - reqVO.setCode(1); | |
| 99 | - reqVO.setMessage("ma"); | |
| 100 | - reqVO.setCreateTime(buildBetweenTime(2020, 11, 1, 2020, 11, 30)); | |
| 101 | - | |
| 102 | - // 调用 | |
| 103 | - PageResult<ErrorCodeDO> pageResult = errorCodeService.getErrorCodePage(reqVO); | |
| 104 | - // 断言 | |
| 105 | - assertEquals(1, pageResult.getTotal()); | |
| 106 | - assertEquals(1, pageResult.getList().size()); | |
| 107 | - assertPojoEquals(dbErrorCode, pageResult.getList().get(0)); | |
| 108 | - } | |
| 109 | - | |
| 110 | - /** | |
| 111 | - * 初始化 getErrorCodePage 方法的测试数据 | |
| 112 | - */ | |
| 113 | - private ErrorCodeDO initGetErrorCodePage() { | |
| 114 | - ErrorCodeDO dbErrorCode = randomErrorCodeDO(o -> { // 等会查询到 | |
| 115 | - o.setType(ErrorCodeTypeEnum.AUTO_GENERATION.getType()); | |
| 116 | - o.setApplicationName("tudou"); | |
| 117 | - o.setCode(1); | |
| 118 | - o.setMessage("yuanma"); | |
| 119 | - o.setCreateTime(buildTime(2020, 11, 11)); | |
| 120 | - }); | |
| 121 | - errorCodeMapper.insert(dbErrorCode); | |
| 122 | - // 测试 type 不匹配 | |
| 123 | - errorCodeMapper.insert(cloneIgnoreId(dbErrorCode, o -> o.setType(ErrorCodeTypeEnum.MANUAL_OPERATION.getType()))); | |
| 124 | - // 测试 applicationName 不匹配 | |
| 125 | - errorCodeMapper.insert(cloneIgnoreId(dbErrorCode, o -> o.setApplicationName("yuan"))); | |
| 126 | - // 测试 code 不匹配 | |
| 127 | - errorCodeMapper.insert(cloneIgnoreId(dbErrorCode, o -> o.setCode(2))); | |
| 128 | - // 测试 message 不匹配 | |
| 129 | - errorCodeMapper.insert(cloneIgnoreId(dbErrorCode, o -> o.setMessage("nai"))); | |
| 130 | - // 测试 createTime 不匹配 | |
| 131 | - errorCodeMapper.insert(cloneIgnoreId(dbErrorCode, o -> o.setCreateTime(buildTime(2020, 12, 12)))); | |
| 132 | - return dbErrorCode; | |
| 133 | - } | |
| 134 | - | |
| 135 | - @Test | |
| 136 | - public void testValidateCodeDuplicate_codeDuplicateForCreate() { | |
| 137 | - // 准备参数 | |
| 138 | - Integer code = randomInteger(); | |
| 139 | - // mock 数据 | |
| 140 | - errorCodeMapper.insert(randomErrorCodeDO(o -> o.setCode(code))); | |
| 141 | - | |
| 142 | - // 调用,校验异常 | |
| 143 | - assertServiceException(() -> errorCodeService.validateCodeDuplicate(code, null), | |
| 144 | - ERROR_CODE_DUPLICATE); | |
| 145 | - } | |
| 146 | - | |
| 147 | - @Test | |
| 148 | - public void testValidateCodeDuplicate_codeDuplicateForUpdate() { | |
| 149 | - // 准备参数 | |
| 150 | - Long id = randomLongId(); | |
| 151 | - Integer code = randomInteger(); | |
| 152 | - // mock 数据 | |
| 153 | - errorCodeMapper.insert(randomErrorCodeDO(o -> o.setCode(code))); | |
| 154 | - | |
| 155 | - // 调用,校验异常 | |
| 156 | - assertServiceException(() -> errorCodeService.validateCodeDuplicate(code, id), | |
| 157 | - ERROR_CODE_DUPLICATE); | |
| 158 | - } | |
| 159 | - | |
| 160 | - @Test | |
| 161 | - public void testValidateErrorCodeExists_notExists() { | |
| 162 | - assertServiceException(() -> errorCodeService.validateErrorCodeExists(null), | |
| 163 | - ERROR_CODE_NOT_EXISTS); | |
| 164 | - } | |
| 165 | - | |
| 166 | - /** | |
| 167 | - * 情况 1,错误码不存在的情况 | |
| 168 | - */ | |
| 169 | - @Test | |
| 170 | - public void testAutoGenerateErrorCodes_01() { | |
| 171 | - // 准备参数 | |
| 172 | - ErrorCodeAutoGenerateReqDTO generateReqDTO = randomPojo(ErrorCodeAutoGenerateReqDTO.class); | |
| 173 | - // mock 方法 | |
| 174 | - | |
| 175 | - // 调用 | |
| 176 | - errorCodeService.autoGenerateErrorCodes(Lists.newArrayList(generateReqDTO)); | |
| 177 | - // 断言 | |
| 178 | - ErrorCodeDO errorCode = errorCodeMapper.selectOne(null); | |
| 179 | - assertPojoEquals(generateReqDTO, errorCode); | |
| 180 | - assertEquals(ErrorCodeTypeEnum.AUTO_GENERATION.getType(), errorCode.getType()); | |
| 181 | - } | |
| 182 | - | |
| 183 | - /** | |
| 184 | - * 情况 2.1,错误码存在,但是是 ErrorCodeTypeEnum.MANUAL_OPERATION 类型 | |
| 185 | - */ | |
| 186 | - @Test | |
| 187 | - public void testAutoGenerateErrorCodes_021() { | |
| 188 | - // mock 数据 | |
| 189 | - ErrorCodeDO dbErrorCode = randomErrorCodeDO(o -> o.setType(ErrorCodeTypeEnum.MANUAL_OPERATION.getType())); | |
| 190 | - errorCodeMapper.insert(dbErrorCode); | |
| 191 | - // 准备参数 | |
| 192 | - ErrorCodeAutoGenerateReqDTO generateReqDTO = randomPojo(ErrorCodeAutoGenerateReqDTO.class, | |
| 193 | - o -> o.setCode(dbErrorCode.getCode())); | |
| 194 | - // mock 方法 | |
| 195 | - | |
| 196 | - // 调用 | |
| 197 | - errorCodeService.autoGenerateErrorCodes(Lists.newArrayList(generateReqDTO)); | |
| 198 | - // 断言,相等,说明不会更新 | |
| 199 | - ErrorCodeDO errorCode = errorCodeMapper.selectById(dbErrorCode.getId()); | |
| 200 | - assertPojoEquals(dbErrorCode, errorCode); | |
| 201 | - } | |
| 202 | - | |
| 203 | - /** | |
| 204 | - * 情况 2.2,错误码存在,但是是 applicationName 不匹配 | |
| 205 | - */ | |
| 206 | - @Test | |
| 207 | - public void testAutoGenerateErrorCodes_022() { | |
| 208 | - // mock 数据 | |
| 209 | - ErrorCodeDO dbErrorCode = randomErrorCodeDO(o -> o.setType(ErrorCodeTypeEnum.AUTO_GENERATION.getType())); | |
| 210 | - errorCodeMapper.insert(dbErrorCode); | |
| 211 | - // 准备参数 | |
| 212 | - ErrorCodeAutoGenerateReqDTO generateReqDTO = randomPojo(ErrorCodeAutoGenerateReqDTO.class, | |
| 213 | - o -> o.setCode(dbErrorCode.getCode()).setApplicationName(randomString())); | |
| 214 | - // mock 方法 | |
| 215 | - | |
| 216 | - // 调用 | |
| 217 | - errorCodeService.autoGenerateErrorCodes(Lists.newArrayList(generateReqDTO)); | |
| 218 | - // 断言,相等,说明不会更新 | |
| 219 | - ErrorCodeDO errorCode = errorCodeMapper.selectById(dbErrorCode.getId()); | |
| 220 | - assertPojoEquals(dbErrorCode, errorCode); | |
| 221 | - } | |
| 222 | - | |
| 223 | - /** | |
| 224 | - * 情况 2.3,错误码存在,但是是 message 相同 | |
| 225 | - */ | |
| 226 | - @Test | |
| 227 | - public void testAutoGenerateErrorCodes_023() { | |
| 228 | - // mock 数据 | |
| 229 | - ErrorCodeDO dbErrorCode = randomErrorCodeDO(o -> o.setType(ErrorCodeTypeEnum.AUTO_GENERATION.getType())); | |
| 230 | - errorCodeMapper.insert(dbErrorCode); | |
| 231 | - // 准备参数 | |
| 232 | - ErrorCodeAutoGenerateReqDTO generateReqDTO = randomPojo(ErrorCodeAutoGenerateReqDTO.class, | |
| 233 | - o -> o.setCode(dbErrorCode.getCode()).setApplicationName(dbErrorCode.getApplicationName()) | |
| 234 | - .setMessage(dbErrorCode.getMessage())); | |
| 235 | - // mock 方法 | |
| 236 | - | |
| 237 | - // 调用 | |
| 238 | - errorCodeService.autoGenerateErrorCodes(Lists.newArrayList(generateReqDTO)); | |
| 239 | - // 断言,相等,说明不会更新 | |
| 240 | - ErrorCodeDO errorCode = errorCodeMapper.selectById(dbErrorCode.getId()); | |
| 241 | - assertPojoEquals(dbErrorCode, errorCode); | |
| 242 | - } | |
| 243 | - | |
| 244 | - /** | |
| 245 | - * 情况 2.3,错误码存在,但是是 message 不同,则进行更新 | |
| 246 | - */ | |
| 247 | - @Test | |
| 248 | - public void testAutoGenerateErrorCodes_024() { | |
| 249 | - // mock 数据 | |
| 250 | - ErrorCodeDO dbErrorCode = randomErrorCodeDO(o -> o.setType(ErrorCodeTypeEnum.AUTO_GENERATION.getType())); | |
| 251 | - errorCodeMapper.insert(dbErrorCode); | |
| 252 | - // 准备参数 | |
| 253 | - ErrorCodeAutoGenerateReqDTO generateReqDTO = randomPojo(ErrorCodeAutoGenerateReqDTO.class, | |
| 254 | - o -> o.setCode(dbErrorCode.getCode()).setApplicationName(dbErrorCode.getApplicationName())); | |
| 255 | - // mock 方法 | |
| 256 | - | |
| 257 | - // 调用 | |
| 258 | - errorCodeService.autoGenerateErrorCodes(Lists.newArrayList(generateReqDTO)); | |
| 259 | - // 断言,匹配 | |
| 260 | - ErrorCodeDO errorCode = errorCodeMapper.selectById(dbErrorCode.getId()); | |
| 261 | - assertPojoEquals(generateReqDTO, errorCode); | |
| 262 | - } | |
| 263 | - | |
| 264 | - @Test | |
| 265 | - public void testGetErrorCode() { | |
| 266 | - // 准备参数 | |
| 267 | - ErrorCodeDO errorCodeDO = randomErrorCodeDO(); | |
| 268 | - errorCodeMapper.insert(errorCodeDO); | |
| 269 | - // mock 方法 | |
| 270 | - Long id = errorCodeDO.getId(); | |
| 271 | - | |
| 272 | - // 调用 | |
| 273 | - ErrorCodeDO dbErrorCode = errorCodeService.getErrorCode(id); | |
| 274 | - // 断言 | |
| 275 | - assertPojoEquals(errorCodeDO, dbErrorCode); | |
| 276 | - } | |
| 277 | - | |
| 278 | - @Test | |
| 279 | - public void testGetErrorCodeList() { | |
| 280 | - // 准备参数 | |
| 281 | - ErrorCodeDO errorCodeDO01 = randomErrorCodeDO( | |
| 282 | - o -> o.setApplicationName("yunai_server").setUpdateTime(buildTime(2022, 1, 10))); | |
| 283 | - errorCodeMapper.insert(errorCodeDO01); | |
| 284 | - ErrorCodeDO errorCodeDO02 = randomErrorCodeDO( | |
| 285 | - o -> o.setApplicationName("yunai_server").setUpdateTime(buildTime(2022, 1, 12))); | |
| 286 | - errorCodeMapper.insert(errorCodeDO02); | |
| 287 | - // mock 方法 | |
| 288 | - String applicationName = "yunai_server"; | |
| 289 | - LocalDateTime minUpdateTime = buildTime(2022, 1, 11); | |
| 290 | - | |
| 291 | - // 调用 | |
| 292 | - List<ErrorCodeRespDTO> errorCodeList = errorCodeService.getErrorCodeList(applicationName, minUpdateTime); | |
| 293 | - // 断言 | |
| 294 | - assertEquals(1, errorCodeList.size()); | |
| 295 | - assertPojoEquals(errorCodeDO02, errorCodeList.get(0)); | |
| 296 | - } | |
| 297 | - | |
| 298 | - // ========== 随机对象 ========== | |
| 299 | - | |
| 300 | - @SafeVarargs | |
| 301 | - private static ErrorCodeDO randomErrorCodeDO(Consumer<ErrorCodeDO>... consumers) { | |
| 302 | - Consumer<ErrorCodeDO> consumer = (o) -> { | |
| 303 | - o.setType(randomEle(ErrorCodeTypeEnum.values()).getType()); // 保证 key 的范围 | |
| 304 | - }; | |
| 305 | - return randomPojo(ErrorCodeDO.class, ArrayUtils.append(consumer, consumers)); | |
| 306 | - } | |
| 307 | - | |
| 308 | -} |
yudao-module-system/yudao-module-system-biz/src/test/resources/sql/clean.sql
| ... | ... | @@ -16,7 +16,6 @@ DELETE FROM "system_sms_channel"; |
| 16 | 16 | DELETE FROM "system_sms_template"; |
| 17 | 17 | DELETE FROM "system_sms_log"; |
| 18 | 18 | DELETE FROM "system_sms_code"; |
| 19 | -DELETE FROM "system_error_code"; | |
| 20 | 19 | DELETE FROM "system_social_client"; |
| 21 | 20 | DELETE FROM "system_social_user"; |
| 22 | 21 | DELETE FROM "system_social_user_bind"; | ... | ... |
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" ( |
| 332 | 332 | PRIMARY KEY ("id") |
| 333 | 333 | ) COMMENT '短信日志'; |
| 334 | 334 | |
| 335 | -CREATE TABLE IF NOT EXISTS "system_error_code" ( | |
| 336 | - "id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY, | |
| 337 | - "type" tinyint NOT NULL DEFAULT '0', | |
| 338 | - "application_name" varchar(50) NOT NULL, | |
| 339 | - "code" int NOT NULL DEFAULT '0', | |
| 340 | - "message" varchar(512) NOT NULL DEFAULT '', | |
| 341 | - "memo" varchar(512) DEFAULT '', | |
| 342 | - "creator" varchar(64) DEFAULT '', | |
| 343 | - "create_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, | |
| 344 | - "updater" varchar(64) DEFAULT '', | |
| 345 | - "update_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, | |
| 346 | - "deleted" bit NOT NULL DEFAULT FALSE, | |
| 347 | - PRIMARY KEY ("id") | |
| 348 | -) COMMENT '错误码表'; | |
| 349 | - | |
| 350 | 335 | CREATE TABLE IF NOT EXISTS "system_social_client" ( |
| 351 | 336 | "id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY, |
| 352 | 337 | "name" varchar(255) NOT NULL, | ... | ... |
yudao-server/src/main/resources/application-local.yaml
| ... | ... | @@ -227,8 +227,6 @@ yudao: |
| 227 | 227 | refund-notify-url: http://yunai.natapp1.cc/admin-api/pay/notify/refund # 支付渠道的【退款】回调地址 |
| 228 | 228 | access-log: # 访问日志的配置项 |
| 229 | 229 | enable: false |
| 230 | - error-code: # 错误码相关配置项 | |
| 231 | - enable: false | |
| 232 | 230 | demo: false # 关闭演示模式 |
| 233 | 231 | tencent-lbs-key: TVDBZ-TDILD-4ON4B-PFDZA-RNLKH-VVF6E # QQ 地图的密钥 https://lbs.qq.com/service/staticV2/staticGuide/staticDoc |
| 234 | 232 | ... | ... |
yudao-server/src/main/resources/application.yaml
| ... | ... | @@ -181,14 +181,6 @@ yudao: |
| 181 | 181 | base-package: ${yudao.info.base-package} |
| 182 | 182 | db-schemas: ${spring.datasource.dynamic.datasource.master.name} |
| 183 | 183 | front-type: 10 # 前端模版的类型,参见 CodegenFrontTypeEnum 枚举类 |
| 184 | - error-code: # 错误码相关配置项 | |
| 185 | - constants-class-list: | |
| 186 | - - cn.iocoder.yudao.module.bpm.enums.ErrorCodeConstants | |
| 187 | - - cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants | |
| 188 | - - cn.iocoder.yudao.module.member.enums.ErrorCodeConstants | |
| 189 | - - cn.iocoder.yudao.module.pay.enums.ErrorCodeConstants | |
| 190 | - - cn.iocoder.yudao.module.system.enums.ErrorCodeConstants | |
| 191 | - - cn.iocoder.yudao.module.mp.enums.ErrorCodeConstants | |
| 192 | 184 | tenant: # 多租户相关配置项 |
| 193 | 185 | enable: true |
| 194 | 186 | ignore-urls: | ... | ... |