init: web_hgcg_server 初始化提交

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
2026-10-27 16:44:55 +08:00
co-authored by Claude Opus 4.7
commit 189556e702
627 changed files with 126228 additions and 0 deletions
+136
View File
@@ -0,0 +1,136 @@
package homeconfigmod
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 初始化 home_config 集合和常用查询索引
func Init(ctx context.Context) {
coll = mongos.Shop.Coll(TN.HomeConfig)
initIndex(ctx)
}
func initIndex(ctx context.Context) {
indexes := []mongo.IndexModel{
{
Keys: bson.D{
{Key: FN.HomeConfig.Type, Value: 1},
{Key: FN.HomeConfig.SortType, Value: 1},
{Key: FN.HomeConfig.Status, Value: 1},
{Key: FN.HomeConfig.Rank, Value: -1},
{Key: FN.HomeConfig.UpdatedAt, Value: -1},
{Key: FN.HomeConfig.Id, Value: -1},
},
},
{
Keys: bson.D{
{Key: FN.HomeConfig.Name, Value: 1},
},
},
{
Keys: bson.D{
{Key: FN.HomeConfig.Slug, Value: 1},
},
},
{
Keys: bson.D{
{Key: FN.HomeConfig.CreatedAt, Value: -1},
{Key: FN.HomeConfig.Id, Value: -1},
},
},
{
Keys: bson.D{
{Key: FN.HomeConfig.Status, Value: 1},
{Key: FN.HomeConfig.Rank, Value: -1},
{Key: FN.HomeConfig.Id, Value: -1},
},
},
}
if _, err := coll.CreateIndex(ctx, indexes); err != nil {
panic(fmt.Sprintf("(homeconfigmod:initIndex) Create Index failed! err:%+v:", err))
}
}
// InsertOneHomeConfig 插入一条首页配置数据
func InsertOneHomeConfig(ctx context.Context, doc *HomeConfig) *srvdef.SrvErr {
if doc.Id == 0 {
doc.Id = mongos.Lid.HomeConfig.Get()
}
now := time.Now()
if doc.CreatedAt.IsZero() {
doc.CreatedAt = now
}
doc.UpdatedAt = now
if _, err := coll.InsertOne(ctx, doc); err != nil {
log.Warn("homeconfigmod:InsertOneHomeConfig failed", log.Any("doc", doc), log.E(err))
return srvdef.New(srvdef.ErrDbInsertError, "", "")
}
return nil
}
// DelOneHomeConfig 删除一条首页配置数据
func DelOneHomeConfig(ctx context.Context, filter bson.M) (int64, *srvdef.SrvErr) {
count, err := coll.DeleteOne(ctx, filter)
if err != nil {
log.Warn("homeconfigmod:DelOneHomeConfig failed", log.E(err), log.Any("filter", filter))
return 0, srvdef.New(srvdef.ErrDbDeleteError, "", "")
}
return count, nil
}
// FindManyHomeConfig 按条件查询多条首页配置数据
func FindManyHomeConfig(ctx context.Context, filter bson.M, opts ...*options.FindOptions) (HomeConfigList, *srvdef.SrvErr) {
data := make(HomeConfigList, 0)
if err := coll.Find(ctx, &data, filter, opts...); err != nil {
log.Warn("homeconfigmod:FindManyHomeConfig failed", log.E(err), log.Any("filter", filter))
return nil, srvdef.New(srvdef.ErrDbQueryError, "", "")
}
return data, nil
}
// CountHomeConfig 统计满足条件的首页配置数量
func CountHomeConfig(ctx context.Context, filter bson.M, opts ...*options.CountOptions) (int64, *srvdef.SrvErr) {
count, err := coll.Count(ctx, filter, opts...)
if err != nil {
log.Warn("homeconfigmod:CountHomeConfig failed", log.E(err), log.Any("filter", filter))
return 0, srvdef.New(srvdef.ErrDbQueryError, "", "")
}
return count, nil
}
// FindOneHomeConfig 查询一条首页配置
func FindOneHomeConfig(ctx context.Context, filter bson.M, opts ...*options.FindOneOptions) (*HomeConfig, *srvdef.SrvErr) {
var doc HomeConfig
if err := coll.FindOneEx(ctx, &doc, filter, opts...); err != nil {
if err != mongo.ErrNoDocuments {
log.Warn("homeconfigmod:FindOneHomeConfig failed", log.E(err), log.Any("filter", filter))
return nil, srvdef.New(srvdef.ErrDbQueryError, "", "")
}
return nil, srvdef.New(srvdef.ErrNoData, "", "")
}
return &doc, nil
}
// UpdateOneHomeConfig 更新一条首页配置数据
func UpdateOneHomeConfig(ctx context.Context, filter bson.M, update bson.M) *srvdef.SrvErr {
result, err := coll.UpdateOne(ctx, filter, update)
if err != nil {
log.Warn("homeconfigmod:UpdateOneHomeConfig failed", log.E(err), log.Any("filter", filter))
return srvdef.New(srvdef.ErrDbUpdateError, "", "")
}
if result.MatchedCount != 1 {
return srvdef.New(srvdef.ErrNoData, "", "")
}
return nil
}
+47
View File
@@ -0,0 +1,47 @@
package homeconfigmod
// TagHomeConfig 保存 home_config 集合字段名
type TagHomeConfig struct {
Id string
Name string
Slug string
MoreCategoryId string
Desc string
Type string
Rank string
SortType string
Status string
SeoInfo struct {
Description string
Keyworld string
Title string
}
CreatedAt string
UpdatedAt string
}
var FN struct {
HomeConfig TagHomeConfig
}
var TN struct {
HomeConfig string
}
func init() {
FN.HomeConfig.Id = "_id"
FN.HomeConfig.Name = "name"
FN.HomeConfig.Slug = "slug"
FN.HomeConfig.MoreCategoryId = "moreCategoryId"
FN.HomeConfig.Desc = "desc"
FN.HomeConfig.Type = "type"
FN.HomeConfig.Rank = "rank"
FN.HomeConfig.SortType = "sortType"
FN.HomeConfig.Status = "status"
FN.HomeConfig.SeoInfo.Description = "seoInfo.webDescription"
FN.HomeConfig.SeoInfo.Keyworld = "seoInfo.webKeyworld"
FN.HomeConfig.SeoInfo.Title = "seoInfo.webTitle"
FN.HomeConfig.CreatedAt = "createdAt"
FN.HomeConfig.UpdatedAt = "updatedAt"
TN.HomeConfig = "home_config"
}
+40
View File
@@ -0,0 +1,40 @@
package homeconfigmod
import (
"time"
"videoweb/pub/pubdef"
)
const (
TypePlaylet int32 = 1 // 短剧
TypeTheme int32 = 3 // 题材
)
const (
SortTypeRecommend int32 = 1 // 推荐
SortTypeHot int32 = 2 // 热播
SortTypeLatest int32 = 3 // 最近更新
SortTypePreview int32 = 4 // 新片预告
SortTypeCategory int32 = 5 // 分类
)
// HomeConfig @table: home_config
//
// HomeConfig 首页配置集合模型
type HomeConfig struct {
Id int64 `json:"id" bson:"_id,omitempty"` // ID
Name string `json:"name" bson:"name"` // 名称
Slug string `json:"slug" bson:"slug"` // slug,H5 更多页路径优先用它
MoreCategoryId int64 `json:"moreCategoryId" bson:"moreCategoryId,omitempty"` // “更多”跳转导航分类ID,0表示不展示
Desc string `json:"desc" bson:"desc"` // 描述
Type int32 `json:"type" bson:"type"` // 类型 1-短剧 3-题材
Rank int64 `json:"rank" bson:"rank"` // 排序,越大越靠前
SortType int32 `json:"sortType" bson:"sortType"` // 排序字段 1-推荐 2-热播 3-最近更新 4-最新上架 5-分类
Status bool `json:"status" bson:"status"` // 上下架状态 true-上架 false-下架
SeoInfo pubdef.SeoValue `json:"seoInfo" bson:"seoInfo"` // Seo相关信息
CreatedAt time.Time `json:"createdAt" bson:"createdAt"` // 创建时间
UpdatedAt time.Time `json:"updatedAt" bson:"updatedAt"` // 修改时间
}
// HomeConfigList 首页配置列表
type HomeConfigList []*HomeConfig