107 lines
2.5 KiB
Go
107 lines
2.5 KiB
Go
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
|
|
}
|