From e818078392e2db431107af198b8a03ba23473e23 Mon Sep 17 00:00:00 2001
From: RuoYi <yzz_ivy@163.com>
Date: 星期五, 24 七月 2020 15:22:28 +0800
Subject: [PATCH] 代码生成支持自定义路径

---
 src/main/java/com/ruoyi/project/tool/gen/service/GenTableServiceImpl.java |   70 +++++++++++++++++++++++++++++++++--
 1 files changed, 66 insertions(+), 4 deletions(-)

diff --git a/src/main/java/com/ruoyi/project/tool/gen/service/GenTableServiceImpl.java b/src/main/java/com/ruoyi/project/tool/gen/service/GenTableServiceImpl.java
index 8dc18fb..4347993 100644
--- a/src/main/java/com/ruoyi/project/tool/gen/service/GenTableServiceImpl.java
+++ b/src/main/java/com/ruoyi/project/tool/gen/service/GenTableServiceImpl.java
@@ -1,6 +1,7 @@
 package com.ruoyi.project.tool.gen.service;
 
 import java.io.ByteArrayOutputStream;
+import java.io.File;
 import java.io.IOException;
 import java.io.StringWriter;
 import java.util.LinkedHashMap;
@@ -21,9 +22,11 @@
 import com.alibaba.fastjson.JSONObject;
 import com.ruoyi.common.constant.Constants;
 import com.ruoyi.common.constant.GenConstants;
+import com.ruoyi.common.core.text.CharsetKit;
 import com.ruoyi.common.exception.CustomException;
 import com.ruoyi.common.utils.SecurityUtils;
 import com.ruoyi.common.utils.StringUtils;
+import com.ruoyi.common.utils.file.FileUtils;
 import com.ruoyi.project.tool.gen.domain.GenTable;
 import com.ruoyi.project.tool.gen.domain.GenTableColumn;
 import com.ruoyi.project.tool.gen.mapper.GenTableColumnMapper;
@@ -202,13 +205,13 @@
     }
 
     /**
-     * 鐢熸垚浠g爜
+     * 鐢熸垚浠g爜锛堜笅杞芥柟寮忥級
      * 
      * @param tableName 琛ㄥ悕绉�
      * @return 鏁版嵁
      */
     @Override
-    public byte[] generatorCode(String tableName)
+    public byte[] downloadCode(String tableName)
     {
         ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
         ZipOutputStream zip = new ZipOutputStream(outputStream);
@@ -218,13 +221,55 @@
     }
 
     /**
-     * 鎵归噺鐢熸垚浠g爜
+     * 鐢熸垚浠g爜锛堣嚜瀹氫箟璺緞锛�
+     * 
+     * @param tableName 琛ㄥ悕绉�
+     * @return 鏁版嵁
+     */
+    @Override
+    public void generatorCode(String tableName)
+    {
+        // 鏌ヨ琛ㄤ俊鎭�
+        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)
+        {
+            if (!StringUtils.containsAny(template, "sql.vm", "api.js.vm", "index.vue.vm", "index-tree.vue.vm"))
+            {
+                // 娓叉煋妯℃澘
+                StringWriter sw = new StringWriter();
+                Template tpl = Velocity.getTemplate(template, Constants.UTF8);
+                tpl.merge(context, sw);
+                try
+                {
+                    String path = getGenPath(table, template);
+                    FileUtils.writeStringToFile(new File(path), sw.toString(), CharsetKit.UTF_8);
+                }
+                catch (IOException e)
+                {
+                    throw new CustomException("娓叉煋妯℃澘澶辫触锛岃〃鍚嶏細" + table.getTableName());
+                }
+            }
+        }
+    }
+
+    /**
+     * 鎵归噺鐢熸垚浠g爜锛堜笅杞芥柟寮忥級
      * 
      * @param tableNames 琛ㄦ暟缁�
      * @return 鏁版嵁
      */
     @Override
-    public byte[] generatorCode(String[] tableNames)
+    public byte[] downloadCode(String[] tableNames)
     {
         ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
         ZipOutputStream zip = new ZipOutputStream(outputStream);
@@ -347,4 +392,21 @@
             genTable.setParentMenuName(parentMenuName);
         }
     }
+
+    /**
+     * 鑾峰彇浠g爜鐢熸垚鍦板潃
+     * 
+     * @param table 涓氬姟琛ㄤ俊鎭�
+     * @param template 妯℃澘鏂囦欢璺緞
+     * @return 鐢熸垚鍦板潃
+     */
+    public static String getGenPath(GenTable table, String template)
+    {
+        String genPath = table.getGenPath();
+        if (StringUtils.equals(genPath, "/"))
+        {
+            return System.getProperty("user.dir") + File.separator + "src" + File.separator + VelocityUtils.getFileName(template, table);
+        }
+        return genPath + File.separator + VelocityUtils.getFileName(template, table);
+    }
 }
\ No newline at end of file

--
Gitblit v1.9.3