mongo.go重构
This commit is contained in:
@@ -4,46 +4,40 @@ 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 AdSource = &adSourceDao{
|
||||
NoCache: true,
|
||||
}
|
||||
var AdSource = &adSourceDao{}
|
||||
|
||||
type adSourceDao struct {
|
||||
NoCache bool
|
||||
}
|
||||
|
||||
func (d *adSourceDao) SetNoCache() {
|
||||
AdSource.NoCache = true
|
||||
}
|
||||
|
||||
// GetByName 根据名称获取广告源
|
||||
func (d *adSourceDao) GetByName(ctx context.Context, name string) (adSource *entity.AdSource, err error) {
|
||||
err = mongo.FindOne(ctx, d.NoCache, bson.M{"name": name}, &adSource, "ad_sources")
|
||||
err = mongo.DB().FindOne(ctx, bson.M{"name": name}, &adSource, "ad_sources")
|
||||
return
|
||||
}
|
||||
|
||||
// GetAvailableSources 获取可用的广告源
|
||||
func (d *adSourceDao) GetAvailableSources(ctx context.Context) (list []*entity.AdSource, err error) {
|
||||
err = mongo.Find(ctx, d.NoCache, bson.M{"status": "active"}, &list, "ad_sources",
|
||||
err = mongo.DB().Find(ctx, bson.M{"status": "active"}, &list, "ad_sources",
|
||||
options.Find().SetSort(bson.M{"priority": -1, "createdAt": 1}))
|
||||
return
|
||||
}
|
||||
|
||||
// GetSourcesByProvider 根据提供商获取广告源
|
||||
func (d *adSourceDao) GetSourcesByProvider(ctx context.Context, provider string) (list []*entity.AdSource, err error) {
|
||||
err = mongo.Find(ctx, d.NoCache, bson.M{"provider": provider, "status": "active"}, &list, "ad_sources",
|
||||
err = mongo.DB().Find(ctx, bson.M{"provider": provider, "status": "active"}, &list, "ad_sources",
|
||||
options.Find().SetSort(bson.M{"priority": -1}))
|
||||
return
|
||||
}
|
||||
|
||||
// Create 创建广告源
|
||||
func (d *adSourceDao) Create(ctx context.Context, adSource *entity.AdSource) (id string, err error) {
|
||||
ids, err := mongo.Insert(ctx, []interface{}{adSource}, "ad_sources")
|
||||
ids, err := mongo.DB().Insert(ctx, []interface{}{adSource}, "ad_sources")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -55,7 +49,7 @@ func (d *adSourceDao) Create(ctx context.Context, adSource *entity.AdSource) (id
|
||||
|
||||
// Update 更新广告源
|
||||
func (d *adSourceDao) Update(ctx context.Context, adSource *entity.AdSource) (affected int64, err error) {
|
||||
result, err := mongo.Update(ctx, bson.M{"_id": adSource.Id}, bson.M{"$set": adSource}, "ad_sources")
|
||||
result, err := mongo.DB().Update(ctx, bson.M{"_id": adSource.Id}, bson.M{"$set": adSource}, "ad_sources")
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
@@ -64,7 +58,7 @@ func (d *adSourceDao) Update(ctx context.Context, adSource *entity.AdSource) (af
|
||||
|
||||
// Delete 删除广告源
|
||||
func (d *adSourceDao) Delete(ctx context.Context, id string) (affected int64, err error) {
|
||||
count, err := mongo.Delete(ctx, bson.M{"_id": id}, "ad_sources")
|
||||
count, err := mongo.DB().Delete(ctx, bson.M{"_id": id}, "ad_sources")
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
@@ -73,13 +67,13 @@ func (d *adSourceDao) Delete(ctx context.Context, id string) (affected int64, er
|
||||
|
||||
// GetByID 根据ID获取广告源
|
||||
func (d *adSourceDao) GetByID(ctx context.Context, id string) (adSource *entity.AdSource, err error) {
|
||||
err = mongo.FindOne(ctx, d.NoCache, bson.M{"_id": id}, &adSource, "ad_sources")
|
||||
err = mongo.DB().FindOne(ctx, bson.M{"_id": id}, &adSource, "ad_sources")
|
||||
return
|
||||
}
|
||||
|
||||
// UpdateFields 更新广告源部分字段
|
||||
func (d *adSourceDao) UpdateFields(ctx context.Context, id string, data *entity.AdSource) (affected int64, err error) {
|
||||
result, err := mongo.Update(ctx, bson.M{"_id": id}, bson.M{"$set": data}, "ad_sources")
|
||||
result, err := mongo.DB().Update(ctx, bson.M{"_id": id}, bson.M{"$set": data}, "ad_sources")
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user