GenTableServiceImpl.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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.FileUtils;
  13. import org.apache.commons.io.IOUtils;
  14. import org.apache.velocity.Template;
  15. import org.apache.velocity.VelocityContext;
  16. import org.apache.velocity.app.Velocity;
  17. import org.slf4j.Logger;
  18. import org.slf4j.LoggerFactory;
  19. import org.springframework.beans.factory.annotation.Autowired;
  20. import org.springframework.stereotype.Service;
  21. import org.springframework.transaction.annotation.Transactional;
  22. import com.alibaba.fastjson.JSON;
  23. import com.alibaba.fastjson.JSONObject;
  24. import com.ruoyi.common.core.constant.Constants;
  25. import com.ruoyi.common.core.constant.GenConstants;
  26. import com.ruoyi.common.core.exception.ServiceException;
  27. import com.ruoyi.common.core.text.CharsetKit;
  28. import com.ruoyi.common.core.utils.StringUtils;
  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. * @return 表信息集合
  100. */
  101. @Override
  102. public List<GenTable> selectGenTableAll()
  103. {
  104. return genTableMapper.selectGenTableAll();
  105. }
  106. /**
  107. * 修改业务
  108. *
  109. * @param genTable 业务信息
  110. * @return 结果
  111. */
  112. @Override
  113. @Transactional
  114. public void updateGenTable(GenTable genTable)
  115. {
  116. String options = JSON.toJSONString(genTable.getParams());
  117. genTable.setOptions(options);
  118. int row = genTableMapper.updateGenTable(genTable);
  119. if (row > 0)
  120. {
  121. for (GenTableColumn cenTableColumn : genTable.getColumns())
  122. {
  123. genTableColumnMapper.updateGenTableColumn(cenTableColumn);
  124. }
  125. }
  126. }
  127. /**
  128. * 删除业务对象
  129. *
  130. * @param tableIds 需要删除的数据ID
  131. * @return 结果
  132. */
  133. @Override
  134. @Transactional
  135. public void deleteGenTableByIds(Long[] tableIds)
  136. {
  137. genTableMapper.deleteGenTableByIds(tableIds);
  138. genTableColumnMapper.deleteGenTableColumnByIds(tableIds);
  139. }
  140. /**
  141. * 导入表结构
  142. *
  143. * @param tableList 导入表列表
  144. */
  145. @Override
  146. @Transactional
  147. public void importGenTable(List<GenTable> tableList)
  148. {
  149. String operName = SecurityUtils.getUsername();
  150. try
  151. {
  152. for (GenTable table : tableList)
  153. {
  154. String tableName = table.getTableName();
  155. GenUtils.initTable(table, operName);
  156. int row = genTableMapper.insertGenTable(table);
  157. if (row > 0)
  158. {
  159. // 保存列信息
  160. List<GenTableColumn> genTableColumns = genTableColumnMapper.selectDbTableColumnsByName(tableName);
  161. for (GenTableColumn column : genTableColumns)
  162. {
  163. GenUtils.initColumnField(column, table);
  164. genTableColumnMapper.insertGenTableColumn(column);
  165. }
  166. }
  167. }
  168. }
  169. catch (Exception e)
  170. {
  171. throw new ServiceException("导入失败:" + e.getMessage());
  172. }
  173. }
  174. /**
  175. * 预览代码
  176. *
  177. * @param tableId 表编号
  178. * @return 预览数据列表
  179. */
  180. @Override
  181. public Map<String, String> previewCode(Long tableId)
  182. {
  183. Map<String, String> dataMap = new LinkedHashMap<>();
  184. // 查询表信息
  185. GenTable table = genTableMapper.selectGenTableById(tableId);
  186. // 设置主子表信息
  187. setSubTable(table);
  188. // 设置主键列信息
  189. setPkColumn(table);
  190. VelocityInitializer.initVelocity();
  191. VelocityContext context = VelocityUtils.prepareContext(table);
  192. // 获取模板列表
  193. List<String> templates = VelocityUtils.getTemplateList(table.getTplCategory());
  194. for (String template : templates)
  195. {
  196. // 渲染模板
  197. StringWriter sw = new StringWriter();
  198. Template tpl = Velocity.getTemplate(template, Constants.UTF8);
  199. tpl.merge(context, sw);
  200. dataMap.put(template, sw.toString());
  201. }
  202. return dataMap;
  203. }
  204. /**
  205. * 生成代码(下载方式)
  206. *
  207. * @param tableName 表名称
  208. * @return 数据
  209. */
  210. @Override
  211. public byte[] downloadCode(String tableName)
  212. {
  213. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  214. ZipOutputStream zip = new ZipOutputStream(outputStream);
  215. generatorCode(tableName, zip);
  216. IOUtils.closeQuietly(zip);
  217. return outputStream.toByteArray();
  218. }
  219. /**
  220. * 生成代码(自定义路径)
  221. *
  222. * @param tableName 表名称
  223. */
  224. @Override
  225. public void generatorCode(String tableName)
  226. {
  227. // 查询表信息
  228. GenTable table = genTableMapper.selectGenTableByName(tableName);
  229. // 设置主子表信息
  230. setSubTable(table);
  231. // 设置主键列信息
  232. setPkColumn(table);
  233. VelocityInitializer.initVelocity();
  234. VelocityContext context = VelocityUtils.prepareContext(table);
  235. // 获取模板列表
  236. List<String> templates = VelocityUtils.getTemplateList(table.getTplCategory());
  237. for (String template : templates)
  238. {
  239. if (!StringUtils.containsAny(template, "sql.vm", "api.js.vm", "index.vue.vm", "index-tree.vue.vm"))
  240. {
  241. // 渲染模板
  242. StringWriter sw = new StringWriter();
  243. Template tpl = Velocity.getTemplate(template, Constants.UTF8);
  244. tpl.merge(context, sw);
  245. try
  246. {
  247. String path = getGenPath(table, template);
  248. FileUtils.writeStringToFile(new File(path), sw.toString(), CharsetKit.UTF_8);
  249. }
  250. catch (IOException e)
  251. {
  252. throw new ServiceException("渲染模板失败,表名:" + table.getTableName());
  253. }
  254. }
  255. }
  256. }
  257. /**
  258. * 同步数据库
  259. *
  260. * @param tableName 表名称
  261. */
  262. @Override
  263. @Transactional
  264. public void synchDb(String tableName)
  265. {
  266. GenTable table = genTableMapper.selectGenTableByName(tableName);
  267. List<GenTableColumn> tableColumns = table.getColumns();
  268. List<String> tableColumnNames = tableColumns.stream().map(GenTableColumn::getColumnName).collect(Collectors.toList());
  269. List<GenTableColumn> dbTableColumns = genTableColumnMapper.selectDbTableColumnsByName(tableName);
  270. if (StringUtils.isEmpty(dbTableColumns))
  271. {
  272. throw new ServiceException("同步数据失败,原表结构不存在");
  273. }
  274. List<String> dbTableColumnNames = dbTableColumns.stream().map(GenTableColumn::getColumnName).collect(Collectors.toList());
  275. dbTableColumns.forEach(column -> {
  276. if (!tableColumnNames.contains(column.getColumnName()))
  277. {
  278. GenUtils.initColumnField(column, table);
  279. genTableColumnMapper.insertGenTableColumn(column);
  280. }
  281. });
  282. List<GenTableColumn> delColumns = tableColumns.stream().filter(column -> !dbTableColumnNames.contains(column.getColumnName())).collect(Collectors.toList());
  283. if (StringUtils.isNotEmpty(delColumns))
  284. {
  285. genTableColumnMapper.deleteGenTableColumns(delColumns);
  286. }
  287. }
  288. /**
  289. * 批量生成代码(下载方式)
  290. *
  291. * @param tableNames 表数组
  292. * @return 数据
  293. */
  294. @Override
  295. public byte[] downloadCode(String[] tableNames)
  296. {
  297. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  298. ZipOutputStream zip = new ZipOutputStream(outputStream);
  299. for (String tableName : tableNames)
  300. {
  301. generatorCode(tableName, zip);
  302. }
  303. IOUtils.closeQuietly(zip);
  304. return outputStream.toByteArray();
  305. }
  306. /**
  307. * 查询表信息并生成代码
  308. */
  309. private void generatorCode(String tableName, ZipOutputStream zip)
  310. {
  311. // 查询表信息
  312. GenTable table = genTableMapper.selectGenTableByName(tableName);
  313. // 设置主子表信息
  314. setSubTable(table);
  315. // 设置主键列信息
  316. setPkColumn(table);
  317. VelocityInitializer.initVelocity();
  318. VelocityContext context = VelocityUtils.prepareContext(table);
  319. // 获取模板列表
  320. List<String> templates = VelocityUtils.getTemplateList(table.getTplCategory());
  321. for (String template : templates)
  322. {
  323. // 渲染模板
  324. StringWriter sw = new StringWriter();
  325. Template tpl = Velocity.getTemplate(template, Constants.UTF8);
  326. tpl.merge(context, sw);
  327. try
  328. {
  329. // 添加到zip
  330. zip.putNextEntry(new ZipEntry(VelocityUtils.getFileName(template, table)));
  331. IOUtils.write(sw.toString(), zip, Constants.UTF8);
  332. IOUtils.closeQuietly(sw);
  333. zip.flush();
  334. zip.closeEntry();
  335. }
  336. catch (IOException e)
  337. {
  338. log.error("渲染模板失败,表名:" + table.getTableName(), e);
  339. }
  340. }
  341. }
  342. /**
  343. * 修改保存参数校验
  344. *
  345. * @param genTable 业务信息
  346. */
  347. @Override
  348. public void validateEdit(GenTable genTable)
  349. {
  350. if (GenConstants.TPL_TREE.equals(genTable.getTplCategory()))
  351. {
  352. String options = JSON.toJSONString(genTable.getParams());
  353. JSONObject paramsObj = JSONObject.parseObject(options);
  354. if (StringUtils.isEmpty(paramsObj.getString(GenConstants.TREE_CODE)))
  355. {
  356. throw new ServiceException("树编码字段不能为空");
  357. }
  358. else if (StringUtils.isEmpty(paramsObj.getString(GenConstants.TREE_PARENT_CODE)))
  359. {
  360. throw new ServiceException("树父编码字段不能为空");
  361. }
  362. else if (StringUtils.isEmpty(paramsObj.getString(GenConstants.TREE_NAME)))
  363. {
  364. throw new ServiceException("树名称字段不能为空");
  365. }
  366. else if (GenConstants.TPL_SUB.equals(genTable.getTplCategory()))
  367. {
  368. if (StringUtils.isEmpty(genTable.getSubTableName()))
  369. {
  370. throw new ServiceException("关联子表的表名不能为空");
  371. }
  372. else if (StringUtils.isEmpty(genTable.getSubTableFkName()))
  373. {
  374. throw new ServiceException("子表关联的外键名不能为空");
  375. }
  376. }
  377. }
  378. }
  379. /**
  380. * 设置主键列信息
  381. *
  382. * @param table 业务表信息
  383. */
  384. public void setPkColumn(GenTable table)
  385. {
  386. for (GenTableColumn column : table.getColumns())
  387. {
  388. if (column.isPk())
  389. {
  390. table.setPkColumn(column);
  391. break;
  392. }
  393. }
  394. if (StringUtils.isNull(table.getPkColumn()))
  395. {
  396. table.setPkColumn(table.getColumns().get(0));
  397. }
  398. if (GenConstants.TPL_SUB.equals(table.getTplCategory()))
  399. {
  400. for (GenTableColumn column : table.getSubTable().getColumns())
  401. {
  402. if (column.isPk())
  403. {
  404. table.getSubTable().setPkColumn(column);
  405. break;
  406. }
  407. }
  408. if (StringUtils.isNull(table.getSubTable().getPkColumn()))
  409. {
  410. table.getSubTable().setPkColumn(table.getSubTable().getColumns().get(0));
  411. }
  412. }
  413. }
  414. /**
  415. * 设置主子表信息
  416. *
  417. * @param table 业务表信息
  418. */
  419. public void setSubTable(GenTable table)
  420. {
  421. String subTableName = table.getSubTableName();
  422. if (StringUtils.isNotEmpty(subTableName))
  423. {
  424. table.setSubTable(genTableMapper.selectGenTableByName(subTableName));
  425. }
  426. }
  427. /**
  428. * 设置代码生成其他选项值
  429. *
  430. * @param genTable 设置后的生成对象
  431. */
  432. public void setTableFromOptions(GenTable genTable)
  433. {
  434. JSONObject paramsObj = JSONObject.parseObject(genTable.getOptions());
  435. if (StringUtils.isNotNull(paramsObj))
  436. {
  437. String treeCode = paramsObj.getString(GenConstants.TREE_CODE);
  438. String treeParentCode = paramsObj.getString(GenConstants.TREE_PARENT_CODE);
  439. String treeName = paramsObj.getString(GenConstants.TREE_NAME);
  440. String parentMenuId = paramsObj.getString(GenConstants.PARENT_MENU_ID);
  441. String parentMenuName = paramsObj.getString(GenConstants.PARENT_MENU_NAME);
  442. genTable.setTreeCode(treeCode);
  443. genTable.setTreeParentCode(treeParentCode);
  444. genTable.setTreeName(treeName);
  445. genTable.setParentMenuId(parentMenuId);
  446. genTable.setParentMenuName(parentMenuName);
  447. }
  448. }
  449. /**
  450. * 获取代码生成地址
  451. *
  452. * @param table 业务表信息
  453. * @param template 模板文件路径
  454. * @return 生成地址
  455. */
  456. public static String getGenPath(GenTable table, String template)
  457. {
  458. String genPath = table.getGenPath();
  459. if (StringUtils.equals(genPath, "/"))
  460. {
  461. return System.getProperty("user.dir") + File.separator + "src" + File.separator + VelocityUtils.getFileName(template, table);
  462. }
  463. return genPath + File.separator + VelocityUtils.getFileName(template, table);
  464. }
  465. }