117 lines
3.5 KiB
Go
117 lines
3.5 KiB
Go
package seopushmod
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"videoweb/pub/common/log"
|
|
"videoweb/pub/common/srvdef"
|
|
"videoweb/pub/mongos"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
)
|
|
|
|
var db *mongos.Collection
|
|
|
|
func Init(ctx context.Context) {
|
|
db = mongos.Shop.Coll(TN.SeoPushTask)
|
|
indexes := []mongo.IndexModel{
|
|
{
|
|
// 同一个页面的同一种动作只保留一条任务,重复发布时复用并重置状态
|
|
Keys: bson.D{
|
|
{Key: FN.SeoPushTask.Url, Value: 1},
|
|
{Key: FN.SeoPushTask.Action, Value: 1},
|
|
},
|
|
Options: options.Index().SetUnique(true),
|
|
},
|
|
{
|
|
Keys: bson.D{
|
|
{Key: FN.SeoPushTask.GoogleStatus, Value: 1},
|
|
{Key: FN.SeoPushTask.IndexNowStatus, Value: 1},
|
|
},
|
|
},
|
|
{
|
|
Keys: bson.D{
|
|
{Key: FN.SeoPushTask.CreatedAt, Value: -1},
|
|
},
|
|
},
|
|
}
|
|
if _, err := db.CreateIndex(ctx, indexes); err != nil {
|
|
panic(fmt.Sprintf("(SeoPushTask:Init) Create SeoPushTask Index failed! err:%+v:", err))
|
|
}
|
|
}
|
|
|
|
// UpsertPendingSeoPushTask 登记一条待推送任务,已存在则重置为待推送状态
|
|
func UpsertPendingSeoPushTask(ctx context.Context, task *SeoPushTask) *srvdef.SrvErr {
|
|
filter := bson.M{
|
|
FN.SeoPushTask.Url: task.Url,
|
|
FN.SeoPushTask.Action: task.Action,
|
|
}
|
|
update := bson.M{
|
|
"$set": bson.M{
|
|
FN.SeoPushTask.VideoId: task.VideoId,
|
|
FN.SeoPushTask.GoogleStatus: PushStatusPending,
|
|
FN.SeoPushTask.GoogleRetry: 0,
|
|
FN.SeoPushTask.GoogleErr: "",
|
|
FN.SeoPushTask.IndexNowStatus: PushStatusPending,
|
|
FN.SeoPushTask.IndexNowRetry: 0,
|
|
FN.SeoPushTask.IndexNowErr: "",
|
|
FN.SeoPushTask.UpdatedAt: task.UpdatedAt,
|
|
},
|
|
"$setOnInsert": bson.M{
|
|
FN.SeoPushTask.CreatedAt: task.CreatedAt,
|
|
},
|
|
}
|
|
if _, err := db.UpsertOne(ctx, filter, update); err != nil {
|
|
log.Warn("seopushmod:UpsertPendingSeoPushTask db.UpsertOne failed",
|
|
log.E(err), log.Any("filter", filter))
|
|
return srvdef.New(srvdef.ErrDbUpdateError, "", "")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// FindManySeoPushTasks 查找一批推送任务
|
|
func FindManySeoPushTasks(ctx context.Context, filter bson.M,
|
|
opts ...*options.FindOptions) (SeoPushTaskList, *srvdef.SrvErr) {
|
|
data := make(SeoPushTaskList, 0)
|
|
if err := db.Find(ctx, &data, filter, opts...); err != nil {
|
|
log.Warn("seopushmod:FindManySeoPushTasks db.Find failed",
|
|
log.E(err), log.Any("filter", filter))
|
|
return nil, srvdef.New(srvdef.ErrDbQueryError, "", "")
|
|
}
|
|
return data, nil
|
|
}
|
|
|
|
// UpdateOneSeoPushTask 更新一条推送任务
|
|
func UpdateOneSeoPushTask(ctx context.Context, filter bson.M, update bson.M) *srvdef.SrvErr {
|
|
if _, err := db.UpdateOne(ctx, filter, update); err != nil {
|
|
log.Warn("seopushmod:UpdateOneSeoPushTask db.UpdateOne failed",
|
|
log.E(err), log.Any("filter", filter))
|
|
return srvdef.New(srvdef.ErrDbUpdateError, "", "")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// DelManySeoPushTask 删除一批推送任务
|
|
func DelManySeoPushTask(ctx context.Context, filter bson.M) (int64, *srvdef.SrvErr) {
|
|
count, err := db.DeleteMany(ctx, filter)
|
|
if err != nil {
|
|
log.Warn("seopushmod:DelManySeoPushTask db.DeleteMany failed",
|
|
log.E(err), log.Any("filter", filter))
|
|
return 0, srvdef.New(srvdef.ErrDbDeleteError, "", "")
|
|
}
|
|
return count, nil
|
|
}
|
|
|
|
// CountSeoPushTasks 统计推送任务数量
|
|
func CountSeoPushTasks(ctx context.Context, filter bson.M) (int64, *srvdef.SrvErr) {
|
|
count, err := db.Count(ctx, filter)
|
|
if err != nil {
|
|
log.Warn("seopushmod:CountSeoPushTasks db.Count failed",
|
|
log.E(err), log.Any("filter", filter))
|
|
return 0, srvdef.New(srvdef.ErrDbQueryError, "", "")
|
|
}
|
|
return count, nil
|
|
}
|