-- =============================================
-- 武术赛事赛程编排系统 - 数据库表创建脚本
-- =============================================
-- 创建日期: 2025-12-08
-- 版本: v1.0
-- 说明: 创建赛程编排相关的4张核心表
-- =============================================

-- 1. 赛程编排分组表
CREATE TABLE `martial_schedule_group` (
  `id` bigint(0) NOT NULL COMMENT '主键ID',
  `competition_id` bigint(0) NOT NULL COMMENT '赛事ID',
  `group_name` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '分组名称(如:太极拳男组)',
  `project_id` bigint(0) NOT NULL COMMENT '项目ID',
  `project_name` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '项目名称',
  `category` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '组别(成年组、少年组等)',
  `project_type` tinyint(1) NOT NULL DEFAULT 1 COMMENT '项目类型(1=个人 2=集体)',
  `display_order` int(0) NOT NULL DEFAULT 0 COMMENT '显示顺序(集体项目优先,数字越小越靠前)',
  `total_participants` int(0) NULL DEFAULT 0 COMMENT '总参赛人数',
  `total_teams` int(0) NULL DEFAULT 0 COMMENT '总队伍数(仅集体项目)',
  `estimated_duration` int(0) NULL DEFAULT 0 COMMENT '预计时长(分钟)',
  `create_user` bigint(0) NULL DEFAULT NULL,
  `create_dept` bigint(0) NULL DEFAULT NULL,
  `create_time` datetime(0) NULL DEFAULT CURRENT_TIMESTAMP(0),
  `update_user` bigint(0) NULL DEFAULT NULL,
  `update_time` datetime(0) NULL DEFAULT CURRENT_TIMESTAMP(0) ON UPDATE CURRENT_TIMESTAMP(0),
  `status` int(0) NULL DEFAULT 1 COMMENT '状态(1-启用,2-禁用)',
  `is_deleted` int(0) NULL DEFAULT 0,
  `tenant_id` varchar(12) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT '000000',
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `idx_competition` (`competition_id`) USING BTREE,
  INDEX `idx_project` (`project_id`) USING BTREE,
  INDEX `idx_display_order` (`display_order`) USING BTREE,
  INDEX `idx_tenant` (`tenant_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '赛程编排分组表' ROW_FORMAT = Dynamic;

-- 2. 赛程编排明细表(场地时间段分配)
CREATE TABLE `martial_schedule_detail` (
  `id` bigint(0) NOT NULL COMMENT '主键ID',
  `schedule_group_id` bigint(0) NOT NULL COMMENT '分组ID',
  `competition_id` bigint(0) NOT NULL COMMENT '赛事ID',
  `venue_id` bigint(0) NOT NULL COMMENT '场地ID',
  `venue_name` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '场地名称',
  `schedule_date` date NOT NULL COMMENT '比赛日期',
  `time_period` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '时间段(morning/afternoon)',
  `time_slot` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '时间点(08:30/13:30)',
  `estimated_start_time` datetime(0) NULL DEFAULT NULL COMMENT '预计开始时间',
  `estimated_end_time` datetime(0) NULL DEFAULT NULL COMMENT '预计结束时间',
  `estimated_duration` int(0) NULL DEFAULT 0 COMMENT '预计时长(分钟)',
  `participant_count` int(0) NULL DEFAULT 0 COMMENT '参赛人数',
  `sort_order` int(0) NULL DEFAULT 0 COMMENT '场内顺序',
  `create_user` bigint(0) NULL DEFAULT NULL,
  `create_dept` bigint(0) NULL DEFAULT NULL,
  `create_time` datetime(0) NULL DEFAULT CURRENT_TIMESTAMP(0),
  `update_user` bigint(0) NULL DEFAULT NULL,
  `update_time` datetime(0) NULL DEFAULT CURRENT_TIMESTAMP(0) ON UPDATE CURRENT_TIMESTAMP(0),
  `status` int(0) NULL DEFAULT 1 COMMENT '状态(1-未开始,2-进行中,3-已完成)',
  `is_deleted` int(0) NULL DEFAULT 0,
  `tenant_id` varchar(12) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT '000000',
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `idx_group` (`schedule_group_id`) USING BTREE,
  INDEX `idx_competition` (`competition_id`) USING BTREE,
  INDEX `idx_venue_time` (`venue_id`, `schedule_date`, `time_slot`) USING BTREE,
  INDEX `idx_tenant` (`tenant_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '赛程编排明细表(场地时间段分配)' ROW_FORMAT = Dynamic;

-- 3. 赛程编排参赛者关联表
CREATE TABLE `martial_schedule_participant` (
  `id` bigint(0) NOT NULL COMMENT '主键ID',
  `schedule_detail_id` bigint(0) NOT NULL COMMENT '编排明细ID',
  `schedule_group_id` bigint(0) NOT NULL COMMENT '分组ID',
  `participant_id` bigint(0) NOT NULL COMMENT '参赛者ID(关联martial_athlete表)',
  `organization` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '单位名称',
  `player_name` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '选手姓名',
  `project_name` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '项目名称',
  `category` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '组别',
  `performance_order` int(0) NULL DEFAULT 0 COMMENT '出场顺序',
  `create_user` bigint(0) NULL DEFAULT NULL,
  `create_dept` bigint(0) NULL DEFAULT NULL,
  `create_time` datetime(0) NULL DEFAULT CURRENT_TIMESTAMP(0),
  `update_user` bigint(0) NULL DEFAULT NULL,
  `update_time` datetime(0) NULL DEFAULT CURRENT_TIMESTAMP(0) ON UPDATE CURRENT_TIMESTAMP(0),
  `status` int(0) NULL DEFAULT 1 COMMENT '状态(1-待出场,2-已出场)',
  `is_deleted` int(0) NULL DEFAULT 0,
  `tenant_id` varchar(12) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT '000000',
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `idx_detail` (`schedule_detail_id`) USING BTREE,
  INDEX `idx_group` (`schedule_group_id`) USING BTREE,
  INDEX `idx_participant` (`participant_id`) USING BTREE,
  INDEX `idx_tenant` (`tenant_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '赛程编排参赛者关联表' ROW_FORMAT = Dynamic;

-- 4. 赛程编排状态表
CREATE TABLE `martial_schedule_status` (
  `id` bigint(0) NOT NULL COMMENT '主键ID',
  `competition_id` bigint(0) NOT NULL UNIQUE COMMENT '赛事ID(唯一)',
  `schedule_status` tinyint(1) NOT NULL DEFAULT 0 COMMENT '编排状态(0=未编排 1=编排中 2=已保存锁定)',
  `last_auto_schedule_time` datetime(0) NULL DEFAULT NULL COMMENT '最后自动编排时间',
  `locked_time` datetime(0) NULL DEFAULT NULL COMMENT '锁定时间',
  `locked_by` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '锁定人',
  `total_groups` int(0) NULL DEFAULT 0 COMMENT '总分组数',
  `total_participants` int(0) NULL DEFAULT 0 COMMENT '总参赛人数',
  `create_user` bigint(0) NULL DEFAULT NULL,
  `create_dept` bigint(0) NULL DEFAULT NULL,
  `create_time` datetime(0) NULL DEFAULT CURRENT_TIMESTAMP(0),
  `update_user` bigint(0) NULL DEFAULT NULL,
  `update_time` datetime(0) NULL DEFAULT CURRENT_TIMESTAMP(0) ON UPDATE CURRENT_TIMESTAMP(0),
  `status` int(0) NULL DEFAULT 1 COMMENT '状态(1-启用,2-禁用)',
  `is_deleted` int(0) NULL DEFAULT 0,
  `tenant_id` varchar(12) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT '000000',
  PRIMARY KEY (`id`) USING BTREE,
  UNIQUE INDEX `uk_competition` (`competition_id`) USING BTREE,
  INDEX `idx_tenant` (`tenant_id`) USING BTREE,
  INDEX `idx_schedule_status` (`schedule_status`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '赛程编排状态表' ROW_FORMAT = Dynamic;

-- =============================================
-- 说明
-- =============================================
--
-- 使用方法:
-- 1. 在MySQL数据库中执行此脚本
-- 2. 确保已创建martial_competition数据库
--
-- 表关系说明:
-- martial_schedule_status (1) <--> (1) martial_competition (赛事编排状态)
-- martial_schedule_group (N) <--> (1) martial_competition (分组属于赛事)
-- martial_schedule_detail (N) <--> (1) martial_schedule_group (明细属于分组)
-- martial_schedule_participant (N) <--> (1) martial_schedule_detail (参赛者属于明细)
-- martial_schedule_participant (N) <--> (1) martial_athlete (参赛者关联选手)
--
-- 核心流程:
-- 1. 定时任务检查martial_schedule_status,找出schedule_status != 2的赛事
-- 2. 从martial_athlete加载参赛者数据
-- 3. 执行自动分组算法,写入martial_schedule_group
-- 4. 执行场地时间段分配,写入martial_schedule_detail
-- 5. 关联参赛者,写入martial_schedule_participant
-- 6. 更新martial_schedule_status的last_auto_schedule_time
--
-- =============================================
