@@ -42,7 +42,7 @@ router.get('/', async (req, res) => {
// 获取商品列表
const query = `
SELECT id, name, rongdou_price, category, points_price, stock, image_url as image, description, status, payment_methods, created_at, updated_at, sales
SELECT id, name, rongdou_price, points_price, stock, image_url as image, description, status, payment_methods, created_at, updated_at, sales
FROM products
${ whereClause }
ORDER BY created_at DESC
@@ -55,13 +55,33 @@ router.get('/', async (req, res) => {
products . forEach ( item => {
item . payment _methods = JSON . parse ( item . payment _methods )
} )
// for(let item of products){
// if(item.category !== null){
// const categories = [];
// for(let id of item.category){
// const categoryQuery = `SELECT * FROM category WHERE id = ?`;
// const [categoryResult] = await getDB().execute(categoryQuery, [parseInt(id)]);
// if(categoryResult.length !== 0){
// categories.push(categoryResult[0].category_name);
// }
// }
// item.category = categories
// }
// }
for ( let item of products ) {
// console.log("1234567",item)
// const categoryQuery = `SELECT category_name FROM category WHERE id = ?`;
// const [categoryResult] = await getDB().execute(categoryQuery, [parseInt(item.category)]);
// item.category = categoryResult.category_name;
// console.log("qwer",categoryResult)
parseInt ( item . category ) === 20 ? console . log ( 'y es' ) : console . log ( 'no' )
item . category = [ ]
const [ categories ] = await getDB ( ) . execute (
` SELECT * FROM products_category WHERE product_id = ? ` ,
[ item . id ]
) ;
for ( let category of categori es) {
const [ categoryDetails ] = await getDB ( ) . execute (
` SELECT * FROM category WHERE id = ? ` ,
[ category . category _id ]
) ;
item . category . push ( categoryDetails [ 0 ] . category _name )
}
}
res . json ( {
success : true ,
@@ -86,7 +106,7 @@ router.get('/hot', async (req, res) => {
try {
// 从活跃商品中随机获取2个商品
const [ products ] = await getDB ( ) . execute (
` SELECT id, name, category, price, points_price, rongdou_price, stock,
` SELECT id, name, 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
@@ -121,7 +141,7 @@ router.get('/cheap', async (req, res) => {
try {
// 从活跃商品中随机获取2个商品作为秒杀商品
const [ products ] = await getDB ( ) . execute (
` SELECT id, name, category, price, points_price, rongdou_price, stock,
` SELECT id, name, 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
@@ -162,7 +182,7 @@ router.get('/:id', async (req, res) => {
const userId = req . user ? . id ; // 可选的用户ID, 用于检查收藏状态
const query = `
SELECT id, name, category, price, points_price, rongdou_price, stock,
SELECT id, name, price, points_price, rongdou_price, stock,
image_url, images, videos, description, details, shop_name, shop_avatar,
payment_methods, sales, rating, status, created_at, updated_at
FROM products
@@ -177,6 +197,19 @@ router.get('/:id', async (req, res) => {
const product = products [ 0 ] ;
const [ categories ] = await getDB ( ) . execute (
` SELECT * FROM products_category WHERE product_id = ? ` ,
[ id ]
) ;
for ( let item of categories ) {
const category = await getDB ( ) . execute (
` SELECT * FROM category WHERE id = ? ` ,
[ item . category _id ]
) ;
item . category _name = category [ 0 ] [ 0 ] . category _name ;
}
product . category = categories ;
// 获取商品的规格组合(新的笛卡尔积规格系统)
const [ specCombinations ] = await getDB ( ) . execute (
` SELECT psc.*,
@@ -351,7 +384,6 @@ router.get('/:id', async (req, res) => {
// 保持向后兼容
points : product . points _price ,
image : product . image _url ,
tags : product . category ? [ product . category ] : [ ]
} ;
res . json ( {
@@ -413,12 +445,12 @@ router.post('/', async (req, res) => {
}
} ) ;
// 更新商品(管理员权限)
// 更新商品
router . put ( '/:id' , async ( req , res ) => {
try {
const productId = req . params . id ;
const {
name , description , price , points _price , rongdou _price , stock , category ,
name , description , price , points _price , rongdou _price , stock , category , level ,
image _url , images , videos , details , status , shop _name , shop _avatar , payment _methods ,
specifications , attributes
} = req . body ;
@@ -467,11 +499,6 @@ router.put('/:id', async (req, res) => {
updateValues . push ( stock ) ;
}
if ( category !== undefined ) {
updateFields . push ( 'category = ?' ) ;
updateValues . push ( category ) ;
}
if ( image _url !== undefined ) {
updateFields . push ( 'image_url = ?' ) ;
updateValues . push ( image _url ) ;
@@ -543,6 +570,28 @@ router.put('/:id', async (req, res) => {
}
}
if ( category ) {
const [ existCategorys ] = await getDB ( ) . execute (
` SELECT * FROM products_category WHERE product_id = ? ` ,
[ productId ]
) ;
if ( existCategorys . length > 0 ) {
await getDB ( ) . execute (
` DELETE FROM products_category WHERE product_id = ? ` ,
[ productId ]
) ;
}
for ( const id of category ) {
await getDB ( ) . execute (
` INSERT INTO products_category (product_id, category_id, create_time)
VALUES (?, ?, NOW()) ` ,
[ productId , id ]
) ;
}
}
res . json ( {
success : true ,
message : '商品更新成功'
@@ -718,10 +767,9 @@ router.get('/:id/recommended', async (req, res) => {
// 获取同类别的其他商品作为推荐
const query = `
SELECT p2.id, p2.name, p2.category, p2.price, p2.points_price as points,
SELECT p2.id, p2.name, p2.price, p2.points_price as points,
p2.stock, p2.image_url as image, p2.description
FROM products p1
JOIN products p2 ON p1.category = p2.category
WHERE p1.id = ? AND p2.id != ? AND p2.status = 'active'
ORDER BY RAND()
LIMIT 6
@@ -734,7 +782,7 @@ router.get('/:id/recommended', async (req, res) => {
const remainingCount = 6 - recommendedProducts . length ;
if ( remainingCount > 0 ) {
const additionalQuery = `
SELECT id, name, category, price, points_price as points,
SELECT id, name, price, points_price as points,
stock, image_url as image, description
FROM products
WHERE id != ? AND status = 'active'