package playletwatchprogressmod 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 // Init 初始化 playlet_watch_progress 集合和常用查询索引 func Init(ctx context.Context) { coll = mongos.Shop.Coll(TN.PlayletWatchProgress) initIndex(ctx) } func initIndex(ctx context.Context) { indexes := []mongo.IndexModel{ { Keys: bson.D{ {Key: FN.PlayletWatchProgress.UserId, Value: 1}, {Key: FN.PlayletWatchProgress.PlayletId, Value: 1}, }, Options: options.Index().SetUnique(true), }, { Keys: bson.D{ {Key: FN.PlayletWatchProgress.UserId, Value: 1}, {Key: FN.PlayletWatchProgress.UpdatedAt, Value: -1}, }, }, { Keys: bson.D{ {Key: FN.PlayletWatchProgress.PlayletId, Value: 1}, }, }, } if _, err := coll.CreateIndex(ctx, indexes); err != nil { panic(fmt.Sprintf("(playletwatchprogressmod:initIndex) 创建索引失败 err:%+v:", err)) } } // FindManyPlayletWatchProgress 按条件查询用户短剧观看进度 func FindManyPlayletWatchProgress(ctx context.Context, filter bson.M, opts ...*options.FindOptions) (PlayletWatchProgressList, *srvdef.SrvErr) { data := make(PlayletWatchProgressList, 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 } // SavePlayletWatchProgress 保存用户短剧观看进度,并在达到已看条件时维护已看集数集合。 func SavePlayletWatchProgress(ctx context.Context, userId int64, playletId int64, chapterId int64, currentEpisode int64, markWatched bool) (*PlayletWatchProgress, *srvdef.SrvErr) { filter := bson.M{ FN.PlayletWatchProgress.UserId: userId, FN.PlayletWatchProgress.PlayletId: playletId, } doc, err := findOnePlayletWatchProgress(ctx, filter) if err != nil && err.Code != srvdef.ErrNoData { return nil, err } now := time.Now() if doc == nil { doc = &PlayletWatchProgress{ Id: mongos.Lid.PlayletWatchProgress.Get(), UserId: userId, PlayletId: playletId, WatchedEpisodes: make([]int64, 0), CreatedAt: now, } } doc.LastChapterId = chapterId doc.LastEpisode = currentEpisode doc.UpdatedAt = now if markWatched && !hasEpisode(doc.WatchedEpisodes, currentEpisode) { doc.WatchedEpisodes = append(doc.WatchedEpisodes, currentEpisode) } doc.WatchedCount = int64(len(doc.WatchedEpisodes)) update := bson.M{ "$set": bson.M{ FN.PlayletWatchProgress.LastChapterId: doc.LastChapterId, FN.PlayletWatchProgress.LastEpisode: doc.LastEpisode, FN.PlayletWatchProgress.WatchedEpisodes: doc.WatchedEpisodes, FN.PlayletWatchProgress.WatchedCount: doc.WatchedCount, FN.PlayletWatchProgress.UpdatedAt: doc.UpdatedAt, }, "$setOnInsert": bson.M{ FN.PlayletWatchProgress.Id: doc.Id, FN.PlayletWatchProgress.UserId: doc.UserId, FN.PlayletWatchProgress.PlayletId: doc.PlayletId, FN.PlayletWatchProgress.CreatedAt: doc.CreatedAt, }, } if _, e := coll.UpsertOne(ctx, filter, update); e != nil { log.Warn("保存短剧观看进度失败", log.E(e), log.Any("filter", filter)) return nil, srvdef.New(srvdef.ErrDbUpdateError, "", "") } return doc, nil } func findOnePlayletWatchProgress(ctx context.Context, filter bson.M) (*PlayletWatchProgress, *srvdef.SrvErr) { var doc PlayletWatchProgress if err := coll.FindOneEx(ctx, &doc, filter, options.FindOne().SetProjection(bson.D{ {Key: FN.PlayletWatchProgress.Id, Value: 1}, {Key: FN.PlayletWatchProgress.UserId, Value: 1}, {Key: FN.PlayletWatchProgress.PlayletId, Value: 1}, {Key: FN.PlayletWatchProgress.WatchedEpisodes, Value: 1}, {Key: FN.PlayletWatchProgress.WatchedCount, Value: 1}, {Key: FN.PlayletWatchProgress.CreatedAt, Value: 1}, })); err != nil { if err == mongo.ErrNoDocuments { return nil, srvdef.New(srvdef.ErrNoData, "", "") } log.Warn("查询短剧观看进度失败", log.E(err), log.Any("filter", filter)) return nil, srvdef.New(srvdef.ErrDbQueryError, "", "") } return &doc, nil } func hasEpisode(episodes []int64, target int64) bool { for _, episode := range episodes { if episode == target { return true } } return false }