rewards.go 4.21 KB
package apis

import (
	"log"
	"net/http"
	"strconv"

	. "client/icesimba.mail/models"
	"github.com/gin-gonic/gin"
)

var (
	code string
)

/*
 *	刷新用户的每日奖励和假期奖励
 */
func RefreshRewardsApi(c *gin.Context) {
	gameId := c.Request.FormValue("game_id")
	userId := c.Request.FormValue("user_id")
	platform := c.Request.FormValue("platform")
	version := c.Request.FormValue("versionCode")
	versionCode, err := strconv.Atoi(version)
	if gameId == "" || userId == "" || platform == "" || err != nil {
		code = "1003"
		c.JSON(http.StatusOK, gin.H{
			"code": code,
			"msg":  "参数错误",
		})
		return
	}
	rewardIn := RewardIn{
		GameId:      gameId,
		UserId:      userId,
		Platform:    platform,
		VersionCode: int64(versionCode),
	}
	rewardIn.InsertRewardUser()
	code = "0"
	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"obj":  nil,
	})
	return
}

/*
 *	用户是否有未领取的奖励
 */
func IsRewardExistApi(c *gin.Context) {
	gameId := c.Request.FormValue("game_id")
	userId := c.Request.FormValue("user_id")
	platform := c.Request.FormValue("platform")
	version := c.Request.FormValue("versionCode")
	versionCode, err := strconv.Atoi(version)
	if gameId == "" || userId == "" || platform == "" || err != nil {
		code = "1003"
		c.JSON(http.StatusOK, gin.H{
			"code": code,
			"msg":  "参数错误",
		})
		return
	}
	rewardIn := RewardIn{
		GameId:      gameId,
		UserId:      userId,
		Platform:    platform,
		VersionCode: int64(versionCode),
	}
	isHas := rewardIn.IsRewradsExist()
	if isHas {
		code = "0"
		c.JSON(http.StatusOK, gin.H{
			"code": code,
			"obj":  nil,
		})
	} else {
		code = "1000"
		c.JSON(http.StatusOK, gin.H{
			"code": code,
			"msg":  "没有奖励",
		})
		return
	}
	return
}

/*
 *	查询用户奖励列表
 */
func QueryRewardsApi(c *gin.Context) {
	gameId := c.Request.FormValue("game_id")
	userId := c.Request.FormValue("user_id")
	platform := c.Request.FormValue("platform")
	version := c.Request.FormValue("versionCode")
	versionCode, err := strconv.Atoi(version)
	if gameId == "" || userId == "" || platform == "" || err != nil {
		code = "1003"
		c.JSON(http.StatusOK, gin.H{
			"code": code,
			"msg":  "参数错误",
		})
		return
	}
	rewardIn := RewardIn{
		GameId:      gameId,
		UserId:      userId,
		Platform:    platform,
		VersionCode: int64(versionCode),
	}
	rewardOut, err := rewardIn.QueryRewrads()

	if err != nil {
		code = "1000"
		c.JSON(http.StatusOK, gin.H{
			"code": code,
			"msg":  err.Error(),
		})
		return
	}
	code = "0"
	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"obj":  rewardOut,
	})
	return
}

/*
 *	用户领取奖励
 */
func GainRewardsApi(c *gin.Context) {
	rewardId := c.Request.FormValue("reward_id")
	userId := c.Request.FormValue("user_id")
	if rewardId == "" || userId == "" {
		code = "1003"
		c.JSON(http.StatusOK, gin.H{
			"code": code,
			"msg":  "参数错误",
		})
		return
	}
	rewardIn := GainRewardIn{
		RUId:   rewardId,
		UserId: userId,
	}
	props, err := rewardIn.GainRewrads()
	if err != nil {
		code = "1000"
		c.JSON(http.StatusOK, gin.H{
			"code": code,
			"msg":  err.Error(),
		})
		return
	}
	code = "0"
	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"obj":  props,
	})
	return
}

func CheckRewardsApi(c *gin.Context) {
	gameId := c.Request.FormValue("game_id")
	platform := c.Request.FormValue("platform")
	version := c.Request.FormValue("versionCode")
	userId := c.Request.FormValue("user_id")
	funcType := c.Request.FormValue("ischange")
	versionCode, err := strconv.Atoi(version)
	if userId == "" || (funcType != "false" && funcType != "true") || gameId == "" || version == "" || platform == "" {
		code = "1003"
		c.JSON(http.StatusOK, gin.H{
			"code": code,
			"msg":  "参数错误",
		})
		return
	}
	rewardIn := RewardIn{
		GameId:      gameId,
		UserId:      userId,
		Platform:    platform,
		VersionCode: int64(versionCode),
	}
	log.Println(rewardIn)
	result, err, Rewardout := CheckRewards(rewardIn, funcType)
	if err != nil {
		code = "1000"
		c.JSON(http.StatusOK, gin.H{
			"code": code,
			"msg":  err.Error(),
		})
		return
	}
	code = "0"
	if result == "true" {
		c.JSON(http.StatusOK, gin.H{
			"code": code,
			"obj":  Rewardout,
		})
		return
	}
	code = "1000"
	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  "没有新奖励",
	})
	return
}