GenTableServiceImpl.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. package com.ruoyi.gen.service;
  2. import java.io.ByteArrayOutputStream;
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.io.StringWriter;
  6. import java.util.LinkedHashMap;
  7. import java.util.List;
  8. import java.util.Map;
  9. import java.util.stream.Collectors;
  10. import java.util.zip.ZipEntry;
  11. import java.util.zip.ZipOutputStream;
  12. import org.apache.commons.io.IOUtils;
  13. import org.apache.velocity.Template;
  14. import org.apache.velocity.VelocityContext;
  15. import org.apache.velocity.app.Velocity;
  16. import org.slf4j.Logger;
  17. import org.slf4j.LoggerFactory;
  18. import org.springframework.beans.factory.annotation.Autowired;
  19. import org.springframework.stereotype.Service;
  20. import org.springframework.transaction.annotation.Transactional;
  21. import com.alibaba.fastjson.JSON;
  22. import com.alibaba.fastjson.JSONObject;
  23. import com.ruoyi.common.core.constant.Constants;
  24. import com.ruoyi.common.core.constant.GenConstants;
  25. import com.ruoyi.common.core.exception.CustomException;
  26. import com.ruoyi.common.core.text.CharsetKit;
  27. import com.ruoyi.common.core.utils.StringUtils;
  28. import com.ruoyi.common.core.utils.file.FileUtils;
  29. import com.ruoyi.common.security.utils.SecurityUtils;
  30. import com.ruoyi.gen.domain.GenTable;
  31. import com.ruoyi.gen.domain.GenTableColumn;
  32. import com.ruoyi.gen.mapper.GenTableColumnMapper;
  33. import com.ruoyi.gen.mapper.GenTableMapper;
  34. import com.ruoyi.gen.util.GenUtils;
  35. import com.ruoyi.gen.util.VelocityInitializer;
  36. import com.ruoyi.gen.util.VelocityUtils;
  37. /**
  38. * 业务 服务层实现
  39. *
  40. * @author ruoyi
  41. */
  42. @Service
  43. public class GenTableServiceImpl implements IGenTableService
  44. {
  45. private static final Logger log = LoggerFactory.getLogger(GenTableServiceImpl.class);
  46. @Autowired
  47. private GenTableMapper genTableMapper;
  48. @Autowired
  49. private GenTableColumnMapper genTableColumnMapper;
  50. /**
  51. * 查询业务信息
  52. *
  53. * @param id 业务ID
  54. * @return 业务信息
  55. */
  56. @Override
  57. public GenTable selectGenTableById(Long id)
  58. {
  59. GenTable genTable = genTableMapper.selectGenTableById(id);
  60. setTableFromOptions(genTable);
  61. return genTable;
  62. }
  63. /**
  64. * 查询业务列表
  65. *
  66. * @param genTable 业务信息
  67. * @return 业务集合
  68. */
  69. @Override
  70. public List<GenTable> selectGenTableList(GenTable genTable)
  71. {
  72. return genTableMapper.selectGenTableList(genTable);
  73. }
  74. /**
  75. * 查询据库列表
  76. *
  77. * @param genTable 业务信息
  78. * @return 数据库表集合
  79. */
  80. @Override
  81. public List<GenTable> selectDbTableList(GenTable genTable)
  82. {
  83. return genTableMapper.selectDbTableList(genTable);
  84. }
  85. /**
  86. * 查询据库列表
  87. *
  88. * @param tableNames 表名称组
  89. * @return 数据库表集合
  90. */
  91. @Override
  92. public List<GenTable> selectDbTableListByNames(String[] tableNames)
  93. {
  94. return genTableMapper.selectDbTableListByNames(tableNames);
  95. }
  96. /**
  97. * 修改业务
  98. *
  99. * @param genTable 业务信息
  100. * @return 结果
  101. */
  102. @Override
  103. @Transactional
  104. public void updateGenTable(GenTable genTable)
  105. {
  106. String options = JSON.toJSONString(genTable.getParams());
  107. genTable.setOptions(options);
  108. int row = genTableMapper.updateGenTable(genTable);
  109. if (row > 0)
  110. {
  111. for (GenTableColumn cenTableColumn : genTable.getColumns())
  112. {
  113. genTableColumnMapper.updateGenTableColumn(cenTableColumn);
  114. }
  115. }
  116. }
  117. /**
  118. * 删除业务对象
  119. *
  120. * @param tableIds 需要删除的数据ID
  121. * @return 结果
  122. */
  123. @Override
  124. @Transactional
  125. public void deleteGenTableByIds(Long[] tableIds)
  126. {
  127. genTableMapper.deleteGenTableByIds(tableIds);
  128. genTableColumnMapper.deleteGenTableColumnByIds(tableIds);
  129. }
  130. /**
  131. * 导入表结构
  132. *
  133. * @param tableList 导入表列表
  134. */
  135. @Override
  136. @Transactional
  137. public void importGenTable(List<GenTable> tableList)
  138. {
  139. String operName = SecurityUtils.getUsername();
  140. try
  141. {
  142. for (GenTable table : tableList)
  143. {
  144. String tableName = table.getTableName();
  145. GenUtils.initTable(table, operName);
  146. int row = genTableMapper.insertGenTable(table);
  147. if (row > 0)
  148. {
  149. // 保存列信息
  150. List<GenTableColumn> genTableColumns = genTableColumnMapper.selectDbTableColumnsByName(tableName);
  151. for (GenTableColumn column : genTableColumns)
  152. {
  153. GenUtils.initColumnField(column, table);
  154. genTableColumnMapper.insertGenTableColumn(column);
  155. }
  156. }
  157. }
  158. }
  159. catch (Exception e)
  160. {
  161. throw new CustomException("导入失败:" + e.getMessage());
  162. }
  163. }
  164. /**
  165. * 预览代码
  166. *
  167. * @param tableId 表编号
  168. * @return 预览数据列表
  169. */
  170. public Map<String, String> previewCode(Long tableId)
  171. {
  172. Map<String, String> dataMap = new LinkedHashMap<>();
  173. // 查询表信息
  174. GenTable table = genTableMapper.selectGenTableById(tableId);
  175. // 查询列信息
  176. List<GenTableColumn> columns = table.getColumns();
  177. setPkColumn(table, columns);
  178. VelocityInitializer.initVelocity();
  179. VelocityContext context = VelocityUtils.prepareContext(table);
  180. // 获取模板列表
  181. List<String> templates = VelocityUtils.getTemplateList(table.getTplCategory());
  182. for (String template : templates)
  183. {
  184. // 渲染模板
  185. StringWriter sw = new StringWriter();
  186. Template tpl = Velocity.getTemplate(template, Constants.UTF8);
  187. tpl.merge(context, sw);
  188. dataMap.put(template, sw.toString());
  189. }
  190. return dataMap;
  191. }
  192. /**
  193. * 生成代码(下载方式)
  194. *
  195. * @param tableName 表名称
  196. * @return 数据
  197. */
  198. @Override
  199. public byte[] downloadCode(String tableName)
  200. {
  201. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  202. ZipOutputStream zip = new ZipOutputStream(outputStream);
  203. generatorCode(tableName, zip);
  204. IOUtils.closeQuietly(zip);
  205. return outputStream.toByteArray();
  206. }
  207. /**
  208. * 生成代码(自定义路径)
  209. *
  210. * @param tableName 表名称
  211. */
  212. @Override
  213. public void generatorCode(String tableName)
  214. {
  215. // 查询表信息
  216. GenTable table = genTableMapper.selectGenTableByName(tableName);
  217. // 查询列信息
  218. List<GenTableColumn> columns = table.getColumns();
  219. setPkColumn(table, columns);
  220. VelocityInitializer.initVelocity();
  221. VelocityContext context = VelocityUtils.prepareContext(table);
  222. // 获取模板列表
  223. List<String> templates = VelocityUtils.getTemplateList(table.getTplCategory());
  224. for (String template : templates)
  225. {
  226. if (!StringUtils.containsAny(template, "sql.vm", "api.js.vm", "index.vue.vm", "index-tree.vue.vm"))
  227. {
  228. // 渲染模板
  229. StringWriter sw = new StringWriter();
  230. Template tpl = Velocity.getTemplate(template, Constants.UTF8);
  231. tpl.merge(context, sw);
  232. try
  233. {
  234. String path = getGenPath(table, template);
  235. FileUtils.writeStringToFile(new File(path), sw.toString(), CharsetKit.UTF_8);
  236. }
  237. catch (IOException e)
  238. {
  239. throw new CustomException("渲染模板失败,表名:" + table.getTableName());
  240. }
  241. }
  242. }
  243. }
  244. /**
  245. * 同步数据库
  246. *
  247. * @param tableName 表名称
  248. */
  249. @Override
  250. @Transactional
  251. public void synchDb(String tableName)
  252. {
  253. GenTable table = genTableMapper.selectGenTableByName(tableName);
  254. List<GenTableColumn> tableColumns = table.getColumns();
  255. List<String> tableColumnNames = tableColumns.stream().map(GenTableColumn::getColumnName).collect(Collectors.toList());
  256. List<GenTableColumn> dbTableColumns = genTableColumnMapper.selectDbTableColumnsByName(tableName);
  257. List<String> dbTableColumnNames = dbTableColumns.stream().map(GenTableColumn::getColumnName).collect(Collectors.toList());
  258. dbTableColumns.forEach(column -> {
  259. if (!tableColumnNames.contains(column.getColumnName()))
  260. {
  261. GenUtils.initColumnField(column, table);
  262. genTableColumnMapper.insertGenTableColumn(column);
  263. }
  264. });
  265. List<GenTableColumn> delColumns = tableColumns.stream().filter(column -> !dbTableColumnNames.contains(column.getColumnName())).collect(Collectors.toList());
  266. if (StringUtils.isNotEmpty(delColumns))
  267. {
  268. genTableColumnMapper.deleteGenTableColumns(delColumns);
  269. }
  270. }
  271. /**
  272. * 批量生成代码(下载方式)
  273. *
  274. * @param tableNames 表数组
  275. * @return 数据
  276. */
  277. @Override
  278. public byte[] downloadCode(String[] tableNames)
  279. {
  280. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  281. ZipOutputStream zip = new ZipOutputStream(outputStream);
  282. for (String tableName : tableNames)
  283. {
  284. generatorCode(tableName, zip);
  285. }
  286. IOUtils.closeQuietly(zip);
  287. return outputStream.toByteArray();
  288. }
  289. /**
  290. * 查询表信息并生成代码
  291. */
  292. private void generatorCode(String tableName, ZipOutputStream zip)
  293. {
  294. // 查询表信息
  295. GenTable table = genTableMapper.selectGenTableByName(tableName);
  296. // 查询列信息
  297. List<GenTableColumn> columns = table.getColumns();
  298. setPkColumn(table, columns);
  299. VelocityInitializer.initVelocity();
  300. VelocityContext context = VelocityUtils.prepareContext(table);
  301. // 获取模板列表
  302. List<String> templates = VelocityUtils.getTemplateList(table.getTplCategory());
  303. for (String template : templates)
  304. {
  305. // 渲染模板
  306. StringWriter sw = new StringWriter();
  307. Template tpl = Velocity.getTemplate(template, Constants.UTF8);
  308. tpl.merge(context, sw);
  309. try
  310. {
  311. // 添加到zip
  312. zip.putNextEntry(new ZipEntry(VelocityUtils.getFileName(template, table)));
  313. IOUtils.write(sw.toString(), zip, Constants.UTF8);
  314. IOUtils.closeQuietly(sw);
  315. zip.flush();
  316. zip.closeEntry();
  317. }
  318. catch (IOException e)
  319. {
  320. log.error("渲染模板失败,表名:" + table.getTableName(), e);
  321. }
  322. }
  323. }
  324. /**
  325. * 修改保存参数校验
  326. *
  327. * @param genTable 业务信息
  328. */
  329. @Override
  330. public void validateEdit(GenTable genTable)
  331. {
  332. if (GenConstants.TPL_TREE.equals(genTable.getTplCategory()))
  333. {
  334. String options = JSON.toJSONString(genTable.getParams());
  335. JSONObject paramsObj = JSONObject.parseObject(options);
  336. if (StringUtils.isEmpty(paramsObj.getString(GenConstants.TREE_CODE)))
  337. {
  338. throw new CustomException("树编码字段不能为空");
  339. }
  340. else if (StringUtils.isEmpty(paramsObj.getString(GenConstants.TREE_PARENT_CODE)))
  341. {
  342. throw new CustomException("树父编码字段不能为空");
  343. }
  344. else if (StringUtils.isEmpty(paramsObj.getString(GenConstants.TREE_NAME)))
  345. {
  346. throw new CustomException("树名称字段不能为空");
  347. }
  348. }
  349. }
  350. /**
  351. * 设置主键列信息
  352. *
  353. * @param table 业务表信息
  354. * @param columns 业务字段列表
  355. */
  356. public void setPkColumn(GenTable table, List<GenTableColumn> columns)
  357. {
  358. for (GenTableColumn column : columns)
  359. {
  360. if (column.isPk())
  361. {
  362. table.setPkColumn(column);
  363. break;
  364. }
  365. }
  366. if (StringUtils.isNull(table.getPkColumn()))
  367. {
  368. table.setPkColumn(columns.get(0));
  369. }
  370. }
  371. /**
  372. * 设置代码生成其他选项值
  373. *
  374. * @param genTable 设置后的生成对象
  375. */
  376. public void setTableFromOptions(GenTable genTable)
  377. {
  378. JSONObject paramsObj = JSONObject.parseObject(genTable.getOptions());
  379. if (StringUtils.isNotNull(paramsObj))
  380. {
  381. String treeCode = paramsObj.getString(GenConstants.TREE_CODE);
  382. String treeParentCode = paramsObj.getString(GenConstants.TREE_PARENT_CODE);
  383. String treeName = paramsObj.getString(GenConstants.TREE_NAME);
  384. String parentMenuId = paramsObj.getString(GenConstants.PARENT_MENU_ID);
  385. String parentMenuName = paramsObj.getString(GenConstants.PARENT_MENU_NAME);
  386. genTable.setTreeCode(treeCode);
  387. genTable.setTreeParentCode(treeParentCode);
  388. genTable.setTreeName(treeName);
  389. genTable.setParentMenuId(parentMenuId);
  390. genTable.setParentMenuName(parentMenuName);
  391. }
  392. }
  393. /**
  394. * 获取代码生成地址
  395. *
  396. * @param table 业务表信息
  397. * @param template 模板文件路径
  398. * @return 生成地址
  399. */
  400. public static String getGenPath(GenTable table, String template)
  401. {
  402. String genPath = table.getGenPath();
  403. if (StringUtils.equals(genPath, "/"))
  404. {
  405. return System.getProperty("user.dir") + File.separator + "src" + File.separator + VelocityUtils.getFileName(template, table);
  406. }
  407. return genPath + File.separator + VelocityUtils.getFileName(template, table);
  408. }
  409. }