DECLARE
    v_count NUMBER;
BEGIN
    SELECT COUNT(1)
      INTO v_count
      FROM USER_TAB_COLUMNS
     WHERE TABLE_NAME = 'SEAL_CONTRACT_LAUNCH_MAIN'
       AND COLUMN_NAME = 'ARCHIVE_RECEIVE_DEPT';
    IF v_count = 0 THEN
        EXECUTE IMMEDIATE 'ALTER TABLE SEAL_CONTRACT_LAUNCH_MAIN ADD ARCHIVE_RECEIVE_DEPT VARCHAR2(100 CHAR)';
    END IF;
END;
/

COMMENT ON COLUMN SEAL_CONTRACT_LAUNCH_MAIN.ARCHIVE_RECEIVE_DEPT IS '合同归档接收部门编码';

DECLARE
    v_count NUMBER;
BEGIN
    SELECT COUNT(1)
      INTO v_count
      FROM USER_TAB_COLUMNS
     WHERE TABLE_NAME = 'SEAL_CONTRACT_LAUNCH_MAIN'
       AND COLUMN_NAME = 'ARCHIVE_TRANSFEROR';
    IF v_count = 0 THEN
        EXECUTE IMMEDIATE 'ALTER TABLE SEAL_CONTRACT_LAUNCH_MAIN ADD ARCHIVE_TRANSFEROR VARCHAR2(64 CHAR)';
    END IF;
END;
/

COMMENT ON COLUMN SEAL_CONTRACT_LAUNCH_MAIN.ARCHIVE_TRANSFEROR IS '移交人账号';

DECLARE
    v_count NUMBER;
BEGIN
    SELECT COUNT(1)
      INTO v_count
      FROM USER_TAB_COLUMNS
     WHERE TABLE_NAME = 'SEAL_CONTRACT_LAUNCH_FILE'
       AND COLUMN_NAME = 'ARCHIVE_ELE_ID';
    IF v_count = 0 THEN
        EXECUTE IMMEDIATE 'ALTER TABLE SEAL_CONTRACT_LAUNCH_FILE ADD ARCHIVE_ELE_ID NUMBER(19)';
    END IF;
END;
/

COMMENT ON COLUMN SEAL_CONTRACT_LAUNCH_FILE.ARCHIVE_ELE_ID IS '关联合同电子档案材料ID';

DECLARE
    v_count NUMBER;
BEGIN
    SELECT COUNT(1)
      INTO v_count
      FROM USER_TABLES
     WHERE TABLE_NAME = 'SEAL_CONTRACT_LAUNCH_AR_PAPER';
    IF v_count = 0 THEN
        EXECUTE IMMEDIATE '
            CREATE TABLE SEAL_CONTRACT_LAUNCH_AR_PAPER (
                ID NUMBER(19) NOT NULL,
                CONTRACT_ID NUMBER(19) NOT NULL,
                ARCHIVES_NAME VARCHAR2(255 CHAR) NOT NULL,
                ARCHIVES_NUM VARCHAR2(50 CHAR) NOT NULL,
                ARCHIVES_TIME DATE NOT NULL,
                SORT_ORDER NUMBER(10) DEFAULT 1,
                CREATE_BY VARCHAR2(64 CHAR),
                CREATE_TIME DATE DEFAULT SYSDATE,
                UPDATE_BY VARCHAR2(64 CHAR),
                UPDATE_TIME DATE DEFAULT SYSDATE,
                IS_DELETED VARCHAR2(2 CHAR) DEFAULT ''N'',
                REMARK VARCHAR2(1000 CHAR),
                CONSTRAINT PK_SEAL_CONTRACT_LAUNCH_AR_PAPER PRIMARY KEY (ID)
            )';
    END IF;
END;
/

