init: web_hgdjc_server
This commit is contained in:
@@ -0,0 +1,109 @@
|
||||
package playletwatchhistorymod
|
||||
|
||||
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 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
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package playletwatchhistorymod
|
||||
|
||||
// TagPlayletWatchHistory 保存 playlet_watch_history 集合字段名
|
||||
type TagPlayletWatchHistory struct {
|
||||
Id string
|
||||
UserId string
|
||||
PlayletId string
|
||||
LastChapterId string
|
||||
LastEpisode string
|
||||
CreatedAt string
|
||||
UpdatedAt string
|
||||
}
|
||||
|
||||
var FN struct {
|
||||
PlayletWatchHistory TagPlayletWatchHistory
|
||||
}
|
||||
|
||||
var TN struct {
|
||||
PlayletWatchHistory string
|
||||
}
|
||||
|
||||
func init() {
|
||||
FN.PlayletWatchHistory.Id = "_id"
|
||||
FN.PlayletWatchHistory.UserId = "userId"
|
||||
FN.PlayletWatchHistory.PlayletId = "playletId"
|
||||
FN.PlayletWatchHistory.LastChapterId = "lastChapterId"
|
||||
FN.PlayletWatchHistory.LastEpisode = "lastEpisode"
|
||||
FN.PlayletWatchHistory.CreatedAt = "createdAt"
|
||||
FN.PlayletWatchHistory.UpdatedAt = "updatedAt"
|
||||
|
||||
TN.PlayletWatchHistory = "playlet_watch_history"
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package playletwatchhistorymod
|
||||
|
||||
import "time"
|
||||
|
||||
// PlayletWatchHistory @table: playlet_watch_history
|
||||
//
|
||||
// PlayletWatchHistory 用户短剧观看历史模型,用于观看历史列表和清空历史。
|
||||
type PlayletWatchHistory 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"` // 最近观看集数
|
||||
CreatedAt time.Time `json:"createdAt" bson:"createdAt"` // 创建时间
|
||||
UpdatedAt time.Time `json:"updatedAt" bson:"updatedAt"` // 修改时间
|
||||
}
|
||||
|
||||
// PlayletWatchHistoryList 用户短剧观看历史列表
|
||||
type PlayletWatchHistoryList []*PlayletWatchHistory
|
||||
Reference in New Issue
Block a user