解决创建商品时规格组合选择问题

This commit is contained in:
dzl
2025-10-16 17:29:59 +08:00
parent 21585fe3e9
commit a5b2638627
3 changed files with 49 additions and 15 deletions

View File

@@ -77,7 +77,7 @@ router.get('/', async (req, res) => {
SELECT
o.id, o.order_no, o.user_id, o.total_amount, o.total_points,
o.status, o.address, o.created_at, o.updated_at,o.total_rongdou,
u.username,salesperson_id
u.username, o.salesperson_id, o.delivery_code, o.logistics_company
FROM orders o
LEFT JOIN users u ON o.user_id = u.id
${whereClause}

View File

@@ -211,7 +211,7 @@ router.get('/:id', async (req, res) => {
`SELECT * FROM category WHERE id = ?`,
[category[0][0].parent_id]
);
item.parentCategory_name = parentCategory[0][0].category_name;
item.parentCategory_name = parentCategory[0][0]?.category_name || '无';
}
product.category = categories;
@@ -407,22 +407,30 @@ router.post('/', async (req, res) => {
const {
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'],
specifications = [], attributes = []
shop_name, shop_avatar, payment_methods = ['points', 'rongdou', 'points_rongdou'], attributes = []
} = req.body;
if (!name || !price || (!points_price && !rongdou_price) || stock === undefined) {
return res.status(400).json({ message: '商品名称、原价、积分价格或融豆价格、库存不能为空' });
}
// const [result] = await getDB().execute(
// `INSERT INTO products (name, description, price, points_price, rongdou_price, stock, category,
// image_url, images, videos, details, shop_name, shop_avatar, payment_methods, status, created_at, updated_at)
// VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW(), NOW())`,
// [name, description, price, points_price, rongdou_price, stock, category || null,
// image_url, JSON.stringify(images), JSON.stringify(videos), details,
// shop_name, shop_avatar, JSON.stringify(payment_methods), status]
// );
const [result] = await getDB().execute(
`INSERT INTO products (name, description, price, points_price, rongdou_price, stock, category,
image_url, images, videos, details, shop_name, shop_avatar, payment_methods, status, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW(), NOW())`,
`UPDATE products
SET name = ?, description = ?, price = ?, points_price = ?, rongdou_price = ?, stock = ?, category = ?,
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,
image_url, JSON.stringify(images), JSON.stringify(videos), details,
shop_name, shop_avatar, JSON.stringify(payment_methods), status]
);
image_url, JSON.stringify(images), JSON.stringify(videos), details,
shop_name, shop_avatar, JSON.stringify(payment_methods), status, 'temp']
)
const productId = result.insertId;

View File

@@ -420,7 +420,7 @@ router.put('/combinations/:id', async (req, res) => {
barcode,
weight,
volume,
is_available
status
} = req.body;
// 检查规格组合是否存在
@@ -477,9 +477,9 @@ router.put('/combinations/:id', async (req, res) => {
updateValues.push(volume);
}
if (is_available !== undefined) {
if (status !== undefined) {
updateFields.push('status = ?');
updateValues.push(is_available ? 'active' : 'inactive');
updateValues.push(status);
}
if (updateFields.length === 0) {
@@ -648,9 +648,34 @@ router.post('/generate-combinations', async (req, res) => {
const values = [];
const placeholders = [];
const createProduct = `
INSERT INTO products (name, price)
VALUES ('temp', 0)
`
const deleteProduct = `
DELETE FROM products WHERE name = 'temp'
`
var newProductId = null;
if (product_id === 'new') {
await getDB().execute(deleteProduct);
await getDB().execute(createProduct);
// const deleteProduct = `
// DELETE FROM products WHERE name = 'temp'
// `
// await getDB().execute(deleteProduct);
const getProductId = `
SELECT id FROM products WHERE name = 'temp'
`
const [productResult] = await getDB().execute(getProductId);
newProductId = productResult[0].id;
}
newCombinations.forEach(item => {
values.push(
product_id,
newProductId === null ? product_id : newProductId,
item.key,
JSON.stringify(item.combination),
default_stock
@@ -672,7 +697,8 @@ router.post('/generate-combinations', async (req, res) => {
data: {
total_combinations: combinations.length,
created: createdCount,
skipped: skippedCount
skipped: skippedCount,
product_id: newProductId === null ? product_id : newProductId
}
});
} catch (error) {