初始化数据xml代码生成
This commit is contained in:
103
doc/模版.md
103
doc/模版.md
@@ -259,3 +259,106 @@ $operationList:{op |
|
||||
|
||||
|
||||
|
||||
## 模块数据
|
||||
|
||||
|
||||
|
||||
```xml
|
||||
group dbXml;
|
||||
|
||||
moduleTemplate(module) ::= <<
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<databaseChangeLog
|
||||
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
|
||||
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.24.xsd">
|
||||
|
||||
<changeSet id="insert-ms-fun-module-data" author="xinghe">
|
||||
<comment>初始化模块数据</comment>
|
||||
$module:{op |
|
||||
<insert tableName="ms_fun_module">
|
||||
<column name="id" value="$op.id$"/>
|
||||
<column name="module_name" value="$op.moduleCode$"/>
|
||||
</insert>
|
||||
}$
|
||||
</changeSet>
|
||||
|
||||
</databaseChangeLog>
|
||||
>>
|
||||
|
||||
itemTemplate(item) ::= <<
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<databaseChangeLog
|
||||
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
|
||||
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.24.xsd">
|
||||
|
||||
<changeSet id="insert-ms-fun-item-data" author="xinghe">
|
||||
<comment>初始化功能数据</comment>
|
||||
$item:{op |
|
||||
<insert tableName="ms_fun_item">
|
||||
<column name="id" value="$op.id$"/>
|
||||
<column name="module_id" value="$op.moduleId$"/>
|
||||
<column name="item_name" value="$op.itemName$"/>
|
||||
<column name="is_tenant" value="$op.isTenant$"/>
|
||||
<column name="describe" value="$op.describe$"/>
|
||||
</insert>
|
||||
}$
|
||||
</changeSet>
|
||||
|
||||
</databaseChangeLog>
|
||||
>>
|
||||
|
||||
|
||||
funTypeAnnotations ::= [
|
||||
"ADD": "0",
|
||||
"DELETE": "1",
|
||||
"EDIT": "2",
|
||||
"QUERY": "3",
|
||||
"UPLOAD": "4",
|
||||
"DOWNLOAD": "5",
|
||||
"IMPORT": "6",
|
||||
"EXPORT": "7",
|
||||
"PRINT": "8",
|
||||
"REGISTER": "9",
|
||||
"LOGIN": "10",
|
||||
"LOGOUT": "11",
|
||||
"SMS": "12",
|
||||
"EMAIL": "13",
|
||||
"WECHAT": "14",
|
||||
"OTHER": "99"
|
||||
default: "-1"
|
||||
]
|
||||
operationTemplate(operation) ::= <<
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<databaseChangeLog
|
||||
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
|
||||
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.24.xsd">
|
||||
|
||||
<changeSet id="insert-ms-fun-operation-data" author="xinghe">
|
||||
<comment>初始化操作数据</comment>
|
||||
$operation:{op |
|
||||
<insert tableName="ms_fun_item">
|
||||
<column name="id" value="$op.id$"/>
|
||||
<column name="module_id" value="$op.moduleId$"/>
|
||||
<column name="item_id" value="$op.itemId$"/>
|
||||
<column name="is_green_light" value="$op.isGreenLight$"/>
|
||||
<column name="fun_code" value="$op.operationCode$"/>
|
||||
<column name="fun_name" value="$op.funName$"/>
|
||||
<column name="fun_type" value="$funTypeAnnotations.(op.funType)$"/>
|
||||
<column name="usable_config" value="$op.usableConfigJson$"/>
|
||||
<column name="field_cofnig" value="$op.fieldConfigJson$"/>
|
||||
<column name="sort_order" value="$op.sortOrder$"/>
|
||||
<column name="describe" value="$op.describe$"/>
|
||||
</insert>
|
||||
}$
|
||||
</changeSet>
|
||||
|
||||
</databaseChangeLog>
|
||||
>>
|
||||
```
|
||||
|
||||
|
||||
@@ -29,6 +29,9 @@ public class CodeGen implements Serializable {
|
||||
@Schema(description = "功能ID")
|
||||
private Long itemId;
|
||||
|
||||
@Schema(description = "模块ID")
|
||||
private Long moduleId;
|
||||
|
||||
|
||||
@Hidden
|
||||
@AssertTrue(message = "当代码生成类型为 CONTROLLER 时,功能ID不能为空")
|
||||
@@ -39,4 +42,15 @@ public class CodeGen implements Serializable {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Hidden
|
||||
@AssertTrue(message = "当代码生成类型为 MODULE_DATA_INFO 时,模块ID不能为空")
|
||||
public boolean isModuleIdValid() {
|
||||
if (codeGenType == CodeGenType.MODULE_DATA_INFO) {
|
||||
return moduleId != null;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -14,7 +14,8 @@ import lombok.Getter;
|
||||
@Getter
|
||||
public enum CodeGenType {
|
||||
|
||||
CONTROLLER(0, "controller");
|
||||
CONTROLLER(0, "controller"),
|
||||
MODULE_DATA_INFO(1, "module_data_info");
|
||||
|
||||
private final int code;
|
||||
private final String desc;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.cczsa.xinghe.codegen.entity.domain.template;
|
||||
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.cczsa.xinghe.codegen.entity.FunOperationEntity;
|
||||
import com.cczsa.xinghe.codegen.util.StringUtils;
|
||||
import lombok.Data;
|
||||
@@ -42,6 +43,14 @@ public class FunOperationTemp extends FunOperationEntity {
|
||||
* 路径参数 url入参
|
||||
*/
|
||||
private String pathParamsUrl;
|
||||
/**
|
||||
* usableConfig 转成字符串
|
||||
*/
|
||||
private String usableConfigJson;
|
||||
/**
|
||||
* fieldConfig 转成字符串
|
||||
*/
|
||||
private String fieldConfigJson;
|
||||
|
||||
|
||||
public void info(){
|
||||
@@ -54,6 +63,8 @@ public class FunOperationTemp extends FunOperationEntity {
|
||||
pathParamsLong = "@PathVariable(\""+this.getPathParams()+"\") Long "+this.getPathParams();
|
||||
pathParamsUrl = "/{"+this.getPathParams()+"}";
|
||||
}
|
||||
usableConfigJson = JSONObject.toJSONString(this.getUsableConfig());
|
||||
fieldConfigJson = JSONObject.toJSONString(this.getFieldConfig()).replace("\"", "'");;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -24,7 +24,8 @@ public enum TemplateTypeEnum {
|
||||
REQUEST_PARAM(1, "请求参数"),
|
||||
RESPONSE_PARAM(2, "响应参数"),
|
||||
SERVICE(3, "服务接口"),
|
||||
SERVICE_IMPL(4, "服务实现");
|
||||
SERVICE_IMPL(4, "服务实现"),
|
||||
MODULE_DATA_INFO(5, "模块功能操作数据生成");
|
||||
|
||||
private final int code;
|
||||
private final String desc;
|
||||
|
||||
@@ -169,6 +169,7 @@ public class ControllerCodeCreate extends NodeComponent {
|
||||
case RESPONSE_PARAM -> "Res.java";
|
||||
case SERVICE -> "Service.java";
|
||||
case SERVICE_IMPL -> "ServiceImpl.java";
|
||||
default -> throw new IllegalArgumentException("Invalid template type: " + templateType);
|
||||
};
|
||||
}
|
||||
|
||||
@@ -176,7 +177,7 @@ public class ControllerCodeCreate extends NodeComponent {
|
||||
* 设置模板数据
|
||||
*
|
||||
* @param classTemplate 模板对象
|
||||
* @param basics
|
||||
* @param basics 基础信息
|
||||
*/
|
||||
private void setTemplateData(ST classTemplate, Map<String, Object> basics) {
|
||||
classTemplate.add("item", itemEntity);
|
||||
|
||||
@@ -0,0 +1,109 @@
|
||||
package com.cczsa.xinghe.codegen.service.flow.template;
|
||||
|
||||
import com.cczsa.xinghe.codegen.entity.FunItemEntity;
|
||||
import com.cczsa.xinghe.codegen.entity.FunModuleEntity;
|
||||
import com.cczsa.xinghe.codegen.entity.FunOperationEntity;
|
||||
import com.cczsa.xinghe.codegen.entity.domain.template.FunOperationTemp;
|
||||
import com.cczsa.xinghe.codegen.entity.enums.TemplateTypeEnum;
|
||||
import com.cczsa.xinghe.codegen.mapper.FunItemMapper;
|
||||
import com.cczsa.xinghe.codegen.mapper.FunModuleMapper;
|
||||
import com.cczsa.xinghe.codegen.mapper.FunOperationMapper;
|
||||
import com.cczsa.xinghe.codegen.service.TemplateService;
|
||||
import com.mybatisflex.core.query.QueryWrapper;
|
||||
import com.yomahub.liteflow.annotation.LiteflowComponent;
|
||||
import com.yomahub.liteflow.core.NodeComponent;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.stringtemplate.v4.ST;
|
||||
import org.stringtemplate.v4.STGroup;
|
||||
import org.stringtemplate.v4.STGroupString;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
/**
|
||||
* @author xia
|
||||
* @date 2026/1/20
|
||||
* @version 0.0.1
|
||||
*/
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
@LiteflowComponent(id = "moduleDataInfo", name = "模块权限相关数据生成")
|
||||
public class ModuleDataInfoCreate extends NodeComponent {
|
||||
|
||||
private final FunModuleMapper funModuleMapper;
|
||||
private final FunItemMapper funItemMapper;
|
||||
private final FunOperationMapper funOperationMapper;
|
||||
private final TemplateService templateService;
|
||||
|
||||
// 压缩流
|
||||
private ZipOutputStream zos;
|
||||
|
||||
|
||||
@Override
|
||||
public void process() throws Exception {
|
||||
Long moduleId = this.getContextBean(Long.class);
|
||||
|
||||
FunModuleEntity moduleEntity = funModuleMapper.selectOneById(moduleId);
|
||||
if (moduleEntity == null){
|
||||
return;
|
||||
}
|
||||
|
||||
// 获取 模版
|
||||
String template = templateService.getTemplateTypeContent(TemplateTypeEnum.MODULE_DATA_INFO);
|
||||
if (template == null){
|
||||
return;
|
||||
}
|
||||
|
||||
// 流
|
||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||
zos = new ZipOutputStream(outputStream);
|
||||
|
||||
// 生成
|
||||
STGroup group = new STGroupString("dbXml",template, '$', '$');
|
||||
ST moduleTemplate = group.getInstanceOf("moduleTemplate"); // 使用正确的模板名称
|
||||
// 模块数据生成
|
||||
List<FunModuleEntity> funModuleEntities = funModuleMapper.selectAll();
|
||||
// funModuleEntities 按 id 升序排序
|
||||
funModuleEntities.sort(Comparator.comparing(FunModuleEntity::getId));
|
||||
moduleTemplate.add("module", funModuleEntities);
|
||||
writeCodeToZip(moduleTemplate,"module");
|
||||
|
||||
ST itemTemplate = group.getInstanceOf("itemTemplate"); // 使用正确的模板名称
|
||||
// 功能数据生成
|
||||
List<FunItemEntity> funItemEntities = funItemMapper.selectAll();
|
||||
funItemEntities.sort(Comparator.comparing(FunItemEntity::getId));
|
||||
itemTemplate.add("item", funItemEntities);
|
||||
writeCodeToZip(itemTemplate,"item");
|
||||
|
||||
ST operationTemplate = group.getInstanceOf("operationTemplate"); // 使用正确的模板名称
|
||||
// 操作数据生成
|
||||
List<FunOperationTemp> funOperationEntities = funOperationMapper.selectListByQueryAs(QueryWrapper.create(),FunOperationTemp.class);
|
||||
funOperationEntities.sort(Comparator.comparing(FunOperationEntity::getId));
|
||||
funOperationEntities.forEach(FunOperationTemp::info);
|
||||
operationTemplate.add("operation", funOperationEntities);
|
||||
writeCodeToZip(operationTemplate,"operation");
|
||||
|
||||
// 确保 ZIP 流结束
|
||||
zos.finish();
|
||||
// 返回数据
|
||||
this.getSlot().setResponseData(outputStream.toByteArray());
|
||||
}
|
||||
|
||||
private void writeCodeToZip(ST st,String fileName) throws IOException {
|
||||
// 生成代码
|
||||
String result = st.render();
|
||||
// insert-ms-fun-operation-data.xml
|
||||
ZipEntry entry = new ZipEntry("insert-"+ fileName +"-data.xml");
|
||||
zos.putNextEntry(entry);
|
||||
zos.write(result.getBytes(StandardCharsets.UTF_8));
|
||||
zos.closeEntry();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.cczsa.xinghe.codegen.service.impl;
|
||||
|
||||
import com.cczsa.xinghe.codegen.entity.FunItemEntity;
|
||||
import com.cczsa.xinghe.codegen.entity.FunModuleEntity;
|
||||
import com.cczsa.xinghe.codegen.entity.domain.template.CodeGen;
|
||||
import com.cczsa.xinghe.codegen.entity.domain.template.CodeGenType;
|
||||
import com.cczsa.xinghe.codegen.mapper.FunItemMapper;
|
||||
@@ -33,12 +34,26 @@ public class CodeGenServiceImpl implements CodeGenService {
|
||||
@Override
|
||||
public byte[] generateCodeZip(CodeGen req) {
|
||||
|
||||
Long id = null;
|
||||
if(req.getCodeGenType() == CodeGenType.CONTROLLER){
|
||||
FunItemEntity funItemEntity = funItemMapper.selectOneById(req.getItemId());
|
||||
if (funItemEntity == null) {
|
||||
return null;
|
||||
}
|
||||
id = req.getItemId();
|
||||
}
|
||||
|
||||
FunItemEntity funItemEntity = funItemMapper.selectOneById(req.getItemId());
|
||||
if(req.getCodeGenType() == CodeGenType.CONTROLLER && funItemEntity != null){
|
||||
LiteflowResponse response = flowExecutor.execute2Resp("controller",null,req.getItemId());
|
||||
if(req.getCodeGenType() == CodeGenType.MODULE_DATA_INFO){
|
||||
FunModuleEntity funModuleEntity = funModuleMapper.selectOneById(req.getModuleId());
|
||||
if (funModuleEntity == null) {
|
||||
return null;
|
||||
}
|
||||
id = req.getModuleId();
|
||||
}
|
||||
|
||||
if(id != null){
|
||||
LiteflowResponse response = flowExecutor.execute2Resp(req.getCodeGenType().getDesc(),null,id);
|
||||
log.info("执行结果:{}",response);
|
||||
|
||||
if (response.isSuccess()) {
|
||||
return (byte[]) response.getSlot().getResponseData();
|
||||
}
|
||||
|
||||
@@ -1,7 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<flow>
|
||||
<!-- 主流程:用户下单 -->
|
||||
<!-- 主流程:controller 代码生成 -->
|
||||
<chain name="controller">
|
||||
THEN(controllerCodeCreate);
|
||||
</chain>
|
||||
|
||||
<!-- 主流程:数据初始化 -->
|
||||
<chain name="module_data_info">
|
||||
THEN(moduleDataInfo);
|
||||
</chain>
|
||||
|
||||
</flow>
|
||||
Reference in New Issue
Block a user