SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for kylin_group_buy_activity
-- ----------------------------
CREATE TABLE IF NOT EXISTS `kylin_group_buy_activity` (
  `mid` int NOT NULL AUTO_INCREMENT,
  `activity_id` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '拼团活动ID',
  `name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '活动名称',
  `performance_id` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '演出ID',
  `min_group_size` int NOT NULL COMMENT '成团人数',
  `max_buy_per_user` int NOT NULL COMMENT '每人限购',
  `start_time` datetime NOT NULL COMMENT '活动开始时间',
  `end_time` datetime NOT NULL COMMENT '活动结束时间',
  `launch_type` tinyint NOT NULL COMMENT '启动方式 1立即 2手动 3定时',
  `scheduled_time` datetime DEFAULT NULL COMMENT '定时启动时间',
  `status` tinyint NOT NULL COMMENT '状态 0草稿 1待启动 2进行中 3已关闭 4已结束',
  `description` varchar(1000) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '活动说明',
  `created_at` datetime NOT NULL COMMENT '创建时间',
  `updated_at` datetime NOT NULL COMMENT '更新时间',
  PRIMARY KEY (`mid`) USING BTREE,
  UNIQUE KEY `uk_activity_id` (`activity_id`) USING BTREE,
  KEY `idx_performance_status` (`performance_id`, `status`) USING BTREE,
  KEY `idx_status_schedule` (`status`, `launch_type`, `scheduled_time`) USING BTREE,
  KEY `idx_status_end_time` (`status`, `end_time`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ROW_FORMAT=Dynamic COMMENT='演出票拼团活动';

-- ----------------------------
-- Table structure for kylin_group_buy_discount
-- ----------------------------
CREATE TABLE IF NOT EXISTS `kylin_group_buy_discount` (
  `mid` int NOT NULL AUTO_INCREMENT,
  `discount_id` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '优惠ID',
  `activity_id` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '拼团活动ID',
  `performance_id` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '演出ID',
  `time_id` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '场次ID',
  `ticket_id` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '票种ID',
  `discount_type` tinyint NOT NULL COMMENT '优惠类型 1固定价格 2折扣 3满减',
  `discount_value` decimal(10,2) NOT NULL COMMENT '优惠值',
  `created_at` datetime NOT NULL COMMENT '创建时间',
  PRIMARY KEY (`mid`) USING BTREE,
  UNIQUE KEY `uk_discount_id` (`discount_id`) USING BTREE,
  UNIQUE KEY `uk_activity_ticket` (`activity_id`, `time_id`, `ticket_id`) USING BTREE,
  KEY `idx_performance_ticket` (`performance_id`, `time_id`, `ticket_id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ROW_FORMAT=Dynamic COMMENT='演出票拼团优惠配置';

-- ----------------------------
-- Table structure for kylin_group_buy_order
-- ----------------------------
CREATE TABLE IF NOT EXISTS `kylin_group_buy_order` (
  `mid` int NOT NULL AUTO_INCREMENT,
  `group_order_id` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '拼团订单ID',
  `activity_id` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '拼团活动ID',
  `leader_user_id` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '团长用户ID',
  `order_tickets_id` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '团长票务订单ID',
  `target_count` int NOT NULL COMMENT '目标成团人数',
  `current_count` int NOT NULL DEFAULT 0 COMMENT '当前参团人数',
  `status` tinyint NOT NULL COMMENT '状态 -1待支付发起单 0拼单中 1拼成 2失败 3已取消',
  `is_public` tinyint NOT NULL DEFAULT 1 COMMENT '是否公开 1公开 0私密',
  `share_token` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '分享token',
  `expires_at` datetime DEFAULT NULL COMMENT '拼团过期时间',
  `created_at` datetime NOT NULL COMMENT '创建时间',
  `updated_at` datetime NOT NULL COMMENT '更新时间',
  PRIMARY KEY (`mid`) USING BTREE,
  UNIQUE KEY `uk_group_order_id` (`group_order_id`) USING BTREE,
  UNIQUE KEY `uk_share_token` (`share_token`) USING BTREE,
  KEY `idx_activity_status` (`activity_id`, `status`) USING BTREE,
  KEY `idx_status_expires` (`status`, `expires_at`) USING BTREE,
  KEY `idx_leader_status` (`leader_user_id`, `status`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ROW_FORMAT=Dynamic COMMENT='演出票拼团订单';

-- ----------------------------
-- Table structure for kylin_group_buy_member
-- ----------------------------
CREATE TABLE IF NOT EXISTS `kylin_group_buy_member` (
  `mid` int NOT NULL AUTO_INCREMENT,
  `member_id` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '成员ID',
  `group_order_id` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '拼团订单ID',
  `user_id` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '用户ID',
  `order_tickets_id` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '票务订单ID',
  `ticket_count` int NOT NULL COMMENT '购票张数',
  `status` tinyint NOT NULL DEFAULT -1 COMMENT '状态 -1待支付 0已加入 1已退款 2已取消',
  `join_time` datetime NOT NULL COMMENT '加入时间',
  `created_at` datetime NOT NULL COMMENT '创建时间',
  PRIMARY KEY (`mid`) USING BTREE,
  UNIQUE KEY `uk_member_id` (`member_id`) USING BTREE,
  UNIQUE KEY `uk_order_tickets_id` (`order_tickets_id`) USING BTREE,
  KEY `idx_group_order_id` (`group_order_id`) USING BTREE,
  KEY `idx_user_id` (`user_id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ROW_FORMAT=Dynamic COMMENT='演出票拼团成员';

-- 条件状态迁移示例：
-- UPDATE kylin_group_buy_order SET status = 1, updated_at = NOW()
-- WHERE group_order_id = ? AND status = 0 AND current_count >= target_count;
-- UPDATE kylin_group_buy_order SET status = 2, updated_at = NOW()
-- WHERE group_order_id = ? AND status = 0 AND expires_at < NOW();
-- UPDATE kylin_group_buy_order SET status = 3, updated_at = NOW()
-- WHERE group_order_id = ? AND status = 0;

SET FOREIGN_KEY_CHECKS = 1;
