✨ Introducing new features. 代码生成的支持最新的主子表代码生成

This commit is contained in:
lbw committed 2023-06-20 16:00:22 +08:00
1 parent 7976d4401c
commit 64630b1c7e
14 files changed
+233 -75

No files matched your search

@@ -123,6 +123,16 @@ public class GenTableController {
return tableService.list(Wrappers.query(table));
}
@GetMapping("/list/{dsName}")
public R listTable(@PathVariable("dsName") String dsName) {
return R.ok(tableService.queryDsAllTable(dsName));
}
@GetMapping("/column/{dsName}/{tableName}")
public R column(@PathVariable("dsName") String dsName, @PathVariable String tableName) {
return R.ok(tableService.queryColumn(dsName, tableName));
}
/**
* 获取表信息
* @param dsName 数据源
@@ -157,12 +157,33 @@ public class GenTable extends Model<GenTable> {
*/
private Long style;
/**
* 子表名称
*/
private String childTableName;
/**
* 主表关联键
*/
private String mainField;
/**
* 子表关联键
*/
private String childField;
/**
* 字段列表
*/
@TableField(exist = false)
private List<GenTableColumnEntity> fieldList;
/**
* 子表字段列表
*/
@TableField(exist = false)
private List<GenTableColumnEntity> childFieldList;
/**
* 代码风格(模版分组信息)
*/
@@ -36,6 +36,13 @@ import java.util.Map;
*/
public interface GeneratorMapper extends PigxBaseMapper<ColumnEntity> {
/**
* 查询全部的表
* @return
*/
@InterceptorIgnore(tenantLine = "true")
List<Map<String, Object>> queryTable();
/**
* 分页查询表格
* @param page 分页信息
@@ -22,6 +22,7 @@ import com.baomidou.mybatisplus.extension.service.IService;
import com.pig4cloud.pigx.codegen.entity.GenFormConf;
import java.util.List;
import java.util.Map;
/**
* 表单管理
@@ -36,6 +37,6 @@ public interface GenFormConfService extends IService<GenFormConf> {
* @param formInfo json
* @return 字段
*/
List<JSONObject> parse(String formInfo);
Map<String, List<JSONObject>> parse(String formInfo);
}
@@ -22,6 +22,7 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.pig4cloud.pigx.codegen.entity.GenTable;
import java.util.List;
import java.util.Map;
/**
@@ -37,10 +38,14 @@ public interface GenTableService extends IService<GenTable> {
* @param path 配置文件路径
* @return
*/
Map<String, Object> getGeneratorConfig(String path);
Map<String, Object> getGeneratorConfig();
IPage list(Page<GenTable> page, GenTable table);
GenTable queryOrBuildTable(String dsName, String tableName);
List<Map<String, Object>> queryDsAllTable(String dsName);
List<Map<String, String>> queryColumn(String dsName, String tableName);
}
@@ -16,17 +16,19 @@
*/
package com.pig4cloud.pigx.codegen.service.impl;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.pigx.codegen.entity.GenFormConf;
import com.pig4cloud.pigx.codegen.mapper.GenFormConfMapper;
import com.pig4cloud.pigx.codegen.service.GenFormConfService;
import com.pig4cloud.pigx.codegen.util.VFormConfigConsts;
import com.pig4cloud.pigx.codegen.util.VFormTypeEnum;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.*;
/**
* 表单管理
@@ -43,25 +45,60 @@ public class GenFormConfServiceImpl extends ServiceImpl<GenFormConfMapper, GenFo
* @return 字段列表
*/
@Override
public List<JSONObject> parse(String formInfo) {
public Map<String, List<JSONObject>> parse(String formInfo) {
JSONObject jsonObject = JSONUtil.parseObj(formInfo);
List<JSONObject> objectList = new ArrayList<>();
// 使用 Lambda 表达式简化代码
Optional.ofNullable(jsonObject.getJSONArray("widgetList"))//
.ifPresent(widgetList -> widgetList.forEach(obj -> parseWidgetList((JSONObject) obj, objectList)));
return objectList;
Map<String, List<JSONObject>> resultMap = new LinkedHashMap<>();
parseForm(jsonObject, resultMap);
return resultMap;
}
/**
* 解析部件列表
* @param jsonObject 部件 json 对象
* @param objectList 对象列表
* 解析 form config
* @param json 表单配置
* @param resultMap 返回结果
*/
private void parseWidgetList(JSONObject jsonObject, List<JSONObject> objectList) {
Optional.ofNullable(jsonObject.getJSONArray("cols"))//
.filter(cols -> !cols.isEmpty())//
.ifPresent(cols -> cols.forEach(colObj -> parseWidgetList((JSONObject) colObj, objectList)));
objectList.add(jsonObject);
private void parseForm(JSONObject json, Map<String, List<JSONObject>> resultMap) {
JSONArray widgetList = json.getJSONArray(VFormConfigConsts.widgetList);
String groupKey = json.getStr(VFormConfigConsts.groupKey, RandomUtil.randomString(5));
widgetList.stream().map(o -> (JSONObject) o).forEach(jsonObject -> {
String type = jsonObject.getStr(VFormConfigConsts.type);
if (VFormTypeEnum.GRID.getType().equals(type)) {
parseGrid(jsonObject, resultMap);
}
else if (VFormTypeEnum.GRID_COL.getType().equals(type)) {
parseForm(jsonObject, resultMap);
}
else {
int span = Optional.ofNullable(json.getJSONObject(VFormConfigConsts.options))
.map(options -> options.getInt(VFormConfigConsts.span, 24)).orElse(24);
jsonObject.set(VFormConfigConsts.span, span);
List<JSONObject> jsonObjects = resultMap.computeIfAbsent(groupKey, k -> new ArrayList<>());
jsonObjects.add(jsonObject);
}
});
}
/**
* 解析 grid 容器
* @param jsonObject 入参
* @param resultMap 结果
*/
private void parseGrid(JSONObject jsonObject, Map<String, List<JSONObject>> resultMap) {
JSONArray cols = jsonObject.getJSONArray(VFormConfigConsts.cols);
String key = jsonObject.getStr(VFormConfigConsts.key);
cols.stream().map(o -> (JSONObject) o).forEach(col -> {
String colType = col.getStr(VFormConfigConsts.type);
if (VFormTypeEnum.GRID_COL.getType().equals(colType)) {
col.set(VFormConfigConsts.groupKey, key);
parseForm(col, resultMap);
}
if (VFormTypeEnum.GRID.getType().equals(colType)) {
parseGrid(col, resultMap);
}
});
}
}
@@ -73,12 +73,26 @@ public class GenTableServiceImpl extends ServiceImpl<GenTableMapper, GenTable> i
* @return
*/
@Override
public Map<String, Object> getGeneratorConfig(String path) {
ClassPathResource classPathResource = new ClassPathResource(path);
public Map<String, Object> getGeneratorConfig() {
ClassPathResource classPathResource = new ClassPathResource(CONFIG_PATH);
JSONObject jsonObject = JSONUtil.parseObj(IoUtil.readUtf8(classPathResource.getStream()));
return jsonObject.getRaw();
}
@Override
public List<Map<String, Object>> queryDsAllTable(String dsName) {
GeneratorMapper mapper = GenKit.getMapper(dsName);
// 手动切换数据源
DynamicDataSourceContextHolder.push(dsName);
return mapper.queryTable();
}
@Override
public List<Map<String, String>> queryColumn(String dsName, String tableName) {
GeneratorMapper mapper = GenKit.getMapper(dsName);
return mapper.selectMapTableColumn(tableName, dsName);
}
@Override
public IPage list(Page<GenTable> page, GenTable table) {
GeneratorMapper mapper = GenKit.getMapper(table.getDsName());
@@ -126,7 +140,7 @@ public class GenTableServiceImpl extends ServiceImpl<GenTableMapper, GenTable> i
Map<String, String> queryTable = mapper.queryTable(tableName, dsName);
// 获取默认表配置信息 ()
Map<String, Object> generatorConfig = getGeneratorConfig(CONFIG_PATH);
Map<String, Object> generatorConfig = getGeneratorConfig();
JSONObject project = (JSONObject) generatorConfig.get("project");
JSONObject developer = (JSONObject) generatorConfig.get("developer");
@@ -21,6 +21,7 @@ import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.text.NamingCase;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.pig4cloud.pigx.codegen.entity.GenFormConf;
@@ -81,10 +82,17 @@ public class GeneratorServiceImpl implements GeneratorService {
GroupVo groupVo = genGroupService.getGroupVoById(style);
List<GenTemplateEntity> templateList = groupVo.getTemplateList();
Map<String, Object> generatorConfig = tableService.getGeneratorConfig();
JSONObject project = (JSONObject) generatorConfig.get("project");
String frontendPath = project.getStr("frontendPath");
String backendPath = project.getStr("backendPath");
for (GenTemplateEntity template : templateList) {
String templateCode = template.getTemplateCode();
String generatorPath = template.getGeneratorPath();
dataModel.put("frontendPath", frontendPath);
dataModel.put("backendPath", backendPath);
String content = VelocityKit.renderStr(templateCode, dataModel);
String path = VelocityKit.renderStr(generatorPath, dataModel);
@@ -113,10 +121,18 @@ public class GeneratorServiceImpl implements GeneratorService {
// 获取模板列表,Lambda 表达式简化代码
List<GenTemplateEntity> templateList = genGroupService.getGroupVoById(style).getTemplateList();
Map<String, Object> generatorConfig = tableService.getGeneratorConfig();
JSONObject project = (JSONObject) generatorConfig.get("project");
String frontendPath = project.getStr("frontendPath");
String backendPath = project.getStr("backendPath");
return templateList.stream().map(template -> {
String templateCode = template.getTemplateCode();
String generatorPath = template.getGeneratorPath();
// 预览模式下, 使用相对路径展示
dataModel.put("frontendPath", frontendPath);
dataModel.put("backendPath", backendPath);
String content = VelocityKit.renderStr(templateCode, dataModel);
String path = VelocityKit.renderStr(generatorPath, dataModel);
@@ -194,8 +210,10 @@ public class GeneratorServiceImpl implements GeneratorService {
Map<String, Object> dataModel = getDataModel(genTable.getId());
// 解析组件列表
List<JSONObject> widgetList = formConfService.parse(formConf.getFormInfo());
dataModel.put("widgetList", widgetList);
Map<String, List<JSONObject>> resultMap = formConfService.parse(formConf.getFormInfo());
// 遍历 widgetList
dataModel.put("resultMap", resultMap);
// 获取模板信息
GenTemplateEntity genTemplateEntity = Optional
@@ -219,6 +237,7 @@ public class GeneratorServiceImpl implements GeneratorService {
.eq(GenTableColumnEntity::getDsName, table.getDsName())
.eq(GenTableColumnEntity::getTableName, table.getTableName()).orderByAsc(GenTableColumnEntity::getSort)
.list();
table.setFieldList(fieldList);
// 创建数据模型对象
@@ -248,8 +267,24 @@ public class GeneratorServiceImpl implements GeneratorService {
dataModel.put("className", StrUtil.lowerFirst(table.getClassName()));
dataModel.put("ClassName", table.getClassName());
dataModel.put("fieldList", table.getFieldList());
dataModel.put("backendPath", table.getBackendPath());
dataModel.put("frontendPath", table.getFrontendPath());
// 设置子表
String childTableName = table.getChildTableName();
if (StrUtil.isNotBlank(childTableName)) {
List<GenTableColumnEntity> childFieldList = columnService.lambdaQuery()
.eq(GenTableColumnEntity::getDsName, table.getDsName())
.eq(GenTableColumnEntity::getTableName, table.getChildTableName()).list();
dataModel.put("childFieldList", childFieldList);
dataModel.put("childTableName", childTableName);
dataModel.put("mainField", NamingCase.toCamelCase(table.getMainField()));
dataModel.put("childField", NamingCase.toCamelCase(table.getChildField()));
dataModel.put("ChildClassName", NamingCase.toPascalCase(childTableName));
dataModel.put("childClassName", StrUtil.lowerFirst(NamingCase.toPascalCase(childTableName)));
}
return dataModel;
}
@@ -22,7 +22,7 @@ import java.util.Map;
public class GenKit {
/**
* 获取功能名
* 获取功能名 sys_a_b sysAb
* @param tableName 表名
* @return 功能名
*/
@@ -19,6 +19,10 @@ public class NamingCaseTool {
return String.format("get%s", NamingCase.toPascalCase(in));
}
public static String setProperty(String in) {
return String.format("set%s", NamingCase.toPascalCase(in));
}
/**
* 首字母大写
* @param in 字段
@@ -0,0 +1,25 @@
package com.pig4cloud.pigx.codegen.util;
/**
* vform 的通用变量
*
* @author lengleng
* @date 2023/6/5
*/
public interface VFormConfigConsts {
String widgetList = "widgetList";
String groupKey = "groupKey";
String type = "type";
String options = "options";
String span = "span";
String cols = "cols";
String key = "key";
}
@@ -0,0 +1,22 @@
package com.pig4cloud.pigx.codegen.util;
import lombok.AllArgsConstructor;
import lombok.Getter;
/**
* vfrom 字段类型
*
* @author lengleng
* @date 2023/6/5
*/
@Getter
@AllArgsConstructor
public enum VFormTypeEnum {
GRID("grid"),
GRID_COL("grid-col");
private final String type;
}