CREATE TABLE `shop_order` (
  `order_id` BIGINT NOT NULL AUTO_INCREMENT COMMENT '订单唯一ID',
  `ucid` VARCHAR(32) DEFAULT NULL COMMENT 'ucid',
  `openid` VARCHAR(32) NOT NULL COMMENT 'openid',
  `trade_id` VARCHAR(32) NOT NULL COMMENT '支付网关交易id',
  `total_amount` DECIMAL(15,2) NOT NULL COMMENT '订单总金额',
  `status` TINYINT NOT NULL DEFAULT 1 COMMENT '订单状态（1待付款 2已付款 3已发货 4已完成 5已取消）',
  `pay_status` TINYINT NOT NULL DEFAULT 1 COMMENT '支付状态（1未支付 2已支付 3退款中 4已退款）',
  `payment_method` VARCHAR(32) DEFAULT NULL COMMENT '支付方式',
  `pay_time` DATETIME DEFAULT NULL COMMENT '支付时间',
  `creator_id` BIGINT NOT NULL DEFAULT 0 COMMENT '创建者ID',
  `create_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updater_id` BIGINT NOT NULL DEFAULT 0 COMMENT '更新者ID',
  `update_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  `deleted` TINYINT(1) NOT NULL DEFAULT 0 COMMENT '删除标志（0存在 1删除）',
  PRIMARY KEY (`order_id`),
  KEY `idx_status` (`status`),
  KEY `idx_pay_status` (`pay_status`),
  KEY `idx_create_time` (`create_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='商品订单表';

CREATE TABLE `shop_order_goods` (
  `order_goods_id` BIGINT NOT NULL AUTO_INCREMENT COMMENT '订单商品唯一ID',
  `order_id` BIGINT NOT NULL COMMENT '关联订单ID',
  `goods_id` BIGINT NOT NULL COMMENT '关联商品ID',
  `quantity` INT NOT NULL DEFAULT 1 COMMENT '购买数量',
  `price` DECIMAL(15,2) NOT NULL COMMENT '购买时单价',
  `total_amount` DECIMAL(15,2) GENERATED ALWAYS AS (quantity * price) STORED COMMENT '商品总金额',
  `status` TINYINT NOT NULL DEFAULT 1 COMMENT '商品状态（1正常 2已退货 3已换货）',
  `creator_id` BIGINT NOT NULL DEFAULT 0 COMMENT '创建者ID',
  `create_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updater_id` BIGINT NOT NULL DEFAULT 0 COMMENT '更新者ID',
  `update_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  `deleted` TINYINT(1) NOT NULL DEFAULT 0 COMMENT '删除标志（0存在 1删除）',
  PRIMARY KEY (`order_goods_id`),
  KEY `idx_order` (`order_id`),
  KEY `idx_goods` (`goods_id`),
  KEY `idx_status` (`status`),
  KEY `idx_create_time` (`create_time`),
  CONSTRAINT `fk_order_goods_order` FOREIGN KEY (`order_id`) REFERENCES `shop_order` (`order_id`),
  CONSTRAINT `fk_order_goods_goods` FOREIGN KEY (`goods_id`) REFERENCES `shop_goods` (`goods_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='订单商品明细表';