78 lines
1.8 KiB
Go
78 lines
1.8 KiB
Go
package model
|
|
|
|
import (
|
|
"errors"
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
"mongo.games.com/goserver/core/logger"
|
|
"time"
|
|
)
|
|
|
|
//go:generate mongoctl -model-dir=. -model-names=PushCoinPool -dao-dir=../dao/
|
|
type PushCoinPool struct {
|
|
Platform string `bson:"-"`
|
|
ID primitive.ObjectID `bson:"_id" gen:"autoFill"`
|
|
Pool1 *PushCoinPoolInfo `bson:"pool1"`
|
|
Pool2 *PushCoinPoolInfo `bson:"pool2"`
|
|
Pool3 *PushCoinPoolInfo `bson:"pool3"`
|
|
}
|
|
|
|
type PushCoinPoolInfo struct {
|
|
CurCoin int64 `bson:"curcoin"` // 当前金币
|
|
Remain int64 `bson:"remain"` // 剩余金币
|
|
Version int64 `bson:"version"` // 版本号, 达到上限后更新
|
|
}
|
|
|
|
func NewPushCoinPool(plt string) *PushCoinPool {
|
|
return &PushCoinPool{
|
|
Platform: plt,
|
|
ID: primitive.NewObjectID(),
|
|
Pool1: &PushCoinPoolInfo{},
|
|
Pool2: &PushCoinPoolInfo{},
|
|
Pool3: &PushCoinPoolInfo{},
|
|
}
|
|
|
|
}
|
|
|
|
func (p *PushCoinPool) TableName() string {
|
|
return "pushcoin_pool"
|
|
}
|
|
|
|
func (p *PushCoinPool) DatabaseName() string {
|
|
return "log"
|
|
}
|
|
|
|
func PushCoinLoad(plt string) (*PushCoinPool, error) {
|
|
if rpcCli == nil {
|
|
logger.Logger.Error("model.PushCoinLoad rpcCli == nil")
|
|
return nil, errors.New("rpc client is nil")
|
|
}
|
|
|
|
res := &PushCoinPool{}
|
|
err := rpcCli.CallWithTimeout("PushCoinService.Load", &plt, res, time.Second*30)
|
|
if err != nil {
|
|
logger.Logger.Errorf("PushCoinLoad error: %v", err)
|
|
return nil, err
|
|
}
|
|
if res.ID.IsZero() {
|
|
res = NewPushCoinPool(plt)
|
|
}
|
|
|
|
return res, nil
|
|
}
|
|
|
|
func PushCoinSave(data *PushCoinPool) error {
|
|
if rpcCli == nil {
|
|
logger.Logger.Error("model.PushCoinSave rpcCli == nil")
|
|
return errors.New("rpc client is nil")
|
|
}
|
|
|
|
ret := false
|
|
err := rpcCli.CallWithTimeout("PushCoinService.Save", data, &ret, time.Second*30)
|
|
if err != nil {
|
|
logger.Logger.Errorf("PushCoinSave error: %v", err)
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|