rewards.go 3.7 KB
package models

import (
	"errors"
	"fmt"
	"log"
	"time"
)

type GainRewardIn struct {
	RUId   string `json:"reward_id"`
	UserId string `json:"user_id"`
}

type RewardIn struct {
	GameId      string `json:"game_id"`
	UserId      string `json:"user_id"`
	Platform    string `json:"platform" `
	VersionCode int64  `json:"versionCode"`
}

type RewardOut struct {
	RewardJsons []RewardJson `json:"rewards"`
}

type RewardJson struct {
	RUId       string `json:"reward_id"`
	Title      string `json:"title"`
	Content    string `json:"content"`
	Icon       string `json:"icon"`
	Createtime int64  `json:"createtime"`
}

type Props struct {
	PropOut []PropJson `json:"props"`
}

type PropJson struct {
	Id    string `json:"id"`
	Count int64  `json:"count"`
}

func (in *RewardIn) IsRewradsExist() bool {
	in.InsertRewardUser()
	rewardUser := RewardUser{
		UserId: in.UserId,
	}
	return rewardUser.IsUserHasReward()
}

/*查询用户的奖励列表*/
func (in *RewardIn) QueryRewrads() (out RewardOut, err error) {
	in.InsertRewardUser()
	rewardUser := RewardUser{
		UserId: in.UserId,
	}
	userRewards, err := rewardUser.QueryRUByUserId() //返回奖励id数组
	if err != nil {
		return
	}
	for _, value := range userRewards {
		reward := Reward{
			Id: value.RewardId,
		}
		reward, icon, err := reward.QueryReward(in.GameId) //返回奖励具体内容
		if err != nil {
			continue
		}
		rewardJson := RewardJson{
			RUId:       value.RUId,
			Title:      reward.Title,
			Content:    reward.Content,
			Createtime: value.StartTime,
			Icon:       icon,
		}
		//如果为排行榜奖励,则替换title和content为固定格式
		if value.Title != "" {
			rewardJson.Title = value.Title
			rewardJson.Content = value.Content
			log.Println(value.Title, "排行榜奖励")
		}

		out.RewardJsons = append(out.RewardJsons, rewardJson)
	}
	if len(out.RewardJsons) <= 0 {
		err = errors.New("没有奖励")
	}
	return
}

func (in *RewardIn) InsertRewardUser() {
	log.Println("InsertRewardUser success")
	reward := Rewards{}
	//通过gameId查询ice_reward表,一个gameId可以对应多个ice_reward表
	reward, err := reward.QueryRewardByGameId(in.GameId, in.Platform, in.VersionCode)
	if err != nil {
		return
	}
	//遍历ice_reward表
	for _, value := range reward.GameReward {
		currTime := time.Now().UnixNano() / 1e6
		if value.TrigType == 0 {
			dur, _ := time.ParseDuration("24h")
			endDay, _ := time.Parse("2006-01-02 15:04:05", time.Now().Add(dur).Format("2006-01-02")+" 00:00:00")
			endTime := endDay.UnixNano() / 1e6
			fmt.Println("shijian..............", endDay, "------------"+time.Now().Add(dur).Format("2006-01-02")+"00:00:00")
			if currTime > endTime {
				continue
			}
			rewardUser := RewardUser{
				UserId:    in.UserId,
				RewardId:  value.Id,
				IsPost:    0,
				IsGet:     0,
				StartTime: currTime,
				EndTime:   endTime,
			}
			if !rewardUser.IsRUExist() {
				err = rewardUser.InsertRewardUser()
			}

		} else if value.TrigType == 1 {
			if currTime > value.EndTime {
				continue
			}
			rewardUser := RewardUser{
				UserId:    in.UserId,
				RewardId:  value.Id,
				IsPost:    0,
				IsGet:     0,
				StartTime: (time.Now().UnixNano() / 1e6),
				EndTime:   value.EndTime,
			}
			if !rewardUser.IsRUExist() {
				err = rewardUser.InsertRewardUser()
			}
		}
	}
}

func (in *GainRewardIn) GainRewrads() (props Props, err error) {
	currTime := time.Now().UnixNano() / 1e6
	rewardUser := RewardUser{
		RUId:   in.RUId,
		UserId: in.UserId,
	}
	rewardUser, err = rewardUser.QueryRUByRUId()
	if err != nil {
		return
	}
	if rewardUser.IsGet == 0 && rewardUser.StartTime < currTime && currTime < rewardUser.EndTime {
		rewardUser.IsGet = 1
		props, err = rewardUser.GainRewardUser()
	} else {
		err = errors.New("已领取或暂时无法领取")
	}
	return
}