mongo.go重构
This commit is contained in:
@@ -4,26 +4,20 @@ import (
|
||||
"context"
|
||||
|
||||
"cid/model/entity"
|
||||
|
||||
"gitee.com/red-future---jilin-g/common/mongo"
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
"go.mongodb.org/mongo-driver/v2/mongo/options"
|
||||
)
|
||||
|
||||
var CIDRequest = &cidRequestDao{
|
||||
NoCache: true,
|
||||
}
|
||||
var CIDRequest = &cidRequestDao{}
|
||||
|
||||
type cidRequestDao struct {
|
||||
NoCache bool
|
||||
}
|
||||
|
||||
func (d *cidRequestDao) SetNoCache() {
|
||||
CIDRequest.NoCache = true
|
||||
}
|
||||
|
||||
// Create 创建CID请求记录
|
||||
func (d *cidRequestDao) Create(ctx context.Context, request *entity.CidRequest) (id string, err error) {
|
||||
ids, err := mongo.Insert(ctx, []interface{}{request}, entity.CidRequestCollection)
|
||||
ids, err := mongo.DB().Insert(ctx, []interface{}{request}, entity.CidRequestCollection)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -38,14 +32,14 @@ func (d *cidRequestDao) GetHistory(ctx context.Context, userId string, page, siz
|
||||
filter := bson.M{"userId": userId}
|
||||
|
||||
// 获取总数
|
||||
total, err = mongo.Count(ctx, d.NoCache, filter, entity.CidRequestCollection)
|
||||
total, err = mongo.DB().Count(ctx, filter, entity.CidRequestCollection)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// 分页查询
|
||||
offset := (page - 1) * size
|
||||
err = mongo.Find(ctx, d.NoCache, filter, &list, entity.CidRequestCollection,
|
||||
err = mongo.DB().Find(ctx, filter, &list, entity.CidRequestCollection,
|
||||
options.Find().SetSort(bson.M{"createdAt": -1}).
|
||||
SetSkip(int64(offset)).
|
||||
SetLimit(int64(size)))
|
||||
@@ -58,14 +52,14 @@ func (d *cidRequestDao) GetStatistics(ctx context.Context, userId string) (stats
|
||||
stats = make(map[string]interface{})
|
||||
|
||||
// 总请求数
|
||||
totalRequests, err := mongo.Count(ctx, d.NoCache, bson.M{"userId": userId}, entity.CidRequestCollection)
|
||||
totalRequests, err := mongo.DB().Count(ctx, bson.M{"userId": userId}, entity.CidRequestCollection)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
stats["total_requests"] = totalRequests
|
||||
|
||||
// 成功请求数
|
||||
successfulRequests, err := mongo.Count(ctx, d.NoCache, bson.M{"userId": userId, "status": "completed"}, entity.CidRequestCollection)
|
||||
successfulRequests, err := mongo.DB().Count(ctx, bson.M{"userId": userId, "status": "completed"}, entity.CidRequestCollection)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user