优化mongo,封装count逻辑,处理objectId
This commit is contained in:
@@ -5,9 +5,9 @@ import (
|
||||
|
||||
"cid/model/entity"
|
||||
|
||||
"gitee.com/red-future---jilin-g/common/beans"
|
||||
"gitee.com/red-future---jilin-g/common/mongo"
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
"go.mongodb.org/mongo-driver/v2/mongo/options"
|
||||
)
|
||||
|
||||
var Strategy = &strategyDao{}
|
||||
@@ -29,8 +29,7 @@ func (d *strategyDao) GetByID(ctx context.Context, id string) (strategy *entity.
|
||||
|
||||
// GetByTenantLevel 根据租户级别获取策略
|
||||
func (d *strategyDao) GetByTenantLevel(ctx context.Context, tenantLevel string) (strategy *entity.Strategy, err error) {
|
||||
err = mongo.DB().FindOne(ctx, bson.M{"tenantLevel": tenantLevel, "status": "active"}, &strategy, "strategies",
|
||||
options.FindOne().SetSort(bson.M{"priority": -1, "createdAt": 1}))
|
||||
err = mongo.DB().FindOne(ctx, bson.M{"tenantLevel": tenantLevel, "status": "active"}, &strategy, "strategies")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -82,12 +81,8 @@ func (d *strategyDao) GetList(ctx context.Context, page, size int, tenantLevel,
|
||||
return
|
||||
}
|
||||
|
||||
// 分页查询
|
||||
offset := (page - 1) * size
|
||||
err = mongo.DB().Find(ctx, filter, &list, "strategies",
|
||||
options.Find().SetSort(bson.M{"priority": -1, "createdAt": -1}).
|
||||
SetSkip(int64(offset)).
|
||||
SetLimit(int64(size)))
|
||||
|
||||
// 分页查询,使用common/mongo的Find方法,自动处理分页、租户等
|
||||
pageBean := &beans.Page{PageNum: int64(page), PageSize: int64(size)}
|
||||
total, err = mongo.DB().Find(ctx, filter, &list, "strategies", pageBean, nil)
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user