const express = require('express'); const router = express.Router(); const { getDB } = require('../database'); /** * @swagger * /api/coupon: * get: * summary: 获取用户优惠券 * description: 返回用户所有优惠券,包含是否已领取状态 * tags: [coupon] * parameters: * - in: query * name: user_id * schema: * type: string * required: true * description: 用户ID * responses: * 200: * description: 成功返回用户优惠券 * content: * application/json: * schema: * type: object * properties: * success: * type: boolean * example: true * data: * type: array * items: * type: object * properties: * id: * type: string * example: "1" * name: * type: string * example: "满减优惠券" * discount: * type: number * example: 100 * remain: * type: number * example: 10 * got: * type: boolean * example: false * description: 是否已领取 * * 400: * description: 缺少用户ID参数 * 500: * description: 服务器内部错误 */ router.get('/', async (req, res) => { try { const useId = req.query.user_id; const db = getDB(); const query = ` SELECT * FROM coupon_products ` const [coupon] = await db.query(query); if (coupon.length === 0) { 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) { res.status(500).json({ error: '获取优惠券失败' }); } }); /** * @swagger * /api/coupon/{id}: * get: * summary: 用户领取优惠券 * description: 用户通过优惠券ID领取优惠券,优惠券数量减一 * tags: [coupon] * parameters: * - in: path * name: id * schema: * type: string * required: true * description: 用户ID * - in: query * name: coupon_id * schema: * type: string * required: true * description: 优惠券ID * responses: * 200: * description: 成功领取优惠券 * content: * application/json: * schema: * type: object * properties: * success: * type: boolean * example: true * 400: * description: 缺少用户ID或优惠券ID参数 * 500: * description: 服务器内部错误 */ router.get('/:id', async (req, res) => { try { const db = getDB(); 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 { return res.status(500).json({ error: '已有该优惠券' }); } res.json({success: true}); } catch (error) { console.log(error); res.status(500).json({ error: '获取优惠券失败' }); } }); /** * @swagger * /api/coupon/user/{id}: * get: * summary: 获取用户优惠券 * description: 返回用户领取的优惠券,包括优惠券信息和商品信息 * tags: [coupon] * parameters: * - in: path * name: id * schema: * type: string * required: true * description: 用户ID * responses: * 200: * description: 成功返回用户优惠券 * content: * application/json: * schema: * type: object * properties: * success: * type: boolean * example: true * data: * type: array * items: * type: object * properties: * id: * type: string * example: "1" * name: * type: string * example: "满减优惠券" * discount: * type: number * example: 100 * products_id: * type: array * items: * type: string * example: "1" * remain: * type: number * example: 100 * get_time: * type: string * format: date-time * example: "2023-01-01T00:00:00.000Z" * 400: * description: 缺少用户ID参数 * 500: * description: 服务器内部错误 */ router.get('/user/:id', async (req, res) => { try { const db = getDB(); const userId = req.params.id; const query = ` SELECT * FROM coupon_use WHERE user_id = ? ` const [coupon] = await db.query(query, [userId]); for (const item of coupon) { const query = ` SELECT * FROM coupon_products WHERE id = ? ` const [couponInfo] = await db.query(query, [item.coupon_id]); item.couponInfo = couponInfo[0]; item.couponInfo.products = []; for (const product of item.couponInfo.products_id) { const query = ` SELECT name FROM products WHERE id = ? ` const [productInfo] = await db.query(query, [product]); item.couponInfo.products.push(productInfo[0]); } } res.json({success: true, coupon}); } catch (error) { console.log(error); res.status(500).json({ error: '加载优惠券失败' }); } }) module.exports = router;