COMMENT ON TABLE SEAL_CONTRACT_LAUNCH_AR_PAPER IS '合同归档纸质档案材料表';
COMMENT ON COLUMN SEAL_CONTRACT_LAUNCH_AR_PAPER.ID IS '主键ID';
COMMENT ON COLUMN SEAL_CONTRACT_LAUNCH_AR_PAPER.CONTRACT_ID IS '合同主表ID';
COMMENT ON COLUMN SEAL_CONTRACT_LAUNCH_AR_PAPER.ARCHIVES_NAME IS '档案名称';
COMMENT ON COLUMN SEAL_CONTRACT_LAUNCH_AR_PAPER.ARCHIVES_NUM IS '份数';
COMMENT ON COLUMN SEAL_CONTRACT_LAUNCH_AR_PAPER.ARCHIVES_TIME IS '归档时间';
COMMENT ON COLUMN SEAL_CONTRACT_LAUNCH_AR_PAPER.SORT_ORDER IS '排序号';
COMMENT ON COLUMN SEAL_CONTRACT_LAUNCH_AR_PAPER.IS_DELETED IS '逻辑删除标记';
COMMENT ON COLUMN SEAL_CONTRACT_LAUNCH_AR_PAPER.REMARK IS '备注';

DECLARE
    v_count NUMBER;
BEGIN
    SELECT COUNT(1)
      INTO v_count
      FROM USER_TABLES
     WHERE TABLE_NAME = 'SEAL_CONTRACT_LAUNCH_AR_ELE';
    IF v_count = 0 THEN
        EXECUTE IMMEDIATE '
            CREATE TABLE SEAL_CONTRACT_LAUNCH_AR_ELE (
                ID NUMBER(19) NOT NULL,
                CONTRACT_ID NUMBER(19) NOT NULL,
                ARCHIVES_NAME VARCHAR2(255 CHAR) NOT NULL,
                SORT_ORDER NUMBER(10) DEFAULT 1,
                CREATE_BY VARCHAR2(64 CHAR),
                CREATE_TIME DATE DEFAULT SYSDATE,
                UPDATE_BY VARCHAR2(64 CHAR),
                UPDATE_TIME DATE DEFAULT SYSDATE,
                IS_DELETED VARCHAR2(2 CHAR) DEFAULT ''N'',
                REMARK VARCHAR2(1000 CHAR),
                CONSTRAINT PK_SEAL_CONTRACT_LAUNCH_AR_ELE PRIMARY KEY (ID)
            )';
    END IF;
END;
/

COMMENT ON TABLE SEAL_CONTRACT_LAUNCH_AR_ELE IS '合同归档电子档案材料表';
COMMENT ON COLUMN SEAL_CONTRACT_LAUNCH_AR_ELE.ID IS '主键ID';
COMMENT ON COLUMN SEAL_CONTRACT_LAUNCH_AR_ELE.CONTRACT_ID IS '合同主表ID';
COMMENT ON COLUMN SEAL_CONTRACT_LAUNCH_AR_ELE.ARCHIVES_NAME IS '档案名称';
COMMENT ON COLUMN SEAL_CONTRACT_LAUNCH_AR_ELE.SORT_ORDER IS '排序号';
COMMENT ON COLUMN SEAL_CONTRACT_LAUNCH_AR_ELE.IS_DELETED IS '逻辑删除标记';
COMMENT ON COLUMN SEAL_CONTRACT_LAUNCH_AR_ELE.REMARK IS '备注';

DECLARE
    v_count NUMBER;
BEGIN
    SELECT COUNT(1)
      INTO v_count
      FROM USER_SEQUENCES
     WHERE SEQUENCE_NAME = 'SEQ_SEAL_CONTRACT_LAUNCH_AR_PAPER';
    IF v_count = 0 THEN
        EXECUTE IMMEDIATE 'CREATE SEQUENCE SEQ_SEAL_CONTRACT_LAUNCH_AR_PAPER START WITH 1 INCREMENT BY 1 NOCACHE NOCYCLE';
    END IF;
END;
/

DECLARE
    v_count NUMBER;
BEGIN
    SELECT COUNT(1)
      INTO v_count
      FROM USER_SEQUENCES
     WHERE SEQUENCE_NAME = 'SEQ_SEAL_CONTRACT_LAUNCH_AR_ELE';
    IF v_count = 0 THEN
        EXECUTE IMMEDIATE 'CREATE SEQUENCE SEQ_SEAL_CONTRACT_LAUNCH_AR_ELE START WITH 1 INCREMENT BY 1 NOCACHE NOCYCLE';
    END IF;
