package broadcast

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

type ProblemInfo struct {
	ProblemId     string   `json:"problem_id"`
	Problem       string   `json:"problem"`
	AnswerList    string   `json:"answer_list"`
	Answer        []string `json:"answer"`
	Code          string   `json:"code"`
	EndTime       int64    `json:"end_time"`
	AttendLimit   int      `json:"attend_limit"`
	Bean          int      `json:"bean"`
	IsEnd         bool     `json:"is_end"`
	QuestionLimit int      `json:"question_limit"`
}

func (this *ProblemInfo) GetData(userId string) error {
	data := rds.Redis.Get(fmt.Sprintf(PROBLEM, userId)).Val()
	if data != "" {
		err := json.Unmarshal([]byte(data), this)
		//fmt.Println(data)
		if err != nil {
			logs.Errorf("broadcast GetData json err:%v", userId, err)
		}
		json.Unmarshal([]byte(this.AnswerList), &this.Answer)
	} else {
		return fmt.Errorf("不能为空")
	}
	return nil
}

func (this *ProblemInfo) SetData(userId string) {
	data, _ := json.Marshal(this)
	rds.Redis.Set(fmt.Sprintf(PROBLEM, userId), string(data), 24*time.Hour).Val()
}
