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