-- 项目付款进度表
CREATE TABLE oa_payment_progress (
                                     payment_progress_id BIGINT NOT NULL AUTO_INCREMENT COMMENT '付款进度ID',
                                     project_id          BIGINT NOT NULL            COMMENT '关联项目ID，引用 oa_project.project_id',
                                     start_time          DATETIME         NOT NULL            COMMENT '付款开始时间',
                                     end_time            DATETIME         DEFAULT NULL        COMMENT '付款结束时间',
                                     amount              DECIMAL(19,2)    NOT NULL            COMMENT '付款金额',
                                     is_void             TINYINT(1)       NOT NULL DEFAULT 0  COMMENT '是否作废：0正常；1作废',
                                     create_time         DATETIME         NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
                                     update_time         DATETIME         NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
                                     create_by           VARCHAR(64)      NOT NULL            COMMENT '创建人',
                                     update_by           VARCHAR(64)      NOT NULL            COMMENT '更新人',
                                     del_flag            TINYINT(1)       NOT NULL DEFAULT 0  COMMENT '删除标志：0正常；1已删除',
                                     remark              VARCHAR(500)     DEFAULT NULL        COMMENT '备注',
                                     PRIMARY KEY (payment_progress_id),
                                     INDEX idx_pp_proj   (project_id),
                                     CONSTRAINT fk_pp_proj FOREIGN KEY (project_id) REFERENCES sys_oa_project (project_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='项目付款进度表';


ALTER TABLE sys_oa_finance
    -- 添加关联付款进度的字段
    ADD COLUMN payment_progress_id BIGINT NULL COMMENT '关联 oa_payment_progress.payment_progress_id',
    -- 为该字段建立外键，删除进度时自动删除对应财务记录
    ADD CONSTRAINT fk_finance_to_pp
        FOREIGN KEY (payment_progress_id)
            REFERENCES oa_payment_progress (payment_progress_id)
            ON DELETE CASCADE
            ON UPDATE CASCADE;

ALTER TABLE sys_oa_detail
    ADD CONSTRAINT fk_detail_to_finance
        FOREIGN KEY (finance_id)
            REFERENCES sys_oa_finance (finance_id)
            ON DELETE CASCADE
            ON UPDATE CASCADE;


ALTER TABLE sys_oa_finance
    ADD COLUMN detail_start_time DATETIME NULL COMMENT '明细开始时间',
    ADD COLUMN detail_end_time   DATETIME NULL COMMENT '明细结束时间';