新增:ai编码接口
This commit is contained in:
5
pom.xml
5
pom.xml
@@ -29,6 +29,11 @@
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.google.googlejavaformat</groupId>
|
||||
<artifactId>google-java-format</artifactId>
|
||||
<version>1.15.0</version>
|
||||
</dependency>
|
||||
<!-- springframework start -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.cool.modules.base.controller.admin;
|
||||
|
||||
|
||||
import cn.hutool.json.JSONArray;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import com.cool.core.annotation.CoolRestController;
|
||||
import com.cool.core.annotation.TokenIgnore;
|
||||
import com.cool.core.exception.CoolPreconditions;
|
||||
import com.cool.core.request.R;
|
||||
import com.cool.modules.base.dto.sys.CodeContentDto;
|
||||
import com.cool.modules.base.service.sys.BaseCodingService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestAttribute;
|
||||
|
||||
/**
|
||||
* ai 编码
|
||||
*/
|
||||
@CoolRestController
|
||||
@RequiredArgsConstructor
|
||||
public class AdminBaseCodingController {
|
||||
|
||||
private final BaseCodingService baseCodingService;
|
||||
|
||||
@TokenIgnore
|
||||
@Operation(summary = "获取模块目录结构", description = "获取模块目录结构")
|
||||
@GetMapping("/getModuleTree")
|
||||
public R getModuleTree() {
|
||||
return R.ok(baseCodingService.getModuleTree());
|
||||
}
|
||||
|
||||
@TokenIgnore
|
||||
@Operation(summary = "创建代码", description = "创建代码")
|
||||
@PostMapping("/createCode")
|
||||
public R createCode(@RequestAttribute JSONObject requestParams) {
|
||||
JSONArray codes = requestParams.get("codes", JSONArray.class);
|
||||
CoolPreconditions.checkEmpty(codes);
|
||||
this.baseCodingService.createCode(codes.toList(CodeContentDto.class));
|
||||
return R.ok();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.cool.modules.base.dto.sys;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class CodeContentDto {
|
||||
private String path;
|
||||
private String content;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.cool.modules.base.service.sys;
|
||||
|
||||
import com.cool.modules.base.dto.sys.CodeContentDto;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface BaseCodingService {
|
||||
List<String> getModuleTree();
|
||||
|
||||
void createCode(List<CodeContentDto> codes);
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
package com.cool.modules.base.service.sys.impl;
|
||||
|
||||
import com.cool.core.exception.CoolPreconditions;
|
||||
import com.cool.modules.base.dto.sys.CodeContentDto;
|
||||
import com.cool.modules.base.service.sys.BaseCodingService;
|
||||
import com.google.googlejavaformat.java.Formatter;
|
||||
import com.google.googlejavaformat.java.FormatterException;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
public class BaseCodingServiceImpl implements BaseCodingService {
|
||||
|
||||
@Value("${spring.profiles.active}")
|
||||
private String env;
|
||||
|
||||
// 获取模块目录结构
|
||||
public List<String> getModuleTree() {
|
||||
if (!"local".equals(env)) {
|
||||
return List.of(); // 返回空列表
|
||||
}
|
||||
|
||||
// 获取基础目录
|
||||
Path modulesPath = getModulesPath();
|
||||
// 获取模块文件夹
|
||||
try {
|
||||
return Files.list(modulesPath)
|
||||
.filter(path -> !path.getFileName().toString().equals(".DS_Store"))
|
||||
.map(path -> path.getFileName().toString())
|
||||
.collect(Collectors.toList());
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private static Path getModulesPath() {
|
||||
String moduleDir = System.getProperty("user.dir"); // 可通过其他方式获取应用目录
|
||||
String packageName = BaseCodingServiceImpl.class.getPackageName();
|
||||
String modulesParentPath = packageName.split("modules")[0].replace(".", File.separator);
|
||||
return Paths.get(moduleDir, "src", "main", "java", modulesParentPath, "modules");
|
||||
}
|
||||
|
||||
// 创建代码文件
|
||||
public void createCode(List<CodeContentDto> codes) {
|
||||
if (!"local".equals(env)) {
|
||||
throw new IllegalArgumentException("只能在开发环境下创建代码");
|
||||
}
|
||||
Path modulesPath = getModulesPath();
|
||||
String absolutePathStr = modulesPath.toAbsolutePath().toString();
|
||||
try {
|
||||
for (CodeContentDto code : codes) {
|
||||
// 格式化代码内容
|
||||
String formattedContent = formatContent(code.getContent());
|
||||
|
||||
Path filePath = Paths.get(absolutePathStr, code.getPath().replace("src/modules", ""));
|
||||
Path dirPath = filePath.getParent();
|
||||
// 确保目录存在
|
||||
if (!Files.exists(dirPath)) {
|
||||
Files.createDirectories(dirPath);
|
||||
}
|
||||
// 写入文件
|
||||
try (FileWriter writer = new FileWriter(filePath.toFile())) {
|
||||
writer.write(formattedContent);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
CoolPreconditions.alwaysThrow("生成代码失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
// 格式化代码内容
|
||||
public String formatContent(String content) {
|
||||
Formatter formatter = new Formatter();
|
||||
try {
|
||||
// return formatter.formatSource(content);
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
return content;
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package com.cool.modules.dict.controller.admin;
|
||||
import cn.hutool.core.lang.Dict;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import com.cool.core.annotation.CoolRestController;
|
||||
import com.cool.core.annotation.TokenIgnore;
|
||||
import com.cool.core.base.BaseController;
|
||||
import com.cool.core.request.R;
|
||||
import com.cool.modules.dict.entity.DictInfoEntity;
|
||||
@@ -12,6 +13,7 @@ import com.mybatisflex.core.query.QueryWrapper;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
|
||||
@@ -33,4 +35,11 @@ public class AdminDictInfoController extends BaseController<DictInfoService, Dic
|
||||
public R data(@RequestBody Dict body) {
|
||||
return R.ok(this.service.data(body.get("types", null)));
|
||||
}
|
||||
|
||||
@TokenIgnore
|
||||
@GetMapping("/types")
|
||||
@Operation(summary = "获得字典数据", description = "获得字典数据信息")
|
||||
public R types() {
|
||||
return R.ok(this.service.types());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,4 +16,6 @@ public interface DictInfoService extends BaseService<DictInfoEntity> {
|
||||
* @return
|
||||
*/
|
||||
Object data(List<String> types);
|
||||
|
||||
Object types();
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import static com.cool.modules.dict.entity.table.DictTypeEntityTableDef.DICT_TYP
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import cn.hutool.core.lang.Dict;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.util.ObjUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.cool.core.base.BaseServiceImpl;
|
||||
import com.cool.modules.dict.entity.DictInfoEntity;
|
||||
@@ -69,6 +70,29 @@ public class DictInfoServiceImpl extends BaseServiceImpl<DictInfoMapper, DictInf
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object types() {
|
||||
List<DictInfoEntity> infos = this.list();
|
||||
if (ObjUtil.isEmpty(infos)) {
|
||||
return infos;
|
||||
}
|
||||
List<Dict> datas = new ArrayList<>();
|
||||
infos.stream().forEach(d -> {
|
||||
Dict data = Dict.create();
|
||||
data.set("typeId", d.getTypeId());
|
||||
data.set("parentId", d.getParentId());
|
||||
data.set("name", d.getName());
|
||||
data.set("id", d.getId());
|
||||
data.set("value", StrUtil.isEmpty(d.getValue()) ? null : d.getValue());
|
||||
try {
|
||||
data.set("value", Integer.parseInt(d.getValue()));
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
datas.add(data);
|
||||
});
|
||||
return datas;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean delete(Long... ids) {
|
||||
super.delete(ids);
|
||||
|
||||
Reference in New Issue
Block a user