package game

import (
	"context"
	"database/sql"
	"errors"
	"server/MergeConst"
	"server/conf"
	baseCfg "server/conf/base"
	userCfg "server/conf/user"
	"server/db"
	"server/game/mod/item"
	limitedTimeEvent "server/game/mod/limited_time_event"
	Msg "server/game/mod/msg"
	"server/game/mod/order"
	"server/game/mod/quest"
	"server/msg"
	"server/pkg/github.com/name5566/leaf/log"
	"time"

	"google.golang.org/protobuf/proto"
)

type PlayerBaseData struct {
	p    *Player
	Data msg.ResPlayerBaseInfo
}

func (p *PlayerBaseData) GetData() interface{} {
	return &p.Data
}

func (p *PlayerBaseData) BackUp() msg.ResPlayerBaseInfo {
	return msg.ResPlayerBaseInfo{
		Diamond:           p.Data.Diamond,
		DwUin:             p.Data.DwUin,
		Energy:            p.Data.Energy,
		Star:              p.Data.Star,
		RecoverTime:       p.Data.RecoverTime,
		Level:             p.Data.Level,
		Exp:               p.Data.Exp,
		StartOrderId:      p.Data.StartOrderId,
		MusicCode:         p.Data.MusicCode,
		Guild:             p.Data.Guild,
		PackUnlockCount:   p.Data.PackUnlockCount,
		LastPlayTime:      p.Data.LastPlayTime,
		Ban:               p.Data.Ban,
		LoginTime:         p.Data.LoginTime,
		UserName:          p.Data.UserName,
		LogoutTime:        p.Data.LogoutTime,
		Node:              p.Data.Node,
		Rolecreatetime:    p.Data.Rolecreatetime,
		LastChampGroupID:  p.Data.LastChampGroupID,
		ChampshipsGroupID: p.Data.ChampshipsGroupID,
		NoAd:              p.Data.NoAd,
		FaceBookId:        p.Data.FaceBookId,
	}
}

func (p *PlayerBaseData) Recover(old *PlayerBaseData) *PlayerBaseData {
	old.Data = msg.ResPlayerBaseInfo{
		Diamond:           p.Data.Diamond,
		DwUin:             p.Data.DwUin,
		Energy:            p.Data.Energy,
		Star:              p.Data.Star,
		RecoverTime:       p.Data.RecoverTime,
		Level:             p.Data.Level,
		Exp:               p.Data.Exp,
		StartOrderId:      p.Data.StartOrderId,
		MusicCode:         p.Data.MusicCode,
		Guild:             p.Data.Guild,
		PackUnlockCount:   p.Data.PackUnlockCount,
		LastPlayTime:      p.Data.LastPlayTime,
		Ban:               p.Data.Ban,
		LoginTime:         p.Data.LoginTime,
		UserName:          p.Data.UserName,
		LogoutTime:        p.Data.LogoutTime,
		Node:              p.Data.Node,
		Rolecreatetime:    p.Data.Rolecreatetime,
		LastChampGroupID:  p.Data.LastChampGroupID,
		ChampshipsGroupID: p.Data.ChampshipsGroupID,
		NoAd:              p.Data.NoAd,
		FaceBookId:        p.Data.FaceBookId,
	}
	return old
}

func (p *PlayerBaseData) LoadDataFromDB(UserName interface{}) bool {
	sqlStr := "SELECT * FROM t_player_baseinfo WHERE user_name = ?"
	sqlStruck := db.ResPlayerBaseInfo{}
	if err := db.SqlDb.Get(&sqlStruck, sqlStr, UserName); err != nil {
		log.Debug("PlayerBaseData get data failed, err:%v\n", err)
		return false
	}

	p.Data.Diamond = sqlStruck.Diamond
	p.Data.DwUin = sqlStruck.DwUin
	p.Data.Energy = sqlStruck.Energy
	p.Data.Star = sqlStruck.Star
	p.Data.RecoverTime = sqlStruck.RecoverTime
	p.Data.Level = sqlStruck.Level
	p.Data.Exp = sqlStruck.Exp
	p.Data.StartOrderId = sqlStruck.StartOrderId
	p.Data.MusicCode = sqlStruck.MusicCode
	p.Data.Guild = sqlStruck.Guild
	p.Data.PackUnlockCount = sqlStruck.PackUnlockCount
	p.Data.LastPlayTime = sqlStruck.LastPlayTime
	p.Data.Ban = sqlStruck.Ban
	p.Data.UserName = sqlStruck.UserName
	p.Data.LogoutTime = sqlStruck.LogoutTime
	p.Data.Node = sqlStruck.Node
	p.Data.Rolecreatetime = sqlStruck.Rolecreatetime
	p.Data.LastChampGroupID = sqlStruck.LastChampGroupID
	p.Data.ChampshipsGroupID = sqlStruck.ChampshipsGroupID
	p.Data.NoAd = sqlStruck.NoAd
	p.Data.FaceBookId = sqlStruck.FaceBookId

	return true
}

