prize.js
6.91 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/**
* 抽奖model
* @author: qiujun <jun.qiu@yoho.cn>
* @date: 30/01/2018
*/
const mySqlCli = global.yoho.utils.mysqlCli;
const TABLE_PRIZE = 'act_prize_user_log';
const TABLE_USER_DETAIL = 'act_user_detail_info';
class PrizeModel extends global.yoho.BaseModel {
constructor(ctx) {
super(ctx);
}
/**
* 检查用户的获奖信息
* @param actId
* @param userPhone
*/
checkPrize(actId, userPhone) {
let strSql = `SELECT id, user_phone, is_get, user_name, user_address, user_shoes_size FROM ${TABLE_PRIZE}
WHERE act_id = :actId
AND user_phone = :userPhone`;
return mySqlCli.query(strSql, {
actId,
userPhone
}).then(ret => {
let result = {};
console.log(ret);
if (ret.length <= 0) {
result = {
code: 201,
message: '未找到获奖信息'
};
} else if (ret.length > 0) {
result = {
code: 200,
data: ret
};
}
return Promise.resolve(result);
});
}
/**
* 查询用户是否参与抽奖
* @param actId
* @param userPhone
* @returns {Promise.<*>}
*/
async checkUserPrize(actId, userPhone) {
let strFindSql = `SELECT COUNT(*) AS user_prize_count FROM ${TABLE_PRIZE}
WHERE act_id = :actId
AND user_phone = :userPhone;`;
return mySqlCli.query(strFindSql, {
actId,
userPhone
}).then(ret => {
return ret[0].user_prize_count || 0;
});
}
/**
* 查看已中奖的奖品数量
* @param actId
* @param isGet
* @returns {Promise.<*>}
*/
async checkPrizeNums(actId, isGet) {
let strSql = `SELECT COUNT(*) AS total_prize_count FROM ${TABLE_PRIZE}
WHERE act_id = :actId
AND is_get = :isGet;`;
return mySqlCli.query(strSql, {
actId,
isGet
}).then(ret => {
return ret[0].total_prize_count || 0;
});
}
/**
* 插入新的中奖信息
* @param actId
* @param userPhone
* @param isGet
* @returns {Promise.<*>}
*/
async addNewPrize(actId, userPhone, isGet) {
let strInsertSql = `INSERT INTO ${TABLE_PRIZE} (act_id, user_phone, is_get)
VALUES(:actId, :userPhone, :isGet)`;
return mySqlCli.insert(strInsertSql, {
actId,
userPhone,
isGet
});
}
/**
* 检查并添加中奖者基础信息
* @param actId
* @param userPhone
* @param isGet
*/
addPrize(actId, userPhone, isGet) {
let strFindSql = `SELECT COUNT(*) as user_prize_count FROM ${TABLE_PRIZE}
WHERE act_id = :actId
AND user_phone = :userPhone`;
let strCountSql = `SELECT COUNT(*) AS total_prize_count FROM ${TABLE_PRIZE}
WHERE act_id = :actId AND is_get = 1`;
let strInsertSql = `INSERT INTO ${TABLE_PRIZE} (act_id, user_phone, is_get)
VALUES(:actId, :userPhone, :isGet)`;
return mySqlCli.query(strFindSql, {
actId,
userPhone
}).then(ret => {
console.log(ret);
if (ret[0] && ret[0].user_prize_count <= 0) {
return mySqlCli.query(strCountSql, {
actId
}).then(ret2 => {
console.log(ret2);
let result = {};
if (ret2[0].total_prize_count >= 20) {
isGet = 0;
result = {
code: 204,
message: '奖品已领取完'
};
} else {
result = {
code: 200,
message: '抽奖信息添加成功!'
};
}
return mySqlCli.insert(strInsertSql, {
actId,
userPhone,
isGet
}).then(ret3 => {
return Promise.resolve(Object.assign(result, {data: ret3}));
});
});
} else {
return Promise.resolve({
code: 203,
message: '请勿重复领奖'
});
}
});
}
/**
* 更新中奖者的详细信息
* @param actId
* @param userPhone
* @param name
* @param address
* @param shoesSize
*/
updatePrize(actId, userPhone, name, address, shoesSize) {
let strSql = `UPDATE ${TABLE_PRIZE} SET
user_name=:name,
user_address=:address,
user_shoes_size=:shoesSize
WHERE user_phone=:userPhone
AND is_get > 0
AND act_id=:actId`;
return mySqlCli.update(strSql, {
actId,
userPhone,
name,
address,
shoesSize
}).then(ret => {
return Promise.resolve({
code: 200,
data: ret
});
});
}
saveUserInfo(actId, name, birthday, gender, phone, email, province, city) {
let strFind = `SELECT COUNT(*) AS user_count
FROM ${TABLE_USER_DETAIL}
WHERE act_id=:actId
AND user_phone=:phone`;
let strInsert = `INSERT INTO ${TABLE_USER_DETAIL}
(act_id, user_name, user_birthday, user_gender,
user_phone, user_email, user_province, user_city)
VALUES (:actId, :name, :birthday, :gender, :phone, :email, :province, :city)`;
return mySqlCli.query(strFind, {
actId,
phone
}).then(ret => {
if (ret && ret[0].user_count <= 0) {
return mySqlCli.insert(strInsert, {
actId,
name,
birthday,
gender,
phone,
email,
province,
city
}).then(ret2 => {
return Promise.resolve({
code: 200,
message: '用户信息添加成功',
data: ret2
});
});
} else {
return Promise.resolve({
code: 203,
message: '已存在的用户信息'
});
}
});
}
}
module.exports = PrizeModel;