CodeController.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * Redistributions of source code must retain the above copyright notice,
  8. * this list of conditions and the following disclaimer.
  9. * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * Neither the name of the dreamlu.net developer nor the names of its
  13. * contributors may be used to endorse or promote products derived from
  14. * this software without specific prior written permission.
  15. * Author: Chill 庄骞 (smallchill@163.com)
  16. */
  17. package org.springblade.modules.develop.controller;
  18. import com.baomidou.mybatisplus.core.metadata.IPage;
  19. import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
  20. import io.swagger.annotations.*;
  21. import lombok.AllArgsConstructor;
  22. import org.springblade.common.generator.BladeCodeGenerator;
  23. import org.springblade.core.boot.ctrl.BladeController;
  24. import org.springblade.core.launch.constant.AppConstant;
  25. import org.springblade.core.mp.support.Condition;
  26. import org.springblade.core.mp.support.Query;
  27. import org.springblade.core.secure.annotation.PreAuth;
  28. import org.springblade.core.tenant.annotation.NonDS;
  29. import org.springblade.core.tool.api.R;
  30. import org.springblade.core.tool.constant.RoleConstant;
  31. import org.springblade.core.tool.utils.Func;
  32. import org.springblade.modules.develop.entity.Code;
  33. import org.springblade.modules.develop.entity.Datasource;
  34. import org.springblade.modules.develop.service.ICodeService;
  35. import org.springblade.modules.develop.service.IDatasourceService;
  36. import org.springframework.web.bind.annotation.*;
  37. import springfox.documentation.annotations.ApiIgnore;
  38. import javax.validation.Valid;
  39. import java.util.Collection;
  40. import java.util.Map;
  41. /**
  42. * 控制器
  43. *
  44. * @author Chill
  45. */
  46. @NonDS
  47. @ApiIgnore
  48. @RestController
  49. @AllArgsConstructor
  50. @RequestMapping(AppConstant.APPLICATION_DEVELOP_NAME + "/code")
  51. @Api(value = "代码生成", tags = "代码生成")
  52. @PreAuth(RoleConstant.HAS_ROLE_ADMINISTRATOR)
  53. public class CodeController extends BladeController {
  54. private final ICodeService codeService;
  55. private final IDatasourceService datasourceService;
  56. /**
  57. * 详情
  58. */
  59. @GetMapping("/detail")
  60. @ApiOperationSupport(order = 1)
  61. @ApiOperation(value = "详情", notes = "传入code")
  62. public R<Code> detail(Code code) {
  63. Code detail = codeService.getOne(Condition.getQueryWrapper(code));
  64. return R.data(detail);
  65. }
  66. /**
  67. * 分页
  68. */
  69. @GetMapping("/list")
  70. @ApiImplicitParams({
  71. @ApiImplicitParam(name = "codeName", value = "模块名", paramType = "query", dataType = "string"),
  72. @ApiImplicitParam(name = "tableName", value = "表名", paramType = "query", dataType = "string"),
  73. @ApiImplicitParam(name = "modelName", value = "实体名", paramType = "query", dataType = "string")
  74. })
  75. @ApiOperationSupport(order = 2)
  76. @ApiOperation(value = "分页", notes = "传入code")
  77. public R<IPage<Code>> list(@ApiIgnore @RequestParam Map<String, Object> code, Query query) {
  78. IPage<Code> pages = codeService.page(Condition.getPage(query), Condition.getQueryWrapper(code, Code.class));
  79. return R.data(pages);
  80. }
  81. /**
  82. * 新增或修改
  83. */
  84. @PostMapping("/submit")
  85. @ApiOperationSupport(order = 3)
  86. @ApiOperation(value = "新增或修改", notes = "传入code")
  87. public R submit(@Valid @RequestBody Code code) {
  88. return R.status(codeService.submit(code));
  89. }
  90. /**
  91. * 删除
  92. */
  93. @PostMapping("/remove")
  94. @ApiOperationSupport(order = 4)
  95. @ApiOperation(value = "删除", notes = "传入ids")
  96. public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
  97. return R.status(codeService.removeByIds(Func.toLongList(ids)));
  98. }
  99. /**
  100. * 复制
  101. */
  102. @PostMapping("/copy")
  103. @ApiOperationSupport(order = 5)
  104. @ApiOperation(value = "复制", notes = "传入id")
  105. public R copy(@ApiParam(value = "主键", required = true) @RequestParam Long id) {
  106. Code code = codeService.getById(id);
  107. code.setId(null);
  108. code.setCodeName(code.getCodeName() + "-copy");
  109. return R.status(codeService.save(code));
  110. }
  111. /**
  112. * 代码生成
  113. */
  114. @PostMapping("/gen-code")
  115. @ApiOperationSupport(order = 6)
  116. @ApiOperation(value = "代码生成", notes = "传入ids")
  117. public R genCode(@ApiParam(value = "主键集合", required = true) @RequestParam String ids, @RequestParam(defaultValue = "sword") String system) {
  118. Collection<Code> codes = codeService.listByIds(Func.toLongList(ids));
  119. codes.forEach(code -> {
  120. BladeCodeGenerator generator = new BladeCodeGenerator();
  121. // 设置数据源
  122. Datasource datasource = datasourceService.getById(code.getDatasourceId());
  123. generator.setDriverName(datasource.getDriverClass());
  124. generator.setUrl(datasource.getUrl());
  125. generator.setUsername(datasource.getUsername());
  126. generator.setPassword(datasource.getPassword());
  127. // 设置基础配置
  128. generator.setSystemName(system);
  129. generator.setCodeName(code.getCodeName());
  130. generator.setServiceName(code.getServiceName());
  131. generator.setPackageName(code.getPackageName());
  132. generator.setPackageDir(code.getApiPath());
  133. generator.setPackageWebDir(code.getWebPath());
  134. generator.setTablePrefix(Func.toStrArray(code.getTablePrefix()));
  135. generator.setIncludeTables(Func.toStrArray(code.getTableName()));
  136. // 设置是否继承基础业务字段
  137. generator.setHasSuperEntity(code.getBaseMode() == 2);
  138. // 设置是否开启包装器模式
  139. generator.setHasWrapper(code.getWrapMode() == 2);
  140. generator.setIsSwagger2(true);
  141. generator.setHasSuperEntity(true);
  142. generator.run();
  143. });
  144. return R.success("代码生成成功");
  145. }
  146. }