| 
									
										
										
										
											2025-10-09 17:26:34 +08:00
										 |  |  |  | const express = require('express'); | 
					
						
							|  |  |  |  | const router = express.Router(); | 
					
						
							|  |  |  |  | const { getDB } = require('../database'); | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-10-17 17:26:37 +08:00
										 |  |  |  | /** | 
					
						
							|  |  |  |  |  * @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: 服务器内部错误 | 
					
						
							|  |  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2025-10-11 17:33:00 +08:00
										 |  |  |  | router.get('/', async (req, res) => { | 
					
						
							| 
									
										
										
										
											2025-10-09 17:26:34 +08:00
										 |  |  |  |   try { | 
					
						
							| 
									
										
										
										
											2025-10-13 17:28:29 +08:00
										 |  |  |  |     const useId = req.query.user_id; | 
					
						
							| 
									
										
										
										
											2025-10-09 17:26:34 +08:00
										 |  |  |  |     const db = getDB(); | 
					
						
							|  |  |  |  |     const query = `
 | 
					
						
							| 
									
										
										
										
											2025-10-13 17:28:29 +08:00
										 |  |  |  |       SELECT * FROM coupon_products | 
					
						
							| 
									
										
										
										
											2025-10-09 17:26:34 +08:00
										 |  |  |  |     `
 | 
					
						
							|  |  |  |  |     const [coupon] = await db.query(query); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     if (coupon.length === 0) { | 
					
						
							| 
									
										
										
										
											2025-10-13 17:28:29 +08:00
										 |  |  |  |       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; | 
					
						
							|  |  |  |  |       } | 
					
						
							|  |  |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-10-09 17:26:34 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     res.json({coupon, success: true}); | 
					
						
							|  |  |  |  |   } catch (error) { | 
					
						
							|  |  |  |  |     res.status(500).json({ error: '获取优惠券失败' }); | 
					
						
							|  |  |  |  |   } | 
					
						
							|  |  |  |  | }); | 
					
						
							| 
									
										
										
										
											2025-10-17 17:26:37 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | /** | 
					
						
							|  |  |  |  |  * @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: 服务器内部错误 | 
					
						
							|  |  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2025-10-11 17:33:00 +08:00
										 |  |  |  | router.get('/:id', async (req, res) => { | 
					
						
							| 
									
										
										
										
											2025-10-09 17:26:34 +08:00
										 |  |  |  |   try { | 
					
						
							|  |  |  |  |     const db = getDB(); | 
					
						
							| 
									
										
										
										
											2025-10-13 17:28:29 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     const userId = req.params.id; | 
					
						
							|  |  |  |  |     const receiveCouponId = req.query.coupon_id; | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-10-09 17:26:34 +08:00
										 |  |  |  |     const query = `
 | 
					
						
							| 
									
										
										
										
											2025-10-13 17:28:29 +08:00
										 |  |  |  |       SELECT * FROM coupon_use WHERE user_id = ? AND coupon_id = ? | 
					
						
							| 
									
										
										
										
											2025-10-09 17:26:34 +08:00
										 |  |  |  |     `
 | 
					
						
							| 
									
										
										
										
											2025-10-13 17:28:29 +08:00
										 |  |  |  |     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]); | 
					
						
							| 
									
										
										
										
											2025-10-09 17:26:34 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2025-10-13 17:28:29 +08:00
										 |  |  |  |       return res.status(500).json({ error: '已有该优惠券' }); | 
					
						
							| 
									
										
										
										
											2025-10-09 17:26:34 +08:00
										 |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-10-13 17:28:29 +08:00
										 |  |  |  |     res.json({success: true}); | 
					
						
							| 
									
										
										
										
											2025-10-09 17:26:34 +08:00
										 |  |  |  |   } catch (error) { | 
					
						
							| 
									
										
										
										
											2025-10-13 17:28:29 +08:00
										 |  |  |  |     console.log(error); | 
					
						
							| 
									
										
										
										
											2025-10-09 17:26:34 +08:00
										 |  |  |  |     res.status(500).json({ error: '获取优惠券失败' }); | 
					
						
							|  |  |  |  |   } | 
					
						
							|  |  |  |  | }); | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-10-17 17:26:37 +08:00
										 |  |  |  | /** | 
					
						
							|  |  |  |  |  * @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: 服务器内部错误 | 
					
						
							|  |  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2025-10-14 16:25:16 +08:00
										 |  |  |  | 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: '加载优惠券失败' }); | 
					
						
							|  |  |  |  |   } | 
					
						
							|  |  |  |  | }) | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-10-09 17:26:34 +08:00
										 |  |  |  | module.exports = router; |