Skip to content

Commit fa21c6a

Browse files
committed
1. 代码生成全部流程
1 parent cbea33b commit fa21c6a

File tree

14 files changed

+221
-55
lines changed

14 files changed

+221
-55
lines changed

src/main/java/com/tml/otowbackend/controller/generate/SupportedController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,6 @@ public R<Map<String, List<String>>> getSupportedLanguages() {
4747
@GetMapping("/feature")
4848
@TokenRequire
4949
public R<List<FeaturePackage>> getSupportedFeaturePackages() {
50-
return featurePackageService.getSupportedFeaturePackages();
50+
return R.success("获取支持的功能包列表成功", featurePackageService.getSupportedFeaturePackages());
5151
}
5252
}

src/main/java/com/tml/otowbackend/engine/ai/model/QwModel.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class QwModel implements AIModel {
3131
public String generate(String content) {
3232
try {
3333
// 构造请求体
34-
HttpURLConnection connection = createConnection(content, true);
34+
HttpURLConnection connection = createConnection(content);
3535

3636
// 读取响应
3737
StringBuilder response = new StringBuilder();
@@ -52,14 +52,13 @@ public String generate(String content) {
5252
}
5353
}
5454

55-
private HttpURLConnection createConnection(String content, Boolean isStream) throws IOException {
55+
private HttpURLConnection createConnection(String content) throws IOException {
5656
RequestBody requestBody = new RequestBody(
5757
"qwen-plus",
5858
new Message[]{
5959
new Message("system", "You are an expert in java back-end programming"),
6060
new Message("user", content)
61-
},
62-
isStream
61+
}
6362
);
6463

6564
Gson gson = new Gson();
@@ -97,12 +96,10 @@ public Message(String role, String content) {
9796
static class RequestBody {
9897
String model;
9998
Message[] messages;
100-
Boolean stream;
10199

102-
public RequestBody(String model, Message[] messages, Boolean stream) {
100+
public RequestBody(String model, Message[] messages) {
103101
this.model = model;
104102
this.messages = messages;
105-
this.stream = stream;
106103
}
107104
}
108105
}

src/main/java/com/tml/otowbackend/engine/ai/operater/GenerateEntityOperation.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ public String generatePrompt(Map<String, Object> projectOutline) {
4444
"请根据以下项目大纲生成多个对应的实体类,请严格遵循实例格式,保证实体类能够正确运行,并且能够用ObjectMapper正确解析,并将结果放在 ### 和 ### 之间:\n" +
4545
"项目标题:%s\n" +
4646
"项目描述:%s\n" +
47-
"项目复杂程度:%s\n" +
48-
"%s\n" + // 功能包描述部分
47+
"项目复杂程度(复杂度仅供参考,可适当调整功能模块的数量和描述的细节):%s\n" +
48+
"%s\n" +
4949
"为每个实体类选择一个或多个适合的功能包ID(featureIds),并将其包含在生成结果中。\n" +
5050
"返回格式如下:\n" +
5151
"###\n" +
@@ -54,9 +54,9 @@ public String generatePrompt(Map<String, Object> projectOutline) {
5454
" \"className\": \"实体类名\",\n" +
5555
" \"cdesc\": \"实体类中文名\",\n" +
5656
" \"fields\": [\n" +
57-
" {\"fname\": \"字段名\", \"ftype\": \"字段类型例如:String/Integer/Boolean,注意:必须使用包装类\", \"fdesc\": \"字段中文名\"}\n" +
57+
" {\"fname\": \"字段名\", \"ftype\": \"字段类型(例如:String/Integer/Boolean,注意:必须使用包装类)\", \"fdesc\": \"字段中文名\"}\n" +
5858
" ],\n" +
59-
" \"featureIds\": [\"功能包ID1\", \"功能包ID2\"]\n" +
59+
" \"featureIds\": [\"1001\", \"1002\"]\n" +
6060
" }\n" +
6161
"]\n" +
6262
"###",

src/main/java/com/tml/otowbackend/engine/generator/template/java/InitTemplate.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import java.util.LinkedList;
2323
import java.util.List;
2424

25-
import static com.tml.otowbackend.engine.generator.template.meta.MetalUtils.getDescribe;
25+
import static com.tml.otowbackend.engine.generator.utils.MetalUtils.getDescribe;
2626

2727
@NoArgsConstructor
2828
public class InitTemplate {

src/main/java/com/tml/otowbackend/engine/generator/template/java/model/EntityTemplate.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import com.baomidou.mybatisplus.annotation.TableName;
44
import com.tml.otowbackend.engine.generator.template.java.ClassTemplate;
55
import com.tml.otowbackend.engine.generator.template.meta.MetaAnnotation;
6-
import com.tml.otowbackend.engine.generator.template.meta.MetalUtils;
6+
import com.tml.otowbackend.engine.generator.utils.MetalUtils;
77

88
import static com.tml.otowbackend.constants.TemplateConstant.ENTITY_ANNOTATION;
99

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
package com.tml.otowbackend.engine.generator.template.meta;
1+
package com.tml.otowbackend.engine.generator.utils;
22

3+
import com.tml.otowbackend.engine.generator.template.meta.MetaAnnotation;
34
import com.tml.otowbackend.engine.sql.annotation.Describe;
45
import org.apache.commons.lang.StringUtils;
56

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.tml.otowbackend.engine.generator.utils;
2+
3+
import java.util.Date;
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
7+
/**
8+
* 类型转换工具类
9+
* 支持将传入的字符串类型(如 "String"、"int"、"LocalDateTime" 等)转换为对应的包装类 Class 对象
10+
*/
11+
public class TypeConverter {
12+
13+
// 定义一个映射关系,将字符串类型名称与对应的包装类 Class 进行映射
14+
private static final Map<String, Class<?>> TYPE_MAPPING = new HashMap<>();
15+
16+
static {
17+
// 基本类型及其对应的包装类型
18+
TYPE_MAPPING.put("boolean", Boolean.class);
19+
TYPE_MAPPING.put("byte", Byte.class);
20+
TYPE_MAPPING.put("char", Character.class);
21+
TYPE_MAPPING.put("short", Short.class);
22+
TYPE_MAPPING.put("int", Integer.class);
23+
TYPE_MAPPING.put("long", Long.class);
24+
TYPE_MAPPING.put("float", Float.class);
25+
TYPE_MAPPING.put("double", Double.class);
26+
27+
// 包装类型(数据库友好类型)
28+
TYPE_MAPPING.put("Boolean", Boolean.class);
29+
TYPE_MAPPING.put("Byte", Byte.class);
30+
TYPE_MAPPING.put("Character", Character.class);
31+
TYPE_MAPPING.put("Short", Short.class);
32+
TYPE_MAPPING.put("Integer", Integer.class);
33+
TYPE_MAPPING.put("Long", Long.class);
34+
TYPE_MAPPING.put("Float", Float.class);
35+
TYPE_MAPPING.put("Double", Double.class);
36+
37+
// 常见类型
38+
TYPE_MAPPING.put("String", String.class);
39+
TYPE_MAPPING.put("LocalDateTime", Date.class); // 映射为数据库友好的 Date.class
40+
TYPE_MAPPING.put("LocalDate", Date.class); // 映射为数据库友好的 Date.class
41+
TYPE_MAPPING.put("Date", Date.class);
42+
}
43+
44+
/**
45+
* 将字符串类型名称转换为对应的包装类 Class 对象
46+
*
47+
* @param typeName 字符串类型名称(如 "int"、"String"、"LocalDateTime"、"java.util.Date" 等)
48+
* @return 对应的包装类 Class 对象,如果未匹配到则返回 null
49+
*/
50+
public static Class<?> toDatabaseFriendlyType(String typeName) {
51+
if (typeName == null || typeName.trim().isEmpty()) {
52+
throw new IllegalArgumentException("类型名称不能为空");
53+
}
54+
String simpleTypeName = extractSimpleName(typeName);
55+
return TYPE_MAPPING.get(simpleTypeName);
56+
}
57+
58+
/**
59+
* 提取简单类名(去掉包路径),如 "java.util.Date" -> "Date"
60+
*
61+
* @param fullTypeName 完整类名或简单类名
62+
* @return 简单类名
63+
*/
64+
private static String extractSimpleName(String fullTypeName) {
65+
if (fullTypeName.contains(".")) {
66+
return fullTypeName.substring(fullTypeName.lastIndexOf('.') + 1);
67+
}
68+
return fullTypeName;
69+
}
70+
}

src/main/java/com/tml/otowbackend/engine/tree/template/SpringBootTreeTemplate.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ public void master(String fileName, List<String> content) {
3232
addFile("/", fileName, content);
3333
}
3434

35+
public void example(String fileName, List<String> content) {
36+
addFile("example", fileName, content);
37+
}
3538

3639
public void controller(String fileName, List<String> content) {
3740
addFile("controller", fileName, content);
@@ -83,8 +86,11 @@ public void resources(String fileName, List<String> content) {
8386

8487
@Override
8588
public void buildFolderMappings() {
86-
folderMappings.put("resources", "/src/main/java/resources");
89+
folderMappings.put("resources", "/src/main/resources");
8790
folderMappings.put("/", "");
91+
92+
folderMappings.put("example", "/src/main/java/com/example");
93+
8894
List<NodeVO> rootNodes = virtualFileService.getChildrenNodes(treeId, "/src/main/java/com/example");
8995
// 遍历 rootNodes,逐一建立文件夹名称到 ID 的映射关系
9096
for (NodeVO node : rootNodes) {

src/main/java/com/tml/otowbackend/service/FeaturePackageService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public interface FeaturePackageService {
1212
*
1313
* @return 支持的功能包列表
1414
*/
15-
R<List<FeaturePackage>> getSupportedFeaturePackages();
15+
List<FeaturePackage> getSupportedFeaturePackages();
1616

1717
/**
1818
* 校验功能包列表是否合法

src/main/java/com/tml/otowbackend/service/Impl/IFeaturePackageService.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@ public class IFeaturePackageService implements FeaturePackageService {
2424
* @return 动态支持的功能包列表
2525
*/
2626
@Override
27-
public R<List<FeaturePackage>> getSupportedFeaturePackages() {
28-
List<FeaturePackage> supportedFeaturePackages = funcPackManager.getSupportedFeaturePackages();
29-
return R.success("获取支持的功能包列表成功", supportedFeaturePackages);
27+
public List<FeaturePackage> getSupportedFeaturePackages() {
28+
return funcPackManager.getSupportedFeaturePackages();
3029
}
3130

3231
/**

0 commit comments

Comments
 (0)