优惠券
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: '获取优惠券失败' });
|
||||
}
|
||||
});
|
||||
|
||||
@@ -919,41 +919,112 @@ router.get('/:id/reviews', async (req, res) => {
|
||||
// 获取推荐商品
|
||||
router.get('/:id/recommended', async (req, res) => {
|
||||
try {
|
||||
// 获取商品id
|
||||
const id = parseInt(req.params.id);
|
||||
|
||||
// 获取同类别的其他商品作为推荐
|
||||
const query = `
|
||||
SELECT p2.id, p2.name, p2.price, p2.points_price as points,
|
||||
p2.stock, p2.image_url as image, p2.description
|
||||
FROM products p1
|
||||
WHERE p1.id = ? AND p2.id != ? AND p2.status = 'active'
|
||||
ORDER BY RAND()
|
||||
LIMIT 6
|
||||
|
||||
// 通过商品id获取该商品的分类id
|
||||
const categoryQuery = `
|
||||
SELECT category_id FROM products_category WHERE product_id = ?
|
||||
`;
|
||||
const [currentCategory] = await getDB().execute(categoryQuery, [id]);
|
||||
|
||||
const [recommendedProducts] = await getDB().execute(query, [id, id]);
|
||||
let parentId = null;
|
||||
let categoryIds = currentCategory.map(item => item.category_id);
|
||||
|
||||
// 如果同类别商品不足,补充其他热门商品
|
||||
if (recommendedProducts.length < 6) {
|
||||
const remainingCount = 6 - recommendedProducts.length;
|
||||
if (remainingCount > 0) {
|
||||
const additionalQuery = `
|
||||
if(currentCategory.length > 0 && currentCategory[0] !== null) {
|
||||
// 获取该商品的一级分类id
|
||||
const item = currentCategory[0];
|
||||
const levelQuery = `
|
||||
SELECT * FROM category WHERE id = ? AND level = 1
|
||||
`;
|
||||
const [fatherCategory] = await getDB().execute(levelQuery, [item.category_id]);
|
||||
|
||||
if(fatherCategory.length > 0) {
|
||||
// 该商品为一级分类商品,不存在二级分类
|
||||
parentId = fatherCategory[0].id;
|
||||
categoryIds.push(parentId);
|
||||
} else {
|
||||
// 获取该商品的二级分类id
|
||||
const secondlevelQuery = `
|
||||
SELECT * FROM category WHERE id = ? AND level = 2
|
||||
`;
|
||||
const [secondlevelCategory] = await getDB().execute(secondlevelQuery, [item.category_id]);
|
||||
if(secondlevelCategory.length > 0) {
|
||||
// 通过二级分类获取一级分类id
|
||||
parentId = secondlevelCategory[0].parent_id;
|
||||
categoryIds.push(parentId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// categoryIds目前存储该商品的一级分类id及其目录下所有二级分类的id
|
||||
const allSecondCategoryIdsQuery = `
|
||||
SELECT id FROM category WHERE parent_id = ? AND level = 2
|
||||
`;
|
||||
const [secondCategoryIds] = await getDB().execute(allSecondCategoryIdsQuery, [parentId]);
|
||||
categoryIds.push(...secondCategoryIds.map(item => item.id));
|
||||
|
||||
let recommendedProducts = [];
|
||||
|
||||
// 如果有分类ID,先获取同类商品
|
||||
if (categoryIds.length > 0) {
|
||||
const recommendId = `
|
||||
SELECT * FROM products_category WHERE category_id IN (${categoryIds.map(() => '?').join(',')})
|
||||
`;
|
||||
const [recommendProductIds] = await getDB().execute(recommendId, categoryIds);
|
||||
console.log(1234, recommendProductIds);
|
||||
|
||||
const filteredRecommendProductIds = [...new Set(recommendProductIds.map(item => item.product_id))];
|
||||
console.log(12345, filteredRecommendProductIds);
|
||||
|
||||
// 获取同类别的其他商品作为推荐
|
||||
if (filteredRecommendProductIds.length > 0) {
|
||||
const query = `
|
||||
SELECT id, name, price, points_price as points,
|
||||
stock, image_url as image, description
|
||||
FROM products
|
||||
WHERE id != ? AND status = 'active'
|
||||
ORDER BY RAND()
|
||||
LIMIT ${remainingCount}
|
||||
WHERE id IN (${filteredRecommendProductIds.map(() => '?').join(',')}) AND id != ?
|
||||
`;
|
||||
|
||||
const [additionalProducts] = await getDB().execute(
|
||||
additionalQuery,
|
||||
[id]
|
||||
);
|
||||
|
||||
recommendedProducts.push(...additionalProducts);
|
||||
const [categoryProducts] = await getDB().execute(query, [...filteredRecommendProductIds, id]);
|
||||
recommendedProducts = categoryProducts;
|
||||
}
|
||||
}
|
||||
|
||||
// 如果同类别商品不足,补充其他热门商品
|
||||
// if (recommendedProducts.length < 6) {
|
||||
// const remainingCount = 6 - recommendedProducts.length;
|
||||
// const additionalQuery = `
|
||||
// SELECT id, name, price, points_price as points,
|
||||
// stock, image_url as image, description
|
||||
// FROM products
|
||||
// WHERE id NOT IN (${filteredRecommendProductIds.map(() => '?').join(',')})
|
||||
// ORDER BY RAND()
|
||||
// LIMIT ?
|
||||
// `;
|
||||
// }
|
||||
// 如果同类别商品不足,补充其他热门商品
|
||||
// if (recommendedProducts.length < 6) {
|
||||
// const remainingCount = 6 - recommendedProducts.length;
|
||||
// console.log(11111,typeof remainingCount,remainingCount);
|
||||
// if (remainingCount > 0) {
|
||||
// const additionalQuery = `
|
||||
// SELECT id, name, price, points_price as points,
|
||||
// stock, image_url as image, description
|
||||
// FROM products
|
||||
// WHERE id != ?
|
||||
// ORDER BY RAND()
|
||||
// LIMIT ?
|
||||
// `;
|
||||
|
||||
// const [additionalProducts] = await getDB().execute(
|
||||
// additionalQuery,
|
||||
// [id, remainingCount] // 现在有两个占位符:id 和 limit
|
||||
// );
|
||||
|
||||
// recommendedProducts.push(...additionalProducts);
|
||||
// }
|
||||
// }
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
|
||||
Reference in New Issue
Block a user