¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.ruoyi.generator.service; |
| | | |
| | | import java.io.ByteArrayOutputStream; |
| | | import java.io.IOException; |
| | | import java.io.StringWriter; |
| | | import java.util.LinkedHashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.zip.ZipEntry; |
| | | import java.util.zip.ZipOutputStream; |
| | | import org.apache.commons.io.IOUtils; |
| | | import org.apache.velocity.Template; |
| | | import org.apache.velocity.VelocityContext; |
| | | import org.apache.velocity.app.Velocity; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.ruoyi.common.constant.Constants; |
| | | import com.ruoyi.common.constant.GenConstants; |
| | | import com.ruoyi.common.exception.CustomException; |
| | | import com.ruoyi.common.utils.SecurityUtils; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.ruoyi.generator.domain.GenTable; |
| | | import com.ruoyi.generator.domain.GenTableColumn; |
| | | import com.ruoyi.generator.mapper.GenTableColumnMapper; |
| | | import com.ruoyi.generator.mapper.GenTableMapper; |
| | | import com.ruoyi.generator.util.GenUtils; |
| | | import com.ruoyi.generator.util.VelocityInitializer; |
| | | import com.ruoyi.generator.util.VelocityUtils; |
| | | |
| | | /** |
| | | * ä¸å¡ æå¡å±å®ç° |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | @Service |
| | | public class GenTableServiceImpl implements IGenTableService |
| | | { |
| | | private static final Logger log = LoggerFactory.getLogger(GenTableServiceImpl.class); |
| | | |
| | | @Autowired |
| | | private GenTableMapper genTableMapper; |
| | | |
| | | @Autowired |
| | | private GenTableColumnMapper genTableColumnMapper; |
| | | |
| | | /** |
| | | * æ¥è¯¢ä¸å¡ä¿¡æ¯ |
| | | * |
| | | * @param id ä¸å¡ID |
| | | * @return ä¸å¡ä¿¡æ¯ |
| | | */ |
| | | @Override |
| | | public GenTable selectGenTableById(Long id) |
| | | { |
| | | GenTable genTable = genTableMapper.selectGenTableById(id); |
| | | setTableFromOptions(genTable); |
| | | return genTable; |
| | | } |
| | | |
| | | /** |
| | | * æ¥è¯¢ä¸å¡å表 |
| | | * |
| | | * @param genTable ä¸å¡ä¿¡æ¯ |
| | | * @return ä¸å¡éå |
| | | */ |
| | | @Override |
| | | public List<GenTable> selectGenTableList(GenTable genTable) |
| | | { |
| | | return genTableMapper.selectGenTableList(genTable); |
| | | } |
| | | |
| | | /** |
| | | * æ¥è¯¢æ®åºå表 |
| | | * |
| | | * @param genTable ä¸å¡ä¿¡æ¯ |
| | | * @return æ°æ®åºè¡¨éå |
| | | */ |
| | | @Override |
| | | public List<GenTable> selectDbTableList(GenTable genTable) |
| | | { |
| | | return genTableMapper.selectDbTableList(genTable); |
| | | } |
| | | |
| | | /** |
| | | * æ¥è¯¢æ®åºå表 |
| | | * |
| | | * @param tableNames 表åç§°ç» |
| | | * @return æ°æ®åºè¡¨éå |
| | | */ |
| | | @Override |
| | | public List<GenTable> selectDbTableListByNames(String[] tableNames) |
| | | { |
| | | return genTableMapper.selectDbTableListByNames(tableNames); |
| | | } |
| | | |
| | | /** |
| | | * ä¿®æ¹ä¸å¡ |
| | | * |
| | | * @param genTable ä¸å¡ä¿¡æ¯ |
| | | * @return ç»æ |
| | | */ |
| | | @Override |
| | | @Transactional |
| | | public void updateGenTable(GenTable genTable) |
| | | { |
| | | String options = JSON.toJSONString(genTable.getParams()); |
| | | genTable.setOptions(options); |
| | | int row = genTableMapper.updateGenTable(genTable); |
| | | if (row > 0) |
| | | { |
| | | for (GenTableColumn cenTableColumn : genTable.getColumns()) |
| | | { |
| | | genTableColumnMapper.updateGenTableColumn(cenTableColumn); |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * å é¤ä¸å¡å¯¹è±¡ |
| | | * |
| | | * @param tableIds éè¦å é¤çæ°æ®ID |
| | | * @return ç»æ |
| | | */ |
| | | @Override |
| | | @Transactional |
| | | public void deleteGenTableByIds(Long[] tableIds) |
| | | { |
| | | genTableMapper.deleteGenTableByIds(tableIds); |
| | | genTableColumnMapper.deleteGenTableColumnByIds(tableIds); |
| | | } |
| | | |
| | | /** |
| | | * 导å
¥è¡¨ç»æ |
| | | * |
| | | * @param tableList 导å
¥è¡¨å表 |
| | | */ |
| | | @Override |
| | | @Transactional |
| | | public void importGenTable(List<GenTable> tableList) |
| | | { |
| | | String operName = SecurityUtils.getUsername(); |
| | | try |
| | | { |
| | | for (GenTable table : tableList) |
| | | { |
| | | String tableName = table.getTableName(); |
| | | GenUtils.initTable(table, operName); |
| | | int row = genTableMapper.insertGenTable(table); |
| | | if (row > 0) |
| | | { |
| | | // ä¿ååä¿¡æ¯ |
| | | List<GenTableColumn> genTableColumns = genTableColumnMapper.selectDbTableColumnsByName(tableName); |
| | | for (GenTableColumn column : genTableColumns) |
| | | { |
| | | GenUtils.initColumnField(column, table); |
| | | genTableColumnMapper.insertGenTableColumn(column); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | throw new CustomException("导å
¥å¤±è´¥ï¼" + e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * é¢è§ä»£ç |
| | | * |
| | | * @param tableId 表ç¼å· |
| | | * @return é¢è§æ°æ®å表 |
| | | */ |
| | | @Override |
| | | public Map<String, String> previewCode(Long tableId) |
| | | { |
| | | Map<String, String> dataMap = new LinkedHashMap<>(); |
| | | // æ¥è¯¢è¡¨ä¿¡æ¯ |
| | | GenTable table = genTableMapper.selectGenTableById(tableId); |
| | | // æ¥è¯¢åä¿¡æ¯ |
| | | List<GenTableColumn> columns = table.getColumns(); |
| | | setPkColumn(table, columns); |
| | | VelocityInitializer.initVelocity(); |
| | | |
| | | VelocityContext context = VelocityUtils.prepareContext(table); |
| | | |
| | | // è·å模æ¿å表 |
| | | List<String> templates = VelocityUtils.getTemplateList(table.getTplCategory()); |
| | | for (String template : templates) |
| | | { |
| | | // æ¸²ææ¨¡æ¿ |
| | | StringWriter sw = new StringWriter(); |
| | | Template tpl = Velocity.getTemplate(template, Constants.UTF8); |
| | | tpl.merge(context, sw); |
| | | dataMap.put(template, sw.toString()); |
| | | } |
| | | return dataMap; |
| | | } |
| | | |
| | | /** |
| | | * çæä»£ç |
| | | * |
| | | * @param tableName 表åç§° |
| | | * @return æ°æ® |
| | | */ |
| | | @Override |
| | | public byte[] generatorCode(String tableName) |
| | | { |
| | | ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
| | | ZipOutputStream zip = new ZipOutputStream(outputStream); |
| | | generatorCode(tableName, zip); |
| | | IOUtils.closeQuietly(zip); |
| | | return outputStream.toByteArray(); |
| | | } |
| | | |
| | | /** |
| | | * æ¹éçæä»£ç |
| | | * |
| | | * @param tableNames 表æ°ç» |
| | | * @return æ°æ® |
| | | */ |
| | | @Override |
| | | public byte[] generatorCode(String[] tableNames) |
| | | { |
| | | ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
| | | ZipOutputStream zip = new ZipOutputStream(outputStream); |
| | | for (String tableName : tableNames) |
| | | { |
| | | generatorCode(tableName, zip); |
| | | } |
| | | IOUtils.closeQuietly(zip); |
| | | return outputStream.toByteArray(); |
| | | } |
| | | |
| | | /** |
| | | * æ¥è¯¢è¡¨ä¿¡æ¯å¹¶çæä»£ç |
| | | */ |
| | | private void generatorCode(String tableName, ZipOutputStream zip) |
| | | { |
| | | // æ¥è¯¢è¡¨ä¿¡æ¯ |
| | | GenTable table = genTableMapper.selectGenTableByName(tableName); |
| | | // æ¥è¯¢åä¿¡æ¯ |
| | | List<GenTableColumn> columns = table.getColumns(); |
| | | setPkColumn(table, columns); |
| | | |
| | | VelocityInitializer.initVelocity(); |
| | | |
| | | VelocityContext context = VelocityUtils.prepareContext(table); |
| | | |
| | | // è·å模æ¿å表 |
| | | List<String> templates = VelocityUtils.getTemplateList(table.getTplCategory()); |
| | | for (String template : templates) |
| | | { |
| | | // æ¸²ææ¨¡æ¿ |
| | | StringWriter sw = new StringWriter(); |
| | | Template tpl = Velocity.getTemplate(template, Constants.UTF8); |
| | | tpl.merge(context, sw); |
| | | try |
| | | { |
| | | // æ·»å å°zip |
| | | zip.putNextEntry(new ZipEntry(VelocityUtils.getFileName(template, table))); |
| | | IOUtils.write(sw.toString(), zip, Constants.UTF8); |
| | | IOUtils.closeQuietly(sw); |
| | | zip.flush(); |
| | | zip.closeEntry(); |
| | | } |
| | | catch (IOException e) |
| | | { |
| | | log.error("æ¸²ææ¨¡æ¿å¤±è´¥ï¼è¡¨åï¼" + table.getTableName(), e); |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * ä¿®æ¹ä¿ååæ°æ ¡éª |
| | | * |
| | | * @param genTable ä¸å¡ä¿¡æ¯ |
| | | */ |
| | | @Override |
| | | public void validateEdit(GenTable genTable) |
| | | { |
| | | if (GenConstants.TPL_TREE.equals(genTable.getTplCategory())) |
| | | { |
| | | String options = JSON.toJSONString(genTable.getParams()); |
| | | JSONObject paramsObj = JSONObject.parseObject(options); |
| | | if (StringUtils.isEmpty(paramsObj.getString(GenConstants.TREE_CODE))) |
| | | { |
| | | throw new CustomException("æ ç¼ç åæ®µä¸è½ä¸ºç©º"); |
| | | } |
| | | else if (StringUtils.isEmpty(paramsObj.getString(GenConstants.TREE_PARENT_CODE))) |
| | | { |
| | | throw new CustomException("æ ç¶ç¼ç åæ®µä¸è½ä¸ºç©º"); |
| | | } |
| | | else if (StringUtils.isEmpty(paramsObj.getString(GenConstants.TREE_NAME))) |
| | | { |
| | | throw new CustomException("æ åç§°åæ®µä¸è½ä¸ºç©º"); |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 设置主é®åä¿¡æ¯ |
| | | * |
| | | * @param table ä¸å¡è¡¨ä¿¡æ¯ |
| | | * @param columns ä¸å¡å段å表 |
| | | */ |
| | | public void setPkColumn(GenTable table, List<GenTableColumn> columns) |
| | | { |
| | | for (GenTableColumn column : columns) |
| | | { |
| | | if (column.isPk()) |
| | | { |
| | | table.setPkColumn(column); |
| | | break; |
| | | } |
| | | } |
| | | if (StringUtils.isNull(table.getPkColumn())) |
| | | { |
| | | table.setPkColumn(columns.get(0)); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 设置代ç çæå
¶ä»éé¡¹å¼ |
| | | * |
| | | * @param genTable 设置åççæå¯¹è±¡ |
| | | */ |
| | | public void setTableFromOptions(GenTable genTable) |
| | | { |
| | | JSONObject paramsObj = JSONObject.parseObject(genTable.getOptions()); |
| | | if (StringUtils.isNotNull(paramsObj)) |
| | | { |
| | | String treeCode = paramsObj.getString(GenConstants.TREE_CODE); |
| | | String treeParentCode = paramsObj.getString(GenConstants.TREE_PARENT_CODE); |
| | | String treeName = paramsObj.getString(GenConstants.TREE_NAME); |
| | | genTable.setTreeCode(treeCode); |
| | | genTable.setTreeParentCode(treeParentCode); |
| | | genTable.setTreeName(treeName); |
| | | } |
| | | } |
| | | } |