来源:hgdjnet-server 9bfbafe10d417deea959bd53117bc88a10e60a83 项目身份 HKDJ / JHA-216,独立官网仓库,Jenkins 守卫键 hkdjnet 测试目录 /server/hkdj_net,App/Web API 30331/30332 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
110 lines
3.6 KiB
Go
110 lines
3.6 KiB
Go
package playletwatchhistorymod
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"hkdj-web-server/pub/common/log"
|
|
"hkdj-web-server/pub/common/srvdef"
|
|
"hkdj-web-server/pub/mongos"
|
|
"time"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
)
|
|
|
|
var coll *mongos.Collection
|
|
|
|
const playletWatchHistoryTTLSeconds int32 = 90 * 24 * 60 * 60
|
|
|
|
// Init 初始化 playlet_watch_history 集合和常用查询索引
|
|
func Init(ctx context.Context) {
|
|
coll = mongos.Shop.Coll(TN.PlayletWatchHistory)
|
|
initIndex(ctx)
|
|
}
|
|
|
|
func initIndex(ctx context.Context) {
|
|
indexes := []mongo.IndexModel{
|
|
{
|
|
Keys: bson.D{
|
|
{Key: FN.PlayletWatchHistory.UserId, Value: 1},
|
|
{Key: FN.PlayletWatchHistory.PlayletId, Value: 1},
|
|
},
|
|
Options: options.Index().SetUnique(true),
|
|
},
|
|
{
|
|
Keys: bson.D{
|
|
{Key: FN.PlayletWatchHistory.UserId, Value: 1},
|
|
{Key: FN.PlayletWatchHistory.UpdatedAt, Value: -1},
|
|
{Key: FN.PlayletWatchHistory.Id, Value: -1},
|
|
},
|
|
},
|
|
{
|
|
Keys: bson.D{
|
|
{Key: FN.PlayletWatchHistory.UpdatedAt, Value: 1},
|
|
},
|
|
Options: options.Index().SetExpireAfterSeconds(playletWatchHistoryTTLSeconds),
|
|
},
|
|
}
|
|
if _, err := coll.CreateIndex(ctx, indexes); err != nil {
|
|
panic(fmt.Sprintf("(playletwatchhistorymod:initIndex) 创建索引失败 err:%+v:", err))
|
|
}
|
|
}
|
|
|
|
// FindManyPlayletWatchHistory 按条件查询用户短剧观看历史
|
|
func FindManyPlayletWatchHistory(ctx context.Context, filter bson.M, opts ...*options.FindOptions) (PlayletWatchHistoryList, *srvdef.SrvErr) {
|
|
data := make(PlayletWatchHistoryList, 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
|
|
}
|
|
|
|
// CountPlayletWatchHistory 统计满足条件的短剧观看历史数量
|
|
func CountPlayletWatchHistory(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
|
|
}
|
|
|
|
// SavePlayletWatchHistory 保存用户短剧观看历史
|
|
func SavePlayletWatchHistory(ctx context.Context, userId int64, playletId int64, chapterId int64, currentEpisode int64) *srvdef.SrvErr {
|
|
now := time.Now()
|
|
filter := bson.M{
|
|
FN.PlayletWatchHistory.UserId: userId,
|
|
FN.PlayletWatchHistory.PlayletId: playletId,
|
|
}
|
|
update := bson.M{
|
|
"$set": bson.M{
|
|
FN.PlayletWatchHistory.LastChapterId: chapterId,
|
|
FN.PlayletWatchHistory.LastEpisode: currentEpisode,
|
|
FN.PlayletWatchHistory.UpdatedAt: now,
|
|
},
|
|
"$setOnInsert": bson.M{
|
|
FN.PlayletWatchHistory.Id: mongos.Lid.PlayletWatchHistory.Get(),
|
|
FN.PlayletWatchHistory.UserId: userId,
|
|
FN.PlayletWatchHistory.PlayletId: playletId,
|
|
FN.PlayletWatchHistory.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
|
|
}
|
|
|
|
// DeleteManyPlayletWatchHistory 删除满足条件的短剧观看历史
|
|
func DeleteManyPlayletWatchHistory(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
|
|
}
|