优化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"
|
||||
)
|
||||
|
||||
// applicationDao 应用DAO
|
||||
@@ -38,8 +38,10 @@ func (d *applicationDao) GetByID(ctx context.Context, id string) (*entity.Applic
|
||||
// GetByTenantID 根据租户ID获取应用列表
|
||||
func (d *applicationDao) GetByTenantID(ctx context.Context, tenantID string) ([]*entity.Application, error) {
|
||||
var apps []*entity.Application
|
||||
err := mongo.DB().Find(ctx,
|
||||
bson.M{"tenantId": tenantID}, &apps, "application")
|
||||
// 使用空的Page参数获取所有数据
|
||||
page := &beans.Page{PageNum: 1, PageSize: -1} // -1表示不分页
|
||||
_, err := mongo.DB().Find(ctx,
|
||||
bson.M{"tenantId": tenantID}, &apps, "application", page, nil)
|
||||
return apps, err
|
||||
}
|
||||
|
||||
@@ -76,11 +78,9 @@ func (d *applicationDao) List(ctx context.Context, tenantID string, page, pageSi
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
offset := (page - 1) * pageSize
|
||||
err = mongo.DB().Find(ctx, filter, &apps, "application",
|
||||
options.Find().SetSort(bson.M{"createdAt": -1}).
|
||||
SetSkip(int64(offset)).
|
||||
SetLimit(int64(pageSize)))
|
||||
// 使用common/mongo的Find方法,自动处理分页、租户等
|
||||
pageBean := &beans.Page{PageNum: int64(page), PageSize: int64(pageSize)}
|
||||
total, err = mongo.DB().Find(ctx, filter, &apps, "application", pageBean, nil)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user