init: web_hgdjc_server

This commit is contained in:
2026-11-07 10:46:18 +08:00
commit b2ff6ac8ff
707 changed files with 147645 additions and 0 deletions
+107
View File
@@ -0,0 +1,107 @@
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
}
+28
View File
@@ -0,0 +1,28 @@
package videowatchhistorymod
// TagVideoWatchHistory 保存 video_watch_history 集合字段名
type TagVideoWatchHistory struct {
Id string
UserId string
VideoId string
CreatedAt string
UpdatedAt string
}
var FN struct {
VideoWatchHistory TagVideoWatchHistory
}
var TN struct {
VideoWatchHistory string
}
func init() {
FN.VideoWatchHistory.Id = "_id"
FN.VideoWatchHistory.UserId = "userId"
FN.VideoWatchHistory.VideoId = "videoId"
FN.VideoWatchHistory.CreatedAt = "createdAt"
FN.VideoWatchHistory.UpdatedAt = "updatedAt"
TN.VideoWatchHistory = "video_watch_history"
}
+17
View File
@@ -0,0 +1,17 @@
package videowatchhistorymod
import "time"
// VideoWatchHistory @table: video_watch_history
//
// VideoWatchHistory 用户视频观看历史模型
type VideoWatchHistory struct {
Id int64 `json:"id" bson:"_id,omitempty"` // 观看历史记录ID
UserId int64 `json:"userId" bson:"userId"` // 用户ID
VideoId int64 `json:"videoId" bson:"videoId"` // 视频ID
CreatedAt time.Time `json:"createdAt" bson:"createdAt"` // 创建时间
UpdatedAt time.Time `json:"updatedAt" bson:"updatedAt"` // 修改时间
}
// VideoWatchHistoryList 用户视频观看历史列表
type VideoWatchHistoryList []*VideoWatchHistory