|
@@ -0,0 +1,80 @@
|
|
|
+package cn.iocoder.dashboard.modules.system.service.errorcode;
|
|
|
+
|
|
|
+import cn.iocoder.dashboard.common.exception.ErrorCode;
|
|
|
+import cn.iocoder.dashboard.common.pojo.CommonResult;
|
|
|
+import cn.iocoder.dashboard.framework.errorcode.core.ErrorCodeAutoGenerator;
|
|
|
+import cn.iocoder.dashboard.modules.system.controller.errorcode.dto.ErrorCodeAutoGenerateDTO;
|
|
|
+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.util.StringUtils;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+@RequiredArgsConstructor
|
|
|
+@Slf4j
|
|
|
+public class ErrorCodeAutoGeneratorImpl implements ErrorCodeAutoGenerator {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 应用分组
|
|
|
+ */
|
|
|
+ private final String group;
|
|
|
+ /**
|
|
|
+ * 错误码枚举类
|
|
|
+ */
|
|
|
+ private String errorCodeConstantsClass;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private ErrorCodeService errorCodeService;
|
|
|
+
|
|
|
+ public ErrorCodeAutoGeneratorImpl setErrorCodeConstantsClass(String errorCodeConstantsClass) {
|
|
|
+ this.errorCodeConstantsClass = errorCodeConstantsClass;
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @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 {
|
|
|
+ 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());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|