Initial commit: web_hghl_server
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user