Files
youlai-boot/src/main/java/com/youlai/boot/codegen/controller/CodegenController.java
T
Ray.Hao fcd8381bd1 refactor(codegen): 收敛命名转换并统一模板注释
- 新增 NamingUtils,目录、路由与模板变量共用同一套命名规则
- 字段特性标记合并为集合,模板变量按常量循环生成
- 删除模板未引用的 entityComment
- 权限前缀按模块映射配置
- 模板方法注释统一为多行
2026-11-06 17:56:33 +08:00

128 lines
5.4 KiB
Java

package com.youlai.boot.codegen.controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youlai.boot.common.exception.BusinessException;
import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import com.youlai.boot.codegen.property.CodegenProperties;
import com.youlai.boot.common.enums.ActionTypeEnum;
import com.youlai.boot.common.enums.LogModuleEnum;
import com.youlai.boot.codegen.service.CodegenService;
import com.youlai.boot.codegen.model.form.AiFillConfigForm;
import com.youlai.boot.codegen.model.form.GenConfigForm;
import com.youlai.boot.codegen.model.query.TableQuery;
import com.youlai.boot.codegen.model.vo.CodegenPreviewVO;
import com.youlai.boot.codegen.model.vo.TablePageVO;
import com.youlai.boot.common.annotation.Log;
import com.youlai.boot.codegen.service.GenTableService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.ServletOutputStream;
import jakarta.validation.Valid;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
/**
* 代码生成器控制层
*
* @author Ray
* @since 2.10.0
*/
@Tag(name = "11.代码生成")
@RestController
@RequestMapping("/api/v1/codegen")
@RequiredArgsConstructor
@Slf4j
public class CodegenController {
private final CodegenService codegenService;
private final GenTableService genTableService;
private final CodegenProperties codegenProperties;
@Operation(summary = "获取数据表分页列表")
@GetMapping("/table")
public PageResult<TablePageVO> getTablePage(
@Valid TableQuery queryParams
) {
Page<TablePageVO> result = codegenService.getTablePage(queryParams);
return PageResult.success(result);
}
@Operation(summary = "获取代码生成配置")
@GetMapping("/{tableName}/config")
public Result<GenConfigForm> getGenTableFormData(
@Parameter(description = "表名", example = "sys_user") @PathVariable String tableName
) {
GenConfigForm formData = genTableService.getGenTableFormData(tableName);
return Result.success(formData);
}
@Operation(summary = "保存代码生成配置")
@PostMapping("/{tableName}/config")
@Log(module = LogModuleEnum.CODEGEN, value = ActionTypeEnum.UPDATE)
public Result<Void> saveGenConfig(@RequestBody @Valid GenConfigForm formData) {
genTableService.saveGenConfig(formData);
return Result.success();
}
@Operation(summary = "删除代码生成配置")
@DeleteMapping("/{tableName}/config")
public Result<Void> deleteGenConfig(
@Parameter(description = "表名", example = "sys_user") @PathVariable String tableName
) {
genTableService.deleteGenConfig(tableName);
return Result.success();
}
@Operation(summary = "获取预览生成代码")
@GetMapping("/{tableName}/preview")
public Result<List<CodegenPreviewVO>> getTablePreviewData(@PathVariable String tableName,
@RequestParam(value = "pageType", required = false, defaultValue = "crud") String pageType,
@RequestParam(value = "type", required = false, defaultValue = "ts") String type) {
List<CodegenPreviewVO> list = codegenService.getCodegenPreviewData(tableName, pageType, type);
return Result.success(list);
}
@Operation(summary = "下载代码")
@GetMapping("/{tableName}/download")
@Log(module = LogModuleEnum.CODEGEN, value = ActionTypeEnum.DOWNLOAD)
public void downloadZip(HttpServletResponse response, @PathVariable String tableName,
@RequestParam(value = "pageType", required = false, defaultValue = "crud") String pageType,
@RequestParam(value = "type", required = false, defaultValue = "ts") String type) {
String[] tableNames = tableName.split(",");
byte[] data = codegenService.downloadCode(tableNames, pageType, type);
response.reset();
response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(codegenProperties.getDownloadFileName(), StandardCharsets.UTF_8));
response.setContentType("application/octet-stream; charset=UTF-8");
try (ServletOutputStream outputStream = response.getOutputStream()) {
outputStream.write(data);
outputStream.flush();
} catch (IOException e) {
log.error("Error while writing the zip file1 to response", e);
throw new BusinessException("代码生成压缩包响应写入失败", e);
}
}
// AI 辅助:未开启 AI 时调用返回业务异常
@Operation(summary = "AI 填充代码生成配置")
@PostMapping("/{tableName}/ai-config")
public Result<GenConfigForm> fillConfig(
@Parameter(description = "表名", example = "sys_user") @PathVariable String tableName,
@RequestBody(required = false) @Valid AiFillConfigForm form
) {
String requirement = form == null ? null : form.getRequirement();
return Result.success(genTableService.aiFillConfig(tableName, requirement));
}
}