mongo.go重构
This commit is contained in:
@@ -14,16 +14,9 @@ import (
|
||||
"go.mongodb.org/mongo-driver/v2/mongo/options"
|
||||
)
|
||||
|
||||
var Advertisement = &advertisement{
|
||||
NoCache: true,
|
||||
}
|
||||
var Advertisement = &advertisement{}
|
||||
|
||||
type advertisement struct {
|
||||
NoCache bool
|
||||
}
|
||||
|
||||
func (d *advertisement) SetNoCache() {
|
||||
Advertisement.NoCache = true
|
||||
}
|
||||
|
||||
// Insert 插入广告
|
||||
@@ -37,7 +30,7 @@ func (d *advertisement) Insert(ctx context.Context, advertisement *entity.Advert
|
||||
g.Log().Infof(ctx, "获取到stream消息: %v", streamMsg)
|
||||
}
|
||||
|
||||
_, err = mongo.Insert(ctx, []interface{}{advertisement}, entity.AdvertisementCollection)
|
||||
_, err = mongo.DB().Insert(ctx, []interface{}{advertisement}, entity.AdvertisementCollection)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -116,7 +109,7 @@ func (d *advertisement) Update(ctx context.Context, req *dto.UpdateAdvertisement
|
||||
|
||||
if len(updateFields) > 0 {
|
||||
update := bson.M{"$set": updateFields}
|
||||
_, err = mongo.Update(ctx, filter, update, entity.AdvertisementCollection)
|
||||
_, err = mongo.DB().Update(ctx, filter, update, entity.AdvertisementCollection)
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -130,7 +123,7 @@ func (d *advertisement) UpdateStatus(ctx context.Context, id, status string) (er
|
||||
filter := bson.M{"_id": objectId}
|
||||
update := bson.M{"$set": bson.M{"status": status}}
|
||||
|
||||
_, err = mongo.Update(ctx, filter, update, entity.AdvertisementCollection)
|
||||
_, err = mongo.DB().Update(ctx, filter, update, entity.AdvertisementCollection)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -155,7 +148,7 @@ func (d *advertisement) Audit(ctx context.Context, id, auditStatus, auditReason
|
||||
},
|
||||
}
|
||||
|
||||
_, err = mongo.Update(ctx, filter, update, entity.AdvertisementCollection)
|
||||
_, err = mongo.DB().Update(ctx, filter, update, entity.AdvertisementCollection)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -168,7 +161,7 @@ func (d *advertisement) UpdateStatistics(ctx context.Context, id string, stats m
|
||||
filter := bson.M{"_id": objectId}
|
||||
update := bson.M{"$set": stats}
|
||||
|
||||
_, err = mongo.Update(ctx, filter, update, entity.AdvertisementCollection)
|
||||
_, err = mongo.DB().Update(ctx, filter, update, entity.AdvertisementCollection)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -181,7 +174,7 @@ func (d *advertisement) GetOne(ctx context.Context, id string) (advertisement *e
|
||||
filter := bson.M{"_id": objectId}
|
||||
|
||||
advertisement = &entity.Advertisement{}
|
||||
err = mongo.FindOne(ctx, d.NoCache, filter, advertisement, entity.AdvertisementCollection)
|
||||
err = mongo.DB().FindOne(ctx, filter, advertisement, entity.AdvertisementCollection)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -223,7 +216,7 @@ func (d *advertisement) buildListFilter(req *dto.ListAdvertisementReq) bson.M {
|
||||
|
||||
// checkTotalCount 检查总数
|
||||
func (d *advertisement) checkTotalCount(ctx context.Context, filter bson.M) (total int64, err error) {
|
||||
total, err = mongo.Count(ctx, d.NoCache, filter, entity.AdvertisementCollection)
|
||||
total, err = mongo.DB().Count(ctx, filter, entity.AdvertisementCollection)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -256,6 +249,6 @@ func (d *advertisement) List(ctx context.Context, req *dto.ListAdvertisementReq)
|
||||
|
||||
opts := options.Find().SetLimit(limit).SetSkip(skip).SetSort(sort)
|
||||
|
||||
err = mongo.Find(ctx, d.NoCache, filter, &list, entity.AdvertisementCollection, opts)
|
||||
err = mongo.DB().Find(ctx, filter, &list, entity.AdvertisementCollection, opts)
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user