func (p *PlayerBaseData) LoadDataFromDBByUid(Uid int) bool {
	sqlStr := "SELECT * FROM t_player_baseinfo WHERE DwUin = ?"
	sqlStruck := db.ResPlayerBaseInfo{}
	if err := db.SqlDb.Get(&sqlStruck, sqlStr, Uid); err != nil {
		log.Debug("PlayerBaseData get data failed, err:%v\n", err)
		return false
	}

	p.Data.Diamond = sqlStruck.Diamond
	p.Data.DwUin = sqlStruck.DwUin
	p.Data.Energy = sqlStruck.Energy
	p.Data.Star = sqlStruck.Star
	p.Data.RecoverTime = sqlStruck.RecoverTime
	p.Data.Level = sqlStruck.Level
	p.Data.Exp = sqlStruck.Exp
	p.Data.StartOrderId = sqlStruck.StartOrderId
	p.Data.MusicCode = sqlStruck.MusicCode
	p.Data.Guild = sqlStruck.Guild
	p.Data.PackUnlockCount = sqlStruck.PackUnlockCount
	p.Data.LastPlayTime = sqlStruck.LastPlayTime
	p.Data.Ban = sqlStruck.Ban
	p.Data.UserName = sqlStruck.UserName
	p.Data.LogoutTime = sqlStruck.LogoutTime
	p.Data.Node = sqlStruck.Node
	p.Data.Rolecreatetime = sqlStruck.Rolecreatetime
	p.Data.LastChampGroupID = sqlStruck.LastChampGroupID
	p.Data.ChampshipsGroupID = sqlStruck.ChampshipsGroupID
	p.Data.NoAd = sqlStruck.NoAd
	p.Data.FaceBookId = sqlStruck.FaceBookId

	return true
}

func (p *PlayerBaseData) SaveDataFromDB(Key interface{}) bool {
	BaseMod := p.p.PlayMod.getBaseMod()
	sqlStruck := db.ResPlayerBaseInfo{}
	sqlStruck.Diamond = int32(BaseMod.Diamond)
	sqlStruck.DwUin = p.Data.DwUin
	sqlStruck.Energy = int32(BaseMod.Energy)
	sqlStruck.Star = int32(BaseMod.Star)
	sqlStruck.RecoverTime = int32(BaseMod.RecoverTime)
	sqlStruck.Level = int32(BaseMod.Level)
	sqlStruck.Exp = int32(BaseMod.Exp)
	sqlStruck.StartOrderId = p.Data.StartOrderId
	sqlStruck.MusicCode = p.Data.MusicCode
	sqlStruck.Guild = p.Data.Guild
	sqlStruck.PackUnlockCount = p.Data.PackUnlockCount
	sqlStruck.LastPlayTime = p.Data.LastPlayTime
	sqlStruck.Ban = int64(p.Data.Ban)
	sqlStruck.LoginTime = int32(BaseMod.LoginTime)
	sqlStruck.UserName = p.Data.UserName
	sqlStruck.LogoutTime = int32(BaseMod.LogoutTime)
	sqlStruck.Node = int32(conf.Server.ServerID)
	sqlStruck.Rolecreatetime = p.Data.Rolecreatetime
	sqlStruck.NoAd = p.Data.NoAd
	sqlStruck.ChampshipsGroupID = p.Data.ChampshipsGroupID
	sqlStruck.LastChampGroupID = p.Data.LastChampGroupID
	sqlStruck.FaceBookId = p.Data.FaceBookId
	sqlStruck.NickName = BaseMod.NickName
	db.FormatAllMemUpdateDb(&sqlStruck, "t_player_baseinfo", "dwUin")

	return true
}

