完善商品创建逻辑

This commit is contained in:
dzl
2025-10-17 17:26:49 +08:00
parent a5b2638627
commit 779a7fa57b

View File

@@ -405,7 +405,7 @@ router.get('/:id', async (req, res) => {
router.post('/', async (req, res) => {
try {
const {
name, description, price, points_price, rongdou_price = 0, stock, category,
name, description, price, points_price, rongdou_price = 0, stock, category = [],
image_url, images = [], videos = [], details, status = 'active',
shop_name, shop_avatar, payment_methods = ['points', 'rongdou', 'points_rongdou'], attributes = []
} = req.body;
@@ -424,15 +424,33 @@ router.post('/', async (req, res) => {
// );
const [result] = await getDB().execute(
`UPDATE products
SET name = ?, description = ?, price = ?, points_price = ?, rongdou_price = ?, stock = ?, category = ?,
SET name = ?, description = ?, price = ?, points_price = ?, rongdou_price = ?, stock = ?,
image_url = ?, images = ?, videos = ?, details = ?, shop_name = ?, shop_avatar = ?, payment_methods = ?, status = ?, updated_at = NOW()
WHERE name = ?`,
[name, description, price, points_price, rongdou_price, stock, category || null,
[name, description, price, points_price, rongdou_price, stock,
image_url, JSON.stringify(images), JSON.stringify(videos), details,
shop_name, shop_avatar, JSON.stringify(payment_methods), status, 'temp']
)
const productId = result.insertId;
const [product] = await getDB().execute(
'SELECT * FROM products WHERE name = ?',
[name]
);
const productId = product[0].id;
// console.log(123,product)
// console.log(123,category)
// 处理分类
if (category.length > 0) {
for (const cat of category) {
// console.log(123,cat,product)
await getDB().execute(
`INSERT INTO products_category (product_id, category_id, create_time)
VALUES (?, ?, NOW())`,
[productId, cat]
);
}
}
@@ -629,7 +647,7 @@ router.delete('/:id', async (req, res) => {
}
// 检查是否有相关订单
const orderCheckQuery = 'SELECT id FROM orders WHERE product_id = ? LIMIT 1';
const orderCheckQuery = 'SELECT id FROM order_items WHERE product_id = ? LIMIT 1';
const [orders] = await getDB().execute(orderCheckQuery, [id]);
if (orders.length > 0) {