game_sync/dbproxy/svc/u_lottery.go

85 lines
1.9 KiB
Go

package svc
import (
"errors"
"net/rpc"
"github.com/globalsign/mgo"
"github.com/globalsign/mgo/bson"
"mongo.games.com/game/dbproxy/mongo"
"mongo.games.com/game/model"
)
var (
LotteryDBErr = errors.New("user_coinlog db open failed.")
)
func LotteryCollection(plt string) *mongo.Collection {
s := mongo.MgoSessionMgrSington.GetPltMgoSession(plt, model.LotteryDBName)
if s != nil {
c, first := s.DB().C(model.LotteryCollName)
if first {
c.EnsureIndex(mgo.Index{Key: []string{"snid"}, Background: true, Sparse: true})
c.EnsureIndex(mgo.Index{Key: []string{"cid"}, Background: true, Sparse: true})
c.EnsureIndex(mgo.Index{Key: []string{"time"}, Background: true, Sparse: true})
c.EnsureIndex(mgo.Index{Key: []string{"-time"}, Background: true, Sparse: true})
}
return c
}
return nil
}
func GetLottery(plt string, snid int32, cid int64, startts int64) (ret []*model.Lottery, err error) {
c := LotteryCollection(plt)
if c == nil {
return nil, LotteryDBErr
}
where := bson.M{"snid": snid}
if cid > 0 {
where["cid"] = cid
}
if startts > 0 {
where["startts"] = bson.M{"$gte": startts}
}
err = c.Find(where).All(&ret)
if err != nil && !errors.Is(err, mgo.ErrNotFound) {
return nil, err
}
return
}
func UpsertLottery(plt string, item []*model.Lottery) (err error) {
c := LotteryCollection(plt)
if c == nil {
return LotteryDBErr
}
for _, v := range item {
_, err = c.Upsert(bson.M{"snid": v.SnId, "cid": v.CId}, v)
if err != nil {
return
}
}
return
}
type LotterySvc struct {
}
func (svc *LotterySvc) Get(req *model.GetLotteryReq, ret *model.GetLotteryResp) (err error) {
ret.Lottery, err = GetLottery(req.Platform, req.SnId, req.CId, req.StartTs)
return
}
func (svc *LotterySvc) Upsert(req *model.UpsertLotteryReq, ret *bool) (err error) {
err = UpsertLottery(req.Platform, req.Lottery)
if err == nil {
*ret = true
}
return
}
func init() {
rpc.Register(new(LotterySvc))
}