升级商城逻辑

This commit is contained in:
2025-09-02 09:29:20 +08:00
parent 16bfc525c2
commit 49eed40ad0
30 changed files with 22710 additions and 1339 deletions

View File

@@ -53,6 +53,7 @@ async function createTables() {
role ENUM('user', 'admin') DEFAULT 'user',
avatar VARCHAR(255),
points INT DEFAULT 0,
rongdou INT DEFAULT 0,
balance DECIMAL(10,2) DEFAULT 0.00,
real_name VARCHAR(100),
id_card VARCHAR(18),
@@ -111,7 +112,8 @@ async function createTables() {
order_no VARCHAR(50) UNIQUE NOT NULL,
total_amount INT NOT NULL,
total_points INT NOT NULL,
status ENUM('pending', 'paid', 'shipped', 'delivered', 'cancelled') DEFAULT 'pending',
total_rongdou INT NOT NULL DEFAULT 0,
status ENUM('pending', 'paid', 'shipped', 'delivered', 'cancelled', 'pre_order') DEFAULT 'pending',
address JSON,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
@@ -158,12 +160,18 @@ async function createTables() {
id INT AUTO_INCREMENT PRIMARY KEY,
order_id INT NOT NULL,
product_id INT NOT NULL,
spec_combination_id INT NULL COMMENT '规格组合ID',
quantity INT NOT NULL,
price INT NOT NULL,
points INT NOT NULL,
points_price INT NOT NULL DEFAULT 0,
rongdou INT DEFAULT 0 COMMENT '融豆价格',
rongdou_price INT NOT NULL DEFAULT 0,
spec_info JSON COMMENT '规格信息快照',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (order_id) REFERENCES orders(id),
FOREIGN KEY (product_id) REFERENCES products(id)
FOREIGN KEY (product_id) REFERENCES products(id),
FOREIGN KEY (spec_combination_id) REFERENCES product_spec_combinations(id) ON DELETE SET NULL
)
`);
@@ -182,6 +190,21 @@ async function createTables() {
)
`);
// 融豆记录表
await getDB().execute(`
CREATE TABLE IF NOT EXISTS rongdou_history (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL,
type ENUM('earn', 'spend') NOT NULL,
amount INT NOT NULL,
description VARCHAR(255),
order_id INT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users(id),
FOREIGN KEY (order_id) REFERENCES orders(id)
)
`);
// 管理员操作日志表
await getDB().execute(`
CREATE TABLE IF NOT EXISTS admin_operation_logs (
@@ -213,22 +236,75 @@ async function createTables() {
)
`);
// 商品规格表
// 规格名称表(规格维度)
await getDB().execute(`
CREATE TABLE IF NOT EXISTS product_specifications (
CREATE TABLE IF NOT EXISTS spec_names (
id INT AUTO_INCREMENT PRIMARY KEY,
product_id INT NOT NULL,
spec_name VARCHAR(100) NOT NULL,
spec_value VARCHAR(100) NOT NULL,
price_adjustment INT DEFAULT 0,
points_adjustment INT DEFAULT 0,
rongdou_adjustment INT DEFAULT 0,
stock INT DEFAULT 0,
sku_code VARCHAR(100),
name VARCHAR(100) NOT NULL COMMENT '规格名称,如:颜色、尺寸、材质',
display_name VARCHAR(100) NOT NULL COMMENT '显示名称',
sort_order INT DEFAULT 0 COMMENT '排序',
status ENUM('active', 'inactive') DEFAULT 'active',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
FOREIGN KEY (product_id) REFERENCES products(id) ON DELETE CASCADE
UNIQUE KEY unique_name (name)
)
`);
// 规格值表
await getDB().execute(`
CREATE TABLE IF NOT EXISTS spec_values (
id INT AUTO_INCREMENT PRIMARY KEY,
spec_name_id INT NOT NULL COMMENT '规格名称ID',
value VARCHAR(100) NOT NULL COMMENT '规格值红色、XL、棉质',
display_value VARCHAR(100) NOT NULL COMMENT '显示值',
color_code VARCHAR(20) COMMENT '颜色代码(仅颜色规格使用)',
image_url VARCHAR(500) COMMENT '规格图片',
sort_order INT DEFAULT 0 COMMENT '排序',
status ENUM('active', 'inactive') DEFAULT 'active',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
FOREIGN KEY (spec_name_id) REFERENCES spec_names(id) ON DELETE CASCADE,
UNIQUE KEY unique_spec_value (spec_name_id, value)
)
`);
// 商品规格组合表(笛卡尔积结果)
await getDB().execute(`
CREATE TABLE IF NOT EXISTS product_spec_combinations (
id INT AUTO_INCREMENT PRIMARY KEY,
product_id INT NOT NULL COMMENT '商品ID',
combination_key VARCHAR(255) NOT NULL COMMENT '组合键color_1-size_2-material_3',
spec_values JSON NOT NULL COMMENT '规格值组合存储spec_value_id数组',
price_adjustment INT DEFAULT 0 COMMENT '价格调整',
points_adjustment INT DEFAULT 0 COMMENT '积分调整',
rongdou_adjustment INT DEFAULT 0 COMMENT '融豆调整',
stock INT DEFAULT 0 COMMENT '库存',
sku_code VARCHAR(100) COMMENT 'SKU编码',
barcode VARCHAR(100) COMMENT '条形码',
weight DECIMAL(8,3) COMMENT '重量kg',
volume DECIMAL(10,3) COMMENT '体积cm³',
status ENUM('active', 'inactive') DEFAULT 'active',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
FOREIGN KEY (product_id) REFERENCES products(id) ON DELETE CASCADE,
UNIQUE KEY unique_product_combination (product_id, combination_key),
INDEX idx_product_status (product_id, status),
INDEX idx_sku_code (sku_code)
)
`);
// 商品规格关联表(定义商品使用哪些规格维度)
await getDB().execute(`
CREATE TABLE IF NOT EXISTS product_spec_names (
id INT AUTO_INCREMENT PRIMARY KEY,
product_id INT NOT NULL COMMENT '商品ID',
spec_name_id INT NOT NULL COMMENT '规格名称ID',
is_required BOOLEAN DEFAULT TRUE COMMENT '是否必选规格',
sort_order INT DEFAULT 0 COMMENT '排序',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (product_id) REFERENCES products(id) ON DELETE CASCADE,
FOREIGN KEY (spec_name_id) REFERENCES spec_names(id) ON DELETE CASCADE,
UNIQUE KEY unique_product_spec_name (product_id, spec_name_id)
)
`);
@@ -259,6 +335,23 @@ async function createTables() {
)
`);
// 购物车表
await getDB().execute(`
CREATE TABLE IF NOT EXISTS cart_items (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL,
product_id INT NOT NULL,
quantity INT NOT NULL DEFAULT 1,
spec_combination_id INT NULL COMMENT '规格组合ID',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
FOREIGN KEY (product_id) REFERENCES products(id) ON DELETE CASCADE,
FOREIGN KEY (spec_combination_id) REFERENCES product_spec_combinations(id) ON DELETE CASCADE,
UNIQUE KEY unique_user_product_spec (user_id, product_id, spec_combination_id)
)
`);
// 用户收货地址表
await getDB().execute(`
CREATE TABLE IF NOT EXISTS user_addresses (