优化mongo,封装count逻辑,处理objectId
This commit is contained in:
@@ -6,12 +6,10 @@ import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"gitee.com/red-future---jilin-g/common/http"
|
||||
"gitee.com/red-future---jilin-g/common/mongo"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
"go.mongodb.org/mongo-driver/v2/mongo/options"
|
||||
)
|
||||
|
||||
var Advertisement = &advertisement{}
|
||||
@@ -231,24 +229,7 @@ func (d *advertisement) List(ctx context.Context, req *dto.ListAdvertisementReq)
|
||||
return
|
||||
}
|
||||
|
||||
// 分页参数处理
|
||||
pageNum := req.PageNum
|
||||
if pageNum <= 0 {
|
||||
pageNum = 1
|
||||
}
|
||||
pageSize := req.PageSize
|
||||
if pageSize <= 0 {
|
||||
pageSize = http.PageSize
|
||||
}
|
||||
|
||||
limit := int64(pageSize)
|
||||
skip := int64((pageNum - 1) * pageSize)
|
||||
|
||||
// 排序处理
|
||||
sort := bson.M{"createdAt": -1}
|
||||
|
||||
opts := options.Find().SetLimit(limit).SetSkip(skip).SetSort(sort)
|
||||
|
||||
err = mongo.DB().Find(ctx, filter, &list, entity.AdvertisementCollection, opts)
|
||||
// 使用common/mongo的Find方法,自动处理分页、租户等
|
||||
total, err = mongo.DB().Find(ctx, filter, &list, entity.AdvertisementCollection, req.Page, nil)
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user