game_sync/dbproxy/svc/l_lotterylog.go

102 lines
2.9 KiB
Go

package svc
import (
"errors"
"net/rpc"
"time"
"github.com/globalsign/mgo"
"github.com/globalsign/mgo/bson"
"mongo.games.com/goserver/core/logger"
"mongo.games.com/game/dbproxy/mongo"
"mongo.games.com/game/model"
)
var (
LotteryLogDBErr = errors.New("log_lottery db open failed.")
)
func LotteryLogsCollection(plt string) *mongo.Collection {
s := mongo.MgoSessionMgrSington.GetPltMgoSession(plt, model.LotteryLogDBName)
if s != nil {
c, first := s.DB().C(model.LotteryLogCollName)
if first {
c.EnsureIndex(mgo.Index{Key: []string{"cid"}, Background: true, Sparse: true})
c.EnsureIndex(mgo.Index{Key: []string{"ctime"}, Background: true, Sparse: true})
c.EnsureIndex(mgo.Index{Key: []string{"-ctime"}, Background: true, Sparse: true})
c.EnsureIndex(mgo.Index{Key: []string{"num"}, Background: true, Sparse: true})
c.EnsureIndex(mgo.Index{Key: []string{"snid"}, Background: true, Sparse: true})
c.EnsureIndex(mgo.Index{Key: []string{"name"}, Background: true, Sparse: true})
c.EnsureIndex(mgo.Index{Key: []string{"playernum"}, Background: true, Sparse: true})
c.EnsureIndex(mgo.Index{Key: []string{"code"}, Background: true, Sparse: true})
c.EnsureIndex(mgo.Index{Key: []string{"isrobot"}, Background: true, Sparse: true})
c.EnsureIndex(mgo.Index{Key: []string{"ismust"}, Background: true, Sparse: true})
c.EnsureIndex(mgo.Index{Key: []string{"ts"}, Background: true, Sparse: true})
c.EnsureIndex(mgo.Index{Key: []string{"-ts"}, Background: true, Sparse: true})
}
return c
}
return nil
}
func InsertLotteryLogs(plt string, logs ...*model.LotteryLog) (err error) {
clog := LotteryLogsCollection(plt)
if clog == nil {
return
}
switch len(logs) {
case 0:
return errors.New("no data")
case 1:
err = clog.Insert(logs[0])
default:
docs := make([]interface{}, 0, len(logs))
for _, log := range logs {
docs = append(docs, log)
}
err = clog.Insert(docs...)
}
if err != nil {
logger.Logger.Warn("InsertLotteryLogs error:", err)
return
}
return
}
func GetLotteryLogs(plt string, count int) (ret []*model.LotteryLog, err error) {
clog := LotteryLogsCollection(plt)
if clog == nil {
return nil, LotteryLogDBErr
}
err = clog.Find(bson.M{"ts": bson.M{"$lt": time.Now().Unix()}}).Sort("-ts").Limit(count).All(&ret)
if err != nil && !errors.Is(err, mgo.ErrNotFound) {
return nil, err
}
return
}
type LotteryLogSvc struct{}
func (svc *LotteryLogSvc) GetLotteryLogs(req *model.GetLotteryLogReq, ret *model.GetLotteryLogResp) (err error) {
ret.LotteryLog, err = GetLotteryLogs(req.Platform, req.Num)
return
}
func (svc *LotteryLogSvc) UpdateMedia(req *model.UpdateLotteryMediaReq, ret *bool) error {
clog := LotteryLogsCollection(req.Platform)
if clog == nil {
return LotteryLogDBErr
}
err := clog.Update(bson.M{"_id": req.LogId}, bson.M{"$set": bson.M{"media": req.Media}})
if err != nil && !errors.Is(err, mgo.ErrNotFound) {
return err
}
*ret = true
return nil
}
func init() {
rpc.Register(new(LotteryLogSvc))
}