gift.go
4.01 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
package model
import (
"database/sql"
"errors"
"log"
"time"
)
type sqlExecutor interface {
Exec(query string, args ...interface{}) (sql.Result, error)
Query(query string, args ...interface{}) (*sql.Rows, error)
QueryRow(query string, args ...interface{}) *sql.Row
}
type GiftResults struct {
Gifts []GiftResult `json:"gifts"`
}
type GiftResult struct {
Code string `json:"id"`
Count int `json:"count"`
}
type PropResult struct {
PropID string
Count int
}
//1,参数:giftCode,userID,
//2,根据参数取出propid,count集合
//3,count置入结果集,propid用来取code
//4,code置入结果集,调用收尾
//5,收尾:user置入DB,并设置checkTime、status。
const (
checkTaskRepeatSQL = "SELECT task_id FROM ice_gift_code WHERE code = ? " +
" AND task_id IN" +
"(SELECT DISTINCT(task_id) FROM ice_gift_code" +
" WHERE user = ?" +
")"
propidSQL = "SELECT PROPID,COUNT FROM ice_reward_prop" +
" WHERE REWARDID = (select reward_id from ice_gift_task" +
" where task_id =(select task_id from ice_gift_code" +
" where STATUS ='2'" +
" and code= ?" +
")" +
" and ? BETWEEN start_time and end_time" +
")"
propCodeSQL = "select CODE from ice_prop where ID = ? and GAMEID = ?"
setCheckUserSQL = "UPDATE ice_gift_code set check_time=? ,user=? ,status=? WHERE code=?"
checkTimeSQL = "select start_time,end_time from ice_gift_task" +
" where task_id =(select task_id from ice_gift_code" +
" where code=?)"
checkStatusSQL = "select STATUS from ice_gift_code where code = ?"
checkedStatus = 3
)
//bool 为true则领过,为false则未领过。
func CheckTaskRepeat(db sqlExecutor, code, userId string) (bool, error) {
var taskId sql.NullString
err := db.QueryRow(checkTaskRepeatSQL, code, userId).Scan(&taskId)
if err==sql.ErrNoRows {
return false,nil
}
if err != nil {
return false, err
}
if taskId.String == "" {
return false, nil
}
return true, nil
}
func CheckNullReason(db sqlExecutor, giftCode string) error {
//1,判断status是不是2。分类:1,2,3
var status int
err := db.QueryRow(checkStatusSQL, giftCode).Scan(&status)
if err != nil {
return err
}
log.Println("status", status)
if status != 2 {
if status == 3 {
return errors.New(" 礼品已被领取")
}
if status == 1 {
return errors.New(" 礼品未发放")
}
return errors.New(" 礼品不存在")
}
//2,判断时间是超出了,还是不到
var starTime, endTime int64
ResErr := db.QueryRow(checkTimeSQL, giftCode).Scan(&starTime, &endTime)
if ResErr != nil {
return ResErr
}
nowTime := time.Now().Unix() * 1000
if nowTime < starTime {
return errors.New(" 未到礼包生效时间")
}
if nowTime > endTime {
return errors.New(" 已过礼包有效时间")
}
return nil
}
func GetGiftPropid(db sqlExecutor, giftCode string, gameId string) (GiftResults, error) {
giftResults := new(GiftResults)
propResult := &PropResult{}
giftResult := new(GiftResult)
var code string
rows, err := db.Query(propidSQL, giftCode, time.Now().Unix()*1000)
if err == sql.ErrNoRows {
return *giftResults, nil
}
if err != nil {
return *giftResults, errors.New(" " + err.Error())
}
defer rows.Close()
for rows.Next() {
rows.Scan(&propResult.PropID, &propResult.Count)
code, err = GetPropCodeByID(db, propResult.PropID, gameId)
if err != nil {
continue
}
giftResult.Code = code
giftResult.Count = propResult.Count
giftResults.Gifts = append(giftResults.Gifts, *giftResult)
}
return *giftResults, nil
}
func GetPropCodeByID(db sqlExecutor, propID string, gameID string) (string, error) {
var code string
err := db.QueryRow(propCodeSQL, propID, gameID).Scan(&code)
if err != nil {
log.Println("请检查gameID,获取propID时:" + err.Error())
}
return code, err
}
func SetCheckStatus(db sqlExecutor, userID string, giftCode string) (int64, error) {
result, err := db.Exec(setCheckUserSQL, time.Now().Unix()*1000, userID, checkedStatus, giftCode)
if err != nil {
return 0, err
}
rowCnt, err := result.RowsAffected()
if err != nil {
return 0, err
}
return rowCnt, nil
}