mirror of
				https://gitee.com/hhyykk/ipms-sjy.git
				synced 2025-10-31 18:28:43 +08:00 
			
		
		
		
	代码预览的功能,完成
This commit is contained in:
		| @@ -1,8 +1,10 @@ | ||||
| package cn.iocoder.dashboard.modules.tool.controller.codegen; | ||||
|  | ||||
| import cn.hutool.core.io.IoUtil; | ||||
| import cn.iocoder.dashboard.common.pojo.CommonResult; | ||||
| import cn.iocoder.dashboard.common.pojo.PageResult; | ||||
| import cn.iocoder.dashboard.modules.tool.controller.codegen.vo.ToolCodegenDetailRespVO; | ||||
| import cn.iocoder.dashboard.modules.tool.controller.codegen.vo.ToolCodegenPreviewRespVO; | ||||
| import cn.iocoder.dashboard.modules.tool.controller.codegen.vo.ToolCodegenUpdateReqVO; | ||||
| import cn.iocoder.dashboard.modules.tool.controller.codegen.vo.table.ToolCodegenTablePageReqVO; | ||||
| import cn.iocoder.dashboard.modules.tool.controller.codegen.vo.table.ToolCodegenTableRespVO; | ||||
| @@ -11,13 +13,20 @@ import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolCodegenColum | ||||
| import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolCodegenTableDO; | ||||
| import cn.iocoder.dashboard.modules.tool.service.codegen.ToolCodegenService; | ||||
| import io.swagger.annotations.Api; | ||||
| import io.swagger.annotations.ApiImplicitParam; | ||||
| import io.swagger.annotations.ApiOperation; | ||||
| import org.springframework.validation.annotation.Validated; | ||||
| import org.springframework.web.bind.annotation.*; | ||||
|  | ||||
| import javax.annotation.Resource; | ||||
| import javax.servlet.http.HttpServletResponse; | ||||
| import javax.validation.Valid; | ||||
| import java.io.ByteArrayOutputStream; | ||||
| import java.io.IOException; | ||||
| import java.util.List; | ||||
| import java.util.Map; | ||||
| import java.util.zip.ZipEntry; | ||||
| import java.util.zip.ZipOutputStream; | ||||
|  | ||||
| import static cn.iocoder.dashboard.common.pojo.CommonResult.success; | ||||
|  | ||||
| @@ -55,9 +64,6 @@ public class ToolCodegenController { | ||||
|         return success(codegenService.createCodegen(tableName)); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 修改保存代码生成业务 | ||||
|      */ | ||||
|     @ApiOperation("更新数据库的表和字段定义") | ||||
|     @PutMapping("/update") | ||||
| //    @PreAuthorize("@ss.hasPermi('tool:gen:edit')") TODO 权限 | ||||
| @@ -66,4 +72,70 @@ public class ToolCodegenController { | ||||
|         return success(true); | ||||
|     } | ||||
|  | ||||
|     @ApiOperation("预览生成代码") | ||||
|     @GetMapping("/preview") | ||||
|     @ApiImplicitParam(name = "tableId", required = true, example = "表编号", dataTypeClass = Long.class) | ||||
| //    @PreAuthorize("@ss.hasPermi('tool:gen:preview')") TODO 权限 | ||||
|     public CommonResult<List<ToolCodegenPreviewRespVO>> previewCodegen(@RequestParam("tableId") Long tableId) { | ||||
|         Map<String, String> codes = codegenService.generationCodes(tableId); | ||||
|         return success(ToolCodegenConvert.INSTANCE.convert(codes)); | ||||
|     } | ||||
|  | ||||
|     @ApiOperation("下载生成代码") | ||||
|     @GetMapping("/download") | ||||
|     @ApiImplicitParam(name = "tableId", required = true, example = "表编号", dataTypeClass = Long.class) | ||||
|     public void downloadCodegen(@RequestParam("tableId") Long tableId, | ||||
|                                 HttpServletResponse response) throws IOException { | ||||
|         // 生成代码 | ||||
|         Map<String, String> codes = codegenService.generationCodes(tableId); | ||||
|         // 构建压缩包 | ||||
|         byte[] data; | ||||
|         ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); | ||||
|         ZipOutputStream zip = new ZipOutputStream(outputStream); | ||||
|         for (Map.Entry<String, String> entry : codes.entrySet()) { | ||||
| //                zip.putNextEntry(new ZipEntry(entry.getKey())); | ||||
|             zip.putNextEntry(new ZipEntry("123")); | ||||
| //                IoUtil.write(zip, Charset.defaultCharset(), false, entry.getValue()); | ||||
|             zip.write(entry.getValue().getBytes()); | ||||
|             zip.flush(); | ||||
|             zip.closeEntry(); | ||||
|             if (true) { | ||||
|                 break; | ||||
|             } | ||||
|         } | ||||
|         data = outputStream.toByteArray(); | ||||
|         IoUtil.close(zip); | ||||
| //        try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); | ||||
| //             ZipOutputStream zip = new ZipOutputStream(outputStream)) { | ||||
| //            for (Map.Entry<String, String> entry : codes.entrySet()) { | ||||
| ////                zip.putNextEntry(new ZipEntry(entry.getKey())); | ||||
| //                zip.putNextEntry(new ZipEntry("123")); | ||||
| ////                IoUtil.write(zip, Charset.defaultCharset(), false, entry.getValue()); | ||||
| //                zip.write(entry.getValue().getBytes()); | ||||
| //                zip.flush(); | ||||
| //                zip.closeEntry(); | ||||
| //                if (true) { | ||||
| //                    break; | ||||
| //                } | ||||
| //            } | ||||
| //            data = outputStream.toByteArray(); | ||||
| //        } | ||||
|         // 返回 | ||||
| //        ServletUtils.writeAttachment(response, "yudao.zip", data); | ||||
|         genCode(response, data); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 生成zip文件 | ||||
|      */ | ||||
|     private void genCode(HttpServletResponse response, byte[] data) throws IOException | ||||
|     { | ||||
|         response.reset(); | ||||
|         response.addHeader("Access-Control-Allow-Origin", "*"); | ||||
|         response.addHeader("Access-Control-Expose-Headers", "Content-Disposition"); | ||||
|         response.setHeader("Content-Disposition", "attachment; filename=\"ruoyi.zip\""); | ||||
|         response.addHeader("Content-Length", "" + data.length); | ||||
|         response.setContentType("application/octet-stream; charset=UTF-8"); | ||||
|         IoUtil.write(response.getOutputStream(), false, data); | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -0,0 +1,17 @@ | ||||
| package cn.iocoder.dashboard.modules.tool.controller.codegen.vo; | ||||
|  | ||||
| import io.swagger.annotations.ApiModel; | ||||
| import io.swagger.annotations.ApiModelProperty; | ||||
| import lombok.Data; | ||||
|  | ||||
| @ApiModel(value = "代码生成预览 Response VO", description ="注意,每个文件都是一个该对象") | ||||
| @Data | ||||
| public class ToolCodegenPreviewRespVO { | ||||
|  | ||||
|     @ApiModelProperty(value = "文件路径", required = true, example = "java/cn/iocoder/dashboard/modules/system/controller/test/SysTestDemoController.java") | ||||
|     private String filePath; | ||||
|  | ||||
|     @ApiModelProperty(value = "代码", required = true, example = "Hello World") | ||||
|     private String code; | ||||
|  | ||||
| } | ||||
| @@ -1,9 +1,10 @@ | ||||
| package cn.iocoder.dashboard.modules.tool.convert.codegen; | ||||
|  | ||||
| import cn.iocoder.dashboard.common.pojo.PageResult; | ||||
| import cn.iocoder.dashboard.modules.tool.controller.codegen.vo.ToolCodegenDetailRespVO; | ||||
| import cn.iocoder.dashboard.modules.tool.controller.codegen.vo.ToolCodegenPreviewRespVO; | ||||
| import cn.iocoder.dashboard.modules.tool.controller.codegen.vo.ToolCodegenUpdateReqVO; | ||||
| import cn.iocoder.dashboard.modules.tool.controller.codegen.vo.column.ToolCodegenColumnRespVO; | ||||
| import cn.iocoder.dashboard.modules.tool.controller.codegen.vo.ToolCodegenDetailRespVO; | ||||
| import cn.iocoder.dashboard.modules.tool.controller.codegen.vo.table.ToolCodegenTableRespVO; | ||||
| import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolCodegenColumnDO; | ||||
| import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolCodegenTableDO; | ||||
| @@ -13,6 +14,8 @@ import org.mapstruct.Mapper; | ||||
| import org.mapstruct.factory.Mappers; | ||||
|  | ||||
| import java.util.List; | ||||
| import java.util.Map; | ||||
| import java.util.stream.Collectors; | ||||
|  | ||||
| @Mapper | ||||
| public interface ToolCodegenConvert { | ||||
| @@ -52,4 +55,13 @@ public interface ToolCodegenConvert { | ||||
|         return respVO; | ||||
|     } | ||||
|  | ||||
|     default List<ToolCodegenPreviewRespVO> convert(Map<String, String> codes) { | ||||
|         return codes.entrySet().stream().map(entry -> { | ||||
|             ToolCodegenPreviewRespVO respVO = new ToolCodegenPreviewRespVO(); | ||||
|             respVO.setFilePath(entry.getKey()); | ||||
|             respVO.setCode(entry.getValue()); | ||||
|             return respVO; | ||||
|         }).collect(Collectors.toList()); | ||||
|     } | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -7,6 +7,7 @@ import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolCodegenColum | ||||
| import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolCodegenTableDO; | ||||
|  | ||||
| import java.util.List; | ||||
| import java.util.Map; | ||||
|  | ||||
| /** | ||||
|  * 代码生成 Service 接口 | ||||
| @@ -54,4 +55,12 @@ public interface ToolCodegenService { | ||||
|      */ | ||||
|     List<ToolCodegenColumnDO> getCodegenColumnListByTableId(Long tableId); | ||||
|  | ||||
|     /** | ||||
|      * 执行指定表的代码生成 | ||||
|      * | ||||
|      * @param tableId 表编号 | ||||
|      * @return 生成结果。key 为文件路径,value 为对应的代码内容 | ||||
|      */ | ||||
|     Map<String, String> generationCodes(Long tableId); | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -21,6 +21,7 @@ import org.springframework.stereotype.Component; | ||||
|  | ||||
| import javax.annotation.Resource; | ||||
| import java.util.HashMap; | ||||
| import java.util.LinkedHashMap; | ||||
| import java.util.List; | ||||
| import java.util.Map; | ||||
|  | ||||
| @@ -42,7 +43,7 @@ public class ToolCodegenEngine { | ||||
|      * key:模板在 resources 的地址 | ||||
|      * value:生成的路径 | ||||
|      */ | ||||
|     private static final Map<String, String> TEMPLATES = MapUtil.<String, String>builder() | ||||
|     private static final Map<String, String> TEMPLATES = MapUtil.<String, String>builder(new LinkedHashMap<>()) // 有序 | ||||
|             // Java | ||||
|             .put("codegen/java/controller/controller.vm", "java/${basePackage}/${table.moduleName}/controller/${table.businessName}/${table.className}Controller.java") | ||||
|             .put("codegen/java/controller/vo/baseVO.vm", "java/${basePackage}/${table.moduleName}/controller/${table.businessName}/vo/${table.className}BaseVO.java") | ||||
| @@ -53,7 +54,7 @@ public class ToolCodegenEngine { | ||||
|             .put("codegen/java/convert/convert.vm", "java/${basePackage}/${table.moduleName}/convert/${table.businessName}/${table.className}Convert.java") | ||||
|             .put("codegen/java/dal/do.vm", "java/${basePackage}/${table.moduleName}/dal/dataobject/${table.businessName}/${table.className}DO.java") | ||||
|             .put("codegen/java/dal/mapper.vm", "java/${basePackage}/${table.moduleName}/dal/mysql/${table.businessName}/${table.className}Mapper.java") | ||||
|             .put("codegen/java/enums/errorcode.vm", "java/${basePackage}.${table.moduleName}.enums.${simpleModuleName_upperFirst}ErrorCodeConstants.java") | ||||
|             .put("codegen/java/enums/errorcode.vm", "java/${basePackage}/${table.moduleName}/enums/${simpleModuleName_upperFirst}ErrorCodeConstants.java") | ||||
|             .put("codegen/java/service/service.vm", "java/${basePackage}/${table.moduleName}/service/${table.businessName}/${table.className}Service.java") | ||||
|             .put("codegen/java/service/serviceImpl.vm", "java/${basePackage}/${table.moduleName}/service/${table.businessName}/impl/${table.className}ServiceImpl.java") | ||||
|             // Vue | ||||
| @@ -117,7 +118,7 @@ public class ToolCodegenEngine { | ||||
|         bindingMap.put("simpleClassName_strikeCase", toSymbolCase(simpleClassName, '-')); // 将 DictType 转换成 dict-type | ||||
|  | ||||
|         // 执行生成 | ||||
|         final Map<String, String> result = Maps.newHashMapWithExpectedSize(TEMPLATES.size()); | ||||
|         final Map<String, String> result = Maps.newLinkedHashMapWithExpectedSize(TEMPLATES.size()); // 有序 | ||||
|         TEMPLATES.forEach((vmPath, filePath) -> { | ||||
|             filePath = formatFilePath(filePath, bindingMap); | ||||
|             String content = templateEngine.getTemplate(vmPath).render(bindingMap); | ||||
|   | ||||
| @@ -19,6 +19,7 @@ import org.springframework.transaction.annotation.Transactional; | ||||
|  | ||||
| import javax.annotation.Resource; | ||||
| import java.util.List; | ||||
| import java.util.Map; | ||||
|  | ||||
| /** | ||||
|  * 代码生成 Service 实现类 | ||||
| @@ -102,4 +103,20 @@ public class ToolCodegenServiceImpl implements ToolCodegenService { | ||||
|         return codegenColumnMapper.selectListByTableId(tableId); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public Map<String, String> generationCodes(Long tableId) { | ||||
|         // 校验是否已经存在 | ||||
|         ToolCodegenTableDO table = codegenTableMapper.selectById(tableId); | ||||
|         if (codegenTableMapper.selectById(tableId) == null) { | ||||
|             throw new RuntimeException(""); // TODO | ||||
|         } | ||||
|         List<ToolCodegenColumnDO> columns = codegenColumnMapper.selectListByTableId(tableId); | ||||
|         if (CollUtil.isEmpty(columns)) { | ||||
|             throw new RuntimeException(""); // TODO | ||||
|         } | ||||
|  | ||||
|         // 执行生成 | ||||
|         return codegenEngine.execute(table, columns); | ||||
|     } | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -42,8 +42,8 @@ public class ${table.className}Controller { | ||||
|         return success(true); | ||||
|     } | ||||
|  | ||||
| 	@ApiOperation("删除${table.classComment}") | ||||
| 	@DeleteMapping("/delete") | ||||
|     @ApiOperation("删除${table.classComment}") | ||||
|     @DeleteMapping("/delete") | ||||
|     @ApiImplicitParam(name = "id", value = "编号", required = true) | ||||
|     public CommonResult<Boolean> delete${simpleClassName}(@RequestParam("id") ${primaryColumn.javaType} id) { | ||||
|         ${classNameVar}Service.delete${simpleClassName}(id); | ||||
| @@ -52,7 +52,7 @@ public class ${table.className}Controller { | ||||
|  | ||||
|     @GetMapping("/get") | ||||
|     @ApiOperation("获得${table.classComment}") | ||||
|     @ApiImplicitParam(name = "id", value = "编号", required = true) | ||||
|     @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = ${primaryColumn.javaType}.class) | ||||
|     public CommonResult<${table.className}RespVO> get${simpleClassName}(@RequestParam("id") ${primaryColumn.javaType} id) { | ||||
|         ${table.className}DO ${classNameVar} = ${classNameVar}Service.get${simpleClassName}(id); | ||||
|         return success(${table.className}Convert.INSTANCE.convert(${classNameVar})); | ||||
| @@ -60,7 +60,7 @@ public class ${table.className}Controller { | ||||
|  | ||||
|     @GetMapping("/list") | ||||
|     @ApiOperation("获得${table.classComment}列表") | ||||
|     @ApiImplicitParam(name = "ids", value = "编号列表", required = true) | ||||
|     @ApiImplicitParam(name = "ids", value = "编号列表", required = true, dataTypeClass = List.class) | ||||
|     public CommonResult<List<${table.className}RespVO>> get${simpleClassName}List(@RequestParam("ids") Collection<${primaryColumn.javaType}> ids) { | ||||
|         List<${table.className}DO> list = ${classNameVar}Service.get${simpleClassName}List(ids); | ||||
|         return success(${table.className}Convert.INSTANCE.convertList(list)); | ||||
|   | ||||
| @@ -27,6 +27,6 @@ public interface ${table.className}Convert { | ||||
|  | ||||
|     List<${table.className}RespVO> convertList(List<${table.className}DO> list); | ||||
|  | ||||
| 	PageResult<${table.className}RespVO> convertPage(PageResult<${table.className}DO> page); | ||||
|     PageResult<${table.className}RespVO> convertPage(PageResult<${table.className}DO> page); | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -57,6 +57,6 @@ public interface ${table.className}Service { | ||||
|      * @param pageReqVO 分页查询 | ||||
|      * @return ${table.classComment}分页 | ||||
|      */ | ||||
| 	PageResult<${table.className}DO> get${simpleClassName}Page(${table.className}PageReqVO pageReqVO); | ||||
|     PageResult<${table.className}DO> get${simpleClassName}Page(${table.className}PageReqVO pageReqVO); | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -72,7 +72,7 @@ public class ${table.className}ServiceImpl implements ${table.className}Service | ||||
|         return ${classNameVar}Mapper.selectBatchIds(ids); | ||||
|     } | ||||
|  | ||||
| 	@Override | ||||
|     @Override | ||||
|     public PageResult<${table.className}DO> get${simpleClassName}Page(${table.className}PageReqVO pageReqVO) { | ||||
| 		return ${classNameVar}Mapper.selectPage(pageReqVO); | ||||
|     } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 YunaiV
					YunaiV