drew 5 лет назад
Родитель
Сommit
95af2ed551

+ 30 - 16
src/main/java/com/izouma/awesomeAdmin/service/GenCodeService.java

@@ -33,7 +33,8 @@ public class GenCodeService {
     public void genController(GenCode model) throws IOException, TemplateException {
 
         Path targetFile = StringUtils.isNotBlank(model.getGenPackage()) ?
-                Paths.get(model.getJavaPath(), "web", model.getGenPackage(), model.getClassName() + "Controller.java").toAbsolutePath() :
+                Paths.get(model.getJavaPath(), "web", model.getGenPackage(), model.getClassName() + "Controller.java")
+                        .toAbsolutePath() :
                 Paths.get(model.getJavaPath(), "web", model.getClassName() + "Controller.java").toAbsolutePath();
         Map<String, Object> extra = new HashMap<>();
         if (StringUtil.isNotEmpty(model.getGenPackage())) {
@@ -42,38 +43,38 @@ public class GenCodeService {
             extra.put("imports", imports);
         }
         extra.put("subPackage", StringUtil.isNotEmpty(model.getGenPackage()));
+        extra.put("softDelete", canSoftDelete(model));
         genFile("ControllerTemplate.ftl", model, extra, targetFile);
         log.info("成功生成Controller:{}", targetFile.toString());
     }
 
     public void genService(GenCode model) throws IOException, TemplateException {
         Path targetFile = StringUtils.isNotBlank(model.getGenPackage()) ?
-                Paths.get(model.getJavaPath(), "service", model.getGenPackage(), model.getClassName() + "Service.java").toAbsolutePath() :
+                Paths.get(model.getJavaPath(), "service", model.getGenPackage(), model.getClassName() + "Service.java")
+                        .toAbsolutePath() :
                 Paths.get(model.getJavaPath(), "service", model.getClassName() + "Service.java").toAbsolutePath();
-        genFile("ServiceTemplate.ftl", model, null, targetFile);
+        Map<String, Object> extra = new HashMap<>();
+        extra.put("softDelete", canSoftDelete(model));
+        genFile("ServiceTemplate.ftl", model, extra, targetFile);
         log.info("成功生成Service:{}", targetFile.toString());
     }
 
     public void genRepo(GenCode model) throws IOException, TemplateException, ClassNotFoundException {
         Path targetFile = StringUtils.isNotBlank(model.getGenPackage()) ?
-                Paths.get(model.getJavaPath(), "repo", model.getGenPackage(), model.getClassName() + "Repo.java").toAbsolutePath() :
+                Paths.get(model.getJavaPath(), "repo", model.getGenPackage(), model.getClassName() + "Repo.java")
+                        .toAbsolutePath() :
                 Paths.get(model.getJavaPath(), "repo", model.getClassName() + "Repo.java").toAbsolutePath();
         Map<String, Object> extra = new HashMap<>();
-        extra.put("softDelete", false);
-        try {
-            Field field = Class.forName(model.getTablePackage()).getDeclaredField("enabled");
-            if (field != null && field.getType().equals(Boolean.class)) {
-                extra.put("softDelete", true);
-            }
-        } catch (NoSuchFieldException ignored) {
-        }
+        extra.put("softDelete", canSoftDelete(model));
         genFile("RepoTemplate.ftl", model, extra, targetFile);
         log.info("成功生成Repo:{}", targetFile.toString());
     }
 
     public void genListView(GenCode model) throws IOException, TemplateException {
         Path targetFile = Paths.get(model.getViewPath(), model.getClassName() + "List.vue").toAbsolutePath();
-        genFile("ListViewTemplate.ftl", model, null, targetFile);
+        Map<String, Object> extra = new HashMap<>();
+        extra.put("softDelete", canSoftDelete(model));
+        genFile("ListViewTemplate.ftl", model, extra, targetFile);
         log.info("成功生成ListView:{}", targetFile.toString());
     }
 
@@ -89,6 +90,7 @@ public class GenCodeService {
             }
         }
         map.put("labelWidth", maxLabelWidth + 25 + "px");
+        map.put("softDelete", canSoftDelete(model));
         genFile("EditViewTemplate.ftl", model, map, targetFile);
         log.info("成功生成EditView:{}", targetFile.toString());
     }
