Initial commit: web_hghl_server

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
2026-10-26 16:28:14 +08:00
co-authored by Claude Opus 4.7
commit 65fca60622
653 changed files with 135901 additions and 0 deletions
+168
View File
@@ -0,0 +1,168 @@
package playlethomeconfigmod
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_home_config 集合和常用查询索引
func Init(ctx context.Context) {
coll = mongos.Shop.Coll(TN.PlayletHomeConfig)
backfillSlugs(ctx)
initIndex(ctx)
}
// backfillSlugs 为历史配置生成稳定路径标识
func backfillSlugs(ctx context.Context) {
var docs PlayletHomeConfigList
filter := bson.M{"$or": []bson.M{{FN.PlayletHomeConfig.Slug: bson.M{"$exists": false}}, {FN.PlayletHomeConfig.Slug: nil}, {FN.PlayletHomeConfig.Slug: ""}}}
if err := coll.Find(ctx, &docs, filter, options.Find().SetProjection(bson.M{FN.PlayletHomeConfig.Id: 1})); err != nil {
panic(fmt.Sprintf("(playlethomeconfigmod:backfillSlugs) Find failed! err:%+v", err))
}
for _, doc := range docs {
if doc == nil {
continue
}
if _, err := coll.UpdateOne(ctx, bson.M{FN.PlayletHomeConfig.Id: doc.Id, "$or": filter["$or"]}, bson.M{"$set": bson.M{FN.PlayletHomeConfig.Slug: fmt.Sprintf("home-config-%d", doc.Id)}}); err != nil {
panic(fmt.Sprintf("(playlethomeconfigmod:backfillSlugs) UpdateOne failed! id:%d err:%+v", doc.Id, err))
}
}
}
func initIndex(ctx context.Context) {
indexes := []mongo.IndexModel{
{
Keys: bson.D{
{Key: FN.PlayletHomeConfig.Enabled, Value: 1},
{Key: FN.PlayletHomeConfig.ModuleType, Value: 1},
{Key: FN.PlayletHomeConfig.Rank, Value: -1},
{Key: FN.PlayletHomeConfig.Id, Value: -1},
},
},
{
Keys: bson.D{
{Key: FN.PlayletHomeConfig.CreatedAt, Value: -1},
{Key: FN.PlayletHomeConfig.Id, Value: -1},
},
},
{
Keys: bson.D{{Key: FN.PlayletHomeConfig.Slug, Value: 1}},
Options: options.Index().SetUnique(true).SetName("slug_unique"),
},
}
if _, err := coll.CreateIndex(ctx, indexes); err != nil {
panic(fmt.Sprintf("(playlethomeconfigmod:initIndex) Create Index failed! err:%+v:", err))
}
}
// InsertOnePlayletHomeConfig 插入一条首页配置
func InsertOnePlayletHomeConfig(ctx context.Context, doc *PlayletHomeConfig) *srvdef.SrvErr {
if doc.Id == 0 {
doc.Id = mongos.Lid.PlayletHomeConfig.Get()
}
now := time.Now()
if doc.CreatedAt.IsZero() {
doc.CreatedAt = now
}
normalizePlayletHomeConfig(doc)
doc.UpdatedAt = now
if _, err := coll.InsertOne(ctx, doc); err != nil {
if mongo.IsDuplicateKeyError(err) {
return srvdef.New(srvdef.ErrParamError, "slug已存在", "")
}
log.Warn("插入首页配置失败", log.Any("doc", doc), log.E(err))
return srvdef.New(srvdef.ErrDbInsertError, "", "")
}
return nil
}
func normalizePlayletHomeConfig(doc *PlayletHomeConfig) {
if doc == nil {
return
}
if doc.ModuleType != ModuleTypeTopic {
doc.ModuleType = ModuleTypePlaylet
}
if doc.DataType != DataTypeVideo {
doc.DataType = DataTypeAll
}
if doc.Limit <= 0 {
doc.Limit = DefaultLimit
}
if doc.Limit > MaxLimit {
doc.Limit = MaxLimit
}
}
// DelManyPlayletHomeConfig 删除多条首页配置
func DelManyPlayletHomeConfig(ctx context.Context, filter bson.M) (int64, *srvdef.SrvErr) {
count, err := coll.DeleteMany(ctx, filter)
if err != nil {
log.Warn("删除首页配置失败", log.E(err), log.Any("filter", filter))
return 0, srvdef.New(srvdef.ErrDbDeleteError, "", "")
}
return count, nil
}
// FindOnePlayletHomeConfig 查询一条首页配置
func FindOnePlayletHomeConfig(ctx context.Context, filter bson.M, opts ...*options.FindOneOptions) (*PlayletHomeConfig, *srvdef.SrvErr) {
var doc PlayletHomeConfig
if err := coll.FindOneEx(ctx, &doc, filter, opts...); err != nil {
if err != mongo.ErrNoDocuments {
log.Warn("查询首页配置失败", log.E(err), log.Any("filter", filter))
return nil, srvdef.New(srvdef.ErrDbQueryError, "", "")
}
return nil, srvdef.New(srvdef.ErrNoData, "", "")
}
normalizePlayletHomeConfig(&doc)
return &doc, nil
}
// FindManyPlayletHomeConfig 按条件查询多条首页配置
func FindManyPlayletHomeConfig(ctx context.Context, filter bson.M, opts ...*options.FindOptions) (PlayletHomeConfigList, *srvdef.SrvErr) {
data := make(PlayletHomeConfigList, 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, "", "")
}
for _, item := range data {
normalizePlayletHomeConfig(item)
}
return data, nil
}
// CountPlayletHomeConfig 统计满足条件的首页配置数量
func CountPlayletHomeConfig(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
}
// UpdateOnePlayletHomeConfig 更新一条首页配置
func UpdateOnePlayletHomeConfig(ctx context.Context, filter bson.M, update bson.M) *srvdef.SrvErr {
result, err := coll.UpdateOne(ctx, filter, update)
if err != nil {
if mongo.IsDuplicateKeyError(err) {
return srvdef.New(srvdef.ErrParamError, "slug已存在", "")
}
log.Warn("更新首页配置失败", log.E(err), log.Any("filter", filter))
return srvdef.New(srvdef.ErrDbUpdateError, "", "")
}
if result.MatchedCount != 1 {
return srvdef.New(srvdef.ErrNoData, "", "")
}
return nil
}
+52
View File
@@ -0,0 +1,52 @@
package playlethomeconfigmod
// TagPlayletHomeConfig 保存 playlet_home_config 集合字段名
type TagPlayletHomeConfig struct {
Id string
Slug string
Title string
Desc string
Label string
Style string
CardBackgroundImage string
ModuleType string
DataType string
RecommendType string
CategoryId string
TopicIds string
Rank string
Enabled string
Limit string
CreatedAt string
UpdatedAt string
}
var FN struct {
PlayletHomeConfig TagPlayletHomeConfig
}
var TN struct {
PlayletHomeConfig string
}
func init() {
FN.PlayletHomeConfig.Id = "_id"
FN.PlayletHomeConfig.Slug = "slug"
FN.PlayletHomeConfig.Title = "title"
FN.PlayletHomeConfig.Desc = "desc"
FN.PlayletHomeConfig.Label = "label"
FN.PlayletHomeConfig.Style = "style"
FN.PlayletHomeConfig.CardBackgroundImage = "cardBackgroundImage"
FN.PlayletHomeConfig.ModuleType = "moduleType"
FN.PlayletHomeConfig.DataType = "dataType"
FN.PlayletHomeConfig.RecommendType = "recommendType"
FN.PlayletHomeConfig.CategoryId = "categoryId"
FN.PlayletHomeConfig.TopicIds = "topicIds"
FN.PlayletHomeConfig.Rank = "rank"
FN.PlayletHomeConfig.Enabled = "enabled"
FN.PlayletHomeConfig.Limit = "limit"
FN.PlayletHomeConfig.CreatedAt = "createdAt"
FN.PlayletHomeConfig.UpdatedAt = "updatedAt"
TN.PlayletHomeConfig = "playlet_home_config"
}
+60
View File
@@ -0,0 +1,60 @@
package playlethomeconfigmod
import "time"
const (
RecommendTypeUpdated int64 = 1 // 按更新
RecommendTypeLike int64 = 2 // 按点赞
RecommendTypeBrowse int64 = 3 // 按观看
RecommendTypeHeat int64 = 4 // 按热度
RecommendTypeCategory int64 = 5 // 按分类
DefaultLimit int64 = 30
MaxLimit int64 = 100
)
const (
DataTypeAll int64 = 1 // 全部上架短剧
DataTypeVideo int64 = 2 // 全部上架视频
)
const (
ModuleTypePlaylet int64 = 1 // 列表,具体为短剧或视频由 DataType 决定
ModuleTypeTopic int64 = 2 // 专题
)
const (
StyleA = "A"
StyleB = "B"
StyleC = "C"
StyleD = "D"
StyleE = "E"
StyleF = "F"
StyleG = "G"
StyleH = "H"
)
// PlayletHomeConfig @table: playlet_home_config
//
// PlayletHomeConfig 短剧首页配置表
type PlayletHomeConfig struct {
Id int64 `json:"id" bson:"_id,omitempty"`
Slug string `json:"slug" bson:"slug"` // 页面路径标识
Title string `json:"title" bson:"title"` // 模块标题
Desc string `json:"desc" bson:"desc"` // 模块描述
Label string `json:"label" bson:"label"` // 样式名称
Style string `json:"style" bson:"style"` // 样式 A-H
CardBackgroundImage string `json:"cardBackgroundImage" bson:"cardBackgroundImage"` // 卡片背景图
ModuleType int64 `json:"moduleType" bson:"moduleType"` // 模块类型 1列表 2专题,短剧或视频由 DataType 决定
DataType int64 `json:"dataType" bson:"dataType"` // 数据类型 1全部上架短剧 2全部上架视频
RecommendType int64 `json:"recommendType" bson:"recommendType"` // 推荐类型 1按更新 2按点赞 3按观看 4按热度 5按分类
CategoryId int64 `json:"categoryId" bson:"categoryId"` // 按分类推荐时使用的分类ID
TopicIds []int64 `json:"topicIds" bson:"topicIds"` // 专题模块关联的题材或视频分类ID
Rank int64 `json:"rank" bson:"rank"` // 排序,越大越靠前
Enabled bool `json:"enabled" bson:"enabled"` // 是否启用
Limit int64 `json:"limit" bson:"limit"` // 展示条数
CreatedAt time.Time `json:"createdAt" bson:"createdAt"` // 创建时间
UpdatedAt time.Time `json:"updatedAt" bson:"updatedAt"` // 修改时间
}
// PlayletHomeConfigList 首页配置列表
type PlayletHomeConfigList []*PlayletHomeConfig