refactor: 重构数据库配置为SQLite开发环境并移除冗余文档
This commit is contained in:
280
backend/api/database/create_tables.sql
Normal file
280
backend/api/database/create_tables.sql
Normal file
@@ -0,0 +1,280 @@
|
||||
-- ======================================
|
||||
-- 锡林郭勒盟智慧养殖数字化管理平台数据库表结构
|
||||
-- ======================================
|
||||
|
||||
-- 用户表
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
username VARCHAR(50) NOT NULL UNIQUE,
|
||||
phone VARCHAR(20) NOT NULL UNIQUE,
|
||||
email VARCHAR(100) UNIQUE,
|
||||
password_hash VARCHAR(255) NOT NULL,
|
||||
salt VARCHAR(32) NOT NULL,
|
||||
avatar VARCHAR(255),
|
||||
real_name VARCHAR(50),
|
||||
id_card VARCHAR(18),
|
||||
gender INTEGER, -- 1-男,2-女
|
||||
birthday DATE,
|
||||
address VARCHAR(255),
|
||||
status INTEGER NOT NULL DEFAULT 1, -- 0-禁用,1-正常
|
||||
user_type VARCHAR(20) DEFAULT 'farmer', -- farmer, trader, consumer, finance, government
|
||||
last_login_at DATETIME,
|
||||
last_login_ip VARCHAR(45),
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
deleted_at DATETIME
|
||||
);
|
||||
|
||||
-- 牛只表
|
||||
CREATE TABLE IF NOT EXISTS cattle (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
ear_tag VARCHAR(50) NOT NULL UNIQUE,
|
||||
name VARCHAR(100),
|
||||
breed VARCHAR(50) NOT NULL,
|
||||
gender VARCHAR(10) NOT NULL, -- male, female
|
||||
birth_date DATE,
|
||||
color VARCHAR(50),
|
||||
weight DECIMAL(8,2),
|
||||
height DECIMAL(6,2),
|
||||
health_status VARCHAR(20) DEFAULT 'healthy', -- healthy, sick, quarantine, dead
|
||||
status VARCHAR(20) DEFAULT 'active', -- active, sold, dead, transferred
|
||||
owner_id INTEGER NOT NULL,
|
||||
farm_location VARCHAR(255),
|
||||
parent_male_id INTEGER,
|
||||
parent_female_id INTEGER,
|
||||
vaccination_records TEXT, -- JSON格式
|
||||
health_records TEXT, -- JSON格式
|
||||
feeding_records TEXT, -- JSON格式
|
||||
images TEXT, -- JSON格式的图片URLs
|
||||
notes TEXT,
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (owner_id) REFERENCES users(id),
|
||||
FOREIGN KEY (parent_male_id) REFERENCES cattle(id),
|
||||
FOREIGN KEY (parent_female_id) REFERENCES cattle(id)
|
||||
);
|
||||
|
||||
-- 交易表
|
||||
CREATE TABLE IF NOT EXISTS transactions (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
transaction_no VARCHAR(50) NOT NULL UNIQUE,
|
||||
buyer_id INTEGER NOT NULL,
|
||||
seller_id INTEGER NOT NULL,
|
||||
product_type VARCHAR(20) NOT NULL, -- cattle, product
|
||||
product_id INTEGER NOT NULL,
|
||||
quantity INTEGER NOT NULL DEFAULT 1,
|
||||
unit_price DECIMAL(12,2) NOT NULL,
|
||||
total_amount DECIMAL(12,2) NOT NULL,
|
||||
currency VARCHAR(10) DEFAULT 'CNY',
|
||||
status VARCHAR(20) DEFAULT 'pending', -- pending, confirmed, completed, cancelled
|
||||
payment_status VARCHAR(20) DEFAULT 'pending', -- pending, paid, partial, refunded
|
||||
payment_method VARCHAR(20), -- wechat_pay, alipay, bank_transfer, cash
|
||||
delivery_status VARCHAR(20) DEFAULT 'pending', -- pending, shipped, delivered, received
|
||||
delivery_method VARCHAR(20), -- self_pickup, express, logistics
|
||||
delivery_address TEXT,
|
||||
delivery_phone VARCHAR(20),
|
||||
delivery_contact VARCHAR(50),
|
||||
contract_url VARCHAR(255),
|
||||
notes TEXT,
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (buyer_id) REFERENCES users(id),
|
||||
FOREIGN KEY (seller_id) REFERENCES users(id)
|
||||
);
|
||||
|
||||
-- 商品表(牛肉商城)
|
||||
CREATE TABLE IF NOT EXISTS products (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
name VARCHAR(200) NOT NULL,
|
||||
sku VARCHAR(50) UNIQUE,
|
||||
category VARCHAR(50) NOT NULL, -- beef, dairy, snacks, processed, equipment, feed
|
||||
subcategory VARCHAR(50),
|
||||
description TEXT,
|
||||
price DECIMAL(10,2) NOT NULL,
|
||||
original_price DECIMAL(10,2),
|
||||
cost_price DECIMAL(10,2),
|
||||
currency VARCHAR(10) DEFAULT 'CNY',
|
||||
stock INTEGER DEFAULT 0,
|
||||
sales_count INTEGER DEFAULT 0,
|
||||
weight DECIMAL(8,3),
|
||||
dimensions VARCHAR(100),
|
||||
shelf_life INTEGER, -- 保质期(天)
|
||||
storage_conditions VARCHAR(200),
|
||||
origin VARCHAR(100),
|
||||
brand VARCHAR(100),
|
||||
specifications TEXT, -- JSON格式的规格参数
|
||||
images TEXT, -- JSON格式的图片URLs
|
||||
video_url VARCHAR(255),
|
||||
seller_id INTEGER NOT NULL,
|
||||
status VARCHAR(20) DEFAULT 'pending_review', -- active, inactive, out_of_stock, pending_review, rejected
|
||||
featured INTEGER DEFAULT 0,
|
||||
rating DECIMAL(3,2) DEFAULT 0,
|
||||
review_count INTEGER DEFAULT 0,
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (seller_id) REFERENCES users(id)
|
||||
);
|
||||
|
||||
-- 订单表
|
||||
CREATE TABLE IF NOT EXISTS orders (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
order_number VARCHAR(50) UNIQUE NOT NULL,
|
||||
user_id INTEGER NOT NULL,
|
||||
order_type VARCHAR(20) DEFAULT 'normal', -- normal, group_buy, presale, custom
|
||||
total_amount DECIMAL(12,2) NOT NULL,
|
||||
discount_amount DECIMAL(12,2) DEFAULT 0,
|
||||
shipping_fee DECIMAL(10,2) DEFAULT 0,
|
||||
tax_amount DECIMAL(10,2) DEFAULT 0,
|
||||
final_amount DECIMAL(12,2) NOT NULL,
|
||||
currency VARCHAR(10) DEFAULT 'CNY',
|
||||
payment_method VARCHAR(20), -- wechat_pay, alipay, bank_transfer, cash_on_delivery
|
||||
payment_status VARCHAR(20) DEFAULT 'pending', -- pending, paid, partial, refunded, failed
|
||||
shipping_method VARCHAR(20), -- express, self_pickup, same_city
|
||||
shipping_address TEXT,
|
||||
shipping_phone VARCHAR(20),
|
||||
shipping_name VARCHAR(50),
|
||||
tracking_number VARCHAR(100),
|
||||
status VARCHAR(20) DEFAULT 'pending_payment', -- pending_payment, paid, processing, shipping, delivered, completed, cancelled, refunded
|
||||
coupon_code VARCHAR(50),
|
||||
notes TEXT,
|
||||
order_date DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
payment_date DATETIME,
|
||||
shipping_date DATETIME,
|
||||
delivery_date DATETIME,
|
||||
completion_date DATETIME,
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (user_id) REFERENCES users(id)
|
||||
);
|
||||
|
||||
-- 订单商品表
|
||||
CREATE TABLE IF NOT EXISTS order_items (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
order_id INTEGER NOT NULL,
|
||||
product_id INTEGER NOT NULL,
|
||||
product_name VARCHAR(200) NOT NULL, -- 商品名称快照
|
||||
product_sku VARCHAR(50),
|
||||
product_image VARCHAR(255),
|
||||
unit_price DECIMAL(10,2) NOT NULL,
|
||||
quantity INTEGER NOT NULL,
|
||||
total_price DECIMAL(12,2) NOT NULL,
|
||||
currency VARCHAR(10) DEFAULT 'CNY',
|
||||
specifications TEXT, -- JSON格式的规格参数快照
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (order_id) REFERENCES orders(id) ON DELETE CASCADE,
|
||||
FOREIGN KEY (product_id) REFERENCES products(id)
|
||||
);
|
||||
|
||||
-- 金融服务表
|
||||
CREATE TABLE IF NOT EXISTS financial_services (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
service_type VARCHAR(20) NOT NULL, -- loan, insurance, investment
|
||||
service_name VARCHAR(100) NOT NULL,
|
||||
provider VARCHAR(100) NOT NULL,
|
||||
description TEXT,
|
||||
interest_rate DECIMAL(5,4), -- 利率
|
||||
min_amount DECIMAL(12,2),
|
||||
max_amount DECIMAL(12,2),
|
||||
term_months INTEGER, -- 期限(月)
|
||||
requirements TEXT, -- JSON格式的申请要求
|
||||
documents_required TEXT, -- JSON格式的所需文档
|
||||
status VARCHAR(20) DEFAULT 'active', -- active, inactive, suspended
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
-- 金融申请表
|
||||
CREATE TABLE IF NOT EXISTS financial_applications (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
application_no VARCHAR(50) NOT NULL UNIQUE,
|
||||
user_id INTEGER NOT NULL,
|
||||
service_id INTEGER NOT NULL,
|
||||
application_type VARCHAR(20) NOT NULL, -- loan, insurance, investment
|
||||
amount DECIMAL(12,2) NOT NULL,
|
||||
term_months INTEGER,
|
||||
purpose TEXT,
|
||||
collateral_info TEXT, -- JSON格式的抵押物信息
|
||||
documents TEXT, -- JSON格式的文档URLs
|
||||
status VARCHAR(20) DEFAULT 'pending', -- pending, reviewing, approved, rejected, completed
|
||||
reviewer_id INTEGER,
|
||||
review_notes TEXT,
|
||||
approval_date DATETIME,
|
||||
disbursement_date DATETIME,
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (user_id) REFERENCES users(id),
|
||||
FOREIGN KEY (service_id) REFERENCES financial_services(id),
|
||||
FOREIGN KEY (reviewer_id) REFERENCES users(id)
|
||||
);
|
||||
|
||||
-- 政府监管记录表
|
||||
CREATE TABLE IF NOT EXISTS government_inspections (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
inspection_no VARCHAR(50) NOT NULL UNIQUE,
|
||||
inspector_id INTEGER NOT NULL,
|
||||
farm_id INTEGER NOT NULL,
|
||||
inspection_type VARCHAR(20) NOT NULL, -- health, safety, environment, quality
|
||||
inspection_date DATE NOT NULL,
|
||||
status VARCHAR(20) DEFAULT 'scheduled', -- scheduled, in_progress, completed, cancelled
|
||||
findings TEXT, -- JSON格式的检查结果
|
||||
violations TEXT, -- JSON格式的违规记录
|
||||
recommendations TEXT,
|
||||
follow_up_required INTEGER DEFAULT 0, -- 0-否,1-是
|
||||
follow_up_date DATE,
|
||||
documents TEXT, -- JSON格式的文档URLs
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (inspector_id) REFERENCES users(id),
|
||||
FOREIGN KEY (farm_id) REFERENCES users(id)
|
||||
);
|
||||
|
||||
-- 系统日志表
|
||||
CREATE TABLE IF NOT EXISTS system_logs (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
user_id INTEGER,
|
||||
action VARCHAR(50) NOT NULL,
|
||||
resource VARCHAR(50),
|
||||
resource_id INTEGER,
|
||||
ip_address VARCHAR(45),
|
||||
user_agent TEXT,
|
||||
request_data TEXT, -- JSON格式
|
||||
response_data TEXT, -- JSON格式
|
||||
status_code INTEGER,
|
||||
execution_time INTEGER, -- 毫秒
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (user_id) REFERENCES users(id)
|
||||
);
|
||||
|
||||
-- 创建索引
|
||||
CREATE INDEX IF NOT EXISTS idx_users_phone ON users(phone);
|
||||
CREATE INDEX IF NOT EXISTS idx_users_status ON users(status);
|
||||
CREATE INDEX IF NOT EXISTS idx_users_user_type ON users(user_type);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_cattle_ear_tag ON cattle(ear_tag);
|
||||
CREATE INDEX IF NOT EXISTS idx_cattle_owner_id ON cattle(owner_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_cattle_status ON cattle(status);
|
||||
CREATE INDEX IF NOT EXISTS idx_cattle_breed ON cattle(breed);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_transactions_buyer_id ON transactions(buyer_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_transactions_seller_id ON transactions(seller_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_transactions_status ON transactions(status);
|
||||
CREATE INDEX IF NOT EXISTS idx_transactions_created_at ON transactions(created_at);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_products_category ON products(category);
|
||||
CREATE INDEX IF NOT EXISTS idx_products_seller_id ON products(seller_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_products_status ON products(status);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_orders_user_id ON orders(user_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_orders_status ON orders(status);
|
||||
CREATE INDEX IF NOT EXISTS idx_orders_order_date ON orders(order_date);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_financial_applications_user_id ON financial_applications(user_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_financial_applications_status ON financial_applications(status);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_government_inspections_inspector_id ON government_inspections(inspector_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_government_inspections_farm_id ON government_inspections(farm_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_government_inspections_inspection_date ON government_inspections(inspection_date);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_system_logs_user_id ON system_logs(user_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_system_logs_action ON system_logs(action);
|
||||
CREATE INDEX IF NOT EXISTS idx_system_logs_created_at ON system_logs(created_at);
|
||||
217
backend/api/database/mall_tables.sql
Normal file
217
backend/api/database/mall_tables.sql
Normal file
@@ -0,0 +1,217 @@
|
||||
-- ======================================
|
||||
-- 商城管理系统数据库表结构
|
||||
-- ======================================
|
||||
|
||||
-- 商品分类表
|
||||
CREATE TABLE IF NOT EXISTS product_categories (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
name VARCHAR(50) NOT NULL,
|
||||
parent_id INTEGER DEFAULT 0,
|
||||
level INTEGER DEFAULT 1,
|
||||
sort_order INTEGER DEFAULT 0,
|
||||
image_url VARCHAR(255),
|
||||
status INTEGER DEFAULT 1, -- 1-正常, 0-禁用
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
-- 商品表
|
||||
CREATE TABLE IF NOT EXISTS products (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
name VARCHAR(200) NOT NULL,
|
||||
sku VARCHAR(50) UNIQUE,
|
||||
category VARCHAR(50) NOT NULL, -- 'beef', 'dairy', 'snacks', 'processed', 'equipment', 'feed', 'other'
|
||||
subcategory VARCHAR(50),
|
||||
description TEXT,
|
||||
price DECIMAL(10,2) NOT NULL,
|
||||
original_price DECIMAL(10,2),
|
||||
cost_price DECIMAL(10,2),
|
||||
currency VARCHAR(10) DEFAULT 'CNY',
|
||||
stock INTEGER DEFAULT 0,
|
||||
sales_count INTEGER DEFAULT 0,
|
||||
weight DECIMAL(8,3),
|
||||
dimensions VARCHAR(100),
|
||||
shelf_life INTEGER, -- 保质期(天)
|
||||
storage_conditions VARCHAR(200),
|
||||
origin VARCHAR(100),
|
||||
brand VARCHAR(100),
|
||||
specifications TEXT, -- JSON格式的规格参数
|
||||
images TEXT, -- JSON格式的图片URLs
|
||||
video_url VARCHAR(255),
|
||||
seller_id INTEGER NOT NULL,
|
||||
status VARCHAR(20) DEFAULT 'pending_review', -- 'active', 'inactive', 'out_of_stock', 'pending_review', 'rejected'
|
||||
featured INTEGER DEFAULT 0,
|
||||
rating DECIMAL(3,2) DEFAULT 0,
|
||||
review_count INTEGER DEFAULT 0,
|
||||
seo_title VARCHAR(200),
|
||||
seo_description TEXT,
|
||||
seo_keywords VARCHAR(500),
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (seller_id) REFERENCES users(id)
|
||||
);
|
||||
|
||||
-- 订单表
|
||||
CREATE TABLE IF NOT EXISTS orders (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
order_number VARCHAR(50) UNIQUE NOT NULL,
|
||||
user_id INTEGER NOT NULL,
|
||||
order_type VARCHAR(20) DEFAULT 'normal', -- 'normal', 'group_buy', 'presale', 'custom'
|
||||
total_amount DECIMAL(12,2) NOT NULL,
|
||||
discount_amount DECIMAL(12,2) DEFAULT 0,
|
||||
shipping_fee DECIMAL(10,2) DEFAULT 0,
|
||||
tax_amount DECIMAL(10,2) DEFAULT 0,
|
||||
final_amount DECIMAL(12,2) NOT NULL,
|
||||
currency VARCHAR(10) DEFAULT 'CNY',
|
||||
payment_method VARCHAR(20), -- 'wechat_pay', 'alipay', 'bank_transfer', 'cash_on_delivery'
|
||||
payment_status VARCHAR(20) DEFAULT 'pending', -- 'pending', 'paid', 'partial', 'refunded', 'failed'
|
||||
shipping_method VARCHAR(20), -- 'express', 'self_pickup', 'same_city'
|
||||
shipping_address TEXT,
|
||||
shipping_phone VARCHAR(20),
|
||||
shipping_name VARCHAR(50),
|
||||
tracking_number VARCHAR(100),
|
||||
status VARCHAR(20) DEFAULT 'pending_payment', -- 'pending_payment', 'paid', 'processing', 'shipping', 'delivered', 'completed', 'cancelled', 'refunded'
|
||||
coupon_code VARCHAR(50),
|
||||
notes TEXT,
|
||||
order_date DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
payment_date DATETIME,
|
||||
shipping_date DATETIME,
|
||||
delivery_date DATETIME,
|
||||
completion_date DATETIME,
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (user_id) REFERENCES users(id)
|
||||
);
|
||||
|
||||
-- 订单商品表
|
||||
CREATE TABLE IF NOT EXISTS order_items (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
order_id INTEGER NOT NULL,
|
||||
product_id INTEGER NOT NULL,
|
||||
product_name VARCHAR(200) NOT NULL, -- 商品名称快照
|
||||
product_sku VARCHAR(50),
|
||||
product_image VARCHAR(255),
|
||||
unit_price DECIMAL(10,2) NOT NULL,
|
||||
quantity INTEGER NOT NULL,
|
||||
total_price DECIMAL(12,2) NOT NULL,
|
||||
currency VARCHAR(10) DEFAULT 'CNY',
|
||||
specifications TEXT, -- JSON格式的规格参数快照
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (order_id) REFERENCES orders(id) ON DELETE CASCADE,
|
||||
FOREIGN KEY (product_id) REFERENCES products(id)
|
||||
);
|
||||
|
||||
-- 商品评价表
|
||||
CREATE TABLE IF NOT EXISTS product_reviews (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
product_id INTEGER NOT NULL,
|
||||
order_id INTEGER NOT NULL,
|
||||
user_id INTEGER NOT NULL,
|
||||
rating INTEGER NOT NULL, -- 评分(1-5)
|
||||
content TEXT,
|
||||
images TEXT, -- JSON格式的评价图片URLs
|
||||
helpful_count INTEGER DEFAULT 0,
|
||||
reply_content TEXT, -- 商家回复
|
||||
status VARCHAR(20) DEFAULT 'active', -- 'active', 'hidden', 'deleted'
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (product_id) REFERENCES products(id) ON DELETE CASCADE,
|
||||
FOREIGN KEY (order_id) REFERENCES orders(id) ON DELETE CASCADE,
|
||||
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
|
||||
);
|
||||
|
||||
-- 购物车表
|
||||
CREATE TABLE IF NOT EXISTS shopping_cart (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
user_id INTEGER NOT NULL,
|
||||
product_id INTEGER NOT NULL,
|
||||
quantity INTEGER NOT NULL DEFAULT 1,
|
||||
specifications TEXT, -- JSON格式的选择规格
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
|
||||
FOREIGN KEY (product_id) REFERENCES products(id) ON DELETE CASCADE,
|
||||
UNIQUE(user_id, product_id)
|
||||
);
|
||||
|
||||
-- 优惠券表
|
||||
CREATE TABLE IF NOT EXISTS coupons (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
code VARCHAR(50) UNIQUE NOT NULL,
|
||||
name VARCHAR(100) NOT NULL,
|
||||
type VARCHAR(20) NOT NULL, -- 'fixed', 'percentage'
|
||||
value DECIMAL(10,2) NOT NULL,
|
||||
min_amount DECIMAL(10,2) DEFAULT 0, -- 最低消费金额
|
||||
max_discount DECIMAL(10,2), -- 最大优惠金额(百分比优惠券)
|
||||
usage_limit INTEGER DEFAULT 1, -- 使用次数限制
|
||||
used_count INTEGER DEFAULT 0, -- 已使用次数
|
||||
user_limit INTEGER DEFAULT 1, -- 每用户使用次数限制
|
||||
start_date DATETIME,
|
||||
end_date DATETIME,
|
||||
status VARCHAR(20) DEFAULT 'active', -- 'active', 'inactive', 'expired'
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
-- 用户优惠券使用记录表
|
||||
CREATE TABLE IF NOT EXISTS user_coupon_usage (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
user_id INTEGER NOT NULL,
|
||||
coupon_id INTEGER NOT NULL,
|
||||
order_id INTEGER,
|
||||
used_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
|
||||
FOREIGN KEY (coupon_id) REFERENCES coupons(id) ON DELETE CASCADE,
|
||||
FOREIGN KEY (order_id) REFERENCES orders(id) ON DELETE SET NULL
|
||||
);
|
||||
|
||||
-- 创建索引
|
||||
CREATE INDEX IF NOT EXISTS idx_products_category ON products(category);
|
||||
CREATE INDEX IF NOT EXISTS idx_products_seller ON products(seller_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_products_status ON products(status);
|
||||
CREATE INDEX IF NOT EXISTS idx_products_featured ON products(featured);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_orders_user ON orders(user_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_orders_status ON orders(status);
|
||||
CREATE INDEX IF NOT EXISTS idx_orders_payment_status ON orders(payment_status);
|
||||
CREATE INDEX IF NOT EXISTS idx_orders_order_date ON orders(order_date);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_order_items_order ON order_items(order_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_order_items_product ON order_items(product_id);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_reviews_product ON product_reviews(product_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_reviews_user ON product_reviews(user_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_reviews_rating ON product_reviews(rating);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_cart_user ON shopping_cart(user_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_cart_product ON shopping_cart(product_id);
|
||||
|
||||
-- 插入初始商品分类数据
|
||||
INSERT OR IGNORE INTO product_categories (id, name, parent_id, level, sort_order) VALUES
|
||||
(1, '牛肉制品', 0, 1, 1),
|
||||
(2, '乳制品', 0, 1, 2),
|
||||
(3, '休闲食品', 0, 1, 3),
|
||||
(4, '设备用品', 0, 1, 4),
|
||||
(5, '饲料用品', 0, 1, 5),
|
||||
(11, '新鲜牛肉', 1, 2, 1),
|
||||
(12, '牛肉干', 1, 2, 2),
|
||||
(13, '牛肉罐头', 1, 2, 3),
|
||||
(21, '鲜奶', 2, 2, 1),
|
||||
(22, '酸奶', 2, 2, 2),
|
||||
(23, '奶粉', 2, 2, 3);
|
||||
|
||||
-- 插入示例商品数据
|
||||
INSERT OR IGNORE INTO products (id, name, sku, category, description, price, original_price, stock, seller_id, status, featured, images, origin) VALUES
|
||||
(1, '优质牛肉礼盒装', 'BEEF001', 'beef', '来自锡林浩特优质牧场的新鲜牛肉,肉质鲜美,营养丰富', 268.00, 298.00, 45, 1, 'active', 1, '["https://example.com/beef1.jpg"]', '内蒙古锡林浩特'),
|
||||
(2, '有机牛奶', 'MILK001', 'dairy', '纯天然有机牛奶,无添加剂,营养价值高', 25.80, 28.00, 120, 1, 'active', 1, '["https://example.com/milk1.jpg"]', '内蒙古呼伦贝尔'),
|
||||
(3, '手工牛肉干', 'JERKY001', 'beef', '传统工艺制作,口感醇厚,便于携带', 58.00, 68.00, 80, 1, 'active', 0, '["https://example.com/jerky1.jpg"]', '内蒙古阿拉善'),
|
||||
(4, '牧场酸奶', 'YOGURT001', 'dairy', '新鲜牧场奶源,益生菌发酵,口感顺滑', 12.50, 15.00, 200, 1, 'active', 1, '["https://example.com/yogurt1.jpg"]', '内蒙古锡林郭勒'),
|
||||
(5, '精选牛排', 'STEAK001', 'beef', '优质牛排,适合煎烤,肉质鲜嫩', 128.00, 148.00, 30, 1, 'active', 1, '["https://example.com/steak1.jpg"]', '内蒙古通辽');
|
||||
|
||||
-- 插入示例优惠券数据
|
||||
INSERT OR IGNORE INTO coupons (id, code, name, type, value, min_amount, usage_limit, start_date, end_date, status) VALUES
|
||||
(1, 'WELCOME10', '新用户优惠券', 'fixed', 10.00, 50.00, 1000, '2024-01-01 00:00:00', '2024-12-31 23:59:59', 'active'),
|
||||
(2, 'SAVE20', '满200减20', 'fixed', 20.00, 200.00, 500, '2024-01-01 00:00:00', '2024-12-31 23:59:59', 'active'),
|
||||
(3, 'PERCENT5', '95折优惠', 'percentage', 5.00, 100.00, 300, '2024-01-01 00:00:00', '2024-12-31 23:59:59', 'active');
|
||||
|
||||
SELECT '商城数据库表创建完成!' AS message;
|
||||
60
backend/api/database/optimize.sql
Normal file
60
backend/api/database/optimize.sql
Normal file
@@ -0,0 +1,60 @@
|
||||
-- 数据库性能优化脚本
|
||||
-- 为xlxumu项目的核心表创建索引以提升查询性能
|
||||
|
||||
-- 用户表索引优化(已存在的跳过)
|
||||
-- CREATE INDEX IF NOT EXISTS idx_users_username ON users(username);
|
||||
-- CREATE INDEX IF NOT EXISTS idx_users_email ON users(email);
|
||||
-- CREATE INDEX IF NOT EXISTS idx_users_phone ON users(phone);
|
||||
-- CREATE INDEX IF NOT EXISTS idx_users_status ON users(status);
|
||||
-- CREATE INDEX IF NOT EXISTS idx_users_created_at ON users(created_at);
|
||||
|
||||
-- 牛只管理表索引优化(已存在的跳过)
|
||||
-- CREATE INDEX IF NOT EXISTS idx_cattle_ear_tag ON cattle(ear_tag);
|
||||
-- CREATE INDEX IF NOT EXISTS idx_cattle_owner_id ON cattle(owner_id);
|
||||
-- CREATE INDEX IF NOT EXISTS idx_cattle_breed ON cattle(breed);
|
||||
-- CREATE INDEX IF NOT EXISTS idx_cattle_status ON cattle(status);
|
||||
-- CREATE INDEX IF NOT EXISTS idx_cattle_birth_date ON cattle(birth_date);
|
||||
-- CREATE INDEX IF NOT EXISTS idx_cattle_created_at ON cattle(created_at);
|
||||
|
||||
-- 用户类型索引
|
||||
CREATE INDEX IF NOT EXISTS idx_users_user_type ON users(user_type);
|
||||
|
||||
-- 牛只性别和健康状态索引
|
||||
CREATE INDEX IF NOT EXISTS idx_cattle_gender ON cattle(gender);
|
||||
CREATE INDEX IF NOT EXISTS idx_cattle_health_status ON cattle(health_status);
|
||||
|
||||
-- 复合索引优化(针对常见查询组合)
|
||||
-- CREATE INDEX IF NOT EXISTS idx_cattle_owner_status ON cattle(owner_id, status);
|
||||
CREATE INDEX IF NOT EXISTS idx_users_type_status ON users(user_type, status);
|
||||
CREATE INDEX IF NOT EXISTS idx_cattle_owner_health ON cattle(owner_id, health_status);
|
||||
|
||||
-- 分析表统计信息(SQLite特定)
|
||||
ANALYZE;
|
||||
|
||||
-- 查询优化建议注释
|
||||
/*
|
||||
性能优化建议:
|
||||
|
||||
1. 查询优化:
|
||||
- 使用 LIMIT 限制返回结果数量
|
||||
- 避免 SELECT * ,只查询需要的字段
|
||||
- 使用 WHERE 条件过滤数据
|
||||
- 合理使用 ORDER BY 和索引配合
|
||||
|
||||
2. 索引使用:
|
||||
- 经常用于 WHERE 条件的字段应建立索引
|
||||
- 经常用于 ORDER BY 的字段应建立索引
|
||||
- 外键字段应建立索引
|
||||
- 避免在小表上建立过多索引
|
||||
|
||||
3. 数据库维护:
|
||||
- 定期运行 ANALYZE 更新统计信息
|
||||
- 定期运行 VACUUM 整理数据库文件
|
||||
- 监控慢查询日志
|
||||
|
||||
4. 应用层优化:
|
||||
- 使用连接池管理数据库连接
|
||||
- 实现查询结果缓存
|
||||
- 分页查询大数据集
|
||||
- 批量操作减少数据库交互次数
|
||||
*/
|
||||
BIN
backend/api/database/xlxumu_dev.db
Normal file
BIN
backend/api/database/xlxumu_dev.db
Normal file
Binary file not shown.
Reference in New Issue
Block a user