nbcp.myoql.db.mongo.extend.MyOqlMongoTreeData.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ktmyoql Show documentation
Show all versions of ktmyoql Show documentation
kotlin orm -- mysql,mongo , just like ktorm
The newest version!
package nbcp.myoql.db.mongo.extend
import nbcp.base.extend.CloneObject
import nbcp.base.extend.getValueByWbsPath
import nbcp.myoql.db.db
import nbcp.myoql.db.mongo.MongoQueryClip
import nbcp.myoql.db.mongo.base.MongoColumnName
import nbcp.myoql.db.mongo.component.MongoBaseMetaCollection
import org.bson.Document
import java.io.Serializable
/**
* 每个级别会查询一次。
*/
class MyOqlMongoTreeData, T : Any>(
var baseQuery: MongoQueryClip,
var idColumnName: String,
var pidColumn: MongoColumnName,
var pidValue: Any,
var childrenFieldName: String = "children"
) {
var list = mutableListOf();
// fun toList(): List {
// var clazz = baseQuery.moerEntity.entityClass;
// return list.map { it.ConvertType(clazz) as T }
// }
fun toTreeJson(): List {
var pids = listOf(pidValue)
while (true) {
var entitys = loadSubsFromDb(pids);
if (entitys.any() == false) {
break;
}
pids = entitys.map { it.getValueByWbsPath(idColumnName) as Serializable }
list.addAll(entitys);
}
return getChildren(pidValue, childrenFieldName);
}
private fun getChildren(pidValue: Any, childrenFieldName: String = "children"): List {
var level0s =
list.filter { it.getValueByWbsPath(db.mongo.getEntityColumnName(pidColumn.toString())) == pidValue }
level0s.forEach {
it.set(childrenFieldName, getChildren(it.getValue(idColumnName) as Any))
}
return level0s;
}
private fun loadSubsFromDb(pidValue: List): List {
var ret = baseQuery.CloneObject()
ret.where { pidColumn mongoIn pidValue.toTypedArray() }
return ret.toMapList()
}
}