diff --git a/boot/platform/src/main/java/com/plate/boot/commons/utils/query/QueryFragment.java b/boot/platform/src/main/java/com/plate/boot/commons/utils/query/QueryFragment.java index 16dd1c05..2d0449bd 100644 --- a/boot/platform/src/main/java/com/plate/boot/commons/utils/query/QueryFragment.java +++ b/boot/platform/src/main/java/com/plate/boot/commons/utils/query/QueryFragment.java @@ -18,7 +18,7 @@ public class QueryFragment extends HashMap { private final StringJoiner columns = new StringJoiner(","); - private final StringJoiner querySql = new StringJoiner(","); + private final StringJoiner querySql = new StringJoiner(" "); private final StringJoiner whereSql = new StringJoiner(" AND "); @@ -43,13 +43,17 @@ public static QueryFragment of(int size, long offset, Map params return new QueryFragment(size, offset, params); } - public QueryFragment addColumn(CharSequence column) { - columns.add(column); + public QueryFragment addColumn(CharSequence... columns) { + for (CharSequence column : columns) { + this.columns.add(column); + } return this; } - public QueryFragment addQuery(CharSequence query) { - querySql.add(query); + public QueryFragment addQuery(CharSequence... queries) { + for (CharSequence query : queries) { + this.querySql.add(query); + } return this; } @@ -73,15 +77,6 @@ public QueryFragment mergeOrder(StringJoiner order) { return this; } - public QueryFragment merge(QueryFragment fragment) { - this.putAll(fragment); - this.columns.merge(fragment.getColumns()); - this.querySql.merge(fragment.getQuerySql()); - this.whereSql.merge(fragment.getWhereSql()); - this.orderSql.merge(fragment.getOrderSql()); - return of(fragment.getSize(), fragment.getOffset(), this); - } - /** * Generates the WHERE clause part of a SQL query based on the stored conditions. * If conditions have been accumulated, it prefixes the conditions with the 'WHERE' keyword; diff --git a/boot/platform/src/main/java/com/plate/boot/commons/utils/query/QueryHelper.java b/boot/platform/src/main/java/com/plate/boot/commons/utils/query/QueryHelper.java index cee4733b..a72b4f5f 100644 --- a/boot/platform/src/main/java/com/plate/boot/commons/utils/query/QueryHelper.java +++ b/boot/platform/src/main/java/com/plate/boot/commons/utils/query/QueryHelper.java @@ -62,7 +62,8 @@ public static QueryFragment query(Object object, Pageable pageable, Collection jsonMap = (Map) objectMap.get(queryKey); QueryFragment jsonQueryFragment = QueryJsonHelper.queryJson(jsonMap, prefix); - queryFragment.merge(jsonQueryFragment); + queryFragment.mergeWhere(jsonQueryFragment.getWhereSql()); + queryFragment.putAll(jsonQueryFragment); } String securityCodeKey = "securityCode"; diff --git a/boot/platform/src/main/java/com/plate/boot/commons/utils/query/QueryJsonHelper.java b/boot/platform/src/main/java/com/plate/boot/commons/utils/query/QueryJsonHelper.java index 7c532f10..722f24c0 100644 --- a/boot/platform/src/main/java/com/plate/boot/commons/utils/query/QueryJsonHelper.java +++ b/boot/platform/src/main/java/com/plate/boot/commons/utils/query/QueryJsonHelper.java @@ -121,7 +121,9 @@ private static Sort.Order convertSortOrderToCamelCase(Sort.Order order) { public static QueryFragment queryJson(Map params, String prefix) { QueryFragment queryFragment = QueryFragment.of(25, 0, Map.of()); for (Map.Entry entry : params.entrySet()) { - queryFragment.merge(buildJsonCondition(entry, prefix)); + var condition = buildJsonCondition(entry, prefix); + queryFragment.mergeWhere(condition.getWhereSql()); + queryFragment.putAll(condition); } return queryFragment; } diff --git a/boot/platform/src/main/java/com/plate/boot/security/core/tenant/member/TenantMemberRequest.java b/boot/platform/src/main/java/com/plate/boot/security/core/tenant/member/TenantMemberRequest.java index 13c8d6fb..add814db 100644 --- a/boot/platform/src/main/java/com/plate/boot/security/core/tenant/member/TenantMemberRequest.java +++ b/boot/platform/src/main/java/com/plate/boot/security/core/tenant/member/TenantMemberRequest.java @@ -44,8 +44,8 @@ public QueryFragment toParamSql() { QueryFragment fragment = QueryHelper.query(this, List.of("users", "securityCode", "username"), "a"); if (!ObjectUtils.isEmpty(this.getUsers())) { - fragment.addWhere("a.user_code in :users"); - fragment.put("users", this.getUsers()); + fragment.addWhere("a.user_code in (:users)"); + fragment.put("users", StringUtils.collectionToCommaDelimitedString(this.getUsers())); } if (StringUtils.hasLength(this.getSecurityCode())) { diff --git a/boot/platform/src/main/java/com/plate/boot/security/core/tenant/member/TenantMembersService.java b/boot/platform/src/main/java/com/plate/boot/security/core/tenant/member/TenantMembersService.java index 3d4ef3a2..ace4f1bc 100644 --- a/boot/platform/src/main/java/com/plate/boot/security/core/tenant/member/TenantMembersService.java +++ b/boot/platform/src/main/java/com/plate/boot/security/core/tenant/member/TenantMembersService.java @@ -37,8 +37,12 @@ select count(*) from se_tenant_members a private final TenantMembersRepository tenantMembersRepository; public Flux search(TenantMemberRequest request, Pageable pageable) { - QueryFragment queryFragment = QueryHelper.query(request, pageable, "a"); - queryFragment = queryFragment.merge(request.toParamSql()); + QueryFragment queryFragment = request.toParamSql().addColumn("a.*", "b.name as tenant_name", + "b.extend as tenant_extend", "c.name as login_name", "c.username") + .addQuery("a", + "inner join se_tenants b on a.tenant_code = b.code", + "inner join se_users c on c.code = a.user_code"); + QueryHelper.applySort(queryFragment, pageable.getSort(), "a"); return super.queryWithCache(BeanUtils.cacheKey(request, pageable), queryFragment.querySql(), queryFragment, TenantMemberResponse.class); } @@ -46,8 +50,7 @@ public Flux search(TenantMemberRequest request, Pageable p public Mono> page(TenantMemberRequest request, Pageable pageable) { var searchMono = this.search(request, pageable).collectList(); QueryFragment queryFragment = request.toParamSql(); - String query = COUNT_SQL + queryFragment.whereSql(); - Mono countMono = this.countWithCache(BeanUtils.cacheKey(request), query, queryFragment); + Mono countMono = this.countWithCache(BeanUtils.cacheKey(request), queryFragment.countSql(), queryFragment); return searchMono.zipWith(countMono) .map(tuple2 -> new PageImpl<>(tuple2.getT1(), pageable, tuple2.getT2())); }