package activitymanage

import (
	"errors"
	"github.com/gin-gonic/gin"
	"go-admin/app/admin/models"
	"go-admin/logger"
	"go-admin/tools/app"
	"net/http"
)

func RedeemCodeList(c *gin.Context) {
	req := &models.RedeemCodeListReq{}
	if c.ShouldBindJSON(req) != nil {
		logger.Errorf("para err")
		app.Error(c, http.StatusBadRequest, errors.New("para err"), "参数错误")
		return
	}

	codeList, err := req.RedeemCodeStockList()
	if err != nil {
		logger.Errorf("err:", err)
		app.Error(c, http.StatusInternalServerError, err, "查询失败")
		return
	}

	app.OK(c, codeList, "修改成功")
}

func UserRedeemCodeList(c *gin.Context) {
	req := &models.UserRedeemCodeListReq{}
	if c.ShouldBindJSON(req) != nil {
		logger.Errorf("para err")
		app.Error(c, http.StatusBadRequest, errors.New("para err"), "参数错误")
		return
	}

	codeList, err := req.UserSendRedeemCodeList()
	if err != nil {
		logger.Errorf("err:", err)
		app.Error(c, http.StatusInternalServerError, err, "查询失败")
		return
	}

	app.OK(c, codeList, "查询成功")
}

func RedeemCodeSendToUser(c *gin.Context) {
	req := &struct {
		Uid      uint32 `json:"uid"`
		CodeType string `json:"code_type"`
		StoreId  uint32 `json:"store_id"` // 门店id
	}{}
	if c.ShouldBindJSON(req) != nil {
		logger.Errorf("para err")
		app.Error(c, http.StatusBadRequest, errors.New("para err"), "参数错误")
		return
	}

	err := models.CodeSendToUser(req.Uid, req.StoreId, req.CodeType)
	if err != nil {
		logger.Errorf("err:", err)
		if err.Error() == "member_redeem_code_only" {
			app.Error(c, http.StatusInternalServerError, err, "会员兑换码只能推送一个")
		} else if err.Error() == "user_is_member" {
			app.Error(c, http.StatusInternalServerError, err, "用户已经是会员,不能推送会员兑换码")
		} else if err.Error() == "redeem_code_none" {
			app.Error(c, http.StatusInternalServerError, err, "兑换券已消耗完，请联系运营同事添加")
		} else {
			app.Error(c, http.StatusInternalServerError, err, "失败")
		}
		return
	}

	app.OK(c, nil, "成功")
}
