PageRenderTime 76ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/generator/src/main/java/com/ruoyi/generator/util/VelocityUtils.java

https://gitlab.com/mrsunchangemyselfsun/ruoyi-vue
Java | 401 lines | 289 code | 25 blank | 87 comment | 51 complexity | 98a71a56ce5e1403920fcd7fdce632ba MD5 | raw file
  1. package com.ruoyi.generator.util;
  2. import java.util.ArrayList;
  3. import java.util.HashSet;
  4. import java.util.List;
  5. import java.util.Set;
  6. import org.apache.velocity.VelocityContext;
  7. import com.alibaba.fastjson.JSONObject;
  8. import com.ruoyi.common.constant.GenConstants;
  9. import com.ruoyi.common.utils.DateUtils;
  10. import com.ruoyi.common.utils.StringUtils;
  11. import com.ruoyi.generator.domain.GenTable;
  12. import com.ruoyi.generator.domain.GenTableColumn;
  13. /**
  14. * 模板处理工具类
  15. *
  16. * @author ruoyi
  17. */
  18. public class VelocityUtils
  19. {
  20. /** 项目空间路径 */
  21. private static final String PROJECT_PATH = "main/java";
  22. /** mybatis空间路径 */
  23. private static final String MYBATIS_PATH = "main/resources/mapper";
  24. /** 默认上级菜单,系统工具 */
  25. private static final String DEFAULT_PARENT_MENU_ID = "3";
  26. /**
  27. * 设置模板变量信息
  28. *
  29. * @return 模板列表
  30. */
  31. public static VelocityContext prepareContext(GenTable genTable)
  32. {
  33. String moduleName = genTable.getModuleName();
  34. String businessName = genTable.getBusinessName();
  35. String packageName = genTable.getPackageName();
  36. String tplCategory = genTable.getTplCategory();
  37. String functionName = genTable.getFunctionName();
  38. VelocityContext velocityContext = new VelocityContext();
  39. velocityContext.put("tplCategory", genTable.getTplCategory());
  40. velocityContext.put("tableName", genTable.getTableName());
  41. velocityContext.put("functionName", StringUtils.isNotEmpty(functionName) ? functionName : "【请填写功能名称】");
  42. velocityContext.put("ClassName", genTable.getClassName());
  43. velocityContext.put("className", StringUtils.uncapitalize(genTable.getClassName()));
  44. velocityContext.put("moduleName", genTable.getModuleName());
  45. velocityContext.put("BusinessName", StringUtils.capitalize(genTable.getBusinessName()));
  46. velocityContext.put("businessName", genTable.getBusinessName());
  47. velocityContext.put("basePackage", getPackagePrefix(packageName));
  48. velocityContext.put("packageName", packageName);
  49. velocityContext.put("author", genTable.getFunctionAuthor());
  50. velocityContext.put("datetime", DateUtils.getDate());
  51. velocityContext.put("pkColumn", genTable.getPkColumn());
  52. velocityContext.put("importList", getImportList(genTable));
  53. velocityContext.put("permissionPrefix", getPermissionPrefix(moduleName, businessName));
  54. velocityContext.put("columns", genTable.getColumns());
  55. velocityContext.put("table", genTable);
  56. velocityContext.put("dicts", getDicts(genTable));
  57. setMenuVelocityContext(velocityContext, genTable);
  58. if (GenConstants.TPL_TREE.equals(tplCategory))
  59. {
  60. setTreeVelocityContext(velocityContext, genTable);
  61. }
  62. if (GenConstants.TPL_SUB.equals(tplCategory))
  63. {
  64. setSubVelocityContext(velocityContext, genTable);
  65. }
  66. return velocityContext;
  67. }
  68. public static void setMenuVelocityContext(VelocityContext context, GenTable genTable)
  69. {
  70. String options = genTable.getOptions();
  71. JSONObject paramsObj = JSONObject.parseObject(options);
  72. String parentMenuId = getParentMenuId(paramsObj);
  73. context.put("parentMenuId", parentMenuId);
  74. }
  75. public static void setTreeVelocityContext(VelocityContext context, GenTable genTable)
  76. {
  77. String options = genTable.getOptions();
  78. JSONObject paramsObj = JSONObject.parseObject(options);
  79. String treeCode = getTreecode(paramsObj);
  80. String treeParentCode = getTreeParentCode(paramsObj);
  81. String treeName = getTreeName(paramsObj);
  82. context.put("treeCode", treeCode);
  83. context.put("treeParentCode", treeParentCode);
  84. context.put("treeName", treeName);
  85. context.put("expandColumn", getExpandColumn(genTable));
  86. if (paramsObj.containsKey(GenConstants.TREE_PARENT_CODE))
  87. {
  88. context.put("tree_parent_code", paramsObj.getString(GenConstants.TREE_PARENT_CODE));
  89. }
  90. if (paramsObj.containsKey(GenConstants.TREE_NAME))
  91. {
  92. context.put("tree_name", paramsObj.getString(GenConstants.TREE_NAME));
  93. }
  94. }
  95. public static void setSubVelocityContext(VelocityContext context, GenTable genTable)
  96. {
  97. GenTable subTable = genTable.getSubTable();
  98. String subTableName = genTable.getSubTableName();
  99. String subTableFkName = genTable.getSubTableFkName();
  100. String subClassName = genTable.getSubTable().getClassName();
  101. String subTableFkClassName = StringUtils.convertToCamelCase(subTableFkName);
  102. context.put("subTable", subTable);
  103. context.put("subTableName", subTableName);
  104. context.put("subTableFkName", subTableFkName);
  105. context.put("subTableFkClassName", subTableFkClassName);
  106. context.put("subTableFkclassName", StringUtils.uncapitalize(subTableFkClassName));
  107. context.put("subClassName", subClassName);
  108. context.put("subclassName", StringUtils.uncapitalize(subClassName));
  109. context.put("subImportList", getImportList(genTable.getSubTable()));
  110. }
  111. /**
  112. * 获取模板信息
  113. *
  114. * @return 模板列表
  115. */
  116. public static List<String> getTemplateList(String tplCategory)
  117. {
  118. List<String> templates = new ArrayList<String>();
  119. templates.add("vm/java/domain.java.vm");
  120. templates.add("vm/java/mapper.java.vm");
  121. templates.add("vm/java/service.java.vm");
  122. templates.add("vm/java/serviceImpl.java.vm");
  123. templates.add("vm/java/controller.java.vm");
  124. templates.add("vm/xml/mapper.xml.vm");
  125. templates.add("vm/sql/sql.vm");
  126. templates.add("vm/js/api.js.vm");
  127. if (GenConstants.TPL_CRUD.equals(tplCategory))
  128. {
  129. templates.add("vm/vue/index.vue.vm");
  130. }
  131. else if (GenConstants.TPL_TREE.equals(tplCategory))
  132. {
  133. templates.add("vm/vue/index-tree.vue.vm");
  134. }
  135. else if (GenConstants.TPL_SUB.equals(tplCategory))
  136. {
  137. templates.add("vm/vue/index.vue.vm");
  138. templates.add("vm/java/sub-domain.java.vm");
  139. }
  140. return templates;
  141. }
  142. /**
  143. * 获取文件名
  144. */
  145. public static String getFileName(String template, GenTable genTable)
  146. {
  147. // 文件名称
  148. String fileName = "";
  149. // 包路径
  150. String packageName = genTable.getPackageName();
  151. // 模块名
  152. String moduleName = genTable.getModuleName();
  153. // 大写类名
  154. String className = genTable.getClassName();
  155. // 业务名称
  156. String businessName = genTable.getBusinessName();
  157. String javaPath = PROJECT_PATH + "/" + StringUtils.replace(packageName, ".", "/");
  158. String mybatisPath = MYBATIS_PATH + "/" + moduleName;
  159. String vuePath = "vue";
  160. if (template.contains("domain.java.vm"))
  161. {
  162. fileName = StringUtils.format("{}/domain/{}.java", javaPath, className);
  163. }
  164. if (template.contains("sub-domain.java.vm") && StringUtils.equals(GenConstants.TPL_SUB, genTable.getTplCategory()))
  165. {
  166. fileName = StringUtils.format("{}/domain/{}.java", javaPath, genTable.getSubTable().getClassName());
  167. }
  168. else if (template.contains("mapper.java.vm"))
  169. {
  170. fileName = StringUtils.format("{}/mapper/{}Mapper.java", javaPath, className);
  171. }
  172. else if (template.contains("service.java.vm"))
  173. {
  174. fileName = StringUtils.format("{}/service/I{}Service.java", javaPath, className);
  175. }
  176. else if (template.contains("serviceImpl.java.vm"))
  177. {
  178. fileName = StringUtils.format("{}/service/impl/{}ServiceImpl.java", javaPath, className);
  179. }
  180. else if (template.contains("controller.java.vm"))
  181. {
  182. fileName = StringUtils.format("{}/controller/{}Controller.java", javaPath, className);
  183. }
  184. else if (template.contains("mapper.xml.vm"))
  185. {
  186. fileName = StringUtils.format("{}/{}Mapper.xml", mybatisPath, className);
  187. }
  188. else if (template.contains("sql.vm"))
  189. {
  190. fileName = businessName + "Menu.sql";
  191. }
  192. else if (template.contains("api.js.vm"))
  193. {
  194. fileName = StringUtils.format("{}/api/{}/{}.js", vuePath, moduleName, businessName);
  195. }
  196. else if (template.contains("index.vue.vm"))
  197. {
  198. fileName = StringUtils.format("{}/views/{}/{}/index.vue", vuePath, moduleName, businessName);
  199. }
  200. else if (template.contains("index-tree.vue.vm"))
  201. {
  202. fileName = StringUtils.format("{}/views/{}/{}/index.vue", vuePath, moduleName, businessName);
  203. }
  204. return fileName;
  205. }
  206. /**
  207. * 获取包前缀
  208. *
  209. * @param packageName 包名称
  210. * @return 包前缀名称
  211. */
  212. public static String getPackagePrefix(String packageName)
  213. {
  214. int lastIndex = packageName.lastIndexOf(".");
  215. return StringUtils.substring(packageName, 0, lastIndex);
  216. }
  217. /**
  218. * 根据列类型获取导入包
  219. *
  220. * @param genTable 业务表对象
  221. * @return 返回需要导入的包列表
  222. */
  223. public static HashSet<String> getImportList(GenTable genTable)
  224. {
  225. List<GenTableColumn> columns = genTable.getColumns();
  226. GenTable subGenTable = genTable.getSubTable();
  227. HashSet<String> importList = new HashSet<String>();
  228. if (StringUtils.isNotNull(subGenTable))
  229. {
  230. importList.add("java.util.List");
  231. }
  232. for (GenTableColumn column : columns)
  233. {
  234. if (!column.isSuperColumn() && GenConstants.TYPE_DATE.equals(column.getJavaType()))
  235. {
  236. importList.add("java.util.Date");
  237. importList.add("com.fasterxml.jackson.annotation.JsonFormat");
  238. }
  239. else if (!column.isSuperColumn() && GenConstants.TYPE_BIGDECIMAL.equals(column.getJavaType()))
  240. {
  241. importList.add("java.math.BigDecimal");
  242. }
  243. }
  244. return importList;
  245. }
  246. /**
  247. * 根据列类型获取字典组
  248. *
  249. * @param genTable 业务表对象
  250. * @return 返回字典组
  251. */
  252. public static String getDicts(GenTable genTable)
  253. {
  254. List<GenTableColumn> columns = genTable.getColumns();
  255. Set<String> dicts = new HashSet<String>();
  256. addDicts(dicts, columns);
  257. if (StringUtils.isNotNull(genTable.getSubTable()))
  258. {
  259. List<GenTableColumn> subColumns = genTable.getSubTable().getColumns();
  260. addDicts(dicts, subColumns);
  261. }
  262. return StringUtils.join(dicts, ", ");
  263. }
  264. /**
  265. * 添加字典列表
  266. *
  267. * @param dicts 字典列表
  268. * @param columns 列集合
  269. */
  270. public static void addDicts(Set<String> dicts, List<GenTableColumn> columns)
  271. {
  272. for (GenTableColumn column : columns)
  273. {
  274. if (!column.isSuperColumn() && StringUtils.isNotEmpty(column.getDictType()) && StringUtils.equalsAny(
  275. column.getHtmlType(),
  276. new String[] { GenConstants.HTML_SELECT, GenConstants.HTML_RADIO, GenConstants.HTML_CHECKBOX }))
  277. {
  278. dicts.add("'" + column.getDictType() + "'");
  279. }
  280. }
  281. }
  282. /**
  283. * 获取权限前缀
  284. *
  285. * @param moduleName 模块名称
  286. * @param businessName 业务名称
  287. * @return 返回权限前缀
  288. */
  289. public static String getPermissionPrefix(String moduleName, String businessName)
  290. {
  291. return StringUtils.format("{}:{}", moduleName, businessName);
  292. }
  293. /**
  294. * 获取上级菜单ID字段
  295. *
  296. * @param paramsObj 生成其他选项
  297. * @return 上级菜单ID字段
  298. */
  299. public static String getParentMenuId(JSONObject paramsObj)
  300. {
  301. if (StringUtils.isNotEmpty(paramsObj) && paramsObj.containsKey(GenConstants.PARENT_MENU_ID)
  302. && StringUtils.isNotEmpty(paramsObj.getString(GenConstants.PARENT_MENU_ID)))
  303. {
  304. return paramsObj.getString(GenConstants.PARENT_MENU_ID);
  305. }
  306. return DEFAULT_PARENT_MENU_ID;
  307. }
  308. /**
  309. * 获取树编码
  310. *
  311. * @param paramsObj 生成其他选项
  312. * @return 树编码
  313. */
  314. public static String getTreecode(JSONObject paramsObj)
  315. {
  316. if (paramsObj.containsKey(GenConstants.TREE_CODE))
  317. {
  318. return StringUtils.toCamelCase(paramsObj.getString(GenConstants.TREE_CODE));
  319. }
  320. return StringUtils.EMPTY;
  321. }
  322. /**
  323. * 获取树父编码
  324. *
  325. * @param paramsObj 生成其他选项
  326. * @return 树父编码
  327. */
  328. public static String getTreeParentCode(JSONObject paramsObj)
  329. {
  330. if (paramsObj.containsKey(GenConstants.TREE_PARENT_CODE))
  331. {
  332. return StringUtils.toCamelCase(paramsObj.getString(GenConstants.TREE_PARENT_CODE));
  333. }
  334. return StringUtils.EMPTY;
  335. }
  336. /**
  337. * 获取树名称
  338. *
  339. * @param paramsObj 生成其他选项
  340. * @return 树名称
  341. */
  342. public static String getTreeName(JSONObject paramsObj)
  343. {
  344. if (paramsObj.containsKey(GenConstants.TREE_NAME))
  345. {
  346. return StringUtils.toCamelCase(paramsObj.getString(GenConstants.TREE_NAME));
  347. }
  348. return StringUtils.EMPTY;
  349. }
  350. /**
  351. * 获取需要在哪一列上面显示展开按钮
  352. *
  353. * @param genTable 业务表对象
  354. * @return 展开按钮列序号
  355. */
  356. public static int getExpandColumn(GenTable genTable)
  357. {
  358. String options = genTable.getOptions();
  359. JSONObject paramsObj = JSONObject.parseObject(options);
  360. String treeName = paramsObj.getString(GenConstants.TREE_NAME);
  361. int num = 0;
  362. for (GenTableColumn column : genTable.getColumns())
  363. {
  364. if (column.isList())
  365. {
  366. num++;
  367. String columnName = column.getColumnName();
  368. if (columnName.equals(treeName))
  369. {
  370. break;
  371. }
  372. }
  373. }
  374. return num;
  375. }
  376. }