# 商城服务数据库设计文档

## 1. 数据库环境说明

- 数据库名称: xlxumu_mall
- 字符集: utf8mb4
- 排序规则: utf8mb4_unicode_ci

## 2. 商品分类表(product_categories)

存储商城商品的分类信息。

```sql
CREATE TABLE product_categories (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  name VARCHAR(100) NOT NULL COMMENT '分类名称',
  parent_id BIGINT UNSIGNED DEFAULT NULL COMMENT '父分类ID',
  level TINYINT NOT NULL DEFAULT 1 COMMENT '分类层级',
  sort_order INT NOT NULL DEFAULT 0 COMMENT '排序',
  icon VARCHAR(255) COMMENT '分类图标',
  description TEXT COMMENT '分类描述',
  status ENUM('ACTIVE', 'INACTIVE') NOT NULL DEFAULT 'ACTIVE' COMMENT '状态',
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (id),
  INDEX idx_parent_id (parent_id),
  INDEX idx_level (level),
  INDEX idx_status (status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='商品分类表';
```

## 3. 商品信息表(products)

存储商城商品的基本信息。

```sql
CREATE TABLE products (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  category_id BIGINT UNSIGNED NOT NULL COMMENT '分类ID',
  seller_id BIGINT UNSIGNED NOT NULL COMMENT '卖家ID(关联user_center_service的用户ID)',
  name VARCHAR(255) NOT NULL COMMENT '商品名称',
  subtitle VARCHAR(255) COMMENT '商品副标题',
  description TEXT COMMENT '商品描述',
  cover_image VARCHAR(255) NOT NULL COMMENT '封面图片URL',
  images JSON COMMENT '商品图片URL列表',
  price DECIMAL(10,2) NOT NULL COMMENT '商品价格',
  original_price DECIMAL(10,2) COMMENT '原价',
  cost_price DECIMAL(10,2) COMMENT '成本价',
  stock_quantity INT NOT NULL DEFAULT 0 COMMENT '库存数量',
  sales_count INT NOT NULL DEFAULT 0 COMMENT '销售数量',
  status ENUM('DRAFT', 'PENDING', 'ACTIVE', 'INACTIVE', 'DELETED') NOT NULL DEFAULT 'DRAFT' COMMENT '商品状态',
  is_featured BOOLEAN NOT NULL DEFAULT FALSE COMMENT '是否推荐',
  is_new BOOLEAN NOT NULL DEFAULT TRUE COMMENT '是否新品',
  tags JSON COMMENT '商品标签',
  attributes JSON COMMENT '商品属性',
  specifications JSON COMMENT '商品规格',
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (id),
  INDEX idx_category_id (category_id),
  INDEX idx_seller_id (seller_id),
  INDEX idx_status (status),
  INDEX idx_is_featured (is_featured),
  INDEX idx_is_new (is_new),
  FULLTEXT idx_name (name)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='商品信息表';
```

## 4. 购物车表(shopping_carts)

存储用户的购物车信息。

```sql
CREATE TABLE shopping_carts (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  user_id BIGINT UNSIGNED NOT NULL COMMENT '用户ID(关联user_center_service的用户ID)',
  product_id BIGINT UNSIGNED NOT NULL COMMENT '商品ID',
  quantity INT NOT NULL DEFAULT 1 COMMENT '商品数量',
  selected BOOLEAN NOT NULL DEFAULT TRUE COMMENT '是否选中',
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (id),
  UNIQUE KEY uk_user_product (user_id, product_id),
  INDEX idx_user_id (user_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='购物车表';
```

## 5. 订单表(orders)

存储用户的订单信息。

```sql
CREATE TABLE orders (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  order_number VARCHAR(50) UNIQUE NOT NULL COMMENT '订单编号',
  user_id BIGINT UNSIGNED NOT NULL COMMENT '用户ID(关联user_center_service的用户ID)',
  seller_id BIGINT UNSIGNED NOT NULL COMMENT '卖家ID(关联user_center_service的用户ID)',
  total_amount DECIMAL(10,2) NOT NULL COMMENT '订单总金额',
  discount_amount DECIMAL(10,2) NOT NULL DEFAULT 0.00 COMMENT '优惠金额',
  shipping_amount DECIMAL(10,2) NOT NULL DEFAULT 0.00 COMMENT '运费',
  actual_amount DECIMAL(10,2) NOT NULL COMMENT '实际支付金额',
  status ENUM('PENDING_PAYMENT', 'PAID', 'SHIPPED', 'DELIVERED', 'COMPLETED', 'CANCELLED', 'REFUNDED') NOT NULL DEFAULT 'PENDING_PAYMENT' COMMENT '订单状态',
  payment_method VARCHAR(50) COMMENT '支付方式',
  payment_time TIMESTAMP NULL COMMENT '支付时间',
  shipping_address JSON NOT NULL COMMENT '收货地址',
  shipping_company VARCHAR(100) COMMENT '物流公司',
  shipping_number VARCHAR(100) COMMENT '物流单号',
  remark TEXT COMMENT '订单备注',
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (id),
  UNIQUE KEY uk_order_number (order_number),
  INDEX idx_user_id (user_id),
  INDEX idx_seller_id (seller_id),
  INDEX idx_status (status),
  INDEX idx_payment_time (payment_time)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='订单表';
```

## 6. 订单项表(order_items)

存储订单中的具体商品信息。