@@ -112,7 +114,8 @@ public class GenCodeService {
     private int measureText(String text, float fontSize) throws IOException, FontFormatException {
         AffineTransform affinetransform = new AffineTransform();
         FontRenderContext frc = new FontRenderContext(affinetransform, true, true);
-        Font font = Font.createFont(Font.TRUETYPE_FONT, this.getClass().getResourceAsStream("/font/SourceHanSansCN-Normal.ttf"));
+        Font font = Font.createFont(Font.TRUETYPE_FONT, this.getClass()
+                .getResourceAsStream("/font/SourceHanSansCN-Normal.ttf"));
         return (int) (font.deriveFont(fontSize).getStringBounds(text, frc).getWidth());
     }
 
@@ -139,14 +142,14 @@ public class GenCodeService {
                         + "Edit',\n                    component: () => import(/* webpackChunkName: \"" + routePath + "Edit\" */ '@/views/"
                         + routeName
                         + "Edit.vue'),\n                    meta: {\n                       title: '" + remark + "编辑',\n"
-                        +"                    },\n                },\n                {\n                    path: '/"
+                        + "                    },\n                },\n                {\n                    path: '/"
                         + routePath
                         + "List',\n                    name: '"
                         + routeName
                         + "List',\n                    component: () => import(/* webpackChunkName: \"" + routePath + "List\" */ '@/views/"
                         + routeName
                         + "List.vue'),\n                    meta: {\n                       title: '" + remark + "',\n"
-                        +"                    },\n               }\n                ";
+                        + "                    },\n               }\n                ";
                 boolean needComma = !routerJs.toString().substring(0, insertLocation).trim().endsWith(",");
                 if (needComma) {
                     routerJs.insert(routerJs.toString().substring(0, insertLocation).lastIndexOf("}") + 1, ",");
@@ -164,4 +167,15 @@ public class GenCodeService {
             e.printStackTrace();
         }
     }
+
+    private boolean canSoftDelete(GenCode model) {
+        try {
+            Field field = Class.forName(model.getTablePackage()).getDeclaredField("enabled");
+            if (field != null && field.getType().equals(Boolean.class)) {
+                return true;
+            }
+        } catch (NoSuchFieldException | ClassNotFoundException ignored) {
+        }
+        return false;
+    }
 }

+ 10 - 7
src/main/java/com/izouma/awesomeAdmin/web/GenCodeController.java

@@ -34,7 +34,8 @@ public class GenCodeController {
 
         List<GenCode> genCodeList = new ArrayList<>();
 
-        String genJsonPath = Paths.get(System.getProperty("user.dir"), "src", "main", "resources", "genjson").toString();
+        String genJsonPath = Paths.get(System.getProperty("user.dir"), "src", "main", "resources", "genjson")
+                .toString();
 
 //            File file = ResourceUtils.getFile("classpath:genjson");
         File file = new File(genJsonPath);
@@ -58,7 +59,8 @@ public class GenCodeController {
 
     @GetMapping("/getOne")
     public GenCode getGenCode(@RequestParam String className) throws IOException {
-        File file = new File(Paths.get(System.getProperty("user.dir"), "src", "main", "resources", "genjson", className + ".json").toString());
+        File file = new File(Paths.get(System.getProperty("user.dir"), "src", "main", "resources", "genjson", className + ".json")
+                .toString());
 
         BufferedReader reader = new BufferedReader(new FileReader(file));
         Gson gson = new Gson();
@@ -76,7 +78,9 @@ public class GenCodeController {
 
             String basePackage = this.getClass().getPackage().getName().replace(".web", "");
             record.setBasePackage(basePackage);
-            String genPackage = record.getTablePackage().replace(basePackage + ".domain.", "").replace("." + record.getTableName(), "");
+            String genPackage = record.getTablePackage()
+                    .replace(basePackage + ".domain.", "")
+                    .replace("." + record.getTableName(), "");
             if (!genPackage.startsWith(record.getTableName())) {
                 record.setGenPackage(genPackage);
             } else {
@@ -161,11 +165,12 @@ public class GenCodeController {
 //    }
 //
     @GetMapping("/getSrcPath")
-    public Map getSrcPath() {
+    public Map<String, Object> getSrcPath() {
         Map<String, Object> map = new HashMap<>();
         List<String> javaPath = new ArrayList<>(Arrays.asList("src", "main", "java"));
         javaPath.addAll(Arrays.asList(this.getClass().getPackage().getName().split("\\.")));
-        map.put("javaPath", Paths.get(System.getProperty("user.dir"), javaPath.toArray(new String[0])).getParent().toString());
+        map.put("javaPath", Paths.get(System.getProperty("user.dir"),
+                javaPath.toArray(new String[0])).getParent().toString());
         map.put("viewPath", Paths.get(System.getProperty("user.dir"), "src", "main", "vue", "src", "views").toString());
         map.put("routerPath", Paths.get(System.getProperty("user.dir"), "src", "main", "vue", "src").toString());
         map.put("resourcesPath", Paths.get(System.getProperty("user.dir"), "src", "main", "resources").toString());
@@ -218,8 +223,6 @@ public class GenCodeController {
                         desc = (String) enumConstant.getClass().getMethod("getDescription").invoke(enumConstant);
                     } catch (NoSuchMethodException ignore) {
                     }
-                    System.out.println(name);
-                    System.out.println(desc);
                     Map<String, String> map = new HashMap<>();
                     map.put("label", desc);
                     map.put("value", name);

+ 4 - 0
src/main/resources/templates/ControllerTemplate.ftl

@@ -56,7 +56,11 @@ public class ${model.className}Controller extends BaseController {
 
     @PostMapping("/del/{id}")
     public void del(@PathVariable Long id) {
+<#if softDelete == true>
+        ${model.className?uncap_first}Repo.disable(id);
+<#else>
         ${model.className?uncap_first}Repo.deleteById(id);
+</#if>
     }
 
     @GetMapping("/excel")

+ 1 - 1
src/main/resources/templates/RepoTemplate.ftl

@@ -15,6 +15,6 @@ public interface ${model.className}Repo extends JpaRepository<${model.className}
     @Query("update ${model.className} t set t.enabled = false where t.id = ?1")
     @Modifying
     @Transactional
-    void deleteById(Long id);
+    void disable(Long id);
 </#if>
 }