package svc

import (
	"github.com/globalsign/mgo"
	"github.com/globalsign/mgo/bson"
	"mongo.games.com/game/dbproxy/mongo"
	"mongo.games.com/game/model"
	"mongo.games.com/goserver/core/logger"
	"net/rpc"
)

func BurstJackpotCoinLogsCollection(plt string) *mongo.Collection {
	s := mongo.MgoSessionMgrSington.GetPltMgoSession(plt, model.BurstJackpotLogDBName)
	if s != nil {
		c_burstjackpot, first := s.DB().C(model.BurstJackpotLogCollName)
		if first {
			c_burstjackpot.EnsureIndex(mgo.Index{Key: []string{"platform"}, Background: true, Sparse: true})
			c_burstjackpot.EnsureIndex(mgo.Index{Key: []string{"gameid"}, Background: true, Sparse: true})
			c_burstjackpot.EnsureIndex(mgo.Index{Key: []string{"gamefreeid"}, Background: true, Sparse: true})
			c_burstjackpot.EnsureIndex(mgo.Index{Key: []string{"ts"}, Background: true, Sparse: true})
		}
		return c_burstjackpot
	}
	return nil
}

type BurstJackpotSvc struct {
}

func (svc *BurstJackpotSvc) GetBurstJackpotLog(arg *model.BurstJackpotArg, ret *[]model.BurstJackpotLog) (err error) {
	clog := BurstJackpotCoinLogsCollection(arg.Platform)
	if clog == nil {
		return
	}
	err = clog.Find(bson.M{"gameid": arg.GameId}).Limit(80).Sort("-ts").All(ret)
	if err != nil {
		logger.Logger.Error("GetBurstJackpotLog err:", err)
	}
	return
}
func (svc *BurstJackpotSvc) InsertBurstJackpotLogs(log *model.BurstJackpotLog, ret *bool) (err error) {
	clog := BurstJackpotCoinLogsCollection(log.Platform)
	if clog == nil {
		return
	}
	err = clog.Insert(log)
	if err != nil {
		logger.Logger.Warn("svc.InsertBurstJackpotLogs error:", err)
		return
	}
	*ret = true
	return
}
func init() {
	rpc.Register(&BurstJackpotSvc{})
}
