-- 进度模板主表
CREATE TABLE `oa_schedule_template` (
                                        `template_id`      BIGINT       NOT NULL AUTO_INCREMENT COMMENT '模板主键',
                                        `template_name`    VARCHAR(64)  NOT NULL COMMENT '模板名称',
                                        `status`           INT          DEFAULT 1 COMMENT '状态(1启用 0停用)',
                                        `create_by`        VARCHAR(64)  DEFAULT NULL,
                                        `create_time`      DATETIME     DEFAULT CURRENT_TIMESTAMP,
                                        `update_by`        VARCHAR(64)  DEFAULT NULL,
                                        `update_time`      DATETIME     DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
                                        `del_flag`         CHAR(1)      DEFAULT '0' COMMENT '删除标识(0正常 1删除)',
                                        `remark`           VARCHAR(255) DEFAULT NULL,
                                        PRIMARY KEY (`template_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='进度模板主表';

-- 模板步骤明细
CREATE TABLE `oa_schedule_template_step` (
                                             `step_id`          BIGINT       NOT NULL AUTO_INCREMENT COMMENT '步骤主键',
                                             `template_id`      BIGINT       NOT NULL COMMENT '所属模板ID',
                                             `step_order`       INT          NOT NULL COMMENT '步骤顺序(从1开始)',
                                             `step_name`        VARCHAR(64)  NOT NULL COMMENT '步骤名称',
                                             `expected_days`    INT          DEFAULT NULL COMMENT '预期耗时(天)',
                                             `create_by`        VARCHAR(64)  DEFAULT NULL,
                                             `create_time`      DATETIME     DEFAULT CURRENT_TIMESTAMP,
                                             `update_by`        VARCHAR(64)  DEFAULT NULL,
                                             `update_time`      DATETIME     DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
                                             `del_flag`         CHAR(1)      DEFAULT '0',
                                             PRIMARY KEY (`step_id`),
                                             UNIQUE KEY `uniq_template_order` (`template_id`,`step_order`),
                                             CONSTRAINT `fk_tpl_step_template` FOREIGN KEY (`template_id`)
                                                 REFERENCES `oa_schedule_template` (`template_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='进度模板步骤表';

--------------------------------------------------------------------
-- 项目进度主表
CREATE TABLE `oa_project_schedule` (
                                       `schedule_id`      BIGINT       NOT NULL AUTO_INCREMENT COMMENT '项目进度主键',
                                       `project_id`       BIGINT       NOT NULL COMMENT '项目ID',
                                       `template_id`      BIGINT       NOT NULL COMMENT '引用的进度模板ID',
                                       `current_step`     INT          DEFAULT 0 COMMENT '当前进行到的步骤序号',
                                       `start_time`       DATETIME     DEFAULT CURRENT_TIMESTAMP COMMENT '进度开始时间',
                                       `end_time`         DATETIME     DEFAULT NULL COMMENT '进度完成时间',
                                       `status`           INT          DEFAULT 1 COMMENT '状态(1进行中 2已完成 0暂停)',
                                       `create_by`        VARCHAR(64)  DEFAULT NULL,
                                       `create_time`      DATETIME     DEFAULT CURRENT_TIMESTAMP,
                                       `update_by`        VARCHAR(64)  DEFAULT NULL,
                                       `update_time`      DATETIME     DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
                                       `del_flag`         CHAR(1)      DEFAULT '0',
                                       `remark`           VARCHAR(255) DEFAULT NULL,
                                       PRIMARY KEY (`schedule_id`),
                                       KEY `idx_project` (`project_id`),
                                       CONSTRAINT `fk_project_schedule_tpl` FOREIGN KEY (`template_id`)
                                           REFERENCES `oa_schedule_template` (`template_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='项目进度主表';

-- 项目进度步骤跟踪表
CREATE TABLE `oa_project_schedule_step` (
                                            `track_id`         BIGINT       NOT NULL AUTO_INCREMENT COMMENT '跟踪记录主键',
                                            `schedule_id`      BIGINT       NOT NULL COMMENT '所属项目进度ID',
                                            `step_order`       INT          NOT NULL COMMENT '步骤序号',
                                            `step_name`        VARCHAR(64)  NOT NULL COMMENT '步骤名称(冗余存储模板名称)',
                                            `plan_start`       DATETIME     DEFAULT NULL COMMENT '计划开始',
                                            `plan_end`         DATETIME     DEFAULT NULL COMMENT '计划完成',
                                            `actual_start`     DATETIME     DEFAULT NULL,
                                            `actual_end`       DATETIME     DEFAULT NULL,
                                            `status`           INT          DEFAULT 0 COMMENT '0未开始 1进行中 2完成 3暂停',
                                            `create_by`        VARCHAR(64)  DEFAULT NULL,
                                            `create_time`      DATETIME     DEFAULT CURRENT_TIMESTAMP,
                                            `update_by`        VARCHAR(64)  DEFAULT NULL,
                                            `update_time`      DATETIME     DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
                                            `del_flag`         CHAR(1)      DEFAULT '0',
                                            PRIMARY KEY (`track_id`),
                                            UNIQUE KEY `uniq_schedule_order` (`schedule_id`,`step_order`),
                                            CONSTRAINT `fk_project_step_main` FOREIGN KEY (`schedule_id`)
                                                REFERENCES `oa_project_schedule` (`schedule_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='项目进度步骤跟踪表';
``

create table if not exists oa_project_schedule_step
(
    track_id     bigint auto_increment comment '跟踪记录主键'
        primary key,
    accessory    varchar(500)                       null comment '文件列表',
    schedule_id  bigint                             not null comment '所属项目进度ID',
    step_order   int                                not null comment '步骤序号',
    step_name    varchar(64)                        not null comment '步骤名称(冗余存储模板名称)',
    plan_start   datetime                           null comment '计划开始',
    plan_end     datetime                           null comment '计划完成',
    actual_start datetime                           null,
    actual_end   datetime                           null,
    status       int      default 0                 null comment '0进行中 1完成 2暂停',
    create_by    varchar(64)                        null,
    create_time  datetime default CURRENT_TIMESTAMP null,
    update_by    varchar(64)                        null,
    update_time  datetime default CURRENT_TIMESTAMP null on update CURRENT_TIMESTAMP,
    del_flag     char     default '0'               null,
    header       varchar(40)                        null comment '进度负责人',
    use_flag     int      default 1                 null comment '使用标志 1为使用',
    batch_id     int                                null comment '批次号',
    constraint uniq_schedule_order
        unique (schedule_id, step_order,use_flag),
    constraint fk_project_step_main
        foreign key (schedule_id) references oa_project_schedule (schedule_id)
)
    comment '项目进度步骤跟踪表' charset = utf8mb4;


ALTER TABLE oa_project_schedule_step    -- 将其替换成上一步查到的 TABLE_NAME
    DROP FOREIGN KEY uniq_schedule_order;

-- 1) 删除旧唯一索引
ALTER TABLE oa_project_schedule_step
    DROP INDEX uniq_schedule_order;

-- 3.1 新建包含 use_flag 的唯一索引
ALTER TABLE oa_project_schedule_step
    ADD UNIQUE KEY uniq_schedule_order_new (schedule_id, step_order, use_flag);

SELECT
    rc.CONSTRAINT_NAME,
    rc.TABLE_NAME,
    kcu.COLUMN_NAME
FROM information_schema.REFERENTIAL_CONSTRAINTS rc
         JOIN information_schema.KEY_COLUMN_USAGE kcu
              ON rc.CONSTRAINT_NAME = kcu.CONSTRAINT_NAME
WHERE rc.REFERENCED_TABLE_NAME = ''
  AND rc.CONSTRAINT_SCHEMA    = DATABASE();