func (p *PlayerBaseData) GetMaxEnergyMul(player *Player) int {
	BaseMod := player.PlayMod.getBaseMod()
	MaxEnergyMul := baseCfg.GetMaxEnergyMul(BaseMod.GetLevel(), BaseMod.GetEnergy())
	if player.PlayMod.getLimitedTimeEventMod().CheckExist(limitedTimeEvent.EVENT_TYPE_HIGH_ROLLER) {
		MaxEnergyMul = 11
	}
	return MaxEnergyMul
}

func (p *PlayerBaseData) GetMaxEnergy() int {
	BaseMod := p.p.PlayMod.getBaseMod()
	return userCfg.GetEnergyMax(BaseMod.Level)
}

// 更新游戏道具

func (p *PlayerBaseData) ReqRemoveAd(player *Player, buf []byte) {
	req := &msg.ReqRemoveAd{}
	res := &msg.ResRemoveAd{}
	proto.Unmarshal(buf, req)

	p.Data.NoAd = 1
	res.ResultCode = 0

	agent := player.GetAgentByPlayer()
	data, _ := proto.Marshal(res)
	G_getGameLogic().PackResInfo(agent, "ResRemoveAd", data)
}

func (p *PlayerBaseData) ResPlayerBaseInfo(player *Player) {
	BaseMod := player.PlayMod.getBaseMod()
	player.PushClientRes(&msg.ResPlayerBaseInfo{
		DwUin:        p.Data.DwUin,
		Energy:       int32(BaseMod.Energy),
		Star:         int32(BaseMod.Star),
		RecoverTime:  int32(BaseMod.RecoverTime),
		Diamond:      int32(BaseMod.Diamond),
		Level:        int32(BaseMod.Level),
		Exp:          int32(BaseMod.Exp),
		LoginTime:    int32(BaseMod.LoginTime),
		LogoutTime:   int32(BaseMod.LogoutTime),
		RegisterTime: int32(p.GetRegisterTime()),
		FaceBookId:   p.Data.FaceBookId,
	})
}

func (p *PlayerBaseData) ReqBindFacebookAccount(player *Player, buf []byte) {
	req := &msg.ReqBindFacebookAccount{}
	res := &msg.ResBindFacebookAccount{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return
	}

	sqlStr := "SELECT * FROM t_player_baseinfo WHERE FaceBookId = ?"
	sqlStruck := db.ResPlayerBaseInfo{}
	isHaveOther := false

	if err := db.SqlDb.Get(&sqlStruck, sqlStr, req.BindAccountId); err != nil {
		isHaveOther = false
	} else {
		isHaveOther = true
	}
	if isHaveOther {
		if sqlStruck.DwUin == p.Data.DwUin {
			res.ResultCode = MergeConst.Protocol_FaceBook_Binded
		} else {
			res.ResultCode = MergeConst.Protocol_FaceBook_Binded_other
		}
	} else {
		res.ResultCode = 0
		p.Data.FaceBookId = req.BindAccountId
		res.BindAccountId = req.BindAccountId
		player.TeLog("platform_connect", map[string]interface{}{
			"platform_type": "Facebook",
			"platform_id":   req.BindAccountId,
			"is_reward":     false,
		})
	}
	BaseMod := player.PlayMod.getBaseMod()
	BaseMod.FackBookId = req.BindAccountId
	agent := player.GetAgentByPlayer()
	data, _ := proto.Marshal(res)
	p.SaveDataFromDB("")
	G_getGameLogic().PackResInfo(agent, "ResBindFacebookAccount", data)
}

