@@ -0,0 +1,56 @@
|
||||
package active2023mod
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"91porn-server/common/db"
|
||||
"91porn-server/models"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
)
|
||||
|
||||
const active2023LotteryFreeInc = models.Active2023FreeLotteryInc // 用户免费次数新增表
|
||||
|
||||
func initFreeIndex() {
|
||||
coll := active2023LotteryFreeIncColl(nil)
|
||||
many := []mongo.IndexModel{ //batch set indexes //value is the type 1 or -1
|
||||
{
|
||||
Keys: bson.D{{Key: "uid", Value: 1}},
|
||||
},
|
||||
}
|
||||
if _, err := coll.CreateIndex(many); err != nil {
|
||||
panic(fmt.Sprintf("%s model set index err ==>[%+v]", active2023PrizeTable, err))
|
||||
}
|
||||
}
|
||||
|
||||
func active2023LotteryFreeIncColl(t *db.MongoTool) *db.MongoTool {
|
||||
if t == nil {
|
||||
return mdb.Coll(active2023LotteryFreeInc)
|
||||
}
|
||||
return t.Coll(active2023LotteryFreeInc)
|
||||
}
|
||||
|
||||
// 添加免费次数
|
||||
func AddLotteryCountFree(t *db.MongoTool, uid int64, fi FreeLotteryInc) error {
|
||||
f := bson.M{"uid": uid}
|
||||
now := time.Now()
|
||||
update := bson.M{"$inc": bson.M{"lotteryRemain": fi.Count}, "$set": bson.M{"updatedAt": now}}
|
||||
result, err := active2023UserColl(t).UpdateOne(f, update)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if result.ModifiedCount != 1 {
|
||||
if _, err := active2023UserColl(t).InsertOne(&UserActive2023{
|
||||
UID: uid,
|
||||
LotteryRemain: fi.Count,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
_, err = active2023LotteryFreeIncColl(t).InsertOne(fi)
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package active2023mod
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"91porn-server/common/db"
|
||||
"91porn-server/models"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
)
|
||||
|
||||
const active2023LotteryTable = models.Active2023Lottery // 用户抽奖详情表
|
||||
|
||||
func initLotteryIndex() {
|
||||
coll := active2023LotteryColl(nil)
|
||||
many := []mongo.IndexModel{ //batch set indexes //value is the type 1 or -1
|
||||
{
|
||||
Keys: bson.D{{Key: "uid", Value: 1}},
|
||||
},
|
||||
}
|
||||
if _, err := coll.CreateIndex(many); err != nil {
|
||||
panic(fmt.Sprintf("%s model set index err ==>[%+v]", active2023LotteryTable, err))
|
||||
}
|
||||
}
|
||||
|
||||
func active2023LotteryColl(t *db.MongoTool) *db.MongoTool {
|
||||
if t == nil {
|
||||
return mdb.Coll(active2023LotteryTable)
|
||||
}
|
||||
return t.Coll(active2023LotteryTable)
|
||||
}
|
||||
|
||||
func InsertUserLottery(t *db.MongoTool, ul *UserLottery) error {
|
||||
if ul == nil {
|
||||
return nil
|
||||
}
|
||||
_, err := active2023LotteryColl(t).InsertOne(ul)
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package active2023mod
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"91porn-server/common/db"
|
||||
"91porn-server/models"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
)
|
||||
|
||||
const active2023PrizeTable = models.Active2023Prize // 用户中奖奖品详情表
|
||||
|
||||
func initPrizeIndex() {
|
||||
coll := active2023PrizeColl(nil)
|
||||
many := []mongo.IndexModel{ //batch set indexes //value is the type 1 or -1
|
||||
{
|
||||
Keys: bson.D{{Key: "uid", Value: 1}},
|
||||
},
|
||||
}
|
||||
if _, err := coll.CreateIndex(many); err != nil {
|
||||
panic(fmt.Sprintf("%s model set index err ==>[%+v]", active2023PrizeTable, err))
|
||||
}
|
||||
}
|
||||
|
||||
func active2023PrizeColl(t *db.MongoTool) *db.MongoTool {
|
||||
if t == nil {
|
||||
return mdb.Coll(active2023PrizeTable)
|
||||
}
|
||||
return t.Coll(active2023PrizeTable)
|
||||
}
|
||||
|
||||
func InsertUserPrizes(t *db.MongoTool, ps *[]Prize) ([]primitive.ObjectID, error) {
|
||||
if ps == nil || len(*ps) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
result, err := active2023PrizeColl(t).InsertMany(ps)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
insertIDsLen := len(result.InsertedIDs)
|
||||
if insertIDsLen == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
ids := make([]primitive.ObjectID, insertIDsLen)
|
||||
for i := 0; i < insertIDsLen; i++ {
|
||||
ids[i], _ = result.InsertedIDs[i].(primitive.ObjectID)
|
||||
}
|
||||
return ids, nil
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package active2023mod
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
)
|
||||
|
||||
const (
|
||||
PrizeTypeGold = iota + 1 // 金币
|
||||
PrizeTypeGameGold // 游戏币
|
||||
PrizeTypeProduct // 现有产品. 目前仅支持会员卡和视频折扣卡
|
||||
PrizeTypeVIPDate // 会员天数
|
||||
PrizeTypeGoldCoinBonus // 金币加赠券
|
||||
PrizeTypeGoldWatch // 金币观影券
|
||||
PrizeTypeInKind // 实物奖励
|
||||
)
|
||||
|
||||
// 用户抽奖信息
|
||||
type UserActive2023 struct {
|
||||
ID primitive.ObjectID `bson:"_id,omitempty" json:"id"`
|
||||
UID int64 `bson:"uid" json:"uid"`
|
||||
LotteryRemain int64 `bson:"lotteryRemain" json:"lotteryRemain"` // 剩余免费抽奖次数
|
||||
LotteryFree int64 `bson:"lotteryFree" json:"lotteryFree"` // 已经免费抽奖次数
|
||||
LotteryRecharge int64 `bson:"lotteryRecharge" json:"lotteryRecharge"` // 已经付费抽奖次数
|
||||
CreatedAt time.Time `bson:"createdAt" json:"createdAt"` // 创建时间
|
||||
UpdatedAt time.Time `bson:"updatedAt" json:"updatedAt"` // 更新时间
|
||||
}
|
||||
|
||||
// 用户抽奖详情
|
||||
type UserLottery struct {
|
||||
ID primitive.ObjectID `bson:"_id,omitempty" json:"id"`
|
||||
UID int64 `bson:"uid" json:"uid"`
|
||||
Count int64 `bson:"count" json:"count"` // 抽奖次数
|
||||
UseGold int64 `bson:"useGold" json:"useGold"` // 抽奖消耗金币
|
||||
Prizes []primitive.ObjectID `bson:"prizes" json:"prizes"` // 获得奖品
|
||||
Desc string `bson:"desc" json:"desc"` // 说明
|
||||
CreatedAt time.Time `bson:"createdAt" json:"createdAt"` // 创建时间
|
||||
UpdatedAt time.Time `bson:"updatedAt" json:"updatedAt"` // 更新时间
|
||||
}
|
||||
|
||||
// 中奖详情
|
||||
type Prize struct {
|
||||
UID int64 `bson:"uid" json:"uid"`
|
||||
Type int64 `bson:"type" json:"type"` // 奖品类型
|
||||
Name string `bson:"name" json:"name"` // 奖品名字
|
||||
Unit int64 `bson:"unit" json:"unit"` // 单个奖品包含奖品数量, 配合类型. 金币: 金币数量; 金币加购券: 加购券金币面值; 观影券: 观影券抵扣面值; 会员礼包: 会员延续天数
|
||||
ProductID *primitive.ObjectID `bson:"productID,omitempty" json:"productID,omitempty"` // 获得奖品的产品id
|
||||
Amount int64 `bson:"amount" json:"amount"` // 奖品数量
|
||||
CreatedAt time.Time `bson:"createdAt" json:"createdAt"` // 创建时间
|
||||
UpdatedAt time.Time `bson:"updatedAt" json:"updatedAt"` // 更新时间
|
||||
}
|
||||
|
||||
// 免费抽奖次数增加详情
|
||||
type FreeLotteryInc struct {
|
||||
UID int64 `bson:"uid" json:"uid"`
|
||||
Count int64 `bson:"count" json:"count"`
|
||||
Desc string `bson:"desc" json:"desc"` // 说明
|
||||
OrderID *primitive.ObjectID `bson:"orderID,omitempty" json:"orderID"` // 对应的订单id
|
||||
CreatedAt time.Time `bson:"createdAt" json:"createdAt"` // 创建时间
|
||||
UpdatedAt time.Time `bson:"updatedAt" json:"updatedAt"` // 更新时间
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
package active2023mod
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"91porn-server/common/db"
|
||||
"91porn-server/models"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
var mdb *db.MongoDB
|
||||
|
||||
const active2023UserTable = models.Active2023User // 用户抽奖信息表
|
||||
|
||||
func active2023UserColl(t *db.MongoTool) *db.MongoTool {
|
||||
if t == nil {
|
||||
return mdb.Coll(active2023UserTable)
|
||||
}
|
||||
return t.Coll(active2023UserTable)
|
||||
}
|
||||
|
||||
func Init() {
|
||||
mdb = db.Init(active2023UserTable)
|
||||
initIndex()
|
||||
}
|
||||
|
||||
// ActInitIndex 索引设置
|
||||
func initIndex() {
|
||||
initUserIndex()
|
||||
initLotteryIndex()
|
||||
initPrizeIndex()
|
||||
initFreeIndex()
|
||||
}
|
||||
|
||||
func initUserIndex() {
|
||||
coll := active2023UserColl(nil)
|
||||
many := []mongo.IndexModel{ //batch set indexes //value is the type 1 or -1
|
||||
{
|
||||
Keys: bson.D{{Key: "uid", Value: 1}},
|
||||
Options: options.Index().SetUnique(true),
|
||||
},
|
||||
}
|
||||
if _, err := coll.CreateIndex(many); err != nil {
|
||||
panic(fmt.Sprintf("%s model set index err ==>[%+v]", active2023UserTable, err))
|
||||
}
|
||||
}
|
||||
|
||||
func GetActive2023UserInfoByID(t *db.MongoTool, uid int64) (*UserActive2023, error) {
|
||||
var ua UserActive2023
|
||||
if err := active2023UserColl(t).FindOne(&ua, bson.M{"uid": uid}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if ua.UID == 0 {
|
||||
now := time.Now()
|
||||
ua = UserActive2023{
|
||||
UID: uid,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
if _, err := active2023UserColl(t).InsertOne(&ua); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return &ua, nil
|
||||
}
|
||||
|
||||
// 使用免费次数
|
||||
func UseLotteryCountFree(t *db.MongoTool, uid int64, count int64) error {
|
||||
f := bson.M{"uid": uid, "lotteryRemain": bson.M{"$gte": count}}
|
||||
update := bson.M{"$inc": bson.M{"lotteryRemain": -int64(count), "lotteryFree": int64(count)}, "$set": bson.M{"updatedAt": time.Now()}}
|
||||
result, err := active2023UserColl(t).UpdateOne(f, update)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if result.ModifiedCount != 1 {
|
||||
return errors.New("免费抽奖次数不足")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// 付费抽奖
|
||||
func UserLotteryCountPay(t *db.MongoTool, uid int64, count int64) error {
|
||||
now := time.Now()
|
||||
f := bson.M{"uid": uid}
|
||||
update := bson.M{"$inc": bson.M{"lotteryRecharge": int64(count)}, "$set": bson.M{"updatedAt": time.Now()}}
|
||||
result, err := active2023UserColl(t).UpdateOne(f, update)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if result.ModifiedCount != 1 {
|
||||
if _, err := active2023UserColl(t).InsertOne(&UserActive2023{
|
||||
UID: uid,
|
||||
LotteryRecharge: int64(count),
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user