CodegenController.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package com.youlai.boot.module.codegen.controller;
  2. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  3. import com.youlai.boot.common.result.PageResult;
  4. import com.youlai.boot.common.result.Result;
  5. import com.youlai.boot.config.property.CodegenProperties;
  6. import com.youlai.boot.common.enums.LogModuleEnum;
  7. import com.youlai.boot.module.codegen.service.CodegenService;
  8. import com.youlai.boot.module.codegen.model.form.GenConfigForm;
  9. import com.youlai.boot.module.codegen.model.query.TablePageQuery;
  10. import com.youlai.boot.module.codegen.model.vo.CodegenPreviewVO;
  11. import com.youlai.boot.module.codegen.model.vo.TablePageVO;
  12. import com.youlai.boot.common.annotation.Log;
  13. import com.youlai.boot.module.codegen.service.GenConfigService;
  14. import io.swagger.v3.oas.annotations.Operation;
  15. import io.swagger.v3.oas.annotations.Parameter;
  16. import io.swagger.v3.oas.annotations.tags.Tag;
  17. import jakarta.servlet.ServletOutputStream;
  18. import jakarta.servlet.http.HttpServletResponse;
  19. import lombok.RequiredArgsConstructor;
  20. import lombok.extern.slf4j.Slf4j;
  21. import org.springframework.web.bind.annotation.*;
  22. import java.io.IOException;
  23. import java.net.URLEncoder;
  24. import java.nio.charset.StandardCharsets;
  25. import java.util.List;
  26. /**
  27. * 代码生成器控制层
  28. *
  29. * @author Ray
  30. * @since 2.10.0
  31. */
  32. @Tag(name = "09.代码生成")
  33. @RestController
  34. @RequestMapping("/api/v1/codegen")
  35. @RequiredArgsConstructor
  36. @Slf4j
  37. public class CodegenController {
  38. private final CodegenService codegenService;
  39. private final GenConfigService genConfigService;
  40. private final CodegenProperties codegenProperties;
  41. @Operation(summary = "获取数据表分页列表")
  42. @GetMapping("/table/page")
  43. @Log(value = "代码生成分页列表", module = LogModuleEnum.OTHER)
  44. public PageResult<TablePageVO> getTablePage(
  45. TablePageQuery queryParams
  46. ) {
  47. Page<TablePageVO> result = codegenService.getTablePage(queryParams);
  48. return PageResult.success(result);
  49. }
  50. @Operation(summary = "获取代码生成配置")
  51. @GetMapping("/{tableName}/config")
  52. public Result<GenConfigForm> getGenConfigFormData(
  53. @Parameter(description = "表名", example = "sys_user") @PathVariable String tableName
  54. ) {
  55. GenConfigForm formData = genConfigService.getGenConfigFormData(tableName);
  56. return Result.success(formData);
  57. }
  58. @Operation(summary = "保存代码生成配置")
  59. @PostMapping("/{tableName}/config")
  60. @Log(value = "生成代码", module = LogModuleEnum.OTHER)
  61. public Result<?> saveGenConfig(@RequestBody GenConfigForm formData) {
  62. genConfigService.saveGenConfig(formData);
  63. return Result.success();
  64. }
  65. @Operation(summary = "删除代码生成配置")
  66. @DeleteMapping("/{tableName}/config")
  67. public Result<?> deleteGenConfig(
  68. @Parameter(description = "表名", example = "sys_user") @PathVariable String tableName
  69. ) {
  70. genConfigService.deleteGenConfig(tableName);
  71. return Result.success();
  72. }
  73. @Operation(summary = "获取预览生成代码")
  74. @GetMapping("/{tableName}/preview")
  75. @Log(value = "预览生成代码", module = LogModuleEnum.OTHER)
  76. public Result<List<CodegenPreviewVO>> getTablePreviewData(@PathVariable String tableName) {
  77. List<CodegenPreviewVO> list = codegenService.getCodegenPreviewData(tableName);
  78. return Result.success(list);
  79. }
  80. @Operation(summary = "下载代码")
  81. @GetMapping("/{tableName}/download")
  82. @Log(value = "下载代码", module = LogModuleEnum.OTHER)
  83. public void downloadZip(HttpServletResponse response, @PathVariable String tableName) {
  84. String[] tableNames = tableName.split(",");
  85. byte[] data = codegenService.downloadCode(tableNames);
  86. response.reset();
  87. response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(codegenProperties.getDownloadFileName(), StandardCharsets.UTF_8));
  88. response.setContentType("application/octet-stream; charset=UTF-8");
  89. try (ServletOutputStream outputStream = response.getOutputStream()) {
  90. outputStream.write(data);
  91. outputStream.flush();
  92. } catch (IOException e) {
  93. log.error("Error while writing the zip file to response", e);
  94. throw new RuntimeException("Failed to write the zip file to response", e);
  95. }
  96. }
  97. }