75 lines
1.8 KiB
Go
75 lines
1.8 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 (
|
|
LotteryDataDBErr = errors.New("log_lotterydata db open failed.")
|
|
)
|
|
|
|
func LotteryDataCollection(plt string) *mongo.Collection {
|
|
s := mongo.MgoSessionMgrSington.GetPltMgoSession(plt, model.LotteryDataDBName)
|
|
if s != nil {
|
|
c, first := s.DB().C(model.LotteryDataCollName)
|
|
if first {
|
|
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})
|
|
}
|
|
return c
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func GetLotteryData(plt string) ([]*model.LotteryData, error) {
|
|
clog := LotteryDataCollection(plt)
|
|
if clog == nil {
|
|
return nil, LotteryDataDBErr
|
|
}
|
|
var logs []*model.LotteryData
|
|
err := clog.Find(nil).All(&logs)
|
|
if err != nil && !errors.Is(err, mgo.ErrNotFound) {
|
|
return nil, err
|
|
}
|
|
return logs, nil
|
|
}
|
|
|
|
func UpsertLotteryData(plt string, arr []*model.LotteryData) error {
|
|
clog := LotteryDataCollection(plt)
|
|
if clog == nil {
|
|
return LotteryDataDBErr
|
|
}
|
|
for _, log := range arr {
|
|
_, err := clog.Upsert(bson.M{"cid": log.CId, "startts": log.StartTs}, log)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
type LotteryDataSvc struct{}
|
|
|
|
func (s *LotteryDataSvc) Get(req *model.GetLotteryDataReq, reply *model.GetLotteryDataResp) (err error) {
|
|
reply.Data, err = GetLotteryData(req.Platform)
|
|
return
|
|
}
|
|
|
|
func (s *LotteryDataSvc) Upsert(req *model.UpsertLotteryDataReq, reply *bool) (err error) {
|
|
err = UpsertLotteryData(req.Platform, req.Data)
|
|
*reply = err == nil
|
|
return
|
|
}
|
|
|
|
func init() {
|
|
rpc.Register(new(LotteryDataSvc))
|
|
}
|