Initial commit
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,331 @@
|
||||
package playletmod
|
||||
|
||||
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 集合和常用查询索引
|
||||
func Init(ctx context.Context) {
|
||||
coll = mongos.Shop.Coll(TN.Playlet)
|
||||
initIndex(ctx)
|
||||
}
|
||||
|
||||
func initIndex(ctx context.Context) {
|
||||
indexes := []mongo.IndexModel{
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: FN.Playlet.ThemeIds, Value: 1},
|
||||
{Key: FN.Playlet.Status, Value: 1},
|
||||
{Key: FN.Playlet.CreatedAt, Value: -1},
|
||||
},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: FN.Playlet.ThemeIds, Value: 1},
|
||||
{Key: FN.Playlet.Status, Value: 1},
|
||||
{Key: FN.Playlet.CountCollect, Value: -1},
|
||||
{Key: FN.Playlet.Id, Value: -1},
|
||||
},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: FN.Playlet.ThemeIds, Value: 1},
|
||||
{Key: FN.Playlet.Status, Value: 1},
|
||||
{Key: FN.Playlet.UpdatedAt, Value: -1},
|
||||
{Key: FN.Playlet.Id, Value: -1},
|
||||
},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: FN.Playlet.CategoryIds, Value: 1},
|
||||
{Key: FN.Playlet.Status, Value: 1},
|
||||
{Key: FN.Playlet.CreatedAt, Value: -1},
|
||||
},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: FN.Playlet.TagIds, Value: 1},
|
||||
{Key: FN.Playlet.Status, Value: 1},
|
||||
{Key: FN.Playlet.UpdatedAt, Value: -1},
|
||||
{Key: FN.Playlet.Id, Value: -1},
|
||||
},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: FN.Playlet.CategoryIds, Value: 1},
|
||||
{Key: FN.Playlet.Status, Value: 1},
|
||||
{Key: FN.Playlet.RecommendRank, Value: -1},
|
||||
{Key: FN.Playlet.CreatedAt, Value: -1},
|
||||
{Key: FN.Playlet.Id, Value: -1},
|
||||
},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: FN.Playlet.IsFeatured, Value: 1},
|
||||
{Key: FN.Playlet.Status, Value: 1},
|
||||
{Key: FN.Playlet.CreatedAt, Value: -1},
|
||||
},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: FN.Playlet.ShowOnBanner, Value: 1},
|
||||
{Key: FN.Playlet.Status, Value: 1},
|
||||
{Key: FN.Playlet.TopRank, Value: -1},
|
||||
{Key: FN.Playlet.CreatedAt, Value: -1},
|
||||
},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: FN.Playlet.Status, Value: 1},
|
||||
{Key: FN.Playlet.IsRecommend, Value: 1},
|
||||
{Key: FN.Playlet.RecommendRank, Value: -1},
|
||||
{Key: FN.Playlet.CreatedAt, Value: -1},
|
||||
},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: FN.Playlet.CreatedAt, Value: -1},
|
||||
{Key: FN.Playlet.Id, Value: -1},
|
||||
},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: FN.Playlet.Status, Value: 1},
|
||||
{Key: FN.Playlet.CountCollect, Value: -1},
|
||||
{Key: FN.Playlet.Id, Value: -1},
|
||||
},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: FN.Playlet.Status, Value: 1},
|
||||
{Key: FN.Playlet.CountBrowse, Value: -1},
|
||||
{Key: FN.Playlet.Id, Value: -1},
|
||||
},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: FN.Playlet.Status, Value: 1},
|
||||
{Key: FN.Playlet.HeatValue, Value: -1},
|
||||
{Key: FN.Playlet.Id, Value: -1},
|
||||
},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: FN.Playlet.Status, Value: 1},
|
||||
{Key: FN.Playlet.OnlineAt, Value: -1},
|
||||
{Key: FN.Playlet.Id, Value: -1},
|
||||
},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: FN.Playlet.Status, Value: 1},
|
||||
{Key: FN.Playlet.UpdatedAt, Value: -1},
|
||||
{Key: FN.Playlet.Id, Value: -1},
|
||||
},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: FN.Playlet.Slug, Value: 1},
|
||||
},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: FN.Playlet.MediaCenterId, Value: 1},
|
||||
},
|
||||
Options: options.Index().
|
||||
SetUnique(true).
|
||||
SetPartialFilterExpression(bson.M{
|
||||
FN.Playlet.MediaCenterId: bson.M{"$gt": 0},
|
||||
}),
|
||||
},
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: FN.Playlet.OperateType, Value: 1},
|
||||
{Key: FN.Playlet.Status, Value: 1},
|
||||
{Key: FN.Playlet.CreatedAt, Value: -1},
|
||||
},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: FN.Playlet.ContentType, Value: 1},
|
||||
{Key: FN.Playlet.CreatedAt, Value: -1},
|
||||
{Key: FN.Playlet.Id, Value: -1},
|
||||
},
|
||||
Options: options.Index().
|
||||
SetPartialFilterExpression(bson.M{
|
||||
FN.Playlet.MediaCenterId: bson.M{"$gt": 0},
|
||||
}),
|
||||
},
|
||||
}
|
||||
if _, err := coll.CreateIndex(ctx, indexes); err != nil {
|
||||
panic(fmt.Sprintf("(playletmod:initIndex) Create Index failed! err:%+v:", err))
|
||||
}
|
||||
}
|
||||
|
||||
// InsertOnePlaylet 插入一条短剧数据
|
||||
func InsertOnePlaylet(ctx context.Context, doc *Playlet) *srvdef.SrvErr {
|
||||
if doc.Id == 0 {
|
||||
doc.Id = mongos.Lid.Playlet.Get()
|
||||
}
|
||||
now := time.Now()
|
||||
if doc.CreatedAt.IsZero() {
|
||||
doc.CreatedAt = now
|
||||
}
|
||||
doc.UpdatedAt = now
|
||||
if _, err := coll.InsertOne(ctx, doc); err != nil {
|
||||
log.Warn("playletmod:InsertOnePlaylet failed", log.Any("doc", doc), log.E(err))
|
||||
return srvdef.New(srvdef.ErrDbInsertError, "", "")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DelOnePlaylet 删除一条短剧数据
|
||||
func DelOnePlaylet(ctx context.Context, filter bson.M) (int64, *srvdef.SrvErr) {
|
||||
count, err := coll.DeleteOne(ctx, filter)
|
||||
if err != nil {
|
||||
log.Warn("playletmod:DelOnePlaylet failed", log.E(err), log.Any("filter", filter))
|
||||
return 0, srvdef.New(srvdef.ErrDbDeleteError, "", "")
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
|
||||
// FindOnePlaylet 查询一条短剧数据
|
||||
func FindOnePlaylet(ctx context.Context, filter bson.M, opts ...*options.FindOneOptions) (*Playlet, *srvdef.SrvErr) {
|
||||
var doc Playlet
|
||||
if err := coll.FindOneEx(ctx, &doc, filter, opts...); err != nil {
|
||||
if err != mongo.ErrNoDocuments {
|
||||
log.Warn("playletmod:FindOnePlaylet failed", log.E(err), log.Any("filter", filter))
|
||||
return nil, srvdef.New(srvdef.ErrDbQueryError, "", "")
|
||||
}
|
||||
return nil, srvdef.New(srvdef.ErrNoData, "", "")
|
||||
}
|
||||
return &doc, nil
|
||||
}
|
||||
|
||||
// FindManyPlaylet 按条件查询多条短剧数据
|
||||
func FindManyPlaylet(ctx context.Context, filter bson.M, opts ...*options.FindOptions) (PlayletList, *srvdef.SrvErr) {
|
||||
data := make(PlayletList, 0)
|
||||
if err := coll.Find(ctx, &data, filter, opts...); err != nil {
|
||||
log.Warn("playletmod:FindManyPlaylet failed", log.E(err), log.Any("filter", filter))
|
||||
return nil, srvdef.New(srvdef.ErrDbQueryError, "", "")
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
|
||||
// CountPlaylet 统计满足条件的短剧数量
|
||||
func CountPlaylet(ctx context.Context, filter bson.M, opts ...*options.CountOptions) (int64, *srvdef.SrvErr) {
|
||||
count, err := coll.Count(ctx, filter, opts...)
|
||||
if err != nil {
|
||||
log.Warn("playletmod:CountPlaylet failed", log.E(err), log.Any("filter", filter))
|
||||
return 0, srvdef.New(srvdef.ErrDbQueryError, "", "")
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
|
||||
// UpdateOnePlaylet 更新一条短剧数据
|
||||
func UpdateOnePlaylet(ctx context.Context, filter bson.M, update bson.M) *srvdef.SrvErr {
|
||||
result, err := coll.UpdateOne(ctx, filter, update)
|
||||
if err != nil {
|
||||
log.Warn("playletmod:UpdateOnePlaylet failed", log.E(err), log.Any("filter", filter))
|
||||
return srvdef.New(srvdef.ErrDbUpdateError, "", "")
|
||||
}
|
||||
if result.MatchedCount != 1 {
|
||||
return srvdef.New(srvdef.ErrNoData, "", "")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// FindOneAndUpdatePlaylet 更新一条短剧数据并返回更新后的数据
|
||||
func FindOneAndUpdatePlaylet(ctx context.Context, filter bson.M, update bson.M, opts ...*options.FindOneAndUpdateOptions) (*Playlet, *srvdef.SrvErr) {
|
||||
var doc Playlet
|
||||
if err := coll.FindOneAndUpdate(ctx, &doc, filter, update, opts...); err != nil {
|
||||
if err != mongo.ErrNoDocuments {
|
||||
log.Warn("更新并查询短剧数据失败", log.E(err), log.Any("filter", filter))
|
||||
return nil, srvdef.New(srvdef.ErrDbUpdateError, "", "")
|
||||
}
|
||||
return nil, srvdef.New(srvdef.ErrNoData, "", "")
|
||||
}
|
||||
return &doc, nil
|
||||
}
|
||||
|
||||
// UpdateManyPlaylet 批量更新短剧数据
|
||||
func UpdateManyPlaylet(ctx context.Context, filter bson.M, update bson.M) *srvdef.SrvErr {
|
||||
result, err := coll.UpdateMany(ctx, filter, update)
|
||||
if err != nil {
|
||||
log.Warn("批量更新短剧数据失败", log.E(err), log.Any("filter", filter))
|
||||
return srvdef.New(srvdef.ErrDbUpdateError, "", "")
|
||||
}
|
||||
if result.MatchedCount == 0 {
|
||||
return srvdef.New(srvdef.ErrNoData, "", "")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// BulkWritePlaylet 批量写入短剧更新操作。
|
||||
func BulkWritePlaylet(ctx context.Context, docs []mongo.WriteModel, opts ...*options.BulkWriteOptions) *srvdef.SrvErr {
|
||||
if len(docs) == 0 {
|
||||
return nil
|
||||
}
|
||||
result, err := coll.BulkWrite(ctx, docs, opts...)
|
||||
if err != nil {
|
||||
log.Warn("批量写入短剧数据失败", log.E(err))
|
||||
return srvdef.New(srvdef.ErrDbUpdateError, "", "")
|
||||
}
|
||||
if result.MatchedCount == 0 {
|
||||
return srvdef.New(srvdef.ErrNoData, "", "")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// FindManyPlayletByCategoryRank 按指定分类的排序分页查询短剧。
|
||||
func FindManyPlayletByCategoryRank(ctx context.Context, filter bson.M, categoryId int64, skip, limit int64, sort bson.D, projection bson.D) (PlayletList, *srvdef.SrvErr) {
|
||||
if skip < 0 {
|
||||
skip = 0
|
||||
}
|
||||
if limit <= 0 {
|
||||
limit = 10
|
||||
}
|
||||
if sort == nil {
|
||||
sort = CategoryRankSort()
|
||||
}
|
||||
pipeline := []bson.M{
|
||||
{"$match": filter},
|
||||
{"$addFields": CategoryRankAddFields(categoryId)},
|
||||
{"$sort": sort},
|
||||
{"$skip": skip},
|
||||
{"$limit": limit},
|
||||
}
|
||||
pipeline = append(pipeline, bson.M{"$unset": categoryRankField})
|
||||
if len(projection) > 0 {
|
||||
proj := bson.M{}
|
||||
for _, field := range projection {
|
||||
proj[field.Key] = field.Value
|
||||
}
|
||||
pipeline = append(pipeline, bson.M{"$project": proj})
|
||||
}
|
||||
list := make(PlayletList, 0)
|
||||
if err := AggregatePlaylet(ctx, &list, pipeline); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return list, nil
|
||||
}
|
||||
|
||||
// AggregatePlaylet 聚合查询短剧
|
||||
func AggregatePlaylet(ctx context.Context, bind interface{}, pipeline []bson.M) *srvdef.SrvErr {
|
||||
if err := coll.Aggregate(ctx, bind, pipeline); err != nil {
|
||||
log.Warn("playletmod:AggregatePlaylet failed", log.E(err), log.Any("pipeline", pipeline))
|
||||
return srvdef.New(srvdef.ErrDbQueryError, "", "")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user