```sql
CREATE TABLE order_items (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  order_id BIGINT UNSIGNED NOT NULL COMMENT '订单ID',
  product_id BIGINT UNSIGNED NOT NULL COMMENT '商品ID',
  product_name VARCHAR(255) NOT NULL COMMENT '商品名称',
  product_image VARCHAR(255) COMMENT '商品图片URL',
  quantity INT NOT NULL COMMENT '商品数量',
  price DECIMAL(10,2) NOT NULL COMMENT '商品单价',
  total_amount DECIMAL(10,2) NOT NULL COMMENT '商品总价',
  attributes JSON COMMENT '商品属性',
  specifications JSON COMMENT '商品规格',
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (id),
  INDEX idx_order_id (order_id),
  INDEX idx_product_id (product_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='订单项表';
```

## 7. 收货地址表(shipping_addresses)

存储用户的收货地址信息。

```sql
CREATE TABLE shipping_addresses (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  user_id BIGINT UNSIGNED NOT NULL COMMENT '用户ID(关联user_center_service的用户ID)',
  name VARCHAR(50) NOT NULL COMMENT '收货人姓名',
  phone VARCHAR(20) NOT NULL COMMENT '联系电话',
  province VARCHAR(50) NOT NULL COMMENT '省份',
  city VARCHAR(50) NOT NULL COMMENT '城市',
  district VARCHAR(50) NOT NULL COMMENT '区县',
  detail_address VARCHAR(255) NOT NULL COMMENT '详细地址',
  is_default BOOLEAN NOT NULL DEFAULT FALSE COMMENT '是否默认地址',
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (id),
  INDEX idx_user_id (user_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='收货地址表';
```

## 8. 优惠券表(coupons)

存储商城的优惠券信息。

```sql
CREATE TABLE coupons (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  name VARCHAR(100) NOT NULL COMMENT '优惠券名称',
  type ENUM('DISCOUNT', 'FIXED_AMOUNT') NOT NULL COMMENT '优惠券类型',
  value DECIMAL(10,2) NOT NULL COMMENT '优惠值(折扣率或固定金额)',
  min_amount DECIMAL(10,2) COMMENT '使用最低金额',
  max_discount_amount DECIMAL(10,2) COMMENT '最大折扣金额',
  total_quantity INT NOT NULL COMMENT '总发行量',
  issued_quantity INT NOT NULL DEFAULT 0 COMMENT '已发行数量',
  used_quantity INT NOT NULL DEFAULT 0 COMMENT '已使用数量',
  start_time TIMESTAMP NOT NULL COMMENT '生效开始时间',
  end_time TIMESTAMP NOT NULL COMMENT '生效结束时间',
 适用范围 JSON COMMENT '适用范围(商品ID列表或分类ID列表)',
  status ENUM('ACTIVE', 'INACTIVE', 'EXPIRED') NOT NULL DEFAULT 'ACTIVE' COMMENT '状态',
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (id),
  INDEX idx_status (status),
  INDEX idx_start_time (start_time),
  INDEX idx_end_time (end_time)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='优惠券表';
```

## 9. 用户优惠券表(user_coupons)

存储用户领取的优惠券信息。

```sql
CREATE TABLE user_coupons (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  user_id BIGINT UNSIGNED NOT NULL COMMENT '用户ID(关联user_center_service的用户ID)',
  coupon_id BIGINT UNSIGNED NOT NULL COMMENT '优惠券ID',
  status ENUM('AVAILABLE', 'USED', 'EXPIRED') NOT NULL DEFAULT 'AVAILABLE' COMMENT '状态',
  received_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '领取时间',
  used_at TIMESTAMP NULL COMMENT '使用时间',
  order_id BIGINT UNSIGNED NULL COMMENT '使用的订单ID',
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (id),
  INDEX idx_user_id (user_id),
  INDEX idx_coupon_id (coupon_id),
  INDEX idx_status (status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='用户优惠券表';
```

## 10. 商品评价表(product_reviews)

存储用户对商品的评价信息。

```sql
CREATE TABLE product_reviews (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  product_id BIGINT UNSIGNED NOT NULL COMMENT '商品ID',
  user_id BIGINT UNSIGNED NOT NULL COMMENT '用户ID(关联user_center_service的用户ID)',
  order_id BIGINT UNSIGNED NOT NULL COMMENT '订单ID',
  rating TINYINT NOT NULL COMMENT '评分(1-5星)',
  content TEXT COMMENT '评价内容',
  images JSON COMMENT '评价图片URL列表',
  is_anonymous BOOLEAN NOT NULL DEFAULT FALSE COMMENT '是否匿名',
  status ENUM('PENDING', 'APPROVED', 'REJECTED') NOT NULL DEFAULT 'PENDING' COMMENT '审核状态',
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (id),
  INDEX idx_product_id (product_id),
  INDEX idx_user_id (user_id),
  INDEX idx_order_id (order_id),
  INDEX idx_rating (rating),
  INDEX idx_status (status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='商品评价表';
```

## 11. 商品收藏表(product_favorites)

存储用户收藏的商品信息。

```sql
CREATE TABLE product_favorites (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  user_id BIGINT UNSIGNED NOT NULL COMMENT '用户ID(关联user_center_service的用户ID)',
  product_id BIGINT UNSIGNED NOT NULL COMMENT '商品ID',
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  PRIMARY KEY (id),
  UNIQUE KEY uk_user_product (user_id, product_id),
  INDEX idx_user_id (user_id),
  INDEX idx_product_id (product_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='商品收藏表';
```

## 12. 商品浏览记录表(product_views)

存储用户浏览商品的记录。

```sql
CREATE TABLE product_views (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  user_id BIGINT UNSIGNED NOT NULL COMMENT '用户ID(关联user_center_service的用户ID)',
  product_id BIGINT UNSIGNED NOT NULL COMMENT '商品ID',
  view_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '浏览时间',
  PRIMARY KEY (id),
  INDEX idx_user_id (user_id),
  INDEX idx_product_id (product_id),
  INDEX idx_view_time (view_time)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='商品浏览记录表';
```