From 697c8671c868e5489140e2427e4486db9093fa7d Mon Sep 17 00:00:00 2001 From: alex Date: Mon, 18 May 2020 16:27:50 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DMongoQueryTyped=E9=9B=86?= =?UTF-8?q?=E5=90=88=E5=90=8D=E7=A7=B0=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../gaea/query/mongo/MongoQueryTyped.kt | 41 ++++++++++++++++--- 1 file changed, 36 insertions(+), 5 deletions(-) diff --git a/src/gaea.mongo/src/main/kotlin/com/synebula/gaea/query/mongo/MongoQueryTyped.kt b/src/gaea.mongo/src/main/kotlin/com/synebula/gaea/query/mongo/MongoQueryTyped.kt index a011c19..3045ca3 100644 --- a/src/gaea.mongo/src/main/kotlin/com/synebula/gaea/query/mongo/MongoQueryTyped.kt +++ b/src/gaea.mongo/src/main/kotlin/com/synebula/gaea/query/mongo/MongoQueryTyped.kt @@ -1,11 +1,13 @@ package com.synebula.gaea.query.mongo +import com.synebula.gaea.extension.firstCharLowerCase import com.synebula.gaea.log.ILogger import com.synebula.gaea.query.IQueryTyped import com.synebula.gaea.query.PagingData import com.synebula.gaea.query.PagingParam import org.springframework.data.mongodb.core.MongoTemplate import org.springframework.data.mongodb.core.query.Query +import java.lang.RuntimeException /** * 实现IQuery的Mongo查询类 @@ -14,17 +16,30 @@ import org.springframework.data.mongodb.core.query.Query open class MongoQueryTyped(var repo: MongoTemplate, override var logger: ILogger? = null) : IQueryTyped, IMongoQuery { + /** + * 查询的集合名称 + * + * 若没有为成员变量collection赋值,会尝试使用View名称解析集合名称。 + * 规则为移除View后缀并首字母小写,此时使用 + */ + var collection: String = "" + + /** + * 使用View解析是collection时是否校验存在,默认不校验 + */ + var validViewCollection = false + override fun list(params: Map?, clazz: Class): List { val viewFields = this.fields(clazz) val query = Query() this.where(query, params, clazz) this.select(query, viewFields.toTypedArray()) - return this.repo.find(query, clazz, clazz.simpleName) + return this.repo.find(query, clazz, this.collection(clazz)) } override fun count(params: Map?, clazz: Class): Int { val query = Query() - return this.repo.count(this.where(query, params, clazz), clazz.simpleName).toInt() + return this.repo.count(this.where(query, params, clazz), this.collection(clazz)).toInt() } override fun paging(params: PagingParam, clazz: Class): PagingData { @@ -34,15 +49,31 @@ open class MongoQueryTyped(var repo: MongoTemplate, override var logger: ILogger result.page = params.page val query = Query() this.where(query, params.parameters, clazz) - result.total = this.repo.count(query, clazz.simpleName).toInt() + result.total = this.count(params.parameters, clazz) this.select(query, viewFields.toTypedArray()) query.with(order(params.orderBy)) query.skip(params.index).limit(params.size) - result.data = this.repo.find(query, clazz, clazz.simpleName) + result.data = this.repo.find(query, clazz, this.collection(clazz)) return result } override fun get(key: TKey, clazz: Class): TView? { - return this.repo.findOne(idQuery(key), clazz, clazz.simpleName) + return this.repo.findOne(idQuery(key), clazz, this.collection(clazz)) + } + + /** + * 获取collection + */ + protected fun collection(clazz: Class): String { + return if (this.collection.isEmpty()) { + this.logger?.warn(this, "查询集合参数[collection]值为空, 尝试使用View<${clazz.name}>名称解析集合") + val collection = clazz.simpleName.removeSuffix("View").firstCharLowerCase() + if (!validViewCollection || this.repo.collectionExists(collection)) + collection + else { + throw RuntimeException("找不到名为[$collection]的集合") + } + } else + this.collection } }