reward-user.go
4.25 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package models
import (
"common"
"database/sql"
"errors"
"time"
db "client/icesimba.mail/database"
)
/*礼物配置表和用户表关系表*/
type RewardUser struct {
RUId string
UserId string //用户Id
RewardId string //礼品发放配置Id
IsPost int64 //是否发放
IsGet int64 //是否领取
StartTime int64
EndTime int64
Title string
Content string
}
type RUDB struct {
RUId sql.NullString
UserId sql.NullString //用户Id
RewardId sql.NullString //礼品发放配置Id
IsPost sql.NullInt64 //是否发放
IsGet sql.NullInt64 //是否领取
StartTime sql.NullInt64
EndTime sql.NullInt64
Title sql.NullString
Content sql.NullString
}
func (in *RewardUser) IsUserHasReward() bool {
currTime := time.Now().UnixNano() / 1e6
sqlQue := "select ruid from ice_reward_user" +
" where" +
" userid=? AND" +
" STARTTIME < ? AND" +
" ENDTIME > ? AND ISGET=0 limit 1"
var ruid string
err := db.SqlDB.QueryRow(sqlQue, in.UserId, currTime, currTime).Scan(
&ruid,
)
if err == nil && ruid != "" {
return true
}
return false
}
/*通过userId查询rewar_user表,获取rewardId的数组*/
func (in *RewardUser) QueryRUByUserId() (out []RewardUser, err error) {
currTime := time.Now().UnixNano() / 1e6
sqlQue := "select ruid,rewardid,starttime,title,content from ice_reward_user" +
" where" +
" userid=? AND" +
" STARTTIME < ? AND" +
" ENDTIME > ? AND ISGET=0"
rows, err := db.SqlDB.Query(sqlQue, in.UserId, currTime, currTime)
if err != nil {
return
}
for rows.Next() {
userReward := RewardUser{}
rudb := RUDB{}
err = rows.Scan(
&rudb.RUId,
&rudb.RewardId,
&rudb.StartTime,
&rudb.Title,
&rudb.Content,
)
userReward.UserId = in.UserId
if rudb.RUId.Valid {
userReward.RUId = rudb.RUId.String
}
if rudb.RewardId.Valid {
userReward.RewardId = rudb.RewardId.String
}
if rudb.StartTime.Valid {
userReward.StartTime = rudb.StartTime.Int64
}
if rudb.Title.Valid {
userReward.Title = rudb.Title.String
}
if rudb.Content.Valid {
userReward.Content = rudb.Content.String
}
out = append(out, userReward)
if err != nil {
common.Println(err)
continue
}
}
if err != nil {
common.Println(err)
return
}
sqlQue = "update ice_reward_user set ISREAD=1 where USERID=? and ISREAD=0"
_, err = db.SqlDB.Exec(sqlQue, in.UserId)
if err != nil {
return
}
return
}
func (in *RewardUser) InsertRewardUser() (err error) {
ruId := common.GetUuid()
sqlIn := "insert into ice_reward_user(ruid,userid,rewardid,isget,starttime,endtime) VALUES (?, ?, ?, ?, ?, ?)"
_, err = db.SqlDB.Exec(
sqlIn,
ruId,
in.UserId,
in.RewardId,
in.IsGet,
in.StartTime,
in.EndTime,
)
if err != nil {
return
}
return
}
func (in *RewardUser) GainRewardUser() (out Props, err error) {
if in.RewardId == "" {
err = errors.New("奖励不存在")
return
}
reprop := RewardProp{
RewardId: in.RewardId,
}
out, err = reprop.QueryRewardProp()
sqlUp := "update ice_reward_user set isget=? where userid=? and ruid=?"
stmt, err := db.SqlDB.Prepare(sqlUp)
defer stmt.Close()
_, err = stmt.Exec(
in.IsGet,
in.UserId,
in.RUId,
)
if err != nil {
return
}
return
}
func (in *RewardUser) IsRUExist() bool {
sqlQue := "select ruid,starttime,endtime from ice_reward_user where userid=? and rewardid=?"
var (
ruid string
start int64
end int64
)
err := db.SqlDB.QueryRow(
sqlQue,
in.UserId,
in.RewardId,
).Scan(
&ruid,
&start,
&end,
)
curr := time.Now().UnixNano() / 1e6
if err != nil || start > curr || curr > end {
return false
}
return true
}
func (in *RewardUser) QueryRUByRUId() (out RewardUser, err error) {
sqlQue := "select rewardid,isget,starttime,endtime from ice_reward_user where userid=? and ruid=?"
row := db.SqlDB.QueryRow(
sqlQue,
in.UserId,
in.RUId,
)
rudb := RUDB{}
err = row.Scan(
&rudb.RewardId,
&rudb.IsGet,
&rudb.StartTime,
&rudb.EndTime,
)
if err != nil {
err = errors.New("没有奖励")
return
}
out.UserId = in.UserId
out.RUId = in.RUId
if rudb.RewardId.Valid {
out.RewardId = rudb.RewardId.String
}
if rudb.IsGet.Valid {
out.IsGet = rudb.IsGet.Int64
}
if rudb.StartTime.Valid {
out.StartTime = rudb.StartTime.Int64
}
if rudb.EndTime.Valid {
out.EndTime = rudb.EndTime.Int64
}
return
}