package goldbrick

import (
	"active/constant"
	"fmt"
	"git.jiaxianghudong.com/webs/pkg/rds"
	"log"
	"strconv"
	"time"
)

var recordTime = ""

func GetGoldData(userId string, tm string) int {
	data := rds.Redis.HGet(fmt.Sprintf(constant.GoldBrick, tm), userId).Val()
	if data == "" {
		return 0
	} else {
		number, err := strconv.Atoi(data)
		if err != nil {
			return 0
		}
		return number
	}
}

func SetGoldData(userId string, tm string, data int) bool {
	rds.Redis.HSet(fmt.Sprintf(constant.GoldBrick, tm), userId, fmt.Sprintf("%v", data))
	end, _ := time.Parse("2006-01-02", tm)
	expireAt := end.Add(constant.ADVERTISETIMEOUT)
	log.Printf("---更新领取兑换卷过期时间: expireAt:%v \n", expireAt)
	rds.Redis.ExpireAt(fmt.Sprintf(constant.GoldBrick, tm), expireAt)
	recordTime = tm

	return true
}
