package jobs

import (
	"fmt"
	"gadmin/config"
	"gadmin/utility"
	"sync"
	"time"

	"github.com/sirupsen/logrus"
)

var (
	ConvPvpKey = new(jConvPvpKey)
)

type jConvPvpKey struct {
	sync.RWMutex
}

func (j *jConvPvpKey) Run() {
	logrus.Info("jConvPvpKey Run.....")
	//j.handle("pvpplayers:9")  // 版本已经更新过了，注释掉，避免再次执行。
	//j.handle("pvpplayers:10")
}

// handle
func (j *jConvPvpKey) handle(key string) {
	j.RLock()
	defer j.RUnlock()

	var (
		err    error
		cursor uint64 = 0
		match         = ""
		count  int64  = 20000
	)

	keys, cursor, err := config.GameRedis.HScan(key, cursor, match, count).Result()
	if len(keys) == 0 {
		logrus.Info(key, "不存在已跳过..")
		return
	}
	if err != nil {
		logrus.Info("HScan err:", key, err)
		return
	}

	for i := 0; i < len(keys); i++ {
		if utility.IsEven(i) {
			logrus.Warningf("key:%+v, vlue:%+v \n\n", keys[i], keys[i+1])
			config.GameRedis.Set(fmt.Sprintf("pvpplayer:%v", keys[i]), keys[i+1], 86400*14*time.Second)
		}
	}

	logrus.Infof("jConvPvpKey ok!!")
}
