初始化项目
This commit is contained in:
@@ -4,7 +4,9 @@ import (
|
||||
"context"
|
||||
|
||||
"cid/model/entity"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"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{}
|
||||
@@ -13,77 +15,65 @@ type adSourceDao struct{}
|
||||
|
||||
// GetByName 根据名称获取广告源
|
||||
func (d *adSourceDao) GetByName(ctx context.Context, name string) (adSource *entity.AdSource, err error) {
|
||||
err = g.DB().Model("ad_sources").
|
||||
Where("name = ?", name).
|
||||
Scan(&adSource)
|
||||
err = mongo.FindOne(ctx, bson.M{"name": name}, &adSource, "ad_sources")
|
||||
return
|
||||
}
|
||||
|
||||
// GetAvailableSources 获取可用的广告源
|
||||
func (d *adSourceDao) GetAvailableSources(ctx context.Context) (list []*entity.AdSource, err error) {
|
||||
err = g.DB().Model("ad_sources").
|
||||
Where("status = ?", "active").
|
||||
Order("priority DESC, created_at ASC").
|
||||
Scan(&list)
|
||||
err = mongo.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 = g.DB().Model("ad_sources").
|
||||
Where("provider = ? AND status = ?", provider, "active").
|
||||
Order("priority DESC").
|
||||
Scan(&list)
|
||||
err = mongo.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 int64, err error) {
|
||||
result, err := g.DB().Model("ad_sources").Insert(adSource)
|
||||
func (d *adSourceDao) Create(ctx context.Context, adSource *entity.AdSource) (id string, err error) {
|
||||
ids, err := mongo.Insert(ctx, []interface{}{adSource}, "ad_sources")
|
||||
if err != nil {
|
||||
return 0, err
|
||||
return "", err
|
||||
}
|
||||
if len(ids) > 0 {
|
||||
id = ids[0].(string)
|
||||
}
|
||||
id, err = result.LastInsertId()
|
||||
return
|
||||
}
|
||||
|
||||
// Update 更新广告源
|
||||
func (d *adSourceDao) Update(ctx context.Context, adSource *entity.AdSource) (affected int64, err error) {
|
||||
result, err := g.DB().Model("ad_sources").
|
||||
Where("id = ?", adSource.Id).
|
||||
Update(adSource)
|
||||
result, err := mongo.Update(ctx, bson.M{"_id": adSource.Id}, bson.M{"$set": adSource}, "ad_sources")
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected()
|
||||
return result.ModifiedCount, nil
|
||||
}
|
||||
|
||||
// Delete 删除广告源
|
||||
func (d *adSourceDao) Delete(ctx context.Context, id int64) (affected int64, err error) {
|
||||
result, err := g.DB().Model("ad_sources").
|
||||
Where("id = ?", id).
|
||||
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")
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected()
|
||||
return count, nil
|
||||
}
|
||||
|
||||
// GetByID 根据ID获取广告源
|
||||
func (d *adSourceDao) GetByID(ctx context.Context, id int64) (adSource *entity.AdSource, err error) {
|
||||
err = g.DB().Model("ad_sources").
|
||||
Where("id = ?", id).
|
||||
Scan(&adSource)
|
||||
func (d *adSourceDao) GetByID(ctx context.Context, id string) (adSource *entity.AdSource, err error) {
|
||||
err = mongo.FindOne(ctx, bson.M{"_id": id}, &adSource, "ad_sources")
|
||||
return
|
||||
}
|
||||
|
||||
// UpdateFields 更新广告源部分字段
|
||||
func (d *adSourceDao) UpdateFields(ctx context.Context, id int64, data *entity.AdSource) (affected int64, err error) {
|
||||
result, err := g.DB().Model("ad_sources").
|
||||
Where("id = ?", id).
|
||||
Update(data)
|
||||
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")
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected()
|
||||
return result.ModifiedCount, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user