瀏覽代碼

把主表关联的附表的信息和附表的字段信息存进redis

shiqian 4 年之前
父節點
當前提交
3a73c9b722

+ 6 - 0
boman-common/boman-common-redis/src/main/java/com/boman/common/redis/RedisKey.java

@@ -16,4 +16,10 @@ public class RedisKey {
      * 维护redis中每张表的sequence
      */
     public static final String SEQ = "SEQ:";
+
+    /**
+     * 主表关联的附表存在redis中的key
+     * eg: relation:tableName
+     */
+    public static final String RELATION = "relation:";
 }

+ 43 - 9
boman-modules/boman-gen/src/main/java/com/boman/gen/controller/MyController.java

@@ -2,10 +2,13 @@ package com.boman.gen.controller;
 
 import com.boman.common.core.web.controller.BaseController;
 import com.boman.common.core.web.domain.AjaxResult;
+import com.boman.common.redis.RedisKey;
 import com.boman.common.redis.service.RedisService;
 import com.boman.gen.domain.GenTable;
 import com.boman.gen.domain.GenTableColumn;
+import com.boman.gen.domain.GenTableRelation;
 import com.boman.gen.service.IGenTableColumnService;
+import com.boman.gen.service.IGenTableRelationService;
 import com.boman.gen.service.IGenTableService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.GetMapping;
@@ -17,6 +20,9 @@ import java.util.List;
 import java.util.concurrent.TimeUnit;
 import java.util.stream.Collectors;
 
+import static com.boman.common.core.utils.obj.ObjectUtils.requireNonNull;
+import static com.boman.common.core.utils.obj.ObjectUtils.requiredNonNull;
+
 /**
  * @author shiqian
  * @description 获取gen_table
@@ -32,6 +38,8 @@ public class MyController extends BaseController {
     private IGenTableColumnService genTableColumnService;
     @Autowired
     private RedisService redisService;
+    @Autowired
+    private IGenTableRelationService genTableRelationService;
 
     /**
      * 功能描述: 查询代码生成列表,table中封装columnList
@@ -40,22 +48,48 @@ public class MyController extends BaseController {
      * @return AjaxResult
      */
     @GetMapping("/loadTable")
-    public AjaxResult loadTable(GenTable genTable) throws ClassNotFoundException {
+    public AjaxResult loadTable(GenTable genTable) {
         List<GenTable> tableList = genTableService.selectGenTableList(genTable);
-        if (null == tableList || tableList.isEmpty()) {
-            return AjaxResult.success();
-        }
+        requiredNonNull(tableList);
 
+        // load table and tableColumn
         List<Long> tableIdList = tableList.stream().map(GenTable::getTableId).collect(Collectors.toList());
         List<GenTableColumn> genTableColumns = genTableColumnService.listByTableIdList(tableIdList);
-        if (null == genTableColumns || genTableColumns.isEmpty()) {
-            return AjaxResult.success();
-        }
-
+        requiredNonNull(genTableColumns);
         packTableAndInsertToRedis(tableList, genTableColumns);
+
+        // load tableRelation
+        List<GenTableRelation> relationList = genTableRelationService.selectGenTableRelationList(new GenTableRelation());
+        requiredNonNull(relationList);
+        packRelationAndInsertToRedis(tableList, relationList);
+
         return AjaxResult.success(tableList);
     }
 
+    /**
+     * 功能描述: 把主表关联的附表的信息和附表的字段信息存进redis
+     *
+     * @param tableList    tableList
+     * @param relationList 主表和附表的关联信息
+     */
+    private void packRelationAndInsertToRedis(List<GenTable> tableList, List<GenTableRelation> relationList) {
+        for (GenTable table : tableList) {
+            List<GenTable> tableListTemp = new ArrayList<>(16);
+            for (GenTableRelation relation : relationList) {
+                if (relation.getRelationParentId().equals(table.getTableId())) {
+                    Long childId = relation.getRelationChildId();
+                    // 存了附表的建表信息和表的所有字段信息
+                    List<GenTable> collect = tableList.stream()
+                            .filter(genTable -> genTable.getTableId().equals(childId))
+                            .collect(Collectors.toList());
+                    tableListTemp.add(requireNonNull(collect).get(0));
+                }
+            }
+            table.setRelationList(tableListTemp);
+            redisService.setCacheObject(RedisKey.RELATION + table.getTableName(), table, 12L, TimeUnit.DAYS);
+        }
+    }
+
     /**
      * 功能描述: 把table中塞入对应的column, 同时把所有的信息塞到redis中
      *
@@ -72,7 +106,7 @@ public class MyController extends BaseController {
             }
 
             table.setColumns(columnList);
-            redisService.setCacheObject("table:" + table.getTableName(), table, 12L, TimeUnit.DAYS);
+            redisService.setCacheObject(RedisKey.TABLE_INFO + table.getTableName(), table, 12L, TimeUnit.DAYS);
         }
     }
 

+ 12 - 0
boman-modules/boman-gen/src/main/java/com/boman/gen/domain/GenTable.java

@@ -77,6 +77,10 @@ public class GenTable extends BaseEntity
     @Valid
     private List<GenTableColumn> columns;
 
+    /** 主表关联的附表信息(附表包含字段信息) */
+    @Valid
+    private List<GenTable> relationList;
+
     /** 其它生成选项 */
     private String options;
 
@@ -421,4 +425,12 @@ public class GenTable extends BaseEntity
         }
         return StringUtils.equalsAnyIgnoreCase(javaField, GenConstants.BASE_ENTITY);
     }
+
+    public List<GenTable> getRelationList() {
+        return relationList;
+    }
+
+    public void setRelationList(List<GenTable> relationList) {
+        this.relationList = relationList;
+    }
 }