来源:hgdjnet-server 9bfbafe10d417deea959bd53117bc88a10e60a83 项目身份 HKDJ / JHA-216,独立官网仓库,Jenkins 守卫键 hkdjnet 测试目录 /server/hkdj_net,App/Web API 30331/30332 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
133 lines
4.4 KiB
Go
133 lines
4.4 KiB
Go
package playletchapterunlockmod
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"hkdj-web-server/pub/common/log"
|
|
"hkdj-web-server/pub/common/srvdef"
|
|
"hkdj-web-server/pub/common/utils"
|
|
"hkdj-web-server/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_unlock 集合和索引。
|
|
func Init(ctx context.Context) {
|
|
coll = mongos.Shop.Coll(TN.PlayletChapterUnlock)
|
|
initIndex(ctx)
|
|
}
|
|
|
|
func initIndex(ctx context.Context) {
|
|
indexes := []mongo.IndexModel{
|
|
{
|
|
Keys: bson.D{
|
|
{Key: FN.PlayletChapterUnlock.UserId, Value: 1},
|
|
{Key: FN.PlayletChapterUnlock.ChapterId, Value: 1},
|
|
},
|
|
Options: options.Index().SetUnique(true),
|
|
},
|
|
{
|
|
Keys: bson.D{
|
|
{Key: FN.PlayletChapterUnlock.UserId, Value: 1},
|
|
{Key: FN.PlayletChapterUnlock.PlayletId, Value: 1},
|
|
},
|
|
},
|
|
{
|
|
Keys: bson.D{
|
|
{Key: FN.PlayletChapterUnlock.CreatedAt, Value: 1},
|
|
{Key: FN.PlayletChapterUnlock.PlayletId, Value: 1},
|
|
},
|
|
},
|
|
}
|
|
if _, err := coll.CreateIndex(ctx, indexes); err != nil {
|
|
panic(fmt.Sprintf("(playletchapterunlockmod:initIndex) 创建索引失败 err:%+v:", err))
|
|
}
|
|
}
|
|
|
|
// InsertOnePlayletChapterUnlock 插入解锁记录。
|
|
//
|
|
// 返回 inserted=false 表示该用户已经解锁过该剧集;调用方应将其视为幂等成功,不可继续扣费。
|
|
func InsertOnePlayletChapterUnlock(ctx context.Context, doc *PlayletChapterUnlock) (bool, *srvdef.SrvErr) {
|
|
if doc.Id == 0 {
|
|
doc.Id = mongos.Lid.PlayletChapterUnlock.Get()
|
|
}
|
|
now := utils.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
|
|
}
|
|
|
|
// ExistsPlayletChapterUnlock 判断用户是否已解锁指定剧集。
|
|
func ExistsPlayletChapterUnlock(ctx context.Context, userId int64, chapterId int64) (bool, *srvdef.SrvErr) {
|
|
if userId <= 0 || chapterId <= 0 {
|
|
return false, nil
|
|
}
|
|
var doc PlayletChapterUnlock
|
|
err := coll.FindOneEx(ctx, &doc, bson.M{
|
|
FN.PlayletChapterUnlock.UserId: userId,
|
|
FN.PlayletChapterUnlock.ChapterId: chapterId,
|
|
}, options.FindOne().SetProjection(bson.D{
|
|
{Key: FN.PlayletChapterUnlock.Id, Value: 1},
|
|
}))
|
|
if err != nil {
|
|
if err == mongo.ErrNoDocuments {
|
|
return false, nil
|
|
}
|
|
log.Warn("查询短剧剧集解锁记录失败", log.E(err), log.Any("userId", userId), log.Any("chapterId", chapterId))
|
|
return false, srvdef.New(srvdef.ErrDbQueryError, "", "")
|
|
}
|
|
return doc.Id > 0, nil
|
|
}
|
|
|
|
// DeleteManyPlayletChapterUnlock 按条件批量删除剧集解锁记录。
|
|
func DeleteManyPlayletChapterUnlock(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
|
|
}
|
|
|
|
// FindManyPlayletChapterUnlock 按条件查询多条剧集解锁记录。
|
|
func FindManyPlayletChapterUnlock(ctx context.Context, filter bson.M, opts ...*options.FindOptions) (PlayletChapterUnlockList, *srvdef.SrvErr) {
|
|
data := make(PlayletChapterUnlockList, 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
|
|
}
|
|
|
|
// CountPlayletChapterUnlock 统计满足条件的剧集解锁记录数量
|
|
func CountPlayletChapterUnlock(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
|
|
}
|
|
|
|
// AggregatePlayletChapterUnlock 聚合查询剧集解锁记录
|
|
func AggregatePlayletChapterUnlock(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
|
|
}
|