Files
rootandClaude Opus 5.5 7974a42301 Initial commit
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-23 11:07:42 +08:00

108 lines
3.4 KiB
Go

package playletchapterunlockmod
import (
"context"
"fmt"
"videoweb/pub/common/log"
"videoweb/pub/common/srvdef"
"videoweb/pub/common/utils"
"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_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},
},
},
}
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
}