coupon.js
6.29 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
/**
* 优惠券管理controller
* @author: leo <y.huang@yoho.cn>
* @date: 08/08/2018
*/
const _ = require('lodash');
const CouponModel = require('../models/coupon');
const moment = require('moment');
const INVALID_PARAMS = '参数错误';
const xlsx = require('xlsx');
const DO_SUCCESS = '操作成功';
const GET_SUCCESS = '获取成功';
const {excel_export} = require('../../../utils/excel');
let loadExcel = function(path) {
let workbook = xlsx.readFile(path);
const sheetNames = workbook.SheetNames;
const worksheet = workbook.Sheets[sheetNames[0]];
return Promise.resolve(xlsx.utils.sheet_to_json(worksheet));
};
const timeFormat = (time) => {
if (_.isNumber(time)) {
time = moment.unix(time);
}
return moment(time).format('YYYY-MM-DD HH:mm:ss');
};
const couponController = {
loadCouponNoList: async function(req, res, next) {
let file = req.files.up_excel;
let id = req.body.id;
res.setTimeout(1000 * 60 * 3 + 1000);
let data = await loadExcel(file.path);
if (!data.length) {
return res.json({
code: 201,
result: false,
message: '文件内容为空'
});
}
try {
// 通用券只允许导入一条数据
let coupon = await req.ctx(CouponModel).couponById(id);
if (coupon[0].type === 3) {
return res.json({
code: 204,
result: false,
message: '无码券不允许导入数据'
});
}
if (coupon[0].type === 1) {
if (data.length > 1) {
return res.json({
code: 202,
result: false,
message: '通用券只允许导入一条数据'
});
}
let count = await req.ctx(CouponModel).couponNoCid(id);
if (count[0].count) {
return res.json({
code: 203,
result: false,
message: '通用券已有数据'
});
}
}
await req.ctx(CouponModel).couponNoAdd({couponId: id, couponNos: data});
return res.json({
code: 200,
result: true,
message: DO_SUCCESS
});
} catch (e) {
next;
}
},
couponListPage(req, res) {
res.render('coupon/list', {
bodyClass: 'nav-md',
module: 'admin',
page: 'coupon'
});
},
async couponOptionPage(req, res) {
let result = await req.ctx(CouponModel).couponById(req.query.id);
if (result instanceof Array) {
result = result[0];
}
res.render('coupon/coupon-option', {
bodyClass: 'nav-md',
module: 'admin',
content: result,
page: 'coupon-option'
});
},
async couponList(req, res) {
const pageNo = req.query.pageNo || 1;
const pageSize = req.query.pageSize || 20;
const status = req.query.status;
let totalCount = await req.ctx(CouponModel).allCouponNum(status);
totalCount = totalCount[0].total;
let list = await req.ctx(CouponModel).couponList({pageNo, pageSize, status});
_.each(list, item => {
item.createTime = timeFormat(item.createTime);
});
res.json({
code: 200,
data: list,
pageNo: +pageNo,
pageSize: +pageSize,
totalCount,
totalPage: Math.ceil(totalCount / pageSize),
message: GET_SUCCESS
});
},
async modifyCoupon(req, res, next) {
try {
await req.ctx(CouponModel).couponModify(req.body);
res.json({
code: 200,
message: DO_SUCCESS
});
} catch (e) {
next;
}
},
downloadTpl(req, res) {
return excel_export({
title: ['couponNo'],
data: [['c3236233105'], ['c3236233105']],
fileName: 'couponNoTpl',
sheetName: '优惠券',
res
});
},
downloadNoList(req, res, next) {
const couponId = req.query.id;
res.setTimeout(1000 * 60 * 3 + 1000);
if (!couponId) {
return res.json({
code: 400,
message: INVALID_PARAMS
});
}
let conf = {
name: 'mysheet',
cols: ['券ID', '券码', '用户id', '领取时间'],
rows: []
};
req.ctx(CouponModel).couponNoListJoinUser(couponId)
.then(result => {
let temp = [];
_.each(result, item => {
temp = [];
temp.push(couponId);
temp.push(item.couponNo);
temp.push(item.userId);
if (item.createTime) {
temp.push(timeFormat(item.createTime));
} else {
temp.push('');
}
conf.rows.push(temp);
});
return excel_export({
title: conf.cols,
data: conf.rows,
fileName: 'couponList',
sheetName: conf.name,
res
});
})
.catch(next);
},
deleteCoupon: async function(req, res) {
let id = req.query.id;
// 查询有没有用户表里有没,如有返回不给删
let userData = await req.ctx(CouponModel).couponUserListByCId(id);
if (userData.length) {
return res.json({
code: 201,
result: false,
message: '删除失败,有用户数据'
});
}
// 查询查no表里并且删除
await req.ctx(CouponModel).deleteCouponNoByCId(id);
// 删除主表
await req.ctx(CouponModel).deleteCouponById(id);
return res.json({
code: 200,
result: true,
message: DO_SUCCESS
});
}
};
module.exports = couponController;