|
@@ -1,82 +1,12 @@
|
|
package cn.iocoder.dashboard.framework.errorcode.core;
|
|
package cn.iocoder.dashboard.framework.errorcode.core;
|
|
|
|
|
|
-import cn.iocoder.dashboard.common.exception.ErrorCode;
|
|
|
|
-import cn.iocoder.dashboard.common.pojo.CommonResult;
|
|
|
|
-import cn.iocoder.dashboard.modules.system.controller.errorcode.dto.ErrorCodeAutoGenerateDTO;
|
|
|
|
-import cn.iocoder.dashboard.modules.system.service.errorcode.ErrorCodeService;
|
|
|
|
-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.util.StringUtils;
|
|
|
|
-
|
|
|
|
-import javax.annotation.Resource;
|
|
|
|
-import java.util.ArrayList;
|
|
|
|
-import java.util.Arrays;
|
|
|
|
-import java.util.List;
|
|
|
|
-
|
|
|
|
-@Slf4j
|
|
|
|
-public class ErrorCodeAutoGenerator {
|
|
|
|
|
|
+/**
|
|
|
|
+ * @author dylan
|
|
|
|
+ */
|
|
|
|
+public interface ErrorCodeAutoGenerator {
|
|
|
|
|
|
/**
|
|
/**
|
|
- * 应用分组
|
|
|
|
|
|
+ * 将配置类到错误码写入数据库
|
|
*/
|
|
*/
|
|
- private final String group;
|
|
|
|
- /**
|
|
|
|
- * 错误码枚举类
|
|
|
|
- */
|
|
|
|
- private String errorCodeConstantsClass;
|
|
|
|
-
|
|
|
|
- @Resource
|
|
|
|
- private ErrorCodeService errorCodeService;
|
|
|
|
-
|
|
|
|
- public ErrorCodeAutoGenerator(String group) {
|
|
|
|
- this.group = group;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- public ErrorCodeAutoGenerator setErrorCodeConstantsClass(String errorCodeConstantsClass) {
|
|
|
|
- this.errorCodeConstantsClass = errorCodeConstantsClass;
|
|
|
|
- return this;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- @EventListener(ApplicationReadyEvent.class)
|
|
|
|
- @Async // 异步,保证项目的启动过程,毕竟非关键流程
|
|
|
|
- public void execute() {
|
|
|
|
- // 校验 errorCodeConstantsClass 参数
|
|
|
|
- if (!StringUtils.hasText(errorCodeConstantsClass)) {
|
|
|
|
- log.info("[execute][未配置 yudao.error-code.constants-class 配置项,不进行自动写入到当前服务中]");
|
|
|
|
- return;
|
|
|
|
- }
|
|
|
|
- Class errorCodeConstantsClazz;
|
|
|
|
- try {
|
|
|
|
- errorCodeConstantsClazz = Class.forName(errorCodeConstantsClass);
|
|
|
|
- } catch (ClassNotFoundException e) {
|
|
|
|
- log.error("[execute][配置的 ({}) 找不到对应的类]", errorCodeConstantsClass);
|
|
|
|
- return;
|
|
|
|
- }
|
|
|
|
- // 写入 system-service 服务
|
|
|
|
- log.info("[execute][自动将 ({}) 类的错误码,准备写入到当前服务]", errorCodeConstantsClass);
|
|
|
|
- List<ErrorCodeAutoGenerateDTO> autoGenerateDTOs = new ArrayList<>();
|
|
|
|
- Arrays.stream(errorCodeConstantsClazz.getFields()).forEach(field -> {
|
|
|
|
- if (field.getType() != ErrorCode.class) {
|
|
|
|
- return;
|
|
|
|
- }
|
|
|
|
- try {
|
|
|
|
- // TODO 芋艿:校验是否重复了;
|
|
|
|
- ErrorCode errorCode = (ErrorCode) field.get(errorCodeConstantsClazz);
|
|
|
|
- autoGenerateDTOs.add(new ErrorCodeAutoGenerateDTO().setGroup(group)
|
|
|
|
- .setCode(errorCode.getCode()).setMessage(errorCode.getMessage()));
|
|
|
|
- } catch (IllegalAccessException e) {
|
|
|
|
- throw new RuntimeException(e);
|
|
|
|
- }
|
|
|
|
- });
|
|
|
|
- CommonResult<Boolean> autoGenerateErrorCodesResult = errorCodeService.autoGenerateErrorCodes1(autoGenerateDTOs);
|
|
|
|
- if (autoGenerateErrorCodesResult.isSuccess()) {
|
|
|
|
- log.info("[execute][自动将 ({}) 类的错误码,成功写入到当前服务]", errorCodeConstantsClass);
|
|
|
|
- } else {
|
|
|
|
- log.error("[execute][自动将 ({}) 类的错误码,失败写入到当前服务,原因为 ({}/{})]", errorCodeConstantsClass,
|
|
|
|
- autoGenerateErrorCodesResult.getCode(), autoGenerateErrorCodesResult.getMsg());
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
|
|
+ void execute();
|
|
}
|
|
}
|