coupon.js
3.6 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
/* eslint-disable array-callback-return */
const mysqlCli = global.yoho.utils.mysqlCli;
const TABLE_COUPON = 'act_coupon';
const TABLE_COUPON_USER = 'act_coupon_user';
const TABLE_COUPON_NO = 'act_coupon_no';
class CouponModel extends global.yoho.BaseModel {
constructor(ctx) {
super(ctx);
}
couponList() {
return mysqlCli.query(
`select id, coupon_name couponName, coupon_desc couponDesc, shop_logo_url shopLogoUrl, sort ,type
from ${TABLE_COUPON} where status = 1
order by sort desc ,create_time desc`);
}
couponUserFlag(userId) {
return mysqlCli.query(`select cn.send_flag sendFlag, cu.coupon_id couponId
from ${TABLE_COUPON_USER} as cu ,${TABLE_COUPON_NO} as cn where cu.user_id = :userId and cn.id = cu.coupon_no_id `, {userId});
}
couponUserOwner(userId) {
return mysqlCli.query(
`select c.id, c.coupon_name couponName, c.coupon_desc couponDesc, c.shop_logo_url shopLogoUrl,cn.coupon_no couponNo
from ${TABLE_COUPON} c ,${TABLE_COUPON_NO} cn, ${TABLE_COUPON_USER} cu where cu.user_id =:userId and cu.coupon_no_id = cn.id and c.id = cn.coupon_id
order by cu.create_time desc`, {userId});
}
async coouponUserGet(obj) {
// 判断该用户是否有该券
let num = await mysqlCli.query(`select count(1) as count from ${TABLE_COUPON_USER} where user_id = :userId and coupon_id = :couponId`,
{
userId: obj.userId, couponId: obj.couponId
});
if (num[0].count) {
return Promise.resolve({code: 201, result: false, message: '领取失败,该用户已领取过该券'});
}
// 获取当前可用券码(如果是type=1、3则no表就一条数据send_flag都为0)
let couponNolist = await mysqlCli.query(`select id from ${TABLE_COUPON_NO} where coupon_id = :couponId and send_flag = 0 `, {couponId: obj.couponId});
if (!couponNolist.length) {
return Promise.resolve({code: 202, result: false, message: '领取失败,该券已发放完毕'});
}
let noId = couponNolist[0].id;
// 如果type=2(一人一码) 则修改第一张待用券码的send状态
if (obj.type === 2) {
await mysqlCli.update(`UPDATE ${TABLE_COUPON_NO} SET send_flag = 1 where id = :id`, {id: noId});
}
// 在type=2的情况下判断 该券并发是否被别人领取 如果没有再在user表中插入数据
let insertId = await mysqlCli.insert(`insert into ${TABLE_COUPON_USER} (user_id, coupon_id , coupon_no_id)
select :userId,:couponId,:couponNoId from DUAL where NOT EXISTS(
SELECT cu.id
FROM ${TABLE_COUPON_USER} cu left join ${TABLE_COUPON} c on c.id = cu.coupon_id
WHERE cu.coupon_no_id = :couponNoId and c.type = 2 and c.id = :couponId)`,
{userId: obj.userId, couponId: obj.couponId, couponNoId: noId});
if (!insertId) {
await this.coouponUserGet(obj);
} else {
return Promise.resolve({code: 200, result: true});
}
}
couponById(id) {
if (!id) {
return new Promise(resolve => {
return resolve({});
});
} else {
return mysqlCli.query(
`select id, coupon_name couponName, coupon_desc couponDesc, shop_name shopName, shop_logo_url shopLogoUrl, status, type, sort , create_time createTime
from ${TABLE_COUPON}
where id = :id`, {
id
});
}
}
}
module.exports = CouponModel;