Files
2026-09-29 10:47:38 +08:00

108 lines
3.3 KiB
Go

package videowatchhistorymod
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 coll *mongos.Collection
const videoWatchHistoryTTLSeconds int32 = 90 * 24 * 60 * 60
// Init 初始化 video_watch_history 集合和常用查询索引
func Init(ctx context.Context) {
coll = mongos.Shop.Coll(TN.VideoWatchHistory)
initIndex(ctx)
}
func initIndex(ctx context.Context) {
indexes := []mongo.IndexModel{
{
Keys: bson.D{
{Key: FN.VideoWatchHistory.UserId, Value: 1},
{Key: FN.VideoWatchHistory.VideoId, Value: 1},
},
Options: options.Index().SetUnique(true),
},
{
Keys: bson.D{
{Key: FN.VideoWatchHistory.UserId, Value: 1},
{Key: FN.VideoWatchHistory.UpdatedAt, Value: -1},
{Key: FN.VideoWatchHistory.Id, Value: -1},
},
},
{
Keys: bson.D{
{Key: FN.VideoWatchHistory.UpdatedAt, Value: 1},
},
Options: options.Index().SetExpireAfterSeconds(videoWatchHistoryTTLSeconds),
},
}
if _, err := coll.CreateIndex(ctx, indexes); err != nil {
panic(fmt.Sprintf("(videowatchhistorymod:initIndex) 创建索引失败 err:%+v:", err))
}
}
// FindManyVideoWatchHistory 按条件查询用户视频观看历史
func FindManyVideoWatchHistory(ctx context.Context, filter bson.M, opts ...*options.FindOptions) (VideoWatchHistoryList, *srvdef.SrvErr) {
data := make(VideoWatchHistoryList, 0)
if err := coll.Find(ctx, &data, filter, opts...); err != nil {
log.Warn("查询视频观看历史列表失败", log.E(err), log.Any("filter", filter))
return nil, srvdef.New(srvdef.ErrDbQueryError, "", "")
}
return data, nil
}
// CountVideoWatchHistory 统计满足条件的视频观看历史数量
func CountVideoWatchHistory(ctx context.Context, filter bson.M, opts ...*options.CountOptions) (int64, *srvdef.SrvErr) {
count, err := coll.Count(ctx, filter, opts...)
if err != nil {
log.Warn("统计视频观看历史数量失败", log.E(err), log.Any("filter", filter))
return 0, srvdef.New(srvdef.ErrDbQueryError, "", "")
}
return count, nil
}
// SaveVideoWatchHistory 保存用户视频观看历史
func SaveVideoWatchHistory(ctx context.Context, userId int64, videoId int64) *srvdef.SrvErr {
now := time.Now()
filter := bson.M{
FN.VideoWatchHistory.UserId: userId,
FN.VideoWatchHistory.VideoId: videoId,
}
update := bson.M{
"$set": bson.M{
FN.VideoWatchHistory.UpdatedAt: now,
},
"$setOnInsert": bson.M{
FN.VideoWatchHistory.Id: mongos.Lid.VideoWatchHistory.Get(),
FN.VideoWatchHistory.UserId: userId,
FN.VideoWatchHistory.VideoId: videoId,
FN.VideoWatchHistory.CreatedAt: now,
},
}
if _, err := coll.UpsertOne(ctx, filter, update); err != nil {
log.Warn("保存视频观看历史失败", log.E(err), log.Any("filter", filter))
return srvdef.New(srvdef.ErrDbUpdateError, "", "")
}
return nil
}
// DeleteManyVideoWatchHistory 删除满足条件的视频观看历史
func DeleteManyVideoWatchHistory(ctx context.Context, filter bson.M, opts ...*options.DeleteOptions) (int64, *srvdef.SrvErr) {
count, err := coll.DeleteMany(ctx, filter, opts...)
if err != nil {
log.Warn("删除视频观看历史失败", log.E(err), log.Any("filter", filter))
return 0, srvdef.New(srvdef.ErrDbDeleteError, "", "")
}
return count, nil
}