Просмотр исходного кода

通用连表查询接口 完成

shiqian 4 лет назад
Родитель
Сommit
025df0fdfd

+ 20 - 4
boman-api/boman-domain/src/main/java/com.boman.domain/dto/QueryBySqlDto.java

@@ -22,7 +22,7 @@ import static com.boman.domain.constant.SqlConstant.*;
 public class QueryBySqlDto {
 
 
-    private static final String LEFT_JOIN = "left_join";
+    private static final String LEFT_JOIN = " left join ";
     private static final String COUNT_1 = " count(1) ";
 
     /** 主表表名 **/
@@ -138,9 +138,7 @@ public class QueryBySqlDto {
         JSONObject result = new JSONObject(columns.size());
         for (Map.Entry<String, Object> entry : condition.entrySet()) {
             String key = entry.getKey();
-            String[] split = key.split("\\.");
-            // split[0] 表名, split[1] 属性名
-            key = split[1].trim();
+            key = getColumnNameFromCondition(key);
             Object value = entry.getValue();
             for (GenTableColumn column : columns) {
                 if (!column.getColumnName().equalsIgnoreCase(key)) {
@@ -163,6 +161,24 @@ public class QueryBySqlDto {
         return result;
     }
 
+
+    /**
+     * 功能描述: 表名.属性名  取出属性名
+     *
+     * @param key 表名.属性名
+     * @return java.lang.String
+     */
+    public static String getColumnNameFromCondition(String key) {
+        if (StringUtils.isEmpty(key)) {
+            throw new IllegalArgumentException("key为空");
+        }
+
+        String[] split = key.split("\\.");
+        assert split.length == 2;
+        // split[0] 表名, split[1] 属性名
+        return split[1].trim();
+    }
+
     public static boolean isEmpty(Object input) {
         if (input instanceof String) {
             return StringUtils.isEmpty((String) input);

+ 16 - 3
boman-web-core/src/main/java/com/boman/web/core/controller/MessageController.java

@@ -43,14 +43,27 @@ public class MessageController {
     }
 
     /**
-     * 功能描述: 未完成的发文,对应菜单(申请中)
+     * 功能描述:
+     * <>{
+     *     "primaryTableName": "boman_message",
+     *     "deputyTableName": "boman_message_receive",
+     *     "primaryTableNameRelKey": "id",
+     *     "deputyTableNameRelKey": "message_id",
+     *     "pageNo": 1,
+     *     "pageSize": 10,
+     *     "orderBy": "boman_message.create_time desc",
+     *     "condition": {
+     *         "boman_message_receive.status": "N",
+     *         "boman_message_receive.visible": "Y"
+     *     }
+     * }</>
      *
      * @param queryBySql queryBySql
      * @return com.boman.domain.dto.AjaxResult
      */
-    @GetMapping("/notFinishMessages")
+    @PostMapping("/notFinishMessages")
     public AjaxResult notFinishMessages(@RequestBody QueryBySqlDto queryBySql) {
-        return AjaxResult.success(service.notFinishMessages(queryBySql));
+        return service.notFinishMessages(queryBySql);
     }
 
 }

+ 26 - 0
boman-web-core/src/main/java/com/boman/web/core/controller/ObjController.java

@@ -174,6 +174,32 @@ public class ObjController {
         return tableServiceCmdService.queryList(condition);
     }
 
+    /**
+     * 功能描述: 连表查询,目前两张表
+     * <>{
+     *     "primaryTableName": "boman_message",
+     *     "deputyTableName": "boman_message_receive",
+     *     "primaryTableNameRelKey": "id",
+     *     "deputyTableNameRelKey": "message_id",
+     *     "pageNo": 1,
+     *     "pageSize": 10,
+     *     "orderBy": "boman_message.create_time desc",
+     *     "condition": {
+     *         "boman_message_receive.status": "N",
+     *         "boman_message_receive.visible": "Y"
+     *     }
+     * }</>
+     *
+     * @param queryBySql queryBySql
+     * @return com.boman.domain.dto.AjaxResult
+     */
+    @ApiOperation(value = "连表查询")
+    @PostMapping("/queryListByUnionTable")
+    public AjaxResult queryListByUnionTable(@RequestBody QueryBySqlDto queryBySql) {
+        return tableServiceCmdService.queryListByUnionTable(queryBySql);
+    }
+
+
     /**
      * 功能描述: 获取表单查询字段、按钮、表头
      * 注意: 都是从redis中拿的,如果数据库和redis不一致,则需刷新一下redis

+ 21 - 25
boman-web-core/src/main/java/com/boman/web/core/service/TableServiceCmdService.java

@@ -334,7 +334,7 @@ public class TableServiceCmdService {
      * @param queryBySql queryBySql
      * @return com.boman.domain.dto.AjaxResult
      */
-    public AjaxResult queryListBySql(QueryBySqlDto queryBySql) {
+    public AjaxResult queryListByUnionTable(QueryBySqlDto queryBySql) {
         String primaryTableName = queryBySql.getPrimaryTableName();
         String deputyTableName = queryBySql.getDeputyTableName();
         String primaryTableNameRelKey = queryBySql.getPrimaryTableNameRelKey();
@@ -348,51 +348,47 @@ public class TableServiceCmdService {
         List<GenTableColumn> columns = primaryGenTable.getColumns();
         GenTable deputyGenTable = getTableFromRedisByTableName(RedisKey.TABLE_INFO, deputyTableName);
         List<GenTableColumn> deputyColumns = deputyGenTable.getColumns();
-        columns.addAll(deputyColumns);
+        List<GenTableColumn> allColumnName = new ArrayList<>(columns);
+        allColumnName.addAll(deputyColumns);
         // 查询条件
         JSONObject condition = queryBySql.getCondition();
         // 权限
         checkAuthQueryList(primaryGenTable, condition);
         // 检查列
-        checkLeftJoinColumn(condition.keySet(), columns);
+        checkLeftJoinColumn(condition.keySet(), allColumnName);
         // 封装好以后的查询条件
-        JSONObject packCondition = ifNullSetEmpty(ColumnUtils.packColCondition(columns, condition));
+        JSONObject packCondition = ifNullSetEmpty(ColumnUtils.packLeftJoinCondition(allColumnName, condition));
 
         JSONObject rows = new JSONObject();
         int total = selectService.countByLeftJoinCondition(packCondition, queryBySql);
         rows.put(FormDataConstant.PAGE_TOTAL, total);
         rows.put(TABLE_HEAD_LIST, getTableHeadList(primaryGenTable));
         if (total <= 0) {
-            rows.put(FormDataConstant.PAGE_ROWS, new ArrayList<>());
+            rows.put(FormDataConstant.PAGE_ROWS, new ArrayList<>(0));
             return AjaxResult.success(rows);
         }
 
         List<String> showData = queryBySql.getShowData();
-        // 检查列
-        checkColumn(showData, columns);
         // 需要返回到前台的列, 需要判断是否是列表展示, 4为判断列表是否可见
         showData = filterData(columns, 4, showData, MaskConstant.LIST_VISIBLE::equals);
         // 表的id单独处理,不管mask,都查出来返给前台
         IdUtils.putIfNotContains(showData, IdUtils.getPkName(columns));
         List<JSONObject> result = selectService.selectByLeftJoinCondition(queryBySql, packCondition, showData);
-//        result = filter(result, ObjectUtils::isNotEmpty);
-//
-//        // 查询时为null的列不显示的处理
-//        handleNullColumnValue(result, showData);
-//
-//        // 处理blob
-//        handleBlob(result, genTable.getIsContainsBlob());
-//        // 处理日期、外键、字典值
-//        handleDictForQueryList(result, columns);
-//        handleDateTimeForQueryList(result, columns);
-//        handleFkForQueryList(result, columns);
-//
-//        // 定制接口
-//        result = isCustomized(dto.getTable(), result, "trigger_retrieve");
-//
-//        rows.put(PAGE_ROWS, result);
-//        return AjaxResult.success(rows);
-        return AjaxResult.success();
+        result = filter(result, ObjectUtils::isNotEmpty);
+
+        // 查询时为null的列不显示的处理
+        handleNullColumnValue(result, showData);
+        // 处理blob
+        handleBlob(result, primaryGenTable.getIsContainsBlob());
+        // 处理日期、外键、字典值
+        handleDictForQueryList(result, columns);
+        handleDateTimeForQueryList(result, columns);
+        handleFkForQueryList(result, columns);
+
+        // 定制接口
+        result = isCustomized(primaryGenTable.getTableName(), result, "trigger_retrieve");
+        rows.put(PAGE_ROWS, result);
+        return AjaxResult.success(rows);
     }
 
     /**

+ 1 - 1
boman-web-core/src/main/java/com/boman/web/core/service/message/MessageServiceImpl.java

@@ -112,6 +112,6 @@ public class MessageServiceImpl implements MessageService {
      */
     @Override
     public AjaxResult notFinishMessages(QueryBySqlDto queryBySql) {
-        return cmdService.queryListBySql(queryBySql);
+        return cmdService.queryListByUnionTable(queryBySql);
     }
 }

+ 32 - 1
boman-web-core/src/main/java/com/boman/web/core/utils/ColumnUtils.java

@@ -16,6 +16,7 @@ import com.boman.domain.GenTableColumn;
 import com.boman.domain.constant.CacheConstants;
 import com.boman.domain.constant.MysqlDataTypeConst;
 import com.boman.domain.dto.FormDataDto;
+import com.boman.domain.dto.QueryBySqlDto;
 import com.boman.domain.exception.UnknownColumnException;
 import com.boman.system.api.model.LoginUser;
 import com.boman.web.core.domain.TableContext;
@@ -259,7 +260,8 @@ public class ColumnUtils {
         String tableName = origin.get(0).getTableName();
         List<String> allColumnNames = map(origin, GenTableColumn::getColumnName);
         for (String columnName : strings) {
-            if (!allColumnNames.contains(columnName.toLowerCase())) {
+            columnName = QueryBySqlDto.getColumnNameFromCondition(columnName);
+            if (!allColumnNames.contains(columnName)) {
                 throw new UnknownColumnException("此表 [" + tableName + "]中没有 [" + columnName + "] 字段");
             }
         }
@@ -363,6 +365,35 @@ public class ColumnUtils {
         return packColCondition;
     }
 
+    /**
+     * 功能描述: 封装成查询条件 key: 列名,  value:查询条件_查询类别
+     * eg: [{"config_name": ["系统配置", "EQ", "varchar(100)"]}]
+     *
+     * @param columns columns
+     * @return com.alibaba.fastjson.JSONObject
+     */
+    public static JSONObject packLeftJoinCondition(List<GenTableColumn> columns, JSONObject condition) {
+        if (isEmpty(condition)) {
+            return condition;
+        }
+
+        JSONObject packColCondition = new JSONObject(columns.size());
+        for (Map.Entry<String, Object> entry : condition.entrySet()) {
+            String columnName = QueryBySqlDto.getColumnNameFromCondition(entry.getKey());
+            Object value = entry.getValue();
+            for (GenTableColumn column : columns) {
+                // long string collection 暂时只作此三种类型判断
+                if (column.getColumnName().equalsIgnoreCase(columnName) && ObjectUtils.isNotEmpty(value)) {
+                    // columnType 作为判断需不需要转义的一个标准,防止索引失效
+                    packColCondition.put(entry.getKey(), Lists.newArrayList(value, column.getQueryType(), column.getColumnType()));
+                    break;
+                }
+            }
+        }
+
+        return packColCondition;
+    }
+
     /**
      * 功能描述: 数字小的在前面,数字大的在后面
      *