package model

import (
	"errors"
	"time"

	"github.com/globalsign/mgo/bson"
	"mongo.games.com/goserver/core/logger"
)

var (
	DbShopDBName   = "log"
	DbShopCollName = "log_dbshop"
)

type ItemInfo struct {
	ItemId  int32
	ItemNum int64
}
type DbShop struct {
	LogId          bson.ObjectId `bson:"_id"`
	Platform       string        //平台
	PageId         int32
	ShopId         int32      //商品id
	ShopName       string     //商品名称
	SnId           int32      //兑换玩家
	Count          int32      //兑换数量
	Amount         []int32    //0.金币 1.钻石 2.经验 获得
	ItemInfo       []ItemInfo //道具 获得
	Consume        int32      //消费类型 1.金币 2.钻石 3.现金
	ConsumeNum     int32      //消费金额
	ConsumeType    string     //消费类型符号
	ConsumeTypeNum int32      //消费类型符号对应金额
	GainWay        int32
	State          int32     //状态 0.默认 1.成功 2.失败 3.未发货准备发货
	UserName       string    //姓名
	OtherParams    []int32   //额外参数
	UserTel        string    //手机号
	Remark         string    //备注信息
	Operator       string    //操作人
	CreateTs       time.Time //订单生成时间
	OpTs           time.Time //订单最后操作时间
	Ts             int64
}
type DbShopLogArgs struct {
	Log *DbShop
}

func NewDbShop(platform string, pageId int32, amount []int32, consumeType string, consumeTypeNum, consume, consumeNum, gainWay int32,
	itemInfo []ItemInfo, shopId int32, shopName string, snId, state int32, remark string, other []int32) *DbShop {
	t := time.Now()
	return &DbShop{
		LogId:          bson.NewObjectId(),
		Platform:       platform,
		PageId:         pageId,
		Amount:         amount,
		Consume:        consume,
		ConsumeNum:     consumeNum,
		ConsumeType:    consumeType,
		ConsumeTypeNum: consumeTypeNum,
		GainWay:        gainWay,
		ItemInfo:       itemInfo,
		ShopId:         shopId,
		ShopName:       shopName,
		SnId:           snId,
		Count:          1,
		State:          state,
		UserName:       "",
		UserTel:        "",
		Remark:         remark,
		Operator:       "sys",
		CreateTs:       t,
		OpTs:           t,
		Ts:             t.Unix(),
		OtherParams:    other,
	}
}
func InsertDbShopLog(log *DbShop) (err error) {
	if rpcCli == nil {
		logger.Logger.Error("model.InsertDbShopLog rpcCli == nil")
		return
	}
	var ret bool
	args := &DbShopLogArgs{
		Log: log,
	}
	err = rpcCli.CallWithTimeout("DbShopLogSvc.InsertDbShopLog", args, &ret, time.Second*30)
	if err != nil {
		logger.Logger.Warn("InsertDbShopLog error:", err)
		return
	}
	return
}
func GetDbShopLog(platform string, loginId string) *DbShop {
	if rpcCli == nil {
		logger.Logger.Error("model.GetDbShopLog rpcCli == nil")
		return nil
	}
	var ret DbShop
	args := &DbShopLogArgs{
		Log: &DbShop{LogId: bson.ObjectIdHex(loginId), Platform: platform},
	}
	err := rpcCli.CallWithTimeout("DbShopLogSvc.GetDbShopLog", args, &ret, time.Second*30)
	if err != nil {
		logger.Logger.Warn("GetDbShopLog error:", err)
		return nil
	}
	return &ret
}
func UpdateDbShopState(platform string, loginId string, state int32) error {
	if rpcCli == nil {
		logger.Logger.Error("model.UpdateDbShopState rpcCli == nil")
		return errors.New(" rpcCli == nil")
	}
	var ret bool
	args := &DbShopLogArgs{
		Log: &DbShop{LogId: bson.ObjectIdHex(loginId), Platform: platform, State: state},
	}
	err := rpcCli.CallWithTimeout("DbShopLogSvc.UpdateDbShopState", args, &ret, time.Second*30)
	if err != nil {
		logger.Logger.Warn("UpdateDbShopState error:", err)
		return err
	}
	if !ret {
		return errors.New("update fail " + loginId)
	}
	return nil
}
func GetDbShopLogsByPage(platform string, snid, pageId int32) []*DbShop {
	if rpcCli == nil {
		logger.Logger.Error("model.GetDbShopLogsByPage rpcCli == nil")
		return nil
	}
	var ret []*DbShop
	args := &DbShopLogArgs{
		Log: &DbShop{PageId: pageId, Platform: platform, SnId: snid},
	}
	err := rpcCli.CallWithTimeout("DbShopLogSvc.GetDbShopLogsByPage", args, &ret, time.Second*30)
	if err != nil {
		logger.Logger.Warn("GetDbShopLogsByPage error:", err)
		return nil
	}
	return ret
}
func GetDbShopLogsByState(platform string, snid int32) []*DbShop {
	if rpcCli == nil {
		logger.Logger.Error("model.GetDbShopLogsByState rpcCli == nil")
		return nil
	}
	var ret []*DbShop
	args := &DbShopLogArgs{
		Log: &DbShop{State: 3, Platform: platform, SnId: snid},
	}
	err := rpcCli.CallWithTimeout("DbShopLogSvc.GetDbShopLogsByState", args, &ret, time.Second*30)
	if err != nil {
		logger.Logger.Warn("GetDbShopLogsByState error:", err)
		return nil
	}
	return ret
}
