-- =============================================
-- 秒杀活动表
-- =============================================
CREATE TABLE IF NOT EXISTS `shop_seckill` (
  `seckill_id` INT NOT NULL AUTO_INCREMENT COMMENT '秒杀活动ID',
  `goods_id` INT NOT NULL COMMENT '商品ID',
  `sku_id` INT DEFAULT 0 COMMENT 'SKU ID（多规格商品时指定SKU）',
  `seckill_price` DECIMAL(10,2) NOT NULL COMMENT '秒杀价格',
  `seckill_stock` INT NOT NULL DEFAULT 0 COMMENT '秒杀库存',
  `sold_count` INT NOT NULL DEFAULT 0 COMMENT '已售数量',
  `limit_per_user` INT NOT NULL DEFAULT 1 COMMENT '每人限购数量',
  `start_time` DATETIME NOT NULL COMMENT '活动开始时间',
  `end_time` DATETIME NOT NULL COMMENT '活动结束时间',
  `status` INT NOT NULL DEFAULT 0 COMMENT '状态：0-未开始 1-进行中 2-已结束',
  `sort_number` INT DEFAULT 0 COMMENT '排序号',
  `user_id` INT DEFAULT NULL COMMENT '创建者用户ID',
  `tenant_id` INT DEFAULT NULL COMMENT '租户ID',
  `deleted` INT NOT NULL DEFAULT 0 COMMENT '是否删除, 0否, 1是',
  `create_time` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
  PRIMARY KEY (`seckill_id`),
  KEY `idx_goods_id` (`goods_id`),
  KEY `idx_status` (`status`),
  KEY `idx_start_time` (`start_time`),
  KEY `idx_tenant_id` (`tenant_id`),
  KEY `idx_start_end` (`start_time`, `end_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='秒杀活动表';

-- =============================================
-- 秒杀订单表
-- =============================================
CREATE TABLE IF NOT EXISTS `shop_seckill_order` (
  `seckill_order_id` INT NOT NULL AUTO_INCREMENT COMMENT '秒杀订单ID',
  `seckill_id` INT NOT NULL COMMENT '秒杀活动ID',
  `user_id` INT NOT NULL COMMENT '用户ID',
  `goods_id` INT NOT NULL COMMENT '商品ID',
  `sku_id` INT DEFAULT 0 COMMENT 'SKU ID',
  `quantity` INT NOT NULL DEFAULT 1 COMMENT '购买数量',
  `seckill_price` DECIMAL(10,2) NOT NULL COMMENT '秒杀价格',
  `order_no` VARCHAR(64) NOT NULL COMMENT '订单编号',
  `order_id` INT DEFAULT NULL COMMENT '正式订单ID（支付后关联）',
  `order_status` INT NOT NULL DEFAULT 0 COMMENT '订单状态：0-待支付 1-已支付 2-已取消 3-已退款',
  `tenant_id` INT DEFAULT NULL COMMENT '租户ID',
  `deleted` INT NOT NULL DEFAULT 0 COMMENT '是否删除, 0否, 1是',
  `create_time` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
  PRIMARY KEY (`seckill_order_id`),
  UNIQUE KEY `uk_order_no` (`order_no`),
  KEY `idx_seckill_id` (`seckill_id`),
  KEY `idx_user_id` (`user_id`),
  KEY `idx_user_seckill` (`user_id`, `seckill_id`),
  KEY `idx_tenant_id` (`tenant_id`),
  KEY `idx_order_status` (`order_status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='秒杀订单表';
