# 交易服务数据库设计文档

## 1. 概述

本文档详细描述了交易服务(trade-service)的数据库设计，包括商品信息、订单管理、支付记录等核心业务数据表结构。

## 2. 数据库环境

- 数据库类型：MySQL 8.0
- 字符集：utf8mb4
- 排序规则：utf8mb4_unicode_ci
- 数据库名：xlxumu_trade

## 3. 表结构设计

### 3.1 商品信息表 (products)

| 字段名 | 类型 | 约束 | 描述 |
|--------|------|------|------|
| id | BIGINT | PRIMARY KEY, AUTO_INCREMENT | 商品ID |
| seller_id | BIGINT | NOT NULL | 卖家用户ID |
| title | VARCHAR(200) | NOT NULL | 商品标题 |
| description | TEXT |  | 商品描述 |
| category | VARCHAR(50) | NOT NULL | 商品分类 |
| price | DECIMAL(10,2) | NOT NULL | 商品价格 |
| quantity | INT | NOT NULL, DEFAULT 0 | 商品数量 |
| status | VARCHAR(20) | NOT NULL, DEFAULT 'PENDING' | 商品状态(PENDING:待审核, APPROVED:已上架, REJECTED:已拒绝, SOLD_OUT:已售罄) |
| images | JSON |  | 商品图片URL列表 |
| created_at | DATETIME | NOT NULL, DEFAULT CURRENT_TIMESTAMP | 创建时间 |
| updated_at | DATETIME | NOT NULL, DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP | 更新时间 |

索引：
- INDEX idx_seller_id (seller_id)
- INDEX idx_category (category)
- INDEX idx_status (status)
- INDEX idx_created_at (created_at)

### 3.2 订单表 (orders)

| 字段名 | 类型 | 约束 | 描述 |
|--------|------|------|------|
| id | BIGINT | PRIMARY KEY, AUTO_INCREMENT | 订单ID |
| order_no | VARCHAR(32) | UNIQUE, NOT NULL | 订单编号 |
| buyer_id | BIGINT | NOT NULL | 买家用户ID |
| seller_id | BIGINT | NOT NULL | 卖家用户ID |
| product_id | BIGINT | NOT NULL | 商品ID |
| quantity | INT | NOT NULL | 购买数量 |
| unit_price | DECIMAL(10,2) | NOT NULL | 单价 |
| total_amount | DECIMAL(10,2) | NOT NULL | 总金额 |
| status | VARCHAR(20) | NOT NULL, DEFAULT 'CREATED' | 订单状态(CREATED:已创建, PAID:已支付, SHIPPED:已发货, DELIVERED:已送达, COMPLETED:已完成, CANCELLED:已取消) |
| shipping_address | TEXT | NOT NULL | 收货地址 |
| contact_phone | VARCHAR(20) | NOT NULL | 联系电话 |
| remark | TEXT |  | 买家备注 |
| created_at | DATETIME | NOT NULL, DEFAULT CURRENT_TIMESTAMP | 创建时间 |
| updated_at | DATETIME | NOT NULL, DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP | 更新时间 |

索引：
- UNIQUE INDEX uk_order_no (order_no)
- INDEX idx_buyer_id (buyer_id)
- INDEX idx_seller_id (seller_id)
- INDEX idx_product_id (product_id)
- INDEX idx_status (status)
- INDEX idx_created_at (created_at)

### 3.3 支付记录表 (payments)

| 字段名 | 类型 | 约束 | 描述 |
|--------|------|------|------|
| id | BIGINT | PRIMARY KEY, AUTO_INCREMENT | 支付ID |
| order_id | BIGINT | NOT NULL | 关联订单ID |
| payment_no | VARCHAR(64) | UNIQUE, NOT NULL | 支付流水号 |
| amount | DECIMAL(10,2) | NOT NULL | 支付金额 |
| payment_method | VARCHAR(20) | NOT NULL | 支付方式(WECHAT:微信支付, ALIPAY:支付宝, BANK:银行转账) |
| status | VARCHAR(20) | NOT NULL, DEFAULT 'PENDING' | 支付状态(PENDING:待支付, SUCCESS:支付成功, FAILED:支付失败, REFUNDED:已退款) |
| transaction_id | VARCHAR(100) |  | 第三方支付平台交易ID |
| paid_at | DATETIME |  | 实际支付时间 |
| created_at | DATETIME | NOT NULL, DEFAULT CURRENT_TIMESTAMP | 创建时间 |
| updated_at | DATETIME | NOT NULL, DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP | 更新时间 |

索引：
- UNIQUE INDEX uk_payment_no (payment_no)
- INDEX idx_order_id (order_id)
- INDEX idx_transaction_id (transaction_id)
- INDEX idx_status (status)
- INDEX idx_created_at (created_at)

### 3.4 物流信息表 (logistics)

