package fivelucky

import (
	"active/constant"
	"encoding/json"
	"fmt"
	"git.jiaxianghudong.com/webs/pkg/rds"
	"github.com/go-redis/redis"
	"time"
)

type Record struct {
	Avatar   string `json:"avatar"`
	Nickname string `json:"nickname"`
	Userid   int64  `json:"userid"`
	Time     int64  `json:"time"`
	Wufu     int    `json:"wufu"`
}

// 日志存储记录
func (receiver Record) Store() {
	tm := time.Now()
	receiver.Time = tm.Unix()
	marshal, _ := json.Marshal(receiver)

	rds.Redis.ZAdd(constant.FiveBlessingsRecord, redis.Z{
		Score:  float64(tm.Unix()),
		Member: string(marshal),
	})
	rds.Redis.ZRemRangeByScore(constant.FiveBlessingsRecord,
		"0", fmt.Sprintf("%v", tm.AddDate(0, 0, -3).Unix()))
}

func GetRecord() (list []Record) {
	rdList := rds.Redis.ZRevRange(constant.FiveBlessingsRecord, 0, 30).Val()
	list = make([]Record, 0, len(rdList))
	fmt.Println(rdList)
	for _, s := range rdList {
		tmp := Record{}
		json.Unmarshal([]byte(s), &tmp)
		list = append(list, tmp)
	}
	return
}
