模版编制的相关实现
This commit is contained in:
@@ -14,11 +14,13 @@ public interface ErrorCodeConstants {
|
||||
ErrorCode TMPL_TP_DEl_ERROR = new ErrorCode(1_027_000_507, "模版分类删除失败");
|
||||
ErrorCode TEMPLATE_INSTANCE_DATA_NOT_EXISTS = new ErrorCode(1_027_000_508, "实例字段值不存在");
|
||||
ErrorCode TEMPLATE_INSTANCE_ITEM_NOT_EXISTS = new ErrorCode(1_027_000_509, "实例条款值不存在");
|
||||
ErrorCode PARAMS_IS_NULL_OR_ERR = new ErrorCode(1_027_000_510, "参数为空");
|
||||
ErrorCode PARAMS_IS_NULL_OR_ERR = new ErrorCode(1_027_000_510, "参数为空或参数错误");
|
||||
ErrorCode DEPARTMENT_INSTANCE_RELATIVITY_NOT_EXISTS = new ErrorCode(1_027_000_511, "部门与实例关联不存在");
|
||||
ErrorCode ILLEGAL_OPERATION_TYPE = new ErrorCode(1_027_000_511, "非法操作类型");
|
||||
ErrorCode OPERATION_FAIL= new ErrorCode(1_027_000_512, "操作失败");
|
||||
ErrorCode STATUS_OPERATION_FAIL= new ErrorCode(1_027_000_513, "当前状态不支持此操作");
|
||||
ErrorCode NOT_FOUND_CLASS= new ErrorCode(1_027_000_514, "找不到对应的类");
|
||||
ErrorCode UTIL_NOT_INIT= new ErrorCode(1_027_000_515, "工具类为未初始化");
|
||||
//Illegal operation type
|
||||
}
|
||||
|
||||
|
||||
@@ -59,8 +59,8 @@ public class TmplTpFldController {
|
||||
PageResult<TmplTpFldDO> pageResult = tmplTpFldService.tmplTpFldPage(pageReqVO);
|
||||
return success(BeanUtils.toBean(pageResult, TmplFldRespVO.class));
|
||||
}
|
||||
@GetMapping("/getByClass")
|
||||
@Operation(summary = "获得类固定模板字段列表")
|
||||
@GetMapping("/class-fld")
|
||||
@Operation(summary = "获得类固定模板字段列表", description = "clazz为VO类类名")
|
||||
@PreAuthorize("@ss.hasPermission('bse:tmpl-tp-fld:list')")
|
||||
public CommonResult<List<Map<String, Object>>> getTmplTpListByClass(String clazz) {
|
||||
return success(tmplTpFldService.getTmplTpListByClass(clazz));
|
||||
|
||||
@@ -1,19 +1,20 @@
|
||||
package com.zt.plat.module.base.framework.util.config;
|
||||
|
||||
|
||||
import com.zt.plat.module.base.util.ClassInfoScanner;
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
|
||||
|
||||
@Configuration
|
||||
@ConfigurationProperties(prefix = "base") // 指定配置前缀
|
||||
@Slf4j
|
||||
@Getter
|
||||
@Setter
|
||||
public class UtilConfig {
|
||||
|
||||
// 固定的包路径,直接写死在代码中
|
||||
private static final String BASE_PACKAGES = "com.zt.plat.module.base.controller.admin";
|
||||
private String[] packages;
|
||||
|
||||
/**
|
||||
* 当Spring容器初始化该配置类后,自动执行初始化操作
|
||||
@@ -21,9 +22,8 @@ public class UtilConfig {
|
||||
@PostConstruct
|
||||
public void initClassInfoScanner() {
|
||||
try {
|
||||
// 使用固定的包路径初始化类信息扫描工具
|
||||
ClassInfoScanner.init(BASE_PACKAGES);
|
||||
log.info("ClassInfoScanner初始化成功,扫描包路径:【{}】" , BASE_PACKAGES);
|
||||
// 使用从配置获取的包路径初始化类信息扫描工具
|
||||
ClassInfoScanner.init(packages);
|
||||
} catch (Exception e) {
|
||||
log.error("ClassInfoScanner初始化失败:{}", e.getMessage());
|
||||
//throw new RuntimeException("类信息扫描工具初始化失败", e);
|
||||
|
||||
@@ -18,6 +18,9 @@ import java.util.*;
|
||||
import java.util.jar.JarEntry;
|
||||
import java.util.jar.JarFile;
|
||||
|
||||
import static com.zt.plat.framework.common.exception.util.ServiceExceptionUtil.exception;
|
||||
import static com.zt.plat.module.tmpltp.enums.ErrorCodeConstants.*;
|
||||
|
||||
/**
|
||||
* 类信息扫描工具类,用于通过简单类名获取类字段及Schema注解信息
|
||||
* 增强版:支持通配符包路径扫描
|
||||
@@ -34,28 +37,30 @@ public class ClassInfoScanner {
|
||||
* 初始化扫描指定包路径下的所有类
|
||||
* @param basePackages 基础包路径,支持逗号分隔多个包,支持*和**通配符
|
||||
*/
|
||||
public static synchronized void init(String basePackages) {
|
||||
public static synchronized void init(String[] basePackages) {
|
||||
if (initialized) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (basePackages == null || basePackages.trim().isEmpty()) {
|
||||
if (basePackages == null || basePackages.length==0) {
|
||||
throw new IllegalArgumentException("基础包路径不能为空");
|
||||
}
|
||||
|
||||
String[] packages = basePackages.split("[,;\\s]+");
|
||||
for (String pkg : packages) {
|
||||
String trimmedPkg = pkg.trim();
|
||||
if (trimmedPkg.contains("**") || trimmedPkg.contains("*")) {
|
||||
// 处理通配符包路径
|
||||
scanPackageWithWildcard(trimmedPkg);
|
||||
log.info("已扫描通配符包路径:{}", trimmedPkg);
|
||||
} else {
|
||||
// 处理普通包路径
|
||||
scanPackage(trimmedPkg);
|
||||
log.info("已扫描普通包路径:{}", trimmedPkg);
|
||||
Arrays.stream(basePackages).forEach(basePackage -> {
|
||||
String[] packages = basePackage.split("[,;\\s]+");
|
||||
for (String pkg : packages) {
|
||||
String trimmedPkg = pkg.trim();
|
||||
if (trimmedPkg.contains("**") || trimmedPkg.contains("*")) {
|
||||
// 处理通配符包路径
|
||||
scanPackageWithWildcard(trimmedPkg);
|
||||
log.info("已扫描通配符包路径:{}", trimmedPkg);
|
||||
} else {
|
||||
// 处理普通包路径
|
||||
scanPackage(trimmedPkg);
|
||||
log.info("已扫描普通包路径:{}", trimmedPkg);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
initialized = true;
|
||||
}
|
||||
@@ -218,21 +223,20 @@ public class ClassInfoScanner {
|
||||
*/
|
||||
public static List<Map<String, Object>> getClassFieldInfo(String simpleClassName) {
|
||||
if (!initialized) {
|
||||
throw new IllegalStateException("工具类未初始化,请先调用init方法");
|
||||
throw exception(UTIL_NOT_INIT);
|
||||
}
|
||||
|
||||
if (simpleClassName == null || !simpleClassName.endsWith("VO")) {
|
||||
throw new IllegalArgumentException("参数为空或格式错误");
|
||||
throw exception(PARAMS_IS_NULL_OR_ERR);
|
||||
}
|
||||
|
||||
Class<?> targetClass = CLASS_CACHE.get(simpleClassName);
|
||||
if (targetClass == null) {
|
||||
throw new RuntimeException("未找到类: " + simpleClassName);
|
||||
throw exception(NOT_FOUND_CLASS);
|
||||
}
|
||||
|
||||
List<Map<String, Object>> resultList = new ArrayList<>();
|
||||
Field[] fields = targetClass.getDeclaredFields();
|
||||
// PreAuthorize preAuthorize = targetClass.getAnnotation(PreAuthorize.class);
|
||||
for (Field field : fields) {
|
||||
if (field.isAnnotationPresent(Schema.class)) {
|
||||
Schema schema = field.getAnnotation(Schema.class);
|
||||
@@ -241,6 +245,8 @@ public class ClassInfoScanner {
|
||||
fieldMap.put("label", schema.description().isEmpty() ?null:schema.description().split(";")[0]);
|
||||
fieldMap.put("required", schema.requiredMode() == Schema.RequiredMode.REQUIRED);
|
||||
fieldMap.put("example", schema.example());
|
||||
//获取字段的类型
|
||||
fieldMap.put("type", field.getType().getSimpleName());
|
||||
fieldMap.put("orgnType", "class");
|
||||
resultList.add(fieldMap);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user