package wxbroadcastv2

import (
	"encoding/json"
	"fmt"
	"git.jiaxianghudong.com/webs/pkg/rds"
	"time"
)

type UpListConf struct {
	UpawardList []UpAward `json:"upaward_list" yaml:"upaward_list"`
}

type UpAward struct {
	Index      int64   `json:"index" yaml:"index"`
	Number     int64   `json:"number" yaml:"number"`
	Bean       int64   `json:"bean" yaml:"bean"`
	IsTreasure int     `json:"is_treasure" yaml:"is_treasure"`
	Rate       []int   `json:"-" yaml:"rate"`
	BeanList   [][]int `json:"-" yaml:"bean_list"`
	Status     int     `json:"status" yaml:"status"`
}

type UpListAward struct {
	UserId  int64        `json:"-"`
	Tm      time.Time    `json:"-"`
	Up      map[int]bool `json:"up"`
	Receive map[int]bool `json:"receive"`
}

func (this *UpListAward) Store() error {
	marshal, _ := json.Marshal(this)
	err := rds.Redis.HSet(fmt.Sprintf(WXBROADCASTUP, this.Tm.Format("2006-01-02")), fmt.Sprintf("%v", this.UserId), string(marshal)).Err()
	rds.Redis.Expire(fmt.Sprintf(WXBROADCASTUP, this.Tm.Format("2006-01-02")), 25*time.Hour)
	return err
}

func (this *UpListAward) Load() error {
	val := rds.Redis.HGet(fmt.Sprintf(WXBROADCASTUP, this.Tm.Format("2006-01-02")), fmt.Sprintf("%v", this.UserId)).Val()
	if val != "" {
		json.Unmarshal([]byte(val), this)
	}
	if this.Up == nil {
		this.Up = make(map[int]bool)
	}
	if this.Receive == nil {
		this.Receive = make(map[int]bool)
	}
	return nil
}
