GenCodeService.java 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package com.izouma.jiashanxia.service;
  2. import com.izouma.jiashanxia.dto.gen.GenCode;
  3. import com.izouma.jiashanxia.dto.gen.TableField;
  4. import freemarker.template.Configuration;
  5. import freemarker.template.Template;
  6. import freemarker.template.TemplateException;
  7. import jodd.util.StringUtil;
  8. import lombok.extern.slf4j.Slf4j;
  9. import org.apache.commons.lang.StringUtils;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.stereotype.Service;
  12. import java.awt.*;
  13. import java.awt.font.FontRenderContext;
  14. import java.awt.geom.AffineTransform;
  15. import java.io.*;
  16. import java.nio.file.Files;
  17. import java.nio.file.Path;
  18. import java.nio.file.Paths;
  19. import java.util.ArrayList;
  20. import java.util.HashMap;
  21. import java.util.List;
  22. import java.util.Map;
  23. @Service
  24. @Slf4j
  25. public class GenCodeService {
  26. @Autowired
  27. private Configuration cfg;
  28. public void genController(GenCode model) throws IOException, TemplateException {
  29. Path targetFile = StringUtils.isNotBlank(model.getGenPackage()) ?
  30. Paths.get(model.getJavaPath(), "web", model.getGenPackage(), model.getClassName() + "Controller.java")
  31. .toAbsolutePath() :
  32. Paths.get(model.getJavaPath(), "web", model.getClassName() + "Controller.java").toAbsolutePath();
  33. Map<String, Object> extra = new HashMap<>();
  34. if (StringUtil.isNotEmpty(model.getGenPackage())) {
  35. List<String> imports = new ArrayList<>();
  36. imports.add("import " + model.getBasePackage() + ".web.BaseController;");
  37. extra.put("imports", imports);
  38. }
  39. extra.put("subPackage", StringUtil.isNotEmpty(model.getGenPackage()));
  40. extra.put("softDelete", canSoftDelete(model));
  41. genFile("ControllerTemplate.ftl", model, extra, targetFile);
  42. log.info("成功生成Controller:{}", targetFile.toString());
  43. }
  44. public void genService(GenCode model) throws IOException, TemplateException {
  45. Path targetFile = StringUtils.isNotBlank(model.getGenPackage()) ?
  46. Paths.get(model.getJavaPath(), "service", model.getGenPackage(), model.getClassName() + "Service.java")
  47. .toAbsolutePath() :
  48. Paths.get(model.getJavaPath(), "service", model.getClassName() + "Service.java").toAbsolutePath();
  49. Map<String, Object> extra = new HashMap<>();
  50. extra.put("softDelete", canSoftDelete(model));
  51. genFile("ServiceTemplate.ftl", model, extra, targetFile);
  52. log.info("成功生成Service:{}", targetFile.toString());
  53. }
  54. public void genRepo(GenCode model) throws IOException, TemplateException, ClassNotFoundException {
  55. Path targetFile = StringUtils.isNotBlank(model.getGenPackage()) ?
  56. Paths.get(model.getJavaPath(), "repo", model.getGenPackage(), model.getClassName() + "Repo.java")
  57. .toAbsolutePath() :
  58. Paths.get(model.getJavaPath(), "repo", model.getClassName() + "Repo.java").toAbsolutePath();
  59. Map<String, Object> extra = new HashMap<>();
  60. extra.put("softDelete", canSoftDelete(model));
  61. genFile("RepoTemplate.ftl", model, extra, targetFile);
  62. log.info("成功生成Repo:{}", targetFile.toString());
  63. }
  64. public void genListView(GenCode model) throws IOException, TemplateException {
  65. Path targetFile = Paths.get(model.getViewPath(), model.getClassName() + "List.vue").toAbsolutePath();
  66. Map<String, Object> extra = new HashMap<>();
  67. extra.put("softDelete", canSoftDelete(model));
  68. genFile("ListViewTemplate.ftl", model, extra, targetFile);
  69. log.info("成功生成ListView:{}", targetFile.toString());
  70. }
  71. public void genEditView(GenCode model) throws IOException, TemplateException, FontFormatException {
  72. Path targetFile = Paths.get(model.getViewPath(), model.getClassName() + "Edit.vue").toAbsolutePath();
  73. Map<String, Object> map = new HashMap<>();
  74. int maxLabelWidth = 0;
  75. for (TableField field : model.getFields()) {
  76. String label = StringUtils.isEmpty(field.getRemark()) ? field.getModelName() : field.getRemark();
  77. int labelWidth = measureText(label, 14);
  78. if (labelWidth > maxLabelWidth) {
  79. maxLabelWidth = labelWidth;
  80. }
  81. }
  82. map.put("labelWidth", maxLabelWidth + 25 + "px");
  83. map.put("softDelete", canSoftDelete(model));
  84. genFile("EditViewTemplate.ftl", model, map, targetFile);
  85. log.info("成功生成EditView:{}", targetFile.toString());
  86. }
  87. private void genFile(String template, GenCode model, Map<String, Object> extraData, Path path) throws IOException, TemplateException {
  88. Map<String, Object> data = new HashMap<>();
  89. data.put("model", model);
  90. if (extraData != null) {
  91. data.putAll(extraData);
  92. }
  93. Files.createDirectories(path.getParent());
  94. Template temp = cfg.getTemplate(template);
  95. Writer out = new FileWriter(path.toString());
  96. temp.process(data, out);
  97. out.flush();
  98. out.close();
  99. }
  100. private int measureText(String text, float fontSize) throws IOException, FontFormatException {
  101. AffineTransform affinetransform = new AffineTransform();
  102. FontRenderContext frc = new FontRenderContext(affinetransform, true, true);
  103. Font font = Font.createFont(Font.TRUETYPE_FONT, this.getClass()
  104. .getResourceAsStream("/font/SourceHanSansCN-Normal.ttf"));
  105. return (int) (font.deriveFont(fontSize).getStringBounds(text, frc).getWidth());
  106. }
  107. public void genRouter(GenCode model) {
  108. try {
  109. File file = new File(model.getRouterPath(), "router.js");
  110. BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
  111. StringBuilder routerJs = new StringBuilder();
  112. String line = null;
  113. while ((line = reader.readLine()) != null) {
  114. routerJs.append(line).append("\n");
  115. }
  116. reader.close();
  117. int insertLocation = routerJs.indexOf("/**INSERT_LOCATION**/");
  118. if (insertLocation > -1) {
  119. String remark = model.getRemark();
  120. String routeName = StringUtils.capitalize(model.getClassName());
  121. String routePath = StringUtils.uncapitalize(model.getClassName());
  122. String route = "{\n path: '/"
  123. + routePath
  124. + "Edit',\n name: '"
  125. + routeName
  126. + "Edit',\n component: () => import(/* webpackChunkName: \"" + routePath + "Edit\" */ '@/views/"
  127. + routeName
  128. + "Edit.vue'),\n meta: {\n title: '" + remark + "编辑',\n"
  129. + " },\n },\n {\n path: '/"
  130. + routePath
  131. + "List',\n name: '"
  132. + routeName
  133. + "List',\n component: () => import(/* webpackChunkName: \"" + routePath + "List\" */ '@/views/"
  134. + routeName
  135. + "List.vue'),\n meta: {\n title: '" + remark + "',\n"
  136. + " },\n }\n ";
  137. boolean needComma = !routerJs.toString().substring(0, insertLocation).trim().endsWith(",");
  138. if (needComma) {
  139. routerJs.insert(routerJs.toString().substring(0, insertLocation).lastIndexOf("}") + 1, ",");
  140. insertLocation++;
  141. }
  142. routerJs.insert(insertLocation, route);
  143. }
  144. BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file));
  145. bufferedOutputStream.write(routerJs.toString().trim().getBytes());
  146. bufferedOutputStream.close();
  147. System.out.println("成功生成路由:" + file.getAbsolutePath());
  148. } catch (Exception e) {
  149. e.printStackTrace();
  150. }
  151. }
  152. private boolean canSoftDelete(GenCode model) {
  153. return true;
  154. }
  155. }