init: import web_huangguo2 server project
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
package playletchapterunlockmod
|
||||
|
||||
// TagPlayletChapterUnlock 保存 playlet_chapter_unlock 集合字段名。
|
||||
type TagPlayletChapterUnlock struct {
|
||||
Id string
|
||||
UserId string
|
||||
PlayletId string
|
||||
ChapterId string
|
||||
Coins string
|
||||
OrderNo string
|
||||
CreatedAt string
|
||||
UpdatedAt string
|
||||
}
|
||||
|
||||
var FN struct {
|
||||
PlayletChapterUnlock TagPlayletChapterUnlock
|
||||
}
|
||||
|
||||
var TN struct {
|
||||
PlayletChapterUnlock string
|
||||
}
|
||||
|
||||
func init() {
|
||||
FN.PlayletChapterUnlock.Id = "_id"
|
||||
FN.PlayletChapterUnlock.UserId = "userId"
|
||||
FN.PlayletChapterUnlock.PlayletId = "playletId"
|
||||
FN.PlayletChapterUnlock.ChapterId = "chapterId"
|
||||
FN.PlayletChapterUnlock.Coins = "coins"
|
||||
FN.PlayletChapterUnlock.OrderNo = "orderNo"
|
||||
FN.PlayletChapterUnlock.CreatedAt = "createdAt"
|
||||
FN.PlayletChapterUnlock.UpdatedAt = "updatedAt"
|
||||
|
||||
TN.PlayletChapterUnlock = "playlet_chapter_unlock"
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package playletchapterunlockmod
|
||||
|
||||
import "time"
|
||||
|
||||
// PlayletChapterUnlock @table: playlet_chapter_unlock
|
||||
//
|
||||
// PlayletChapterUnlock 记录用户已用金币解锁的短剧剧集;一条记录代表该用户永久可观看该剧集。
|
||||
type PlayletChapterUnlock struct {
|
||||
Id int64 `json:"id" bson:"_id,omitempty"` // 解锁记录ID
|
||||
UserId int64 `json:"userId" bson:"userId"` // 用户ID
|
||||
PlayletId int64 `json:"playletId" bson:"playletId"` // 短剧ID
|
||||
ChapterId int64 `json:"chapterId" bson:"chapterId"` // 短剧剧集ID
|
||||
Coins int64 `json:"coins" bson:"coins"` // 解锁消耗金币
|
||||
OrderNo string `json:"orderNo" bson:"orderNo"` // 解锁流水号
|
||||
CreatedAt time.Time `json:"createdAt" bson:"createdAt"` // 创建时间
|
||||
UpdatedAt time.Time `json:"updatedAt" bson:"updatedAt"` // 修改时间
|
||||
}
|
||||
|
||||
type PlayletChapterUnlockList []*PlayletChapterUnlock
|
||||
@@ -0,0 +1,132 @@
|
||||
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},
|
||||
},
|
||||
},
|
||||
{
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user