122 lines
3.5 KiB
Go
122 lines
3.5 KiB
Go
package svc
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"mongo.games.com/goserver/core/logger"
|
|
"net/rpc"
|
|
|
|
"github.com/globalsign/mgo"
|
|
"github.com/globalsign/mgo/bson"
|
|
|
|
"mongo.games.com/game/dbproxy/mongo"
|
|
"mongo.games.com/game/model"
|
|
)
|
|
|
|
var (
|
|
LotteryCodeDBErr = errors.New("log_lotterycode db open failed.")
|
|
)
|
|
|
|
func LotteryCodeCollection(plt string) *mongo.Collection {
|
|
s := mongo.MgoSessionMgrSington.GetPltMgoSession(plt, model.LotteryCodeDBName)
|
|
if s != nil {
|
|
c, first := s.DB().C(model.LotteryCodeCollName)
|
|
if first {
|
|
c.EnsureIndex(mgo.Index{Key: []string{"snid", "index"}, Background: true, Sparse: true})
|
|
c.EnsureIndex(mgo.Index{Key: []string{"cid"}, Background: true, Sparse: true})
|
|
c.EnsureIndex(mgo.Index{Key: []string{"startts"}, Background: true, Sparse: true})
|
|
c.EnsureIndex(mgo.Index{Key: []string{"-startts"}, Background: true, Sparse: true})
|
|
c.EnsureIndex(mgo.Index{Key: []string{"code"}, Background: true, Sparse: true})
|
|
c.EnsureIndex(mgo.Index{Key: []string{"index"}, Background: true, Sparse: true})
|
|
c.EnsureIndex(mgo.Index{Key: []string{"-index"}, Background: true, Sparse: true})
|
|
}
|
|
return c
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// UpsertLotteryCode 记录玩家抽奖活动开奖码
|
|
func UpsertLotteryCode(plt string, item *model.LotteryCode) (err error) {
|
|
c := LotteryCodeCollection(plt)
|
|
if c == nil {
|
|
return LotteryCodeDBErr
|
|
}
|
|
_, err = c.Upsert(bson.M{"snid": item.SnId, "cid": item.CId, "startts": item.StartTs}, item)
|
|
return
|
|
}
|
|
|
|
// GetLotteryCode 获取玩家抽奖活动开奖码
|
|
func GetLotteryCode(plt string, cid int64, startTs int64, code string) (*model.LotteryCode, error) {
|
|
c := LotteryCodeCollection(plt)
|
|
if c == nil {
|
|
return nil, LotteryCodeDBErr
|
|
}
|
|
var ret model.LotteryCode
|
|
err := c.Find(bson.M{"code": code, "cid": cid, "startts": startTs}).One(&ret)
|
|
if err != nil && !errors.Is(err, mgo.ErrNotFound) {
|
|
return nil, err
|
|
}
|
|
return &ret, nil
|
|
}
|
|
|
|
type LotteryCodeSvc struct{}
|
|
|
|
func (svc *LotteryCodeSvc) Upsert(req *model.UpsertLotteryCodeReq, ret *bool) (err error) {
|
|
err = UpsertLotteryCode(req.Platform, req.Item)
|
|
*ret = err == nil
|
|
return
|
|
}
|
|
|
|
func (svc *LotteryCodeSvc) Get(req *model.GetLotteryCodeReq, resp *model.GetLotteryCodeResp) (err error) {
|
|
resp.Item, err = GetLotteryCode(req.Platform, req.CId, req.StartTs, req.Code)
|
|
return
|
|
}
|
|
|
|
func (svc *LotteryCodeSvc) GetJoinNum(req *model.GetLotteryCodeReq, resp *int) (err error) {
|
|
c := LotteryCodeCollection(req.Platform)
|
|
if c == nil {
|
|
return LotteryCodeDBErr
|
|
}
|
|
type m struct {
|
|
Count int64 `bson:"count"`
|
|
}
|
|
tc := new(m)
|
|
err = c.Pipe([]bson.M{
|
|
{"$match": bson.M{"snid": bson.M{"$gt": 0}, "cid": req.CId, "startts": req.StartTs}},
|
|
{"$group": bson.M{"_id": bson.M{"snid": "$snid"}}},
|
|
{"$count": "count"},
|
|
}).AllowDiskUse().One(tc)
|
|
if err != nil && !errors.Is(err, mgo.ErrNotFound) {
|
|
logger.Logger.Warn("GetJoinNum error:", err)
|
|
return
|
|
}
|
|
*resp = int(tc.Count)
|
|
return nil
|
|
}
|
|
|
|
func (svc *LotteryCodeSvc) GetRandom(req *model.GetLotteryCodeRandomReq, ret *model.GetLotteryCodeResp) error {
|
|
c := LotteryCodeCollection(req.Platform)
|
|
if c == nil {
|
|
return LotteryCodeDBErr
|
|
}
|
|
where := bson.M{"cid": req.CId, "startts": req.StartTs}
|
|
switch req.Tp {
|
|
case 1:
|
|
where["snid"] = 0
|
|
where["index"] = req.Index
|
|
case 2:
|
|
where["code"] = fmt.Sprintf("%v", req.Index)
|
|
case 3:
|
|
where["snid"] = bson.M{"$gt": 0}
|
|
where["index"] = req.Index
|
|
}
|
|
if err := c.Find(where).One(&ret.Item); err != nil && !errors.Is(err, mgo.ErrNotFound) {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func init() {
|
|
rpc.Register(new(LotteryCodeSvc))
|
|
}
|