coupon.js
4.98 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
/* eslint-disable array-callback-return */
const mysqlCli = global.yoho.utils.mysqlCli;
const _ = require('lodash');
const TABLE_COUPON = 'act_coupon';
const TABLE_COUPON_NO = 'act_coupon_no';
const TABLE_COUPON_USER = 'act_coupon_user';
const toLine = (str) => {
return str.replace(/([A-Z])/g, '_$1').toLowerCase();
};
class CouponModel extends global.yoho.BaseModel {
constructor(ctx) {
super(ctx);
}
couponList({pageNo, pageSize, status}) {
let str = '';
if (status === '0' || status === '1') {
str = `where status = ${status}`;
}
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} ${str}
order by sort desc ,create_time desc
limit :start, :page;`, {
start: (pageNo - 1) * pageSize,
page: _.parseInt(pageSize)
});
}
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
});
}
}
couponNoAdd(obj) {
let sql = `insert into ${TABLE_COUPON_NO} (coupon_id, coupon_no) values `;
let arr = obj.couponNos;
arr.map((value, index) => {
sql += '(' + obj.couponId + ',"' + value.couponNo + '")';
if (index !== (arr.length - 1)) {
sql += ',';
}
});
return mysqlCli.insert(sql);
}
async couponModify(obj) {
if (obj.id) {
let sql = `UPDATE ${TABLE_COUPON} SET `;
let sqlParam = {};
let keyArr = Object.keys(obj);
keyArr.map((value, index) => {
if (value !== 'id') {
let col = toLine(value);
sql += col + ' = :' + value;
if (index !== (keyArr.length - 1)) {
sql += ',';
}
}
sqlParam[value] = obj[value];
});
sql += ' WHERE id = :id';
return mysqlCli.update(
sql,
sqlParam
);
} else {
let insertId = await mysqlCli.insert(
`insert into ${TABLE_COUPON} (coupon_name, coupon_desc, shop_name, shop_logo_url, status,type,sort)
values (:couponName, :couponDesc, :shopName, :shopLogoUrl, :status, :type, :sort);`,
{
couponName: obj.couponName,
couponDesc: obj.couponDesc,
shopName: obj.shopName,
shopLogoUrl: obj.shopLogoUrl,
status: obj.status,
type: obj.type,
sort: obj.sort
});
if (parseInt(obj.type) === 3) {
let id = await mysqlCli.insert(
`insert into ${TABLE_COUPON_NO} (coupon_id) values (:couponId);`,
{
couponId: insertId,
});
if (!id) {
return Promise.resolve({code: 204, result: false, message: '插入无码券失败'});
}
}
return Promise.resolve({code: 200, result: true});
}
}
allCouponNum(status) {
let str = '';
if (status === '0' || status === '1') {
str = `where status = ${status}`;
}
return mysqlCli.query(
`select count(1) as total from ${TABLE_COUPON} ${str};`
);
}
couponNoCid(couponId) {
return mysqlCli.query(`select count(1) as count from ${TABLE_COUPON_NO} where coupon_id = :couponId `, {couponId});
}
couponNoListJoinUser(couponId) {
return mysqlCli.query(
`select cn.coupon_id couponId,cn.coupon_no couponNo,cn.send_flag sendFlag,cu.user_id userId,cu.create_time createTime
from ${TABLE_COUPON_NO} as cn left join ${TABLE_COUPON_USER} as cu
on cu.coupon_no_id = cn.id where cn.coupon_id = :couponId`, {couponId});
}
couponUserListByCId(couponId) {
return mysqlCli.query(
`select id, coupon_id couponId,coupon_no_id couponNoId,user_id userId,create_time createTime
from ${TABLE_COUPON_USER}
where coupon_id = :couponId `, {couponId: couponId});
}
deleteCouponNoByCId(couponId) {
return mysqlCli.query(`DELETE FROM ${TABLE_COUPON_NO} WHERE coupon_id = :couponId `, {couponId});
}
deleteCouponById(id) {
return mysqlCli.query(`DELETE FROM ${TABLE_COUPON} WHERE id = :id `, {id});
}
}
module.exports = CouponModel;