-- =====================================================
-- 迁移脚本: 创建编排相关表
-- 版本: V5
-- 描述: 创建 schedule_plan, schedule_slot 等编排表
-- 日期: 2026-01-13
-- =====================================================

-- 1. martial_schedule_plan 编排方案表
CREATE TABLE IF NOT EXISTS martial_schedule_plan (
    id bigint NOT NULL,
    competition_id bigint DEFAULT NULL COMMENT '赛事ID',
    plan_name varchar(200) DEFAULT NULL COMMENT '方案名称',
    plan_type varchar(50) DEFAULT NULL COMMENT '方案类型',
    is_active tinyint DEFAULT 0 COMMENT '是否激活',
    tenant_id varchar(12) DEFAULT '000000',
    create_user bigint DEFAULT NULL,
    create_dept bigint DEFAULT NULL,
    create_time datetime DEFAULT NULL,
    update_user bigint DEFAULT NULL,
    update_time datetime DEFAULT NULL,
    status int DEFAULT 1,
    is_deleted int DEFAULT 0,
    PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='编排方案表';

-- 2. martial_schedule_slot 编排时间槽表
CREATE TABLE IF NOT EXISTS martial_schedule_slot (
    id bigint NOT NULL,
    competition_id bigint DEFAULT NULL COMMENT '赛事ID',
    venue_id bigint DEFAULT NULL COMMENT '场地ID',
    slot_date date DEFAULT NULL COMMENT '日期',
    start_time time DEFAULT NULL COMMENT '开始时间',
    end_time time DEFAULT NULL COMMENT '结束时间',
    slot_type varchar(50) DEFAULT NULL COMMENT '时间槽类型',
    tenant_id varchar(12) DEFAULT '000000',
    create_user bigint DEFAULT NULL,
    create_dept bigint DEFAULT NULL,
    create_time datetime DEFAULT NULL,
    update_user bigint DEFAULT NULL,
    update_time datetime DEFAULT NULL,
    status int DEFAULT 1,
    is_deleted int DEFAULT 0,
    PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='编排时间槽表';

-- 3. martial_schedule_athlete_slot 运动员时间槽关联表
CREATE TABLE IF NOT EXISTS martial_schedule_athlete_slot (
    id bigint NOT NULL,
    slot_id bigint DEFAULT NULL COMMENT '时间槽ID',
    athlete_id bigint DEFAULT NULL COMMENT '运动员ID',
    schedule_id bigint DEFAULT NULL COMMENT '编排ID',
    sequence int DEFAULT 0 COMMENT '顺序',
    tenant_id varchar(12) DEFAULT '000000',
    create_user bigint DEFAULT NULL,
    create_dept bigint DEFAULT NULL,
    create_time datetime DEFAULT NULL,
    update_user bigint DEFAULT NULL,
    update_time datetime DEFAULT NULL,
    status int DEFAULT 1,
    is_deleted int DEFAULT 0,
    PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='运动员时间槽关联表';

-- 4. martial_schedule_conflict 编排冲突表
CREATE TABLE IF NOT EXISTS martial_schedule_conflict (
    id bigint NOT NULL,
    competition_id bigint DEFAULT NULL COMMENT '赛事ID',
    athlete_id bigint DEFAULT NULL COMMENT '运动员ID',
    schedule_id_1 bigint DEFAULT NULL COMMENT '编排ID1',
    schedule_id_2 bigint DEFAULT NULL COMMENT '编排ID2',
    conflict_type varchar(50) DEFAULT NULL COMMENT '冲突类型',
    resolved tinyint DEFAULT 0 COMMENT '是否已解决',
    tenant_id varchar(12) DEFAULT '000000',
    create_user bigint DEFAULT NULL,
    create_dept bigint DEFAULT NULL,
    create_time datetime DEFAULT NULL,
    update_user bigint DEFAULT NULL,
    update_time datetime DEFAULT NULL,
    status int DEFAULT 1,
    is_deleted int DEFAULT 0,
    PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='编排冲突表';

-- 5. martial_schedule_adjustment_log 编排调整日志表
CREATE TABLE IF NOT EXISTS martial_schedule_adjustment_log (
    id bigint NOT NULL,
    competition_id bigint DEFAULT NULL COMMENT '赛事ID',
    schedule_id bigint DEFAULT NULL COMMENT '编排ID',
    adjustment_type varchar(50) DEFAULT NULL COMMENT '调整类型',
    before_value text COMMENT '调整前值',
    after_value text COMMENT '调整后值',
    reason varchar(500) DEFAULT NULL COMMENT '调整原因',
    operator_id bigint DEFAULT NULL COMMENT '操作人ID',
    tenant_id varchar(12) DEFAULT '000000',
    create_user bigint DEFAULT NULL,
    create_dept bigint DEFAULT NULL,
    create_time datetime DEFAULT NULL,
    update_user bigint DEFAULT NULL,
    update_time datetime DEFAULT NULL,
    status int DEFAULT 1,
    is_deleted int DEFAULT 0,
    PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='编排调整日志表';