| 字段名 | 类型 | 约束 | 描述 |
|--------|------|------|------|
| id | BIGINT | PRIMARY KEY, AUTO_INCREMENT | 物流ID |
| order_id | BIGINT | UNIQUE, NOT NULL | 关联订单ID |
| logistics_no | VARCHAR(50) | UNIQUE, NOT NULL | 物流单号 |
| company | VARCHAR(50) | NOT NULL | 物流公司 |
| status | VARCHAR(20) | NOT NULL, DEFAULT 'PREPARING' | 物流状态(PREPARING:备货中, SHIPPED:已发货, IN_TRANSIT:运输中, DELIVERED:已送达) |
| sender_info | TEXT | NOT NULL | 发货人信息 |
| receiver_info | TEXT | NOT NULL | 收货人信息 |
| shipped_at | DATETIME |  | 发货时间 |
| delivered_at | DATETIME |  | 送达时间 |
| tracking_info | JSON |  | 物流跟踪信息 |
| created_at | DATETIME | NOT NULL, DEFAULT CURRENT_TIMESTAMP | 创建时间 |
| updated_at | DATETIME | NOT NULL, DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP | 更新时间 |

索引：
- UNIQUE INDEX uk_order_id (order_id)
- UNIQUE INDEX uk_logistics_no (logistics_no)
- INDEX idx_company (company)
- INDEX idx_status (status)
- INDEX idx_created_at (created_at)

## 4. 表关系说明

1. 一个商品可以对应多个订单项（一对多）
2. 一个订单对应一个支付记录（一对一）
3. 一个订单对应一个物流信息（一对一）
4. 用户与商品、订单等通过用户ID关联

## 5. 数据初始化

### 5.1 商品状态枚举值
- PENDING: 待审核
- APPROVED: 已上架
- REJECTED: 已拒绝
- SOLD_OUT: 已售罄

### 5.2 订单状态流转
- CREATED -> PAID -> SHIPPED -> DELIVERED -> COMPLETED
- CREATED -> CANCELLED
- PAID -> CANCELLED (特殊情况下)

### 5.3 支付状态枚举值
- PENDING: 待支付
- SUCCESS: 支付成功
- FAILED: 支付失败
- REFUNDED: 已退款

### 5.4 物流状态枚举值
- PREPARING: 备货中
- SHIPPED: 已发货
- IN_TRANSIT: 运输中
- DELIVERED: 已送达

## 6. SQL脚本

```sql
-- 创建数据库
CREATE DATABASE IF NOT EXISTS xlxumu_trade 
  CHARACTER SET utf8mb4 
  COLLATE utf8mb4_unicode_ci;

USE xlxumu_trade;

-- 商品信息表
CREATE TABLE products (
  id BIGINT PRIMARY KEY AUTO_INCREMENT,
  seller_id BIGINT NOT NULL,
  title VARCHAR(200) NOT NULL,
  description TEXT,
  category VARCHAR(50) NOT NULL,
  price DECIMAL(10,2) NOT NULL,
  quantity INT NOT NULL DEFAULT 0,
  status VARCHAR(20) NOT NULL DEFAULT 'PENDING',
  images JSON,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  
  INDEX idx_seller_id (seller_id),
  INDEX idx_category (category),
  INDEX idx_status (status),
  INDEX idx_created_at (created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- 订单表
CREATE TABLE orders (
  id BIGINT PRIMARY KEY AUTO_INCREMENT,
  order_no VARCHAR(32) UNIQUE NOT NULL,
  buyer_id BIGINT NOT NULL,
  seller_id BIGINT NOT NULL,
  product_id BIGINT NOT NULL,
  quantity INT NOT NULL,
  unit_price DECIMAL(10,2) NOT NULL,
  total_amount DECIMAL(10,2) NOT NULL,
  status VARCHAR(20) NOT NULL DEFAULT 'CREATED',
  shipping_address TEXT NOT NULL,
  contact_phone VARCHAR(20) NOT NULL,
  remark TEXT,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  
  UNIQUE INDEX uk_order_no (order_no),
  INDEX idx_buyer_id (buyer_id),
  INDEX idx_seller_id (seller_id),
  INDEX idx_product_id (product_id),
  INDEX idx_status (status),
  INDEX idx_created_at (created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- 支付记录表
CREATE TABLE payments (
  id BIGINT PRIMARY KEY AUTO_INCREMENT,
  order_id BIGINT NOT NULL,
  payment_no VARCHAR(64) UNIQUE NOT NULL,
  amount DECIMAL(10,2) NOT NULL,
  payment_method VARCHAR(20) NOT NULL,
  status VARCHAR(20) NOT NULL DEFAULT 'PENDING',
  transaction_id VARCHAR(100),
  paid_at DATETIME,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  
  UNIQUE INDEX uk_payment_no (payment_no),
  INDEX idx_order_id (order_id),
  INDEX idx_transaction_id (transaction_id),
  INDEX idx_status (status),
  INDEX idx_created_at (created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- 物流信息表
CREATE TABLE logistics (
  id BIGINT PRIMARY KEY AUTO_INCREMENT,
  order_id BIGINT UNIQUE NOT NULL,
  logistics_no VARCHAR(50) UNIQUE NOT NULL,
  company VARCHAR(50) NOT NULL,
  status VARCHAR(20) NOT NULL DEFAULT 'PREPARING',
  sender_info TEXT NOT NULL,
  receiver_info TEXT NOT NULL,
  shipped_at DATETIME,
  delivered_at DATETIME,
  tracking_info JSON,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  
  UNIQUE INDEX uk_order_id (order_id),
  UNIQUE INDEX uk_logistics_no (logistics_no),
  INDEX idx_company (company),
  INDEX idx_status (status),
  INDEX idx_created_at (created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
```