调整秒杀和热销的算法
This commit is contained in:
@@ -104,7 +104,7 @@ router.get('/hot', async (req, res) => {
|
|||||||
payment_methods, sales, rating, status, created_at, updated_at
|
payment_methods, sales, rating, status, created_at, updated_at
|
||||||
FROM products
|
FROM products
|
||||||
WHERE status = 'active' AND stock > 0
|
WHERE status = 'active' AND stock > 0
|
||||||
ORDER BY RAND()
|
ORDER BY sales DESC
|
||||||
LIMIT 2`
|
LIMIT 2`
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -156,36 +156,43 @@ router.get('/hot', async (req, res) => {
|
|||||||
*/
|
*/
|
||||||
router.get('/cheap', async (req, res) => {
|
router.get('/cheap', async (req, res) => {
|
||||||
try {
|
try {
|
||||||
// 从活跃商品中随机获取2个商品作为秒杀商品
|
|
||||||
const [products] = await getDB().execute(
|
const [products] = await getDB().execute(
|
||||||
`SELECT id, name, category, price, points_price, rongdou_price, stock,
|
`SELECT id, start_time, end_time, flash_stock, flash_price, products_id
|
||||||
image_url, images, description, shop_name, shop_avatar,
|
FROM flash_product
|
||||||
payment_methods, sales, rating, status, created_at, updated_at
|
WHERE end_time > NOW() AND flash_stock > 0
|
||||||
FROM products
|
|
||||||
WHERE status = 'active' AND stock > 0
|
|
||||||
ORDER BY RAND()
|
ORDER BY RAND()
|
||||||
LIMIT 2`
|
LIMIT 2`
|
||||||
);
|
);
|
||||||
|
|
||||||
// 格式化商品数据,为秒杀商品添加特殊标识
|
const tempProducts = await Promise.all(products.map(async item=>{
|
||||||
const formattedProducts = products.map(product => ({
|
|
||||||
...product,
|
const [product] = await getDB().execute(
|
||||||
|
`SELECT id, name, category, price, points_price, rongdou_price, stock,
|
||||||
|
image_url, images, description, shop_name, shop_avatar,
|
||||||
|
payment_methods, sales, rating, status, created_at, updated_at
|
||||||
|
FROM products
|
||||||
|
WHERE id = ?`,
|
||||||
|
[item.products_id]
|
||||||
|
)
|
||||||
|
|
||||||
|
item = {
|
||||||
|
...product[0],
|
||||||
images: product.images ? JSON.parse(product.images) : (product.image_url ? [product.image_url] : []),
|
images: product.images ? JSON.parse(product.images) : (product.image_url ? [product.image_url] : []),
|
||||||
payment_methods: product.payment_methods ? JSON.parse(product.payment_methods) : ['points'],
|
payment_methods: product.payment_methods ? JSON.parse(product.payment_methods) : ['points'],
|
||||||
// 秒杀商品特殊处理:价格打8折
|
...item,
|
||||||
flash_sale_price: Math.floor(product.price * 0.8),
|
|
||||||
flash_sale_points: Math.floor(product.points_price * 0.8),
|
|
||||||
flash_sale_rongdou: Math.floor(product.rongdou_price * 0.8),
|
|
||||||
is_flash_sale: true,
|
|
||||||
// 保持向后兼容
|
|
||||||
points: product.points_price,
|
points: product.points_price,
|
||||||
image: product.image_url
|
image: product.image_url,
|
||||||
}));
|
id: product[0].id,
|
||||||
|
}
|
||||||
|
console.log("222",item)
|
||||||
|
return item
|
||||||
|
}))
|
||||||
|
|
||||||
res.json({
|
res.json({
|
||||||
success: true,
|
success: true,
|
||||||
data: {
|
data: {
|
||||||
products: formattedProducts
|
products: tempProducts
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user