@@ -0,0 +1,15 @@
|
||||
package currencymod
|
||||
|
||||
import (
|
||||
"91porn-server/common/db"
|
||||
"91porn-server/models"
|
||||
)
|
||||
|
||||
const currencyTable = models.Currency
|
||||
|
||||
var currencyDB *db.MongoDB
|
||||
|
||||
func Init() {
|
||||
currencyDB = db.Init(currencyTable)
|
||||
currencyIndex()
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
package currencymod
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"91porn-server/common/constant/redisconst"
|
||||
"91porn-server/common/db"
|
||||
"91porn-server/common/log"
|
||||
"91porn-server/common/redis"
|
||||
"91porn-server/models/commod"
|
||||
|
||||
"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"
|
||||
)
|
||||
|
||||
func currencyColl(t *db.MongoTool) *db.MongoTool {
|
||||
if t == nil {
|
||||
return currencyDB.Coll(currencyTable)
|
||||
}
|
||||
return t.Coll(currencyTable)
|
||||
}
|
||||
|
||||
func currencyIndex() {
|
||||
many := []mongo.IndexModel{
|
||||
{
|
||||
Keys: bson.D{{Key: "type", Value: 1}, {Key: "isActive", Value: 1}, {Key: "coins", Value: -1}},
|
||||
},
|
||||
}
|
||||
if _, err := currencyColl(nil).CreateIndex(many); err != nil {
|
||||
panic(fmt.Sprintf("currency model set index err ==>[%+v]", err))
|
||||
}
|
||||
}
|
||||
|
||||
// Get 获取单个货币配置
|
||||
func Get(id primitive.ObjectID) (Currency, error) {
|
||||
var item Currency
|
||||
return item, currencyColl(nil).FindOne(&item, bson.M{"_id": id})
|
||||
}
|
||||
|
||||
// List 获取货币配置列表
|
||||
func List(t commod.CurrencyType) ([]Currency, error) {
|
||||
var items []Currency
|
||||
return items, currencyColl(nil).Find(&items, bson.M{"isActive": true, "type": t}, options.Find().SetSort(bson.M{"coins": 1}))
|
||||
}
|
||||
|
||||
// Add 新增
|
||||
func Add(document interface{}) error {
|
||||
_, err := currencyColl(nil).InsertOne(&document)
|
||||
return err
|
||||
}
|
||||
|
||||
// Edit 编辑
|
||||
func Edit(filter primitive.M, update primitive.M) error {
|
||||
result, err := currencyColl(nil).UpdateOne(filter, update)
|
||||
if err != nil {
|
||||
log.Warn("currency edit err", log.Any("filter", filter), log.Any("update", update), log.E(err))
|
||||
return err
|
||||
}
|
||||
if result.ModifiedCount == 0 {
|
||||
log.Warn("currency edit fail", log.Any("filter", filter), log.Any("update", update), log.E(err))
|
||||
return errors.New("ModifiedCount is nil")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// QueryAll 查询
|
||||
func QueryAll(filter primitive.M, opts ...*options.FindOptions) ([]*Currency, error) {
|
||||
var out []*Currency
|
||||
return out, currencyColl(nil).Find(&out, filter, opts...)
|
||||
}
|
||||
|
||||
// QueryCount 查询条目
|
||||
func QueryCount(filter primitive.M) (int64, error) {
|
||||
return currencyColl(nil).Count(filter)
|
||||
}
|
||||
|
||||
// GetCurrencyListByRedis 条件获取金币配置表,缓存拿
|
||||
func GetCurrencyListByRedis(t commod.CurrencyType) ([]*Currency, error) {
|
||||
var back []*Currency
|
||||
redisKey := redisconst.DataCachKey(currencyTable, "trueList")
|
||||
var redisc *redis.Client
|
||||
if redisc == nil || !redisc.Exists(redisKey) {
|
||||
opts := options.FindOptions{}
|
||||
opts.SetSort(bson.D{{Key: "coins", Value: 1}})
|
||||
if err := currencyColl(nil).Find(&back, bson.M{"isActive": true, "type": t}, &opts); err != nil {
|
||||
log.ZapLog.Warn("currencymod GetCurrencyListByRedis 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.EmailCaptchaExpire)
|
||||
return back, err
|
||||
}
|
||||
str, err := redisc.Get(redisKey)
|
||||
if err != nil {
|
||||
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetCurrencyListByRedis", currencyTable, "Find", err))
|
||||
return nil, err
|
||||
}
|
||||
if str == nil {
|
||||
return back, errors.New("redis key is null")
|
||||
}
|
||||
return back, json.Unmarshal([]byte(*str), &back)
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
package currencymod
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"91porn-server/models/commod"
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
)
|
||||
|
||||
// Currency 货币
|
||||
type Currency struct {
|
||||
ID primitive.ObjectID `json:"id" bson:"_id,omitempty"`
|
||||
Name string `json:"name" bson:"name"` //货币名称
|
||||
Type commod.CurrencyType `json:"type" bson:"type"` //货币类型
|
||||
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"` //赠送楼凤解锁次数
|
||||
GiveFruitCoin int64 `json:"giveFruitCoin" bson:"giveFruitCoin"` //赠送果币
|
||||
GiveGold int64 `json:"giveGold" bson:"giveGold"` //赠送金币
|
||||
FirstGiveVipDays int `json:"firstGiveVipDays" bson:"firstGiveVipDays"` //首次赠送vip天数
|
||||
FirstLouFengUnlockTimes int `json:"firstLouFengUnlockTimes" bson:"firsLouFengUnlockTimes"` //首次赠送楼凤解锁次数
|
||||
FirstGiveFruitCoin int64 `json:"firstGiveFruitCoin" bson:"firstGiveFruitCoin"` //首次赠送果币
|
||||
FirstGiveGold int64 `json:"firstGiveGold" bson:"firstGiveGold"` //首次赠送金币
|
||||
NotFirst bool `json:"notFirst" bson:"notFirst"` //非首充依然赠送
|
||||
IsActive bool `json:"isActive" bson:"isActive"` //是否激活
|
||||
CreatedAt time.Time `json:"createdAt" bson:"createdAt,omitempty"` //创建时间
|
||||
UpdatedAt time.Time `json:"updatedAt" bson:"updatedAt,omitempty"` //刷新时间
|
||||
}
|
||||
|
||||
// CurrencyApp 货币
|
||||
type CurrencyApp struct {
|
||||
ID primitive.ObjectID `json:"id" bson:"_id,omitempty"` // ID
|
||||
Name string `json:"name" bson:"name"` // 货币名称
|
||||
Type commod.CurrencyType `json:"type" bson:"type"` // 货币类型
|
||||
Coins int64 `json:"coins" bson:"coins"` // 购买货币数
|
||||
Price int64 `json:"price" bson:"price"` // 价格 单位:分
|
||||
CouponDesc string `json:"couponDesc" bson:"couponDesc"` // 优惠描述
|
||||
}
|
||||
|
||||
// YuanToFen 货币转化
|
||||
func (c *Currency) YuanToFen() int64 {
|
||||
return decimal.NewFromInt(c.Price).Mul(decimal.NewFromInt(100)).IntPart()
|
||||
}
|
||||
|
||||
// HandlerGiveVip 赠送会员天数处理
|
||||
func (c *Currency) HandlerGiveVip(level int, expireTime time.Time, isFirstPay bool) (int, time.Time) {
|
||||
addDay := c.GiveVipDays
|
||||
if isFirstPay {
|
||||
addDay += c.FirstGiveVipDays
|
||||
}
|
||||
// 默认赠送等级为1
|
||||
var giveLevel = 1
|
||||
// VIP未过期
|
||||
if expireTime.After(time.Now()) {
|
||||
expireTime = expireTime.AddDate(0, 0, addDay)
|
||||
if level > giveLevel {
|
||||
return level, expireTime
|
||||
}
|
||||
return giveLevel, expireTime
|
||||
}
|
||||
// VIP已过期
|
||||
return giveLevel, time.Now().AddDate(0, 0, addDay)
|
||||
}
|
||||
|
||||
func ListByIdsMap(ids []primitive.ObjectID) (map[primitive.ObjectID]*Currency, error) {
|
||||
var items []*Currency
|
||||
if err := currencyColl(nil).Find(&items, bson.M{"_id": bson.M{"$in": ids}}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var m = make(map[primitive.ObjectID]*Currency, 0)
|
||||
for _, v := range items {
|
||||
m[v.ID] = v
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
Reference in New Issue
Block a user