package service

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

func (s *UserService) Item(t func(map[uint32]model.SingleItemInfo) bool) {

	//todo待测试
	var player model.ItemEX
	m1 := cool.DBM(s.item.Model).Where("player_id", s.userid)

	err := m1.Scan(&player)
	if err != nil {
		player.Data = make(map[uint32]model.SingleItemInfo)
		_, err := m1.Insert(player)
		if err != nil {
			panic(err)
		}

	}

	if !t(player.Data) {
		return
	}
	_, err = m1.Update(player)
	if err != nil {
		panic(err)
	}
}
func (s *UserService) ItemAdd(t []model.SingleItemInfo) {

	s.Item(func(rer map[uint32]model.SingleItemInfo) bool {
		for _, v := range t {
			tt, ok := rer[v.ItemId]
			if ok {
				tt.ItemCnt += v.ItemCnt
				rer[v.ItemId] = tt
			} else {
				rer[v.ItemId] = v
			}

		}
		return true
	})

}
