init: import web_huangguo2 server project
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,155 @@
|
||||
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.UserId, Value: 1},
|
||||
{Key: FN.PlayletWatchProgress.IsChasing, 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
|
||||
}
|
||||
|
||||
// CountPlayletWatchProgress 统计满足条件的短剧观看进度数量
|
||||
func CountPlayletWatchProgress(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
|
||||
}
|
||||
|
||||
// SavePlayletWatchProgress 保存用户短剧观看进度,并在达到已看条件时维护已看集数集合。
|
||||
func SavePlayletWatchProgress(ctx context.Context, userId int64, playletId int64, chapterId int64, currentEpisode int64, totalEpisode 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))
|
||||
doc.TotalEpisode = totalEpisode
|
||||
doc.IsChasing = totalEpisode > 0 && doc.WatchedCount < totalEpisode
|
||||
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.TotalEpisode: doc.TotalEpisode,
|
||||
FN.PlayletWatchProgress.IsChasing: doc.IsChasing,
|
||||
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
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package playletwatchprogressmod
|
||||
|
||||
// TagPlayletWatchProgress 保存 playlet_watch_progress 集合字段名
|
||||
type TagPlayletWatchProgress struct {
|
||||
Id string
|
||||
UserId string
|
||||
PlayletId string
|
||||
LastChapterId string
|
||||
LastEpisode string
|
||||
WatchedEpisodes string
|
||||
WatchedCount string
|
||||
TotalEpisode string
|
||||
IsChasing string
|
||||
CreatedAt string
|
||||
UpdatedAt string
|
||||
}
|
||||
|
||||
var FN struct {
|
||||
PlayletWatchProgress TagPlayletWatchProgress
|
||||
}
|
||||
|
||||
var TN struct {
|
||||
PlayletWatchProgress string
|
||||
}
|
||||
|
||||
func init() {
|
||||
FN.PlayletWatchProgress.Id = "_id"
|
||||
FN.PlayletWatchProgress.UserId = "userId"
|
||||
FN.PlayletWatchProgress.PlayletId = "playletId"
|
||||
FN.PlayletWatchProgress.LastChapterId = "lastChapterId"
|
||||
FN.PlayletWatchProgress.LastEpisode = "lastEpisode"
|
||||
FN.PlayletWatchProgress.WatchedEpisodes = "watchedEpisodes"
|
||||
FN.PlayletWatchProgress.WatchedCount = "watchedCount"
|
||||
FN.PlayletWatchProgress.TotalEpisode = "totalEpisode"
|
||||
FN.PlayletWatchProgress.IsChasing = "isChasing"
|
||||
FN.PlayletWatchProgress.CreatedAt = "createdAt"
|
||||
FN.PlayletWatchProgress.UpdatedAt = "updatedAt"
|
||||
|
||||
TN.PlayletWatchProgress = "playlet_watch_progress"
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package playletwatchprogressmod
|
||||
|
||||
import "time"
|
||||
|
||||
// PlayletWatchProgress @table: playlet_watch_progress
|
||||
//
|
||||
// PlayletWatchProgress 用户短剧观看进度模型,用于收藏列表计算未看集数和恢复上次观看位置。
|
||||
type PlayletWatchProgress struct {
|
||||
Id int64 `json:"id" bson:"_id,omitempty"` // 观看进度记录ID
|
||||
UserId int64 `json:"userId" bson:"userId"` // 用户ID
|
||||
PlayletId int64 `json:"playletId" bson:"playletId"` // 短剧ID
|
||||
LastChapterId int64 `json:"lastChapterId" bson:"lastChapterId"` // 最近观看剧集ID
|
||||
LastEpisode int64 `json:"lastEpisode" bson:"lastEpisode"` // 最近观看集数
|
||||
WatchedEpisodes []int64 `json:"watchedEpisodes" bson:"watchedEpisodes"` // 已看集数集合
|
||||
WatchedCount int64 `json:"watchedCount" bson:"watchedCount"` // 已看集数冗余值
|
||||
TotalEpisode int64 `json:"totalEpisode" bson:"totalEpisode"` // 短剧总集数快照
|
||||
IsChasing bool `json:"isChasing" bson:"isChasing"` // 是否追剧中 true-未看完 false-已看完
|
||||
CreatedAt time.Time `json:"createdAt" bson:"createdAt"` // 创建时间
|
||||
UpdatedAt time.Time `json:"updatedAt" bson:"updatedAt"` // 修改时间
|
||||
}
|
||||
|
||||
// PlayletWatchProgressList 用户短剧观看进度列表
|
||||
type PlayletWatchProgressList []*PlayletWatchProgress
|
||||
Reference in New Issue
Block a user