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 DbShopLogCollection(plt string) *mongo.Collection {
	s := mongo.MgoSessionMgrSington.GetPltMgoSession(plt, model.DbShopDBName)
	if s != nil {
		dbShopRec, first := s.DB().C(model.DbShopCollName)
		if first {
			dbShopRec.EnsureIndex(mgo.Index{Key: []string{"snid"}, Background: true, Sparse: true})
			dbShopRec.EnsureIndex(mgo.Index{Key: []string{"pageid"}, Background: true, Sparse: true})
			dbShopRec.EnsureIndex(mgo.Index{Key: []string{"state"}, Background: true, Sparse: true})
			dbShopRec.EnsureIndex(mgo.Index{Key: []string{"shopid"}, Background: true, Sparse: true})
		}
		return dbShopRec
	}
	return nil
}

type DbShopLogSvc struct {
}

func (svc *DbShopLogSvc) InsertDbShopLog(args *model.DbShopLogArgs, ret *bool) (err error) {
	clog := DbShopLogCollection(args.Log.Platform)
	if clog == nil {
		return
	}
	logger.Logger.Trace("DbShopLogSvc.InsertDbShopLog")
	err = clog.Insert(args.Log)
	if err != nil {
		logger.Logger.Error("DbShopLogSvc.InsertDbShopLog error:", err)
		return
	}
	*ret = true
	return
}
func (svc *DbShopLogSvc) GetDbShopLog(args *model.DbShopLogArgs, dbShop *model.DbShop) (err error) {
	clog := DbShopLogCollection(args.Log.Platform)
	if clog == nil {
		logger.Logger.Error("GetDbShopLog  == nil")
		return nil
	}
	logger.Logger.Trace("DbShopLogSvc.GetDbShopLog")
	err = clog.Find(bson.M{"_id": args.Log.LogId}).One(dbShop)
	if err != nil {
		logger.Logger.Error("DbShopLogSvc.GetDbShopLog error:", err)
		return nil
	}
	return
}
func (svc *DbShopLogSvc) UpdateDbShopState(args *model.DbShopLogArgs, ret *bool) (err error) {
	clog := DbShopLogCollection(args.Log.Platform)
	if clog == nil {
		logger.Logger.Error("UpdateDbShopState  == nil")
		return nil
	}
	logger.Logger.Trace("DbShopLogSvc.UpdateDbShopState")
	err = clog.UpdateId(args.Log.LogId, bson.M{"$set": bson.M{"state": args.Log.State}})
	if err != nil {
		logger.Logger.Error("DbShopLogSvc.UpdateDbShopState error:", err)
		return nil
	}
	*ret = true
	return
}
func (svc *DbShopLogSvc) GetDbShopLogsByPage(args *model.DbShopLogArgs, dbShops *[]*model.DbShop) (err error) {
	clog := DbShopLogCollection(args.Log.Platform)
	if clog == nil {
		logger.Logger.Error("GetDbShopLogsByPage  == nil")
		return
	}
	logger.Logger.Trace("DbShopLogSvc.GetDbShopLogsByPage")
	err = clog.Find(bson.M{"pageid": args.Log.PageId, "snid": args.Log.SnId}).Limit(100).Sort("-ts").All(dbShops)
	if err != nil {
		logger.Logger.Error("DbShopLogSvc.GetDbShopLogsByPage error:", err)
		return
	}
	return
}
func (svc *DbShopLogSvc) GetDbShopLogsByState(args *model.DbShopLogArgs, dbShops *[]*model.DbShop) (err error) {
	clog := DbShopLogCollection(args.Log.Platform)
	if clog == nil {
		logger.Logger.Error("GetDbShopLogsByPage  == nil")
		return
	}
	logger.Logger.Trace("DbShopLogSvc.GetDbShopLogsByPage")
	err = clog.Find(bson.M{"state": args.Log.State, "snid": args.Log.SnId}).All(dbShops)
	if err != nil {
		logger.Logger.Error("DbShopLogSvc.GetDbShopLogsByPage error:", err)
		return
	}
	return
}
func init() {
	rpc.Register(new(DbShopLogSvc))
}
