完善商品创建逻辑
This commit is contained in:
@@ -405,7 +405,7 @@ router.get('/:id', async (req, res) => {
|
|||||||
router.post('/', async (req, res) => {
|
router.post('/', async (req, res) => {
|
||||||
try {
|
try {
|
||||||
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'], attributes = []
|
shop_name, shop_avatar, payment_methods = ['points', 'rongdou', 'points_rongdou'], attributes = []
|
||||||
} = req.body;
|
} = req.body;
|
||||||
@@ -424,15 +424,33 @@ router.post('/', async (req, res) => {
|
|||||||
// );
|
// );
|
||||||
const [result] = await getDB().execute(
|
const [result] = await getDB().execute(
|
||||||
`UPDATE products
|
`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()
|
image_url = ?, images = ?, videos = ?, details = ?, shop_name = ?, shop_avatar = ?, payment_methods = ?, status = ?, updated_at = NOW()
|
||||||
WHERE name = ?`,
|
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,
|
image_url, JSON.stringify(images), JSON.stringify(videos), details,
|
||||||
shop_name, shop_avatar, JSON.stringify(payment_methods), status, 'temp']
|
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]);
|
const [orders] = await getDB().execute(orderCheckQuery, [id]);
|
||||||
|
|
||||||
if (orders.length > 0) {
|
if (orders.length > 0) {
|
||||||
|
|||||||
Reference in New Issue
Block a user