From ab8bdde9d9edd00fa4cd6d9054ceda4021a15bc1 Mon Sep 17 00:00:00 2001 From: cjy Date: Thu, 8 Jan 2026 16:44:20 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E5=AE=9A=E6=97=B6?= =?UTF-8?q?=E4=BB=BB=E5=8A=A1=E6=9B=B4=E6=96=B0=E8=A7=82=E7=9C=8B=E6=AC=A1?= =?UTF-8?q?=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/cron/task.go | 133 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 133 insertions(+) diff --git a/pkg/cron/task.go b/pkg/cron/task.go index 5e91ef6..ac5d65f 100644 --- a/pkg/cron/task.go +++ b/pkg/cron/task.go @@ -5,6 +5,7 @@ import ( "encoding/json" "errors" "fmt" + "fonchain-fiee/api/aryshare" "fonchain-fiee/api/bundle" "fonchain-fiee/api/cast" "fonchain-fiee/pkg/cache" @@ -15,6 +16,7 @@ import ( "log" "math/rand" "strconv" + "strings" "time" "github.com/go-redis/redis" @@ -39,6 +41,9 @@ func InitTasks() error { if err != nil { log.Printf("添加定时任务失败: %v", err) } + + // 每2分钟执行一次标签观看次数更新任务 + // err = cm.AddTask("updateCastTagWatchCount", "0 */2 * * * *", UpdateCastTagWatchCountTask) cm.Start() // 启动队列消费者 @@ -496,3 +501,131 @@ func AyrshareMetricsCollectorTask() { func RefreshArtistOrderTask() { service.CastProvider.Tools(context.Background(), &cast.ToolsReq{Action: "refreshArtistOrder"}) } + +// UpdateCastTagWatchCountTask 更新标签观看次数的定时任务(每5分钟执行一次) +func UpdateCastTagWatchCountTask() { + ctx := context.Background() + + // 计算两天前的00:00:00 + now := time.Now() + twoDaysAgo := now.AddDate(0, 0, -2) + createdAtStart := time.Date(twoDaysAgo.Year(), twoDaysAgo.Month(), twoDaysAgo.Day(), 0, 0, 0, 0, twoDaysAgo.Location()) + createdAtEnd := now + + // 格式化时间字符串:2026-01-01 00:00:00 + createdAtStartStr := createdAtStart.Format("2006-01-02 15:04:05") + createdAtEndStr := createdAtEnd.Format("2006-01-02 15:04:05") + + // 调用 ListCastTags 接口,筛选 IsWatchCountCalled = 2 的数据 + listReq := &cast.ListCastTagsReq{ + CreatedAtStart: createdAtStartStr, + CreatedAtEnd: createdAtEndStr, + IsWatchCountCalled: 2, // 2表示未调用 + Page: 1, + PageSize: 20, + } + + listResp, err := service.CastProvider.ListCastTags(ctx, listReq) + if err != nil { + zap.L().Error("获取标签列表失败", zap.Error(err)) + return + } + + if listResp.Data == nil || len(listResp.Data) == 0 { + return + } + + zap.L().Info("获取到需要更新的标签", zap.Int("count", len(listResp.Data))) + + // 获取有效的 profileKey + profileKey, err := serverCast.GetValidProfileKey(ctx, []uint32{1}) + if err != nil { + zap.L().Error("获取有效profileKey失败", zap.Error(err)) + return + } + + // 准备批量更新的数据 + updateData := make([]*cast.CastTagInfo, 0, len(listResp.Data)) + + // 遍历每个标签,调用 RecommendHashtags 接口 + for _, tag := range listResp.Data { + if tag.HashTag == "" { + zap.L().Warn("标签HashTag为空,跳过", zap.String("uuid", tag.Uuid)) + // 即使HashTag为空,也要更新IsWatchCountCalled为1 + updateData = append(updateData, &cast.CastTagInfo{ + Uuid: tag.Uuid, + WatchCount: 1, + IsWatchCountCalled: 1, + }) + continue + } + + // 调用 RecommendHashtags 接口 + recommendReq := &aryshare.RecommendHashtagsRequest{ + Keyword: tag.HashTag, + ProfileKey: profileKey, + } + + recommendResp, err := service.AyrshareProvider.RecommendHashtags(ctx, recommendReq) + if err != nil { + zap.L().Error("调用RecommendHashtags接口失败", + zap.String("hashTag", tag.HashTag), + zap.String("uuid", tag.Uuid), + zap.Error(err)) + // 调用失败时,将WatchCount更新为1,IsWatchCountCalled更新为1 + updateData = append(updateData, &cast.CastTagInfo{ + Uuid: tag.Uuid, + WatchCount: 1, + IsWatchCountCalled: 1, + }) + continue + } + + // 对比返回结果,查找完全一致的标签 + var matchedViewCount int64 = 0 + if recommendResp.Recommendations != nil { + for _, recommendation := range recommendResp.Recommendations { + // 完全一致匹配(不区分大小写) + if strings.EqualFold(recommendation.Name, tag.HashTag) { + matchedViewCount = recommendation.ViewCount + break + } + } + } + + // 根据匹配结果更新WatchCount + var watchCount int32 = 1 + if matchedViewCount > 0 { + watchCount = int32(matchedViewCount) + } + + // 添加到更新列表 + updateData = append(updateData, &cast.CastTagInfo{ + Uuid: tag.Uuid, + WatchCount: watchCount, + IsWatchCountCalled: 1, + }) + + zap.L().Debug("处理标签完成", + zap.String("hashTag", tag.HashTag), + zap.String("uuid", tag.Uuid), + zap.Int64("matchedViewCount", matchedViewCount), + zap.Int32("watchCount", watchCount)) + } + + // 如果没有需要更新的数据,直接返回 + if len(updateData) == 0 { + return + } + + // 批量更新标签 + batchUpdateReq := &cast.BatchUpdateCastTagsReq{ + Data: updateData, + } + + _, err = service.CastProvider.BatchUpdateCastTags(ctx, batchUpdateReq) + if err != nil { + zap.L().Error("批量更新标签失败", zap.Error(err), zap.Int("count", len(updateData))) + return + } +}