优化mongo,封装count逻辑,处理objectId
This commit is contained in:
@@ -3,11 +3,12 @@ package dao
|
||||
import (
|
||||
"context"
|
||||
|
||||
"cid/consts"
|
||||
"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"
|
||||
)
|
||||
|
||||
var AdSource = &adSourceDao{}
|
||||
@@ -17,27 +18,29 @@ type adSourceDao struct {
|
||||
|
||||
// GetByName 根据名称获取广告源
|
||||
func (d *adSourceDao) GetByName(ctx context.Context, name string) (adSource *entity.AdSource, err error) {
|
||||
err = mongo.DB().FindOne(ctx, bson.M{"name": name}, &adSource, "ad_sources")
|
||||
err = mongo.DB().FindOne(ctx, bson.M{"name": name}, &adSource, consts.AdSourceCollection)
|
||||
return
|
||||
}
|
||||
|
||||
// GetAvailableSources 获取可用的广告源
|
||||
func (d *adSourceDao) GetAvailableSources(ctx context.Context) (list []*entity.AdSource, err error) {
|
||||
err = mongo.DB().Find(ctx, bson.M{"status": "active"}, &list, "ad_sources",
|
||||
options.Find().SetSort(bson.M{"priority": -1, "createdAt": 1}))
|
||||
// 使用空的Page参数获取所有数据
|
||||
page := &beans.Page{PageNum: 1, PageSize: -1} // -1表示不分页
|
||||
_, err = mongo.DB().Find(ctx, bson.M{"status": "active"}, &list, consts.AdSourceCollection, page, nil)
|
||||
return
|
||||
}
|
||||
|
||||
// GetSourcesByProvider 根据提供商获取广告源
|
||||
func (d *adSourceDao) GetSourcesByProvider(ctx context.Context, provider string) (list []*entity.AdSource, err error) {
|
||||
err = mongo.DB().Find(ctx, bson.M{"provider": provider, "status": "active"}, &list, "ad_sources",
|
||||
options.Find().SetSort(bson.M{"priority": -1}))
|
||||
// 使用空的Page参数获取所有数据
|
||||
page := &beans.Page{PageNum: 1, PageSize: -1} // -1表示不分页
|
||||
_, err = mongo.DB().Find(ctx, bson.M{"provider": provider, "status": "active"}, &list, consts.AdSourceCollection, page, nil)
|
||||
return
|
||||
}
|
||||
|
||||
// Create 创建广告源
|
||||
func (d *adSourceDao) Create(ctx context.Context, adSource *entity.AdSource) (id string, err error) {
|
||||
ids, err := mongo.DB().Insert(ctx, []interface{}{adSource}, "ad_sources")
|
||||
ids, err := mongo.DB().Insert(ctx, []interface{}{adSource}, consts.AdSourceCollection)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -49,7 +52,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.DB().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}, consts.AdSourceCollection)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
@@ -58,7 +61,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.DB().Delete(ctx, bson.M{"_id": id}, "ad_sources")
|
||||
count, err := mongo.DB().Delete(ctx, bson.M{"_id": id}, consts.AdSourceCollection)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
@@ -67,13 +70,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.DB().FindOne(ctx, bson.M{"_id": id}, &adSource, "ad_sources")
|
||||
err = mongo.DB().FindOne(ctx, bson.M{"_id": id}, &adSource, consts.AdSourceCollection)
|
||||
return
|
||||
}
|
||||
|
||||
// UpdateFields 更新广告源部分字段
|
||||
func (d *adSourceDao) UpdateFields(ctx context.Context, id string, data *entity.AdSource) (affected int64, err error) {
|
||||
result, err := mongo.DB().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}, consts.AdSourceCollection)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user