85 lines
2.7 KiB
Go
85 lines
2.7 KiB
Go
package dao
|
|
|
|
import (
|
|
"context"
|
|
|
|
"cid/consts"
|
|
"cid/model/entity"
|
|
|
|
"gitea.com/red-future/common/beans"
|
|
"gitea.com/red-future/common/mongo"
|
|
"go.mongodb.org/mongo-driver/v2/bson"
|
|
)
|
|
|
|
var AdSource = &adSourceDao{}
|
|
|
|
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, consts.AdSourceCollection)
|
|
return
|
|
}
|
|
|
|
// GetAvailableSources 获取可用的广告源
|
|
func (d *adSourceDao) GetAvailableSources(ctx context.Context) (list []*entity.AdSource, err error) {
|
|
// 使用空的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) {
|
|
// 使用空的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}, consts.AdSourceCollection)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if len(ids) > 0 {
|
|
id = ids[0].(string)
|
|
}
|
|
return
|
|
}
|
|
|
|
// 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}, consts.AdSourceCollection)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.ModifiedCount, nil
|
|
}
|
|
|
|
// Delete 删除广告源
|
|
func (d *adSourceDao) Delete(ctx context.Context, id string) (affected int64, err error) {
|
|
count, err := mongo.DB().Delete(ctx, bson.M{"_id": id}, consts.AdSourceCollection)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return count, nil
|
|
}
|
|
|
|
// 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, 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}, consts.AdSourceCollection)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.ModifiedCount, nil
|
|
}
|