func (p *PlayerBaseData) ReqUnBindFacebook(player *Player, buf []byte) {
	req := &msg.ReqUnBindFacebook{}
	res := &msg.ResUnBindFacebook{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return
	}

	res.ResultCode = 0
	res.BindAccountId = req.BindAccountId
	p.Data.FaceBookId = ""
	BaseMod := player.PlayMod.getBaseMod()
	BaseMod.FackBookId = ""
	agent := player.GetAgentByPlayer()
	data, _ := proto.Marshal(res)
	G_getGameLogic().PackResInfo(agent, "ResUnBindFacebook", data)
	p.SaveDataFromDB("")
	player.TeLog("platform_disconnect", map[string]interface{}{
		"platform_type": "Facebook",
		"platform_id":   req.BindAccountId,
	})
}
func (p *PlayerBaseData) ReqOnlyBindFacebook(player *Player, buf []byte) {
	req := &msg.ReqOnlyBindFacebook{}
	res := &msg.ResOnlyBindFacebook{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return
	}

	sqlStr := "SELECT * FROM t_player_baseinfo WHERE FaceBookId = ?"
	sqlStruck := db.ResPlayerBaseInfo{}
	isHaveOther := false
	err = db.SqlDb.Get(&sqlStruck, sqlStr, req.BindAccountId)
	if err != nil {
		isHaveOther = false
	} else {
		isHaveOther = true
	}
	if isHaveOther {
		if sqlStruck.DwUin == p.Data.DwUin {
			res.ResultCode = MergeConst.Protocol_FaceBook_Binded
		} else {
			sqlStruck.FaceBookId = ""

			// 修改双方的faceBookId
			ctx := context.Background()
			txOptions := &sql.TxOptions{}
			tx, _ := db.SqlDb.BeginTx(ctx, txOptions)
			sqlStr := "update t_player_baseinfo set FaceBookId = ? where dwUin = ?"
			_, err := tx.Exec(sqlStr, "", sqlStruck.DwUin)
			if err != nil {
				tx.Rollback()
				return
			}
			sqlStr = "update t_player_baseinfo set FaceBookId = ? where dwUin = ?"
			_, err = tx.Exec(sqlStr, req.BindAccountId, p.Data.DwUin)
			if err != nil {
				tx.Rollback()
				return
			}
			tx.Commit()
			FriendMgrSend(&Msg.Msg{
				Type: Msg.HANDLE_TYPE_FACEBOOK_UNBIND,
				To:   int(sqlStruck.DwUin),
			})
			res.ResultCode = 0
			p.Data.FaceBookId = req.BindAccountId
			res.BindAccountId = req.BindAccountId
			player.TeLog("platform_connect", map[string]interface{}{
				"platform_type": "Facebook",
				"platform_id":   req.BindAccountId,
				"is_reward":     false,
			})

		}
	} else {
		res.ResultCode = 0
		p.Data.FaceBookId = req.BindAccountId
		res.BindAccountId = req.BindAccountId
	}
	BaseMod := player.PlayMod.getBaseMod()
	BaseMod.FackBookId = req.BindAccountId
	p.SaveDataFromDB("")
	agent := player.GetAgentByPlayer()
	data, _ := proto.Marshal(res)
	G_getGameLogic().PackResInfo(agent, "ResOnlyBindFacebook", data)
}

