解决创建商品时规格组合选择问题
This commit is contained in:
@@ -77,7 +77,7 @@ router.get('/', async (req, res) => {
|
|||||||
SELECT
|
SELECT
|
||||||
o.id, o.order_no, o.user_id, o.total_amount, o.total_points,
|
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,
|
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
|
FROM orders o
|
||||||
LEFT JOIN users u ON o.user_id = u.id
|
LEFT JOIN users u ON o.user_id = u.id
|
||||||
${whereClause}
|
${whereClause}
|
||||||
|
|||||||
@@ -211,7 +211,7 @@ router.get('/:id', async (req, res) => {
|
|||||||
`SELECT * FROM category WHERE id = ?`,
|
`SELECT * FROM category WHERE id = ?`,
|
||||||
[category[0][0].parent_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;
|
product.category = categories;
|
||||||
|
|
||||||
@@ -407,22 +407,30 @@ router.post('/', async (req, res) => {
|
|||||||
const {
|
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',
|
image_url, images = [], videos = [], details, status = 'active',
|
||||||
shop_name, shop_avatar, payment_methods = ['points', 'rongdou', 'points_rongdou'],
|
shop_name, shop_avatar, payment_methods = ['points', 'rongdou', 'points_rongdou'], attributes = []
|
||||||
specifications = [], attributes = []
|
|
||||||
} = req.body;
|
} = req.body;
|
||||||
|
|
||||||
if (!name || !price || (!points_price && !rongdou_price) || stock === undefined) {
|
if (!name || !price || (!points_price && !rongdou_price) || stock === undefined) {
|
||||||
return res.status(400).json({ message: '商品名称、原价、积分价格或融豆价格、库存不能为空' });
|
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(
|
const [result] = await getDB().execute(
|
||||||
`INSERT INTO products (name, description, price, points_price, rongdou_price, stock, category,
|
`UPDATE products
|
||||||
image_url, images, videos, details, shop_name, shop_avatar, payment_methods, status, created_at, updated_at)
|
SET name = ?, description = ?, price = ?, points_price = ?, rongdou_price = ?, stock = ?, category = ?,
|
||||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW(), NOW())`,
|
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, category || null,
|
||||||
image_url, JSON.stringify(images), JSON.stringify(videos), details,
|
image_url, JSON.stringify(images), JSON.stringify(videos), details,
|
||||||
shop_name, shop_avatar, JSON.stringify(payment_methods), status]
|
shop_name, shop_avatar, JSON.stringify(payment_methods), status, 'temp']
|
||||||
);
|
)
|
||||||
|
|
||||||
const productId = result.insertId;
|
const productId = result.insertId;
|
||||||
|
|
||||||
|
|||||||
@@ -420,7 +420,7 @@ router.put('/combinations/:id', async (req, res) => {
|
|||||||
barcode,
|
barcode,
|
||||||
weight,
|
weight,
|
||||||
volume,
|
volume,
|
||||||
is_available
|
status
|
||||||
} = req.body;
|
} = req.body;
|
||||||
|
|
||||||
// 检查规格组合是否存在
|
// 检查规格组合是否存在
|
||||||
@@ -477,9 +477,9 @@ router.put('/combinations/:id', async (req, res) => {
|
|||||||
updateValues.push(volume);
|
updateValues.push(volume);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (is_available !== undefined) {
|
if (status !== undefined) {
|
||||||
updateFields.push('status = ?');
|
updateFields.push('status = ?');
|
||||||
updateValues.push(is_available ? 'active' : 'inactive');
|
updateValues.push(status);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (updateFields.length === 0) {
|
if (updateFields.length === 0) {
|
||||||
@@ -648,9 +648,34 @@ router.post('/generate-combinations', async (req, res) => {
|
|||||||
const values = [];
|
const values = [];
|
||||||
const placeholders = [];
|
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 => {
|
newCombinations.forEach(item => {
|
||||||
values.push(
|
values.push(
|
||||||
product_id,
|
newProductId === null ? product_id : newProductId,
|
||||||
item.key,
|
item.key,
|
||||||
JSON.stringify(item.combination),
|
JSON.stringify(item.combination),
|
||||||
default_stock
|
default_stock
|
||||||
@@ -672,7 +697,8 @@ router.post('/generate-combinations', async (req, res) => {
|
|||||||
data: {
|
data: {
|
||||||
total_combinations: combinations.length,
|
total_combinations: combinations.length,
|
||||||
created: createdCount,
|
created: createdCount,
|
||||||
skipped: skippedCount
|
skipped: skippedCount,
|
||||||
|
product_id: newProductId === null ? product_id : newProductId
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user