rewards.go
3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
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
}