func (p *PlayerBaseData) ReqSynGameData(player *Player, buf []byte) {
	req := &msg.ReqSynGameData{}
	res := &msg.ResSynGameData{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return
	}

	sqlStr := "SELECT * FROM t_player_baseinfo WHERE FaceBookId = ?"
	sqlStruck := db.ResPlayerBaseInfo{}
	isHaveOther := false

	if err := db.SqlDb.Get(&sqlStruck, sqlStr, req.NewFBId); err != nil {
		isHaveOther = false
	} else {
		isHaveOther = true
	}
	if sqlStruck.DwUin == player.M_DwUin {
		return
	}
	OldPlayer := G_GameLogicPtr.GetPlayer(sqlStruck.DwUin)
	if OldPlayer != nil {
		agent := OldPlayer.GetAgentByPlayer()
		// notify := &msg.ForceKickOut{}
		notify := &msg.ResSynGameData{}
		data, _ := proto.Marshal(notify)
		if agent != nil {
			G_getGameLogic().PackResInfo(agent, "ResSynGameData", data)
		}
		OldPlayer.ClearData()
		G_GameLogicPtr.M_Players.Delete(sqlStruck.DwUin)
	}

	if isHaveOther {
		if sqlStruck.DwUin == p.Data.DwUin {
			res.ResultCode = MergeConst.Protocol_FaceBook_Binded
		} else {
			ReplaceName := sqlStruck.UserName
			UserName := p.Data.UserName
			res.ResultCode = 0
			// 修改双方的账号
			ctx := context.Background()
			txOptions := &sql.TxOptions{}
			tx, _ := db.SqlDb.BeginTx(ctx, txOptions)
			sqlStr1 := "SELECT * FROM t_account WHERE user_name = ?"
			sqlAccStruck1 := db.Db_Account{}
			err2 := db.SqlDb.Get(&sqlAccStruck1, sqlStr1, UserName)
			if err2 == nil {
				sqlAccStruck1.UserName = ""
				_, err := db.SqlDb.Exec("update t_account set user_name = ?  where auto_id = ?", sqlAccStruck1.UserName, sqlAccStruck1.AutoId)
				if err != nil {
					tx.Rollback()
					return
				}
			}
			p.Data.UserName = ""
			_, err := db.SqlDb.Exec("update t_player_baseinfo set user_name = ? , FaceBookId = ? where dwUin = ?", "", "", p.Data.DwUin)
			if err != nil {
				tx.Rollback()
				return
			}

			sqlStruck.FaceBookId = req.NewFBId
			sqlStruck.UserName = UserName
			_, err = db.SqlDb.Exec("update t_player_baseinfo set user_name = ?, FaceBookId = ? where dwUin = ?", UserName, req.NewFBId, sqlStruck.DwUin)
			if err != nil {
				tx.Rollback()
				return
			}

			sqlStr := "SELECT * FROM t_account WHERE user_name = ?"
			sqlAccStruck := db.Db_Account{}
			err2 = db.SqlDb.Get(&sqlAccStruck, sqlStr, ReplaceName)
			if err2 == nil {
				sqlAccStruck.UserName = UserName
				_, err := db.SqlDb.Exec("update t_account set user_name = ? where auto_id = ?", sqlAccStruck.UserName, sqlAccStruck.AutoId)
				if err != nil {
					tx.Rollback()
					return
				}
			}
			tx.Commit()
		}
	} else {
		res.ResultCode = 0

	}
	p.p.TeLog("ReqSynGameData", map[string]interface{}{
		"platform_type": "facebook",
	})
	p.SaveDataFromDB("")
	agent := player.GetAgentByPlayer()
	data, _ := proto.Marshal(res)
	G_getGameLogic().PackResInfo(agent, "ResSynGameData", data)
}

func (p *PlayerBaseData) ClearData() bool {
	p.Data.LogoutTime = int32(time.Now().Unix())
	p.Data.Node = 0
	p.SaveDataFromDB("")
	return true
}

func (p *PlayerBaseData) GetRegisterTime() int64 {
	return int64(p.Data.Rolecreatetime)
}

// 增加减少体力
func (p *PlayerBaseData) AddEnergy(player *Player, cnt int) error {
	BaseMod := p.p.PlayMod.getBaseMod()
	NewEnergy := BaseMod.Energy + cnt
	if NewEnergy < 0 {
		return errors.New("能量不足")
	}
	if cnt < 0 {
		player.QuestTrigger(&quest.Trigger{Label: quest.TRIGGER_LABEL_ENERGY, A: []interface{}{-cnt}})
		// BaseMod.FormatEnergyMul(NewEnergy)
		p.p.PushClientRes(BaseMod.BackData())
	}
	if BaseMod.Energy >= p.GetMaxEnergy() && NewEnergy < p.GetMaxEnergy() {
		Recover := userCfg.GetRecover(int(BaseMod.Level))
		player.CallEvent(time.Duration(Recover)*time.Second, func() {
			player.lock.Lock()
			defer player.lock.Unlock()
			LimitedTimeEnergyAdd(player)
		}, "AddEnergy")
		BaseMod.RecoverTime = time.Now().Unix()
	}
	BaseMod.Energy = NewEnergy
	return nil
}