END;
/

DECLARE
    v_count NUMBER;
BEGIN
    SELECT COUNT(1)
      INTO v_count
      FROM USER_INDEXES
     WHERE INDEX_NAME = 'IDX_CONTRACT_LAUNCH_FILE_AR_ELE';
    IF v_count = 0 THEN
        EXECUTE IMMEDIATE 'CREATE INDEX IDX_CONTRACT_LAUNCH_FILE_AR_ELE ON SEAL_CONTRACT_LAUNCH_FILE (ARCHIVE_ELE_ID)';
    END IF;
END;
/

DECLARE
    v_count NUMBER;
BEGIN
    SELECT COUNT(1)
      INTO v_count
      FROM USER_INDEXES
     WHERE INDEX_NAME = 'IDX_CONTRACT_LAUNCH_AR_PAPER_CT';
    IF v_count = 0 THEN
        EXECUTE IMMEDIATE 'CREATE INDEX IDX_CONTRACT_LAUNCH_AR_PAPER_CT ON SEAL_CONTRACT_LAUNCH_AR_PAPER (CONTRACT_ID)';
    END IF;
END;
/

DECLARE
    v_count NUMBER;
BEGIN
    SELECT COUNT(1)
      INTO v_count
      FROM USER_INDEXES
     WHERE INDEX_NAME = 'IDX_CONTRACT_LAUNCH_AR_ELE_CT';
    IF v_count = 0 THEN
        EXECUTE IMMEDIATE 'CREATE INDEX IDX_CONTRACT_LAUNCH_AR_ELE_CT ON SEAL_CONTRACT_LAUNCH_AR_ELE (CONTRACT_ID)';
    END IF;
END;
/

BEGIN
    EXECUTE IMMEDIATE '
        UPDATE SEAL_CONTRACT_LAUNCH_MAIN
           SET ARCHIVE_RECEIVE_DEPT = ARCHIVE_KEEPER_UNIT_CODE
         WHERE ARCHIVE_RECEIVE_DEPT IS NULL
           AND ARCHIVE_KEEPER_UNIT_CODE IS NOT NULL';

    EXECUTE IMMEDIATE '
        UPDATE SEAL_CONTRACT_LAUNCH_MAIN
           SET ARCHIVE_TRANSFEROR = ARCHIVE_KEEPER
         WHERE ARCHIVE_TRANSFEROR IS NULL
           AND ARCHIVE_KEEPER IS NOT NULL';
END;
/

DECLARE
    CURSOR c_files IS
        SELECT ID, CONTRACT_ID, FILE_NAME
          FROM SEAL_CONTRACT_LAUNCH_FILE
         WHERE FILE_CATEGORY = 'archive'
           AND IS_DELETED = 'N'
           AND ARCHIVE_ELE_ID IS NULL;
    v_ele_id NUMBER;
BEGIN
    FOR rec IN c_files LOOP
        SELECT SEQ_SEAL_CONTRACT_LAUNCH_AR_ELE.NEXTVAL INTO v_ele_id FROM DUAL;
        INSERT INTO SEAL_CONTRACT_LAUNCH_AR_ELE (
            ID, CONTRACT_ID, ARCHIVES_NAME, SORT_ORDER,
            CREATE_BY, CREATE_TIME, UPDATE_BY, UPDATE_TIME, IS_DELETED, REMARK
        ) VALUES (
            v_ele_id, rec.CONTRACT_ID, NVL(rec.FILE_NAME, '历史归档附件'), 1,
            'patch', SYSDATE, 'patch', SYSDATE, 'N', '历史归档附件迁移'
        );

        UPDATE SEAL_CONTRACT_LAUNCH_FILE
           SET ARCHIVE_ELE_ID = v_ele_id
         WHERE ID = rec.ID;
    END LOOP;
    COMMIT;
END;
/
