From 87148ac0389e397c95bdb3155dc7defb3e15c070 Mon Sep 17 00:00:00 2001 From: 18631081161 <2088094923@qq.com> Date: Mon, 6 Apr 2026 20:25:56 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9C=80=E4=BD=8E=E4=BB=B7=E6=A0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/src/utils/syncPrice.ts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/server/src/utils/syncPrice.ts b/server/src/utils/syncPrice.ts index 09fab1e8..e9631e0a 100644 --- a/server/src/utils/syncPrice.ts +++ b/server/src/utils/syncPrice.ts @@ -1,17 +1,16 @@ import pool from './db' -import { RowDataPacket } from 'mysql2' /** - * 同步商品价格为其规格数据中的最低价。 - * 如果没有规格数据,价格设为 0。 + * 同步商品价格(规格最低价)和库存(规格条数)。 * 支持传入 connection(事务中使用)或使用默认 pool。 */ export async function syncProductMinPrice(productId: number, conn?: any): Promise { const db = conn || pool const [rows]: any = await db.execute( - 'SELECT MIN(total_price) as min_price FROM spec_data WHERE product_id = ?', + 'SELECT MIN(total_price) as min_price, COUNT(*) as spec_count FROM spec_data WHERE product_id = ?', [productId] ) const minPrice = rows[0]?.min_price ?? 0 - await db.execute('UPDATE products SET base_price = ? WHERE id = ?', [minPrice, productId]) + const stock = rows[0]?.spec_count ?? 0 + await db.execute('UPDATE products SET base_price = ?, stock = ? WHERE id = ?', [minPrice, stock, productId]) }