// 增加减少星星
func (p *PlayerBaseData) AddStar(player *Player, cnt int) error {
	BaseMod := p.p.PlayMod.getBaseMod()
	NewStar := BaseMod.Star + cnt
	if NewStar < 0 {
		return errors.New("星星不足")
	}
	if cnt > 0 {
		player.QuestTrigger(&quest.Trigger{Label: quest.TRIGGER_LABEL_STAR, A: []interface{}{cnt}})
	}
	BaseMod.Star = NewStar
	player.UpdateUserInfo()
	return nil
}

// 增加减少钻石
func (p *PlayerBaseData) AddDiamond(cnt int) error {
	BaseMod := p.p.PlayMod.getBaseMod()
	NewDiamond := BaseMod.Diamond + cnt
	if NewDiamond < 0 {
		return errors.New("钻石不足")
	}
	BaseMod.Diamond = NewDiamond
	return nil
}

// 增加经验
func (p *PlayerBaseData) AddExp(player *Player, exp int, pexp int) (int, error) {
	BaseMod := player.PlayMod.getBaseMod()
	BaseMod.Exp += exp
	BaseMod.PExp += pexp
	upLv := 0
	upExp, upPExp := userCfg.GetLevUpExp(BaseMod.Level)
	Num := 0
	UpLevelItem := make([]*item.Item, 0)
	for BaseMod.Exp >= upExp && BaseMod.PExp >= upPExp {
		if Num > 100 {
			break
		}
		Num++
		BaseMod.Level++
		BaseMod.Exp -= upExp
		BaseMod.PExp -= upPExp
		upExp, upPExp = userCfg.GetLevUpExp(BaseMod.Level)
		// 日常任务解锁
		DailyTaskMod := player.PlayMod.getDailyTaskMod()
		DecorateMod := player.PlayMod.getDecorateMod()
		if DailyTaskMod.LevUpTrigger(BaseMod.Level, DecorateMod.GetAreaId()) {
			player.PushClientRes(DailyTaskMod.BackData())
		}
		upLv = BaseMod.Level
		Items := userCfg.GetLevUpReward(upLv)
		UpLevelItem = item.Merge(UpLevelItem, Items)
		// 棋盘背包解锁
		player.PushClientRes(p.BackAsset())
		ChessMod := player.PlayMod.getChessMod()
		if ChessMod.TriggerChessBagUnlock(int(BaseMod.Level)) {
			player.PushClientRes(ChessMod.BackData())
		}
		GuideTaskMod := player.PlayMod.getGuideTaskMod()
		if GuideTaskMod.Unlock(BaseMod.Level) {
			player.PushClientRes(GuideTaskMod.BackData())
		}
		ChargeMod := player.PlayMod.getChargeMod()
		ChargeMod.TriggerChargeUnlock(int(BaseMod.Level), ChessMod.GetEmitList())
		player.ChargeBackData()
		OrderMod := player.PlayMod.getOrderMod()
		player.TriggerOrder(BaseMod.Level, order.TRIGGER_TYPE_LV, ChessMod.GetOrderEmit(), player.PlayMod.getBaseMod().GetEnergyMul())
		player.PushClientRes(OrderMod.BackData())
		// 重载活动
		player.initAcitivity()
		player.BackDataActivity()
		player.QuestTrigger(&quest.Trigger{Label: quest.TRIGGER_LABEL_UPLV})
		player.TeLog("level_up", map[string]interface{}{
			"after_level": BaseMod.Level,
		})
		NewExpLv, NewPetLv := userCfg.GetNewLevUpExp(BaseMod.Level, BaseMod.Exp, BaseMod.PExp)
		player.TeLog("property_level_up", map[string]interface{}{
			"property_level":        BaseMod.Level,
			"property_level_reward": userCfg.GetUnlock(int(BaseMod.Level)),
			"story_level":           NewExpLv,
			"pet_level":             NewPetLv,
		})
		player.HandleInUserRank()
	}
	if len(UpLevelItem) > 0 {
		err := player.HandleItem(UpLevelItem, msg.ITEM_POP_LABEL_LevUpReward.String())
		if err != nil {
			return 0, err
		}
	}
	player.EmitRetireTrigger2()
	player.InitOrderItem()
	player.PushClientRes(p.BackAsset())
	return upLv, nil
}

