package jobs

import (
	"github.com/sirupsen/logrus"
	"sync"
)

var (
	ExchangePray   = new(jExchangePray)
	prayItems      = []int64{3101, 3102, 3103, 3104} // 祈福道具
	prayItemsPrice = 100                             // 祈福道具兑换金币数量
)

type jExchangePray struct {
	sync.RWMutex
}

func (j *jExchangePray) Run() {
	logrus.Info("jExchangePray Run.....")
	j.handle()
}

func (j *jExchangePray) handle() {
	/*j.RLock()
	defer j.RUnlock()

	var (
		results []*model.PlayerMaterial
		u       = query.Use(config.GDB1).PlayerMaterial
	)

	type ExtraData struct {
		Materials []*model2.DropMaterial `json:"materials"`
		Equips    []int64                `json:"equips"`
	}

	err := u.Select(u.ID, u.Playerid, u.Materials).
		Where(u.Playerid.In([]int64{1472136, 1472137, 1472139, 1472140}...)).
		Order(u.ID).
		FindInBatches(&results, 1000, func(tx gen.Dao, batch int) error {
			for _, result := range results {
				if result.Materials == "" {
					continue
				}

				var materials map[int64]*model2.DropMaterial
				if err := json.Unmarshal([]byte(result.Materials), &materials); err != nil {
					logrus.Warningf("Unmarshal Materials err:%+v", err)
					return err
				}

				// 统计个数

				var cnt int64
				for _, itId := range prayItems {
					if v, ok := materials[itId]; ok {
						cnt += int64(v.Count)
						delete(materials, itId)
					}
				}

				if cnt > 0 {
					coin := cnt * int64(prayItemsPrice)
					// 保存档案 删除卡片
					newMaterial, err := json.Marshal(materials)
					if err != nil {
						logrus.Panicf("json.Marshal materials err:%+v, materials%+v", err, materials)
					}

					if _, err = query.Use(config.GDB1).PlayerMaterial.
						Where(u.Playerid.Eq(result.Playerid)).
						Updates(model.PlayerMaterial{
							Materials: string(newMaterial),
						}); err != nil {
						logrus.Warnf("PlayerMaterial Updates2 err . %v", err)
						return err
					}

					dm := new(model2.DropMaterial)
					dm.ID = constants.MaterilaId_Coin
					dm.Count = int(coin)

					var l = query.Use(config.GDB1).PlayerLetter
					letter := model.PlayerLetter{
						PlayerID:    result.Playerid,
						Type:        16, //新年祈福道具
						ExpireTime:  time.Now().Add(time.Hour * 24 * 7),
						CreateTime:  time.Now(),
						DisplayTime: time.Now(),
					}

					var em ExtraData
					em.Materials = append(em.Materials, dm)
					emb, err := json.Marshal(&em)
					if err != nil {
						logrus.Panicf("exchangePrayItems Materials marshal error. %v, error: %v", em, err)
					}
					letter.Extra = string(emb)

					extra := make(map[string]string)
					extra["cardNumber"] = strconv.FormatInt(cnt, 10)
					extra["coinNumber"] = strconv.FormatInt(coin, 10)
					jex, err := json.Marshal(&extra)
					if err != nil {
						logrus.Panicf("exchangePrayItems LetterExtraRank marshal error. %v, error: %v", extra, err)
					}
					letter.ExtraInfo = string(jex)

					if err = l.Create(&letter); err != nil {
						logrus.Warningf("addLetter Create err :%+v, letter:%+v", err, letter)
						return err
					}
				}

			}
			return nil
		})

	if err != nil {
		logrus.WithField("form", "ExchangePray").Errorf("FindInBatches err:%+v", err)
	}

	logrus.Infof("ExchangePray ok!!")*/
}
