Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
116 lines
3.6 KiB
Go
116 lines
3.6 KiB
Go
package playletchaptercollectmod
|
|
|
|
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_chapter_collect 集合和常用查询索引
|
|
func Init(ctx context.Context) {
|
|
coll = mongos.Shop.Coll(TN.PlayletChapterCollect)
|
|
initIndex(ctx)
|
|
}
|
|
|
|
func initIndex(ctx context.Context) {
|
|
indexes := []mongo.IndexModel{
|
|
{
|
|
Keys: bson.D{
|
|
{Key: FN.PlayletChapterCollect.UserId, Value: 1},
|
|
{Key: FN.PlayletChapterCollect.ChapterId, Value: 1},
|
|
},
|
|
Options: options.Index().SetUnique(true),
|
|
},
|
|
{
|
|
Keys: bson.D{
|
|
{Key: FN.PlayletChapterCollect.ChapterId, Value: 1},
|
|
{Key: FN.PlayletChapterCollect.CreatedAt, Value: -1},
|
|
},
|
|
},
|
|
{
|
|
Keys: bson.D{
|
|
{Key: FN.PlayletChapterCollect.PlayletId, Value: 1},
|
|
{Key: FN.PlayletChapterCollect.CreatedAt, Value: -1},
|
|
},
|
|
},
|
|
{
|
|
Keys: bson.D{
|
|
{Key: FN.PlayletChapterCollect.UserId, Value: 1},
|
|
{Key: FN.PlayletChapterCollect.CreatedAt, Value: -1},
|
|
},
|
|
},
|
|
}
|
|
if _, err := coll.CreateIndex(ctx, indexes); err != nil {
|
|
panic(fmt.Sprintf("(playletchaptercollectmod:initIndex) 创建索引失败 err:%+v:", err))
|
|
}
|
|
}
|
|
|
|
// InsertOnePlayletChapterCollect 插入一条短剧剧集收藏关系
|
|
func InsertOnePlayletChapterCollect(ctx context.Context, doc *PlayletChapterCollect) (bool, *srvdef.SrvErr) {
|
|
if doc.Id == 0 {
|
|
doc.Id = mongos.Lid.PlayletChapterCollect.Get()
|
|
}
|
|
now := time.Now()
|
|
if doc.CreatedAt.IsZero() {
|
|
doc.CreatedAt = now
|
|
}
|
|
doc.UpdatedAt = now
|
|
if _, err := coll.InsertOne(ctx, doc); err != nil {
|
|
if mongo.IsDuplicateKeyError(err) {
|
|
return false, nil
|
|
}
|
|
log.Warn("插入短剧剧集收藏关系失败", log.Any("doc", doc), log.E(err))
|
|
return false, srvdef.New(srvdef.ErrDbInsertError, "", "")
|
|
}
|
|
return true, nil
|
|
}
|
|
|
|
// DeleteOnePlayletChapterCollect 删除一条短剧剧集收藏关系
|
|
func DeleteOnePlayletChapterCollect(ctx context.Context, filter bson.M) (int64, *srvdef.SrvErr) {
|
|
count, err := coll.DeleteOne(ctx, filter)
|
|
if err != nil {
|
|
log.Warn("删除短剧剧集收藏关系失败", log.E(err), log.Any("filter", filter))
|
|
return 0, srvdef.New(srvdef.ErrDbDeleteError, "", "")
|
|
}
|
|
return count, nil
|
|
}
|
|
|
|
// DeleteManyPlayletChapterCollect 按条件批量删除短剧剧集收藏关系
|
|
func DeleteManyPlayletChapterCollect(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
|
|
}
|
|
|
|
// FindManyPlayletChapterCollect 按条件查询短剧剧集收藏关系
|
|
func FindManyPlayletChapterCollect(ctx context.Context, filter bson.M, opts ...*options.FindOptions) (PlayletChapterCollectList, *srvdef.SrvErr) {
|
|
data := make(PlayletChapterCollectList, 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
|
|
}
|
|
|
|
// CountPlayletChapterCollect 统计满足条件的短剧剧集收藏关系数量
|
|
func CountPlayletChapterCollect(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
|
|
}
|