131 lines
4.0 KiB
Go
131 lines
4.0 KiB
Go
package playletcollectmod
|
|
|
|
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_collect 集合和常用查询索引
|
|
func Init(ctx context.Context) {
|
|
coll = mongos.Shop.Coll(TN.PlayletCollect)
|
|
initIndex(ctx)
|
|
}
|
|
|
|
func initIndex(ctx context.Context) {
|
|
indexes := []mongo.IndexModel{
|
|
{
|
|
Keys: bson.D{
|
|
{Key: FN.PlayletCollect.UserId, Value: 1},
|
|
{Key: FN.PlayletCollect.PlayletId, Value: 1},
|
|
},
|
|
Options: options.Index().SetUnique(true),
|
|
},
|
|
{
|
|
Keys: bson.D{
|
|
{Key: FN.PlayletCollect.PlayletId, Value: 1},
|
|
{Key: FN.PlayletCollect.CreatedAt, Value: -1},
|
|
},
|
|
},
|
|
{
|
|
Keys: bson.D{
|
|
{Key: FN.PlayletCollect.UserId, Value: 1},
|
|
{Key: FN.PlayletCollect.CreatedAt, Value: -1},
|
|
{Key: FN.PlayletCollect.Id, Value: -1},
|
|
},
|
|
},
|
|
{
|
|
Keys: bson.D{
|
|
{Key: FN.PlayletCollect.CreatedAt, Value: 1},
|
|
{Key: FN.PlayletCollect.PlayletId, Value: 1},
|
|
},
|
|
},
|
|
}
|
|
if _, err := coll.CreateIndex(ctx, indexes); err != nil {
|
|
panic(fmt.Sprintf("(playletcollectmod:initIndex) 创建索引失败 err:%+v:", err))
|
|
}
|
|
}
|
|
|
|
// InsertOnePlayletCollect 插入一条短剧收藏关系
|
|
func InsertOnePlayletCollect(ctx context.Context, doc *PlayletCollect) (bool, *srvdef.SrvErr) {
|
|
if doc.Id == 0 {
|
|
doc.Id = mongos.Lid.PlayletCollect.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
|
|
}
|
|
|
|
// ExistsPlayletCollect 查询短剧收藏关系是否存在
|
|
func ExistsPlayletCollect(ctx context.Context, filter bson.M) (bool, *srvdef.SrvErr) {
|
|
var doc PlayletCollect
|
|
if err := coll.FindOneEx(ctx, &doc, filter, options.FindOne().SetProjection(bson.D{
|
|
{Key: FN.PlayletCollect.Id, Value: 1},
|
|
})); err != nil {
|
|
if err == mongo.ErrNoDocuments {
|
|
return false, nil
|
|
}
|
|
log.Warn("查询短剧收藏关系失败", log.E(err), log.Any("filter", filter))
|
|
return false, srvdef.New(srvdef.ErrDbQueryError, "", "")
|
|
}
|
|
return true, nil
|
|
}
|
|
|
|
// DeleteOnePlayletCollect 删除一条短剧收藏关系
|
|
func DeleteOnePlayletCollect(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
|
|
}
|
|
|
|
// FindManyPlayletCollect 按条件查询短剧收藏关系列表
|
|
func FindManyPlayletCollect(ctx context.Context, filter bson.M, opts ...*options.FindOptions) (PlayletCollectList, *srvdef.SrvErr) {
|
|
data := make(PlayletCollectList, 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
|
|
}
|
|
|
|
// CountPlayletCollect 统计满足条件的短剧收藏关系数量
|
|
func CountPlayletCollect(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
|
|
}
|
|
|
|
// AggregatePlayletCollect 聚合查询短剧收藏关系
|
|
func AggregatePlayletCollect(ctx context.Context, bind interface{}, pipeline []bson.M) *srvdef.SrvErr {
|
|
if err := coll.Aggregate(ctx, bind, pipeline); err != nil {
|
|
log.Warn("聚合短剧收藏关系失败", log.E(err), log.Any("pipeline", pipeline))
|
|
return srvdef.New(srvdef.ErrDbQueryError, "", "")
|
|
}
|
|
return nil
|
|
}
|