func (p *PlayerBaseData) GetLevel() int {
	BaseMod := p.p.PlayMod.getBaseMod()
	return BaseMod.Level
}

func (p *PlayerBaseData) GetExp() int {
	BaseMod := p.p.PlayMod.getBaseMod()
	return BaseMod.Exp
}

func (p *PlayerBaseData) GetDiamond() int {
	BaseMod := p.p.PlayMod.getBaseMod()
	return BaseMod.Diamond
}

func (p *PlayerBaseData) GetEnergy() int {
	BaseMod := p.p.PlayMod.getBaseMod()
	return BaseMod.Energy
}

func (p *PlayerBaseData) SetEnergy(Energy int) {
	BaseMod := p.p.PlayMod.getBaseMod()
	BaseMod.Energy = Energy
}

func (p *PlayerBaseData) GetStar() int {
	BaseMod := p.p.PlayMod.getBaseMod()
	return BaseMod.Star
}

func (p *PlayerBaseData) BackAsset() *msg.ResPlayerAsset {
	BaseMod := p.p.PlayMod.getBaseMod()
	return &msg.ResPlayerAsset{
		DwUin:       BaseMod.Uid,
		Diamond:     int32(BaseMod.Diamond),
		Energy:      int32(BaseMod.Energy),
		Star:        int32(BaseMod.Star),
		RecoverTime: int32(BaseMod.RecoverTime),
		Level:       int32(BaseMod.Level),
		Exp:         int32(BaseMod.Exp),
		Login:       int32(BaseMod.LoginTime),
		Logout:      int32(BaseMod.LogoutTime),
		PExp:        int32(BaseMod.PExp),
		LoginDay:    int32(BaseMod.LoginDay),
	}
}

func (p *PlayerBaseData) GetSevenLoginAdd() int {
	BaseMod := p.p.PlayMod.getBaseMod()
	return userCfg.GetSevenloginAdd(BaseMod.GetLevel())
}

func (p *PlayerBaseData) GetLastLoginTime() int {
	BaseMod := p.p.PlayMod.getBaseMod()
	return int(BaseMod.LoginTime)
}

func (p *PlayerBaseData) GetName() string {
	if p == nil {
		return ""
	}
	return p.Data.UserName
}

func (p *PlayerBaseData) GetLoginTime() int64 {
	BaseMod := p.p.PlayMod.getBaseMod()
	return int64(BaseMod.LoginTime)
}

func (p *PlayerBaseData) GetDataByUid(Uid interface{}) bool {
	sqlStr := "SELECT * FROM t_player_baseinfo WHERE dwUin = ?"
	sqlStruck := db.ResPlayerBaseInfo{}
	if err := db.SqlDb.Get(&sqlStruck, sqlStr, Uid); err != nil {
		// log.Debug("get data failed, err:%v\n", err)
		return false
	}

	p.Data.Diamond = sqlStruck.Diamond
	p.Data.DwUin = sqlStruck.DwUin
	p.Data.Energy = sqlStruck.Energy
	p.Data.Star = sqlStruck.Star
	p.Data.RecoverTime = sqlStruck.RecoverTime
	p.Data.Level = sqlStruck.Level
	p.Data.Exp = sqlStruck.Exp
	p.Data.StartOrderId = sqlStruck.StartOrderId
	p.Data.MusicCode = sqlStruck.MusicCode
	p.Data.Guild = sqlStruck.Guild
	p.Data.PackUnlockCount = sqlStruck.PackUnlockCount
	p.Data.LastPlayTime = sqlStruck.LastPlayTime
	p.Data.Ban = sqlStruck.Ban
	p.Data.LoginTime = sqlStruck.LoginTime
	p.Data.UserName = sqlStruck.UserName
	p.Data.LogoutTime = sqlStruck.LogoutTime
	p.Data.Node = sqlStruck.Node
	p.Data.Rolecreatetime = sqlStruck.Rolecreatetime
	p.Data.LastChampGroupID = sqlStruck.LastChampGroupID
	p.Data.ChampshipsGroupID = sqlStruck.ChampshipsGroupID
	p.Data.NoAd = sqlStruck.NoAd
	p.Data.FaceBookId = sqlStruck.FaceBookId
	return true
}
