@@ -0,0 +1,156 @@
|
||||
package rchgamegoldmod
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"91porn-server/app/appg"
|
||||
"91porn-server/common/constant/redisconst"
|
||||
"91porn-server/common/db"
|
||||
"91porn-server/common/log"
|
||||
"91porn-server/common/redis"
|
||||
"91porn-server/models"
|
||||
"91porn-server/web/webg"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
const table = models.GameGold
|
||||
|
||||
func initIndex() {
|
||||
coll := coll(nil)
|
||||
many := []mongo.IndexModel{
|
||||
{
|
||||
Keys: bson.D{{Key: "coins", Value: 1}, {Key: "price", Value: 1}},
|
||||
Options: options.Index().SetUnique(true),
|
||||
},
|
||||
{
|
||||
Keys: bson.D{{Key: "active", Value: 1}, {Key: "coins", Value: -1}},
|
||||
},
|
||||
}
|
||||
if _, err := coll.CreateIndex(many); err != nil {
|
||||
panic(fmt.Sprintf("%s model set index err ==>[%+v]", table, err))
|
||||
}
|
||||
}
|
||||
|
||||
var trueActiveList = "gameGoldTrueActiveList"
|
||||
|
||||
func coll(t *db.MongoTool) *db.MongoTool {
|
||||
if t == nil {
|
||||
return mdb.Coll(table)
|
||||
}
|
||||
return t.Coll(table)
|
||||
}
|
||||
|
||||
// InsertOne 插入一条数据
|
||||
func InsertGold(g *Gold) error {
|
||||
defer redisCachDel(trueActiveList)
|
||||
if _, err := coll(nil).InsertOne(g); err != nil {
|
||||
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "InsertGold", table, "InsertOne", err), log.Any("g", g))
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Delete 删除数据
|
||||
func DeleteGold(cond bson.M) (int64, error) {
|
||||
defer redisCachDel(trueActiveList)
|
||||
result, err := coll(nil).DeleteMany(cond)
|
||||
if err != nil {
|
||||
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "DeleteGold", table, "DeleteMany", err), log.Any("cond", cond))
|
||||
return 0, err
|
||||
}
|
||||
return result.DeletedCount, err
|
||||
}
|
||||
|
||||
// Update 金币配置更新
|
||||
func UpdateGold(cond bson.M, updt bson.M) (int64, error) {
|
||||
defer redisCachDel(trueActiveList)
|
||||
result, err := coll(nil).UpdateMany(cond, bson.M{"$set": updt})
|
||||
if err != nil {
|
||||
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "UpdateGold", table, "UpdateMany", err),
|
||||
log.Any("cond", cond),
|
||||
log.Any("updt", updt),
|
||||
)
|
||||
return 0, err
|
||||
}
|
||||
return result.ModifiedCount, nil
|
||||
}
|
||||
|
||||
// GetGoldListByActiveTrue 条件获取金币配置表
|
||||
func GetGoldByID(id primitive.ObjectID) (*Gold, error) {
|
||||
var g Gold
|
||||
if err := coll(nil).FindOneByID(&g, id); err != nil {
|
||||
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetGoldByID", table, "FindOneByID", err), log.Any("id", id))
|
||||
return nil, err
|
||||
}
|
||||
return &g, nil
|
||||
}
|
||||
|
||||
// GetGoldListByActiveTrue 条件获取金币配置表
|
||||
func GetGoldListByActiveTrue() ([]*Gold, error) {
|
||||
var back []*Gold
|
||||
redisKey := redisconst.DataCachKey(table, trueActiveList)
|
||||
redisc := getRedis()
|
||||
if redisc == nil || !redisc.Exists(redisKey) {
|
||||
opts := options.FindOptions{}
|
||||
opts.SetSort(bson.D{{Key: "coins", Value: 1}})
|
||||
if err := coll(nil).Find(&back, bson.M{"active": true}, &opts); err != nil {
|
||||
log.ZapLog.Warn("recharge GetGoldListByActiveTrue Find fail", log.E(err))
|
||||
return nil, err
|
||||
}
|
||||
if redisc == nil {
|
||||
return back, nil
|
||||
}
|
||||
jsonBytes, err := json.Marshal(back)
|
||||
if err != nil {
|
||||
return back, err
|
||||
}
|
||||
_ = redisc.Set(redisKey, string(jsonBytes), redisconst.DataCachExpire)
|
||||
return back, err
|
||||
}
|
||||
str, err := redisc.Get(redisKey)
|
||||
if err != nil {
|
||||
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetGoldListByActiveTrue", table, "Find", err))
|
||||
return nil, err
|
||||
}
|
||||
if str == nil {
|
||||
return nil, errors.New("redis key is null")
|
||||
}
|
||||
if err = json.Unmarshal([]byte(*str), &back); err != nil {
|
||||
log.Error(fmt.Sprintf("解析数据异常[%v]", err))
|
||||
return nil, err
|
||||
}
|
||||
return back, nil
|
||||
}
|
||||
|
||||
// GetGoldList 条件获取金币配置表
|
||||
func GetGoldList() ([]*Gold, error) {
|
||||
opts := options.FindOptions{}
|
||||
opts.SetSort(bson.M{"coins": 1})
|
||||
var back []*Gold
|
||||
if err := coll(nil).Find(&back, bson.M{}, &opts); err != nil {
|
||||
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetGoldList", table, "Find", err))
|
||||
return nil, err
|
||||
}
|
||||
return back, nil
|
||||
}
|
||||
|
||||
func redisCachDel(m string) {
|
||||
redisKey := redisconst.DataCachKey(table, m)
|
||||
_, _ = getRedis().Del(redisKey)
|
||||
}
|
||||
|
||||
func getRedis() *redis.Client {
|
||||
if appg.Redis != nil {
|
||||
return appg.Redis
|
||||
}
|
||||
if webg.Redis != nil {
|
||||
return webg.Redis
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package rchgamegoldmod
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"91porn-server/common/db"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
)
|
||||
|
||||
var mdb *db.MongoDB
|
||||
|
||||
// Gold 金币配置表
|
||||
type Gold struct {
|
||||
ID primitive.ObjectID `json:"id" bson:"_id,omitempty"`
|
||||
Name string `json:"name" bson:"name"` //金币名称
|
||||
Coins int64 `json:"coins" bson:"coins"` //购买金币数
|
||||
Price int64 `json:"price" bson:"price"` //价格 单位分
|
||||
CouponDesc string `json:"couponDesc" bson:"couponDesc"` //优惠描述
|
||||
GiveVipDays int `json:"giveVipDays" bson:"giveVipDays"` //赠送vip天数
|
||||
LouFengUnlockTimes int `json:"louFengUnlockTimes" bson:"louFengUnlockTimes"` //赠送楼凤解锁次数
|
||||
GiveGameCoin int64 `json:"giveGameCoin" bson:"giveGameCoin"` //赠送游戏金币
|
||||
NotFirst bool `json:"notFirst" bson:"notFirst"` //非首充依然赠送
|
||||
Active bool `json:"active" bson:"active"` //是否激活
|
||||
CreatedAt time.Time `json:"createdAt" bson:"createdAt,omitempty"` //创建时间
|
||||
UpdatedAt time.Time `json:"updatedAt" bson:"updatedAt,omitempty"` //刷新时间
|
||||
}
|
||||
|
||||
type PayChannelRes struct {
|
||||
Type string `bson:"type" json:"type"` //充值方式
|
||||
TypeName string `form:"typeName" json:"typeName" bson:"typeName" binding:"required"` //类型名称:支付宝,微信,银联
|
||||
Channel string `form:"channel" json:"channel" bson:"channel"` //渠道类型 鲨鱼 金鱼
|
||||
IncrAmount int64 `json:"incrAmount,omitempty" bson:"incrAmount,omitempty"` //增加的优惠额度
|
||||
IncTax float64 `json:"incTax,omitempty" bson:"incTax,omitempty"` //按比率增加额外优惠额 0-1之间 ,如果 incrAmount 与 incrTax 同时存在 以 incrAmount 为准
|
||||
}
|
||||
|
||||
// GoldRes 充值金额列表
|
||||
type GoldRes struct {
|
||||
ID primitive.ObjectID `bson:"_id,omitempty" json:"id"` //表id
|
||||
Amount int64 `json:"amount" bson:"amount" binding:"required"` //货币数量
|
||||
Money int64 `json:"money" bson:"money"` //货币价格
|
||||
TypeName string `json:"typeName" bson:"typeName" binding:"required"` //类型名称
|
||||
RechargeType []PayChannelRes `json:"rchgType" bson:"rchgType"` //充值方式
|
||||
}
|
||||
|
||||
func Init() {
|
||||
mdb = db.Init(table)
|
||||
initIndex()
|
||||
}
|
||||
|
||||
const RedisKey = "rchgamegoldmod"
|
||||
@@ -0,0 +1,33 @@
|
||||
package rchgamegoldmod
|
||||
|
||||
import "time"
|
||||
|
||||
// DelReq 删除请求
|
||||
type DelReq struct {
|
||||
IDs []string `form:"ids" json:"ids"`
|
||||
}
|
||||
|
||||
// OpeResp 操作应答
|
||||
type OpeResp struct {
|
||||
Count int64 `json:"count"`
|
||||
}
|
||||
|
||||
// GoldReq 金币配置添加
|
||||
type GoldReq struct {
|
||||
Name string `form:"name" json:"name"` //金币名称
|
||||
Coins int64 `form:"coins" json:"coins"` //购买金币数
|
||||
Price int64 `form:"price" json:"price"` //价格
|
||||
CouponDesc string `form:"couponDesc" json:"couponDesc"` //优惠描述
|
||||
GiveVipDays int `form:"giveVipDays" json:"giveVipDays"` //赠送vip天数
|
||||
LouFengUnlockTimes int `form:"louFengUnlockTimes" json:"louFengUnlockTimes"` //赠送楼凤解锁次数
|
||||
GiveGameCoin int64 `form:"giveGameCoin" json:"giveGameCoin"` //赠送游戏金币
|
||||
NotFirst bool `form:"notFirst" json:"notFirst"` //非首充依然赠送
|
||||
Active bool `form:"active" json:"active"` //是否激活
|
||||
UpdatedAt time.Time `json:"updatedAt" bson:"updatedAt"`
|
||||
}
|
||||
|
||||
// EditGoldReq 金币编辑请求
|
||||
type EditGoldReq struct {
|
||||
ID string `form:"id" json:"id"`
|
||||
GoldReq
|
||||
}
|
||||
Reference in New Issue
Block a user