优惠券
This commit is contained in:
@@ -5,15 +5,28 @@ const { getDB } = require('../database');
|
||||
|
||||
router.get('/', async (req, res) => {
|
||||
try {
|
||||
const useId = req.query.user_id;
|
||||
const db = getDB();
|
||||
const query = `
|
||||
SELECT * FROM coupon_products where remain > 0
|
||||
SELECT * FROM coupon_products
|
||||
`
|
||||
const [coupon] = await db.query(query);
|
||||
|
||||
if (coupon.length === 0) {
|
||||
res.json({ error: '暂无优惠券' });
|
||||
}
|
||||
res.json({ message: '暂无优惠券' });
|
||||
}
|
||||
|
||||
for(const item of coupon) {
|
||||
const query = `
|
||||
SELECT * FROM coupon_use WHERE user_id = ? AND coupon_id = ?
|
||||
`
|
||||
const [couponExist] = await db.query(query, [useId, item.id]);
|
||||
if (couponExist.length === 0) {
|
||||
item.got = false;
|
||||
} else {
|
||||
item.got = true;
|
||||
}
|
||||
}
|
||||
|
||||
res.json({coupon, success: true});
|
||||
} catch (error) {
|
||||
@@ -22,37 +35,35 @@ router.get('/', async (req, res) => {
|
||||
});
|
||||
router.get('/:id', async (req, res) => {
|
||||
try {
|
||||
const userId = req.params.id;
|
||||
const couponId = req.body.coupon_id;
|
||||
const db = getDB();
|
||||
const query = `
|
||||
SELECT * FROM coupon_products where remain > 0
|
||||
`
|
||||
const [coupon] = await db.query(query);
|
||||
|
||||
if (coupon.length === 0) {
|
||||
return res.status(404).json({ error: '暂无优惠券' });
|
||||
const userId = req.params.id;
|
||||
const receiveCouponId = req.query.coupon_id;
|
||||
|
||||
const query = `
|
||||
SELECT * FROM coupon_use WHERE user_id = ? AND coupon_id = ?
|
||||
`
|
||||
const [couponExist] = await db.query(query, [userId, receiveCouponId]);
|
||||
|
||||
if (couponExist.length === 0) {
|
||||
|
||||
const insertQuery = `
|
||||
INSERT INTO coupon_use (user_id, coupon_id, get_time) VALUES (?, ?, now())
|
||||
`
|
||||
await db.query(insertQuery, [userId, receiveCouponId]);
|
||||
|
||||
const updateQuery = `
|
||||
UPDATE coupon_products SET remain = remain - 1 WHERE id = ?
|
||||
`
|
||||
await db.query(updateQuery, [receiveCouponId]);
|
||||
|
||||
} else {
|
||||
const query = [`
|
||||
select * from coupon_use where user_id = ?
|
||||
`,`
|
||||
update coupon_products set remain = remain - 1 where id = ?
|
||||
`,`
|
||||
insert into coupon_use (user_id, coupon_id, get_time) values (?, ?, now())
|
||||
`]
|
||||
const [checkUser] = await db.query(query[0], [userId]);
|
||||
console.log("12345",checkUser)
|
||||
for (const item of checkUser) {
|
||||
if (item.coupon_id === coupon[0].id && item.use_time === null) {
|
||||
return res.status(400).json({ error: '优惠券已领取' });
|
||||
}
|
||||
}
|
||||
await db.query(query[1], [coupon[0].id]);
|
||||
await db.query(query[2], [userId, coupon[0].id]);
|
||||
return res.status(500).json({ error: '已有该优惠券' });
|
||||
}
|
||||
|
||||
res.json({coupon, success: true});
|
||||
res.json({success: true});
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
res.status(500).json({ error: '获取优惠券失败' });
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user