更新推荐商品逻辑
This commit is contained in:
@@ -965,6 +965,7 @@ router.get('/:id/recommended', async (req, res) => {
|
||||
categoryIds.push(...secondCategoryIds.map(item => item.id));
|
||||
|
||||
let recommendedProducts = [];
|
||||
let filteredRecommendProductIds = [];
|
||||
|
||||
// 如果有分类ID,先获取同类商品
|
||||
if (categoryIds.length > 0) {
|
||||
@@ -974,8 +975,7 @@ router.get('/:id/recommended', async (req, res) => {
|
||||
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);
|
||||
filteredRecommendProductIds = [...new Set(recommendProductIds.map(item => item.product_id))];
|
||||
|
||||
// 获取同类别的其他商品作为推荐
|
||||
if (filteredRecommendProductIds.length > 0) {
|
||||
@@ -991,40 +991,23 @@ router.get('/:id/recommended', async (req, res) => {
|
||||
}
|
||||
}
|
||||
|
||||
// 如果同类别商品不足,补充其他热门商品
|
||||
// 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);
|
||||
// }
|
||||
// }
|
||||
if (recommendedProducts.length < 6) {
|
||||
console.log(123456, filteredRecommendProductIds);
|
||||
// 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 != 3
|
||||
ORDER BY RAND()
|
||||
LIMIT ${6 - recommendedProducts.length}
|
||||
`;
|
||||
const [additionalProducts] = await getDB().execute(
|
||||
additionalQuery,
|
||||
);
|
||||
recommendedProducts.push(...additionalProducts);
|
||||
}
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
|
||||
Reference in New Issue
Block a user