init: 初始化 web_hgdjb_server 项目
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,224 @@
|
||||
package aicommentrecord
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
"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.AiCommentRecord)
|
||||
indexes := []mongo.IndexModel{
|
||||
{Keys: bson.D{{Key: FN.AiCommentRecord.Key, Value: 1}}, Options: options.Index().SetUnique(true).SetPartialFilterExpression(bson.M{FN.AiCommentRecord.Key: bson.M{"$exists": true}})},
|
||||
{Keys: bson.D{{Key: FN.AiCommentRecord.Vid, Value: 1}, {Key: FN.AiCommentRecord.TaskId, Value: 1}}},
|
||||
{Keys: bson.D{{Key: FN.AiCommentRecord.ObjType, Value: 1}, {Key: FN.AiCommentRecord.Vid, Value: 1}}},
|
||||
{Keys: bson.D{{Key: FN.AiCommentRecord.Vid, Value: 1}, {Key: FN.AiCommentRecord.Status, Value: 1}, {Key: FN.AiCommentRecord.CreatedAt, Value: 1}}},
|
||||
{Keys: bson.D{{Key: FN.AiCommentRecord.TaskId, Value: 1}, {Key: FN.AiCommentRecord.Status, Value: 1}}},
|
||||
{Keys: bson.D{{Key: FN.AiCommentRecord.ObjType, Value: 1}, {Key: FN.AiCommentRecord.Status, Value: 1}, {Key: FN.AiCommentRecord.CreatedAt, Value: 1}}},
|
||||
{Keys: bson.D{{Key: FN.AiCommentRecord.Status, Value: 1}, {Key: FN.AiCommentRecord.CreatedAt, Value: 1}}},
|
||||
{Keys: bson.D{{Key: FN.AiCommentRecord.CreatedAt, Value: 1}}},
|
||||
}
|
||||
if _, err := db.CreateIndex(ctx, indexes); err != nil {
|
||||
panic(fmt.Sprintf("(aicommentrecord:Init) Create AiCommentRecord Index failed! err:%+v:", err))
|
||||
}
|
||||
}
|
||||
|
||||
// Reserve claims one playlet before the external submit request. A failed claim
|
||||
// can be retried, while a submitted or completed claim blocks duplicate work.
|
||||
func Reserve(ctx context.Context, record *AiCommentRecord) (bool, *srvdef.SrvErr) {
|
||||
now := time.Now()
|
||||
record.Status = StatusSubmitting
|
||||
record.CreatedAt = now
|
||||
record.UpdatedAt = now
|
||||
result, err := db.UpsertOne(ctx, bson.M{FN.AiCommentRecord.Key: record.Key}, bson.M{"$setOnInsert": record})
|
||||
if err != nil {
|
||||
log.Warn("aicommentrecord:Reserve failed", log.E(err))
|
||||
return false, srvdef.New(srvdef.ErrDbInsertError, "", "")
|
||||
}
|
||||
if result.UpsertedCount > 0 {
|
||||
return true, nil
|
||||
}
|
||||
result, err = db.UpdateOne(ctx,
|
||||
bson.M{FN.AiCommentRecord.Key: record.Key, FN.AiCommentRecord.Status: bson.M{"$in": []int{StatusFailed, StatusAbandoned}}},
|
||||
bson.M{"$set": bson.M{
|
||||
FN.AiCommentRecord.Status: StatusSubmitting,
|
||||
FN.AiCommentRecord.TaskId: "",
|
||||
FN.AiCommentRecord.RetainCount: record.RetainCount,
|
||||
FN.AiCommentRecord.TargetCount: record.TargetCount,
|
||||
FN.AiCommentRecord.Title: record.Title,
|
||||
FN.AiCommentRecord.Desc: record.Desc,
|
||||
FN.AiCommentRecord.CreatedAt: now,
|
||||
FN.AiCommentRecord.UpdatedAt: now,
|
||||
}},
|
||||
)
|
||||
if err != nil {
|
||||
log.Warn("aicommentrecord:Reserve retry failed", log.E(err))
|
||||
return false, srvdef.New(srvdef.ErrDbUpdateError, "", "")
|
||||
}
|
||||
return result.ModifiedCount > 0, nil
|
||||
}
|
||||
|
||||
func SetSubmission(ctx context.Context, key, taskID string, status int, desc string) *srvdef.SrvErr {
|
||||
result, err := db.UpdateOne(ctx,
|
||||
bson.M{FN.AiCommentRecord.Key: key, FN.AiCommentRecord.Status: StatusSubmitting},
|
||||
bson.M{"$set": bson.M{
|
||||
FN.AiCommentRecord.TaskId: taskID,
|
||||
FN.AiCommentRecord.Status: status,
|
||||
FN.AiCommentRecord.Desc: desc,
|
||||
FN.AiCommentRecord.UpdatedAt: time.Now(),
|
||||
}},
|
||||
)
|
||||
if err != nil {
|
||||
log.Warn("aicommentrecord:SetSubmission failed", log.E(err), log.Any("key", key))
|
||||
return srvdef.New(srvdef.ErrDbUpdateError, "", "")
|
||||
}
|
||||
if result.MatchedCount == 0 {
|
||||
log.Warn("aicommentrecord:SetSubmission reservation not found", log.Any("key", key))
|
||||
return srvdef.New(srvdef.ErrDbUpdateError, "", "")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func InsertMany(ctx context.Context, records AiCommentRecordList) *srvdef.SrvErr {
|
||||
if len(records) == 0 {
|
||||
return nil
|
||||
}
|
||||
now := time.Now()
|
||||
for _, record := range records {
|
||||
if record.CreatedAt.IsZero() {
|
||||
record.CreatedAt = now
|
||||
}
|
||||
record.UpdatedAt = now
|
||||
}
|
||||
if _, err := db.InsertMany(ctx, &records); err != nil {
|
||||
log.Warn("aicommentrecord:InsertMany db.InsertMany failed", log.E(err))
|
||||
return srvdef.New(srvdef.ErrDbInsertError, "", "")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func FindByStatus(ctx context.Context, status int, limit int64) (AiCommentRecordList, *srvdef.SrvErr) {
|
||||
if limit <= 0 {
|
||||
limit = 100
|
||||
}
|
||||
records := make(AiCommentRecordList, 0)
|
||||
err := db.Find(ctx,
|
||||
&records,
|
||||
bson.M{FN.AiCommentRecord.Status: status},
|
||||
options.Find().SetSort(bson.D{{Key: FN.AiCommentRecord.CreatedAt, Value: 1}}).SetLimit(limit),
|
||||
)
|
||||
if err != nil {
|
||||
log.Warn("aicommentrecord:FindByStatus db.Find failed", log.E(err), log.Any("status", status))
|
||||
return nil, srvdef.New(srvdef.ErrDbQueryError, "", "")
|
||||
}
|
||||
return records, nil
|
||||
}
|
||||
|
||||
func FindFailedForRetry(ctx context.Context, limit int64) (AiCommentRecordList, *srvdef.SrvErr) {
|
||||
if limit <= 0 {
|
||||
limit = 100
|
||||
}
|
||||
records := make(AiCommentRecordList, 0)
|
||||
err := db.Find(ctx, &records,
|
||||
bson.M{FN.AiCommentRecord.Status: StatusFailed},
|
||||
options.Find().SetSort(bson.D{{Key: FN.AiCommentRecord.UpdatedAt, Value: 1}}).SetLimit(limit),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, srvdef.New(srvdef.ErrDbQueryError, "", "")
|
||||
}
|
||||
return records, nil
|
||||
}
|
||||
|
||||
func SetStatusByKey(ctx context.Context, key string, status int, desc string) *srvdef.SrvErr {
|
||||
_, err := db.UpdateOne(ctx,
|
||||
bson.M{FN.AiCommentRecord.Key: key},
|
||||
bson.M{"$set": bson.M{
|
||||
FN.AiCommentRecord.Status: status,
|
||||
FN.AiCommentRecord.Desc: desc,
|
||||
FN.AiCommentRecord.UpdatedAt: time.Now(),
|
||||
}},
|
||||
)
|
||||
if err != nil {
|
||||
return srvdef.New(srvdef.ErrDbUpdateError, "", "")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func FindByVIDsAndObjType(ctx context.Context, objType int64, vids []string) (AiCommentRecordList, *srvdef.SrvErr) {
|
||||
if len(vids) == 0 {
|
||||
return AiCommentRecordList{}, nil
|
||||
}
|
||||
records := make(AiCommentRecordList, 0)
|
||||
filter := bson.M{FN.AiCommentRecord.Vid: bson.M{"$in": vids}}
|
||||
if objType > 0 {
|
||||
filter[FN.AiCommentRecord.ObjType] = objType
|
||||
}
|
||||
if err := db.Find(ctx, &records, filter); err != nil {
|
||||
log.Warn("aicommentrecord:FindByVIDsAndObjType db.Find failed", log.E(err), log.Any("objType", objType), log.Any("vids", vids))
|
||||
return nil, srvdef.New(srvdef.ErrDbQueryError, "", "")
|
||||
}
|
||||
return records, nil
|
||||
}
|
||||
|
||||
func BatchUpdateStatus(ctx context.Context, taskIDs []string, status int) *srvdef.SrvErr {
|
||||
if len(taskIDs) == 0 {
|
||||
return nil
|
||||
}
|
||||
if _, err := db.UpdateMMany(ctx,
|
||||
bson.M{FN.AiCommentRecord.TaskId: bson.M{"$in": taskIDs}},
|
||||
bson.M{"$set": bson.M{
|
||||
FN.AiCommentRecord.Status: status,
|
||||
FN.AiCommentRecord.UpdatedAt: time.Now(),
|
||||
}},
|
||||
); err != nil {
|
||||
log.Warn("aicommentrecord:BatchUpdateStatus db.UpdateMMany failed", log.E(err), log.Any("taskIDs", taskIDs))
|
||||
return srvdef.New(srvdef.ErrDbUpdateError, "", "")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func UpdateStatusAndDescByTaskID(ctx context.Context, taskID string, status int, desc string) *srvdef.SrvErr {
|
||||
if taskID == "" {
|
||||
return nil
|
||||
}
|
||||
if _, err := db.UpdateOne(ctx,
|
||||
bson.M{FN.AiCommentRecord.TaskId: taskID},
|
||||
bson.M{"$set": bson.M{
|
||||
FN.AiCommentRecord.Status: status,
|
||||
FN.AiCommentRecord.Desc: desc,
|
||||
FN.AiCommentRecord.UpdatedAt: time.Now(),
|
||||
}},
|
||||
); err != nil {
|
||||
log.Warn("aicommentrecord:UpdateStatusAndDescByTaskID db.UpdateOne failed", log.E(err), log.Any("taskID", taskID))
|
||||
return srvdef.New(srvdef.ErrDbUpdateError, "", "")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func ExpireSubmittedBefore(ctx context.Context, deadline time.Time, desc string) (int64, *srvdef.SrvErr) {
|
||||
result, err := db.UpdateMMany(ctx,
|
||||
bson.M{
|
||||
FN.AiCommentRecord.Status: bson.M{"$in": []int{StatusSubmitting, StatusSubmitted}},
|
||||
FN.AiCommentRecord.CreatedAt: bson.M{"$lt": deadline},
|
||||
},
|
||||
bson.M{"$set": bson.M{
|
||||
FN.AiCommentRecord.Status: StatusFailed,
|
||||
FN.AiCommentRecord.Desc: desc,
|
||||
FN.AiCommentRecord.UpdatedAt: time.Now(),
|
||||
}},
|
||||
)
|
||||
if err != nil {
|
||||
log.Warn("aicommentrecord:ExpireSubmittedBefore db.UpdateMMany failed", log.E(err), log.Any("deadline", deadline))
|
||||
return 0, srvdef.New(srvdef.ErrDbUpdateError, "", "")
|
||||
}
|
||||
return result.ModifiedCount, nil
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package aicommentrecord
|
||||
|
||||
type TagAiCommentRecord struct {
|
||||
Id string
|
||||
Key string
|
||||
Vid string
|
||||
Title string
|
||||
TaskId string
|
||||
ObjType string
|
||||
RetainCount string
|
||||
TargetCount string
|
||||
Status string
|
||||
Desc string
|
||||
CreatedAt string
|
||||
UpdatedAt string
|
||||
DeletedAt string
|
||||
}
|
||||
|
||||
var FN struct {
|
||||
AiCommentRecord TagAiCommentRecord
|
||||
}
|
||||
|
||||
var TN struct {
|
||||
AiCommentRecord string
|
||||
}
|
||||
|
||||
func init() {
|
||||
FN.AiCommentRecord.Id = "_id"
|
||||
FN.AiCommentRecord.Key = "key"
|
||||
FN.AiCommentRecord.Vid = "vid"
|
||||
FN.AiCommentRecord.Title = "title"
|
||||
FN.AiCommentRecord.TaskId = "task_id"
|
||||
FN.AiCommentRecord.ObjType = "objType"
|
||||
FN.AiCommentRecord.RetainCount = "retainCount"
|
||||
FN.AiCommentRecord.TargetCount = "targetCount"
|
||||
FN.AiCommentRecord.Status = "status"
|
||||
FN.AiCommentRecord.Desc = "desc"
|
||||
FN.AiCommentRecord.CreatedAt = "createdAt"
|
||||
FN.AiCommentRecord.UpdatedAt = "updatedAt"
|
||||
FN.AiCommentRecord.DeletedAt = "deletedAt"
|
||||
TN.AiCommentRecord = "ai_comment_record"
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package aicommentrecord
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
)
|
||||
|
||||
const (
|
||||
StatusSubmitting = 0
|
||||
StatusSubmitted = 1
|
||||
StatusSuccess = 2
|
||||
StatusFailed = 3
|
||||
StatusAbandoned = 4
|
||||
)
|
||||
|
||||
// AiCommentRecord 记录已提交到自动评论服务的短剧任务,避免重复提交。
|
||||
// @table:ai_comment_record
|
||||
type AiCommentRecord struct {
|
||||
ID primitive.ObjectID `json:"id" bson:"_id,omitempty"`
|
||||
Key string `json:"key" bson:"key,omitempty"`
|
||||
Vid string `json:"vid" bson:"vid"`
|
||||
Title string `json:"title" bson:"title"`
|
||||
TaskId string `json:"taskId" bson:"task_id"`
|
||||
ObjType int64 `json:"objType" bson:"objType"`
|
||||
RetainCount int `json:"retainCount" bson:"retainCount,omitempty"` // 该任务结果需保留的评论数
|
||||
TargetCount int `json:"targetCount" bson:"targetCount,omitempty"` // 本轮内容最终需要达到的AI评论数
|
||||
Status int `json:"status" bson:"status"`
|
||||
Desc string `json:"desc" bson:"desc"`
|
||||
CreatedAt time.Time `json:"createdAt" bson:"createdAt"`
|
||||
UpdatedAt time.Time `json:"updatedAt" bson:"updatedAt"`
|
||||
DeletedAt time.Time `json:"deletedAt" bson:"deletedAt"`
|
||||
}
|
||||
|
||||
type AiCommentRecordList []*AiCommentRecord
|
||||
Reference in New Issue
Block a user