From 823c6063f93d9897b557c2dc2967d3f15505c5f9 Mon Sep 17 00:00:00 2001 From: alex Date: Tue, 10 Nov 2020 20:02:05 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=97=B6=E9=97=B4=E8=8C=83?= =?UTF-8?q?=E5=9B=B4=E7=BB=84=E4=BB=B6=E6=9F=A5=E8=AF=A2=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/kotlin/com/synebula/gaea/mongo/MongoExt.kt | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/gaea.mongo/src/main/kotlin/com/synebula/gaea/mongo/MongoExt.kt b/src/gaea.mongo/src/main/kotlin/com/synebula/gaea/mongo/MongoExt.kt index de22466..4980f82 100644 --- a/src/gaea.mongo/src/main/kotlin/com/synebula/gaea/mongo/MongoExt.kt +++ b/src/gaea.mongo/src/main/kotlin/com/synebula/gaea/mongo/MongoExt.kt @@ -48,7 +48,7 @@ fun Query.where( val where = onWhere(key) if (where == null) { - list.add(tryRangeWhere(param.key, value, fieldType)) + list.add(tryRangeWhere(param.key, value, onFieldType)) } else { //判断执行查询子元素还是本字段 val field = if (where.children.isEmpty()) key else where.children @@ -61,7 +61,7 @@ fun Query.where( Operator.lte -> criteria.lte(value) Operator.gte -> criteria.gte(value) Operator.like -> criteria.regex(value.toString(), if (where.sensitiveCase) "" else "i") - Operator.default -> tryRangeWhere(param.key, value, fieldType) + Operator.default -> tryRangeWhere(param.key, value, onFieldType) } list.add(if (where.children.isEmpty()) criteria else Criteria.where(key).elemMatch(criteria)) } @@ -75,11 +75,12 @@ fun Query.where( /** * 尝试范围查询,失败则返回正常查询条件。 */ -private fun tryRangeWhere(key: String, value: Any, fieldType: Class<*>?): Criteria { +private fun tryRangeWhere(key: String, value: Any, onFieldType: ((v: String) -> Class<*>?) = { null }): Criteria { val rangeStartSuffix = "[0]" //范围查询开始后缀 val rangeEndSuffix = "[1]" //范围查询结束后缀 var condition = value val realKey = key.removeSuffix(rangeStartSuffix).removeSuffix(rangeEndSuffix) + val fieldType = onFieldType(realKey) if (fieldType != null && value.javaClass != fieldType && fieldType == Date::class.java) { condition = DateTime(value.toString(), "yyyy-MM-dd HH:mm:ss").date }