package service

import (
	"blazing/cool"
	"blazing/modules/blazing/model"
	"encoding/json"
)

// 获取精灵信息   0是仓库,1是背包,2是放生
func (s *UserService) GetPetList(flag int) (ret []model.PetInfo) {
	ret = make([]model.PetInfo, 0)
	m := cool.DBM(s.pet.Model).Where("player_id", s.userid).Where("in_bag", flag)
	var tt []model.Pet
	m.Scan(&tt)

	for _, v := range tt {
		var ret11 model.PetInfo
		json.Unmarshal([]byte(v.Data), &ret11)

		ret = append(ret, ret11)
	}

	return

}

func (s *UserService) GetPetInfo(cachetime uint32) (ret model.PetInfo) {

	m := cool.DBM(s.pet.Model).Where("player_id", s.userid).Where("catch_time", cachetime)
	var tt model.Pet
	m.Scan(&tt)
	json.Unmarshal([]byte(tt.Data), &ret)

	return

}
func (s *UserService) PetExec(ctime uint32, t func(uint32, model.PetInfo) model.PetInfo) {
	//todo待测试
	var player model.Pet
	m1 := cool.DBM(s.pet.Model).Where("player_id", s.userid).Where("catch_time", ctime)
	m1.Scan(&player)
	var tt model.PetInfo
	json.Unmarshal([]byte(player.Data), &tt)
	t(player.CatchTime, tt)
	tmep, _ := json.Marshal(tt)
	player.Data = string(tmep)
	m1.Save(player)

}
func (s *UserService) PetAdd(y model.PetInfo) {

	m1 := cool.DBM(s.pet.Model).Where("player_id", s.userid)
	var player model.Pet
	player.PlayerID = s.userid
	player.CatchTime = y.CatchTime
	player.InBag = 0
	tmp, _ := json.Marshal(y)
	player.Data = string(tmp)
	m1.Insert(player)

}
func (s *UserService) PetM(ctime, type1 int) model.PetInfo {

	m1 := cool.DBM(s.pet.Model).Where("player_id", s.userid).Where("catch_time", ctime)
	var player model.Pet
	m1.Scan(&player)
	player.InBag = type1
	var tt model.PetInfo

	json.Unmarshal([]byte(player.Data), &tt)
	m1.Update(player)

	return tt

}
