package v1

import (
	"context"
	"designs/app/common/request"
	"designs/config"
	"designs/global"
	"designs/response"
	"github.com/gin-gonic/gin"
)

func AddUserOption(c *gin.Context) {
	//验证
	form := request.Check(c, &struct {
		OpenId string `json:"openid" binding:"required"`
		Gid    string `json:"gid" binding:"required"`
		Pf     string `json:"pf" binding:"required"`
		Option string `json:"option" binding:"required"`
	}{})

	optionKey := config.Get("app.option_key") + form.Gid + ":" + form.Pf + ":" + form.OpenId

	data := map[string]interface{}{
		"option": form.Option,
	}

	err := global.App.Redis.HMSet(context.Background(), optionKey, data).Err()
	if err != nil {
		response.Fail(c, 1003, err.Error())
		return
	}

	response.Success(c, gin.H{})
}

func GetUserOption(c *gin.Context) {
	form := request.Check(c, &struct {
		OpenId string `json:"openid" binding:"required"`
		Gid    string `json:"gid" binding:"required"`
		Pf     string `json:"pf" binding:"required"`
	}{})

	optionKey := config.Get("app.option_key") + form.Gid + ":" + form.Pf + ":" + form.OpenId
	data, err := global.App.Redis.HGetAll(context.Background(), optionKey).Result()
	if err != nil {
		response.Fail(c, 1003, err.Error())
		return
	}

	response.Success(c, gin.H{
		"option": data["option"],
	})
}
