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