-- =============================================
-- Oracle version for zgwl_manifest_tables.sql
-- Execute with: sqlplus developer/dev123@localhost/XEPDB1 @zgwl_manifest_tables_oracle.sql
-- =============================================

WHENEVER SQLERROR EXIT SQL.SQLCODE;

BEGIN
    EXECUTE IMMEDIATE 'DROP TABLE CONTRACT_TEMPLATE_MANAGE';
EXCEPTION
    WHEN OTHERS THEN
        IF SQLCODE != -942 THEN
            RAISE;
        END IF;
END;
/

BEGIN
    EXECUTE IMMEDIATE 'DROP SEQUENCE SEQ_CONTRACT_TEMPLATE_MANAGE';
EXCEPTION
    WHEN OTHERS THEN
        IF SQLCODE != -2289 THEN
            RAISE;
        END IF;
END;
/

CREATE TABLE CONTRACT_TEMPLATE_MANAGE (
    ID NUMBER(19) NOT NULL,
    TEMPLATE_CODE VARCHAR2(64 CHAR) NOT NULL,
    TEMPLATE_NAME VARCHAR2(200 CHAR) NOT NULL,
    SOURCE VARCHAR2(32 CHAR) NOT NULL,
    CONTRACT_TYPE VARCHAR2(64 CHAR) NOT NULL,
    VERSION VARCHAR2(32 CHAR) NOT NULL,
    STATUS VARCHAR2(20 CHAR) DEFAULT 'draft' NOT NULL,
    VARIABLE_COUNT NUMBER(10) DEFAULT 0 NOT NULL,
    DESCRIPTION VARCHAR2(1000 CHAR),
    USAGE_TIP VARCHAR2(1000 CHAR),
    TEMPLATE_FILE_NAME VARCHAR2(255 CHAR) NOT NULL,
    TEMPLATE_FILE_PATH VARCHAR2(500 CHAR) NOT NULL,
    PUBLISH_ORG_IDS_JSON CLOB,
    PUBLISH_ORG_NAMES_JSON CLOB,
    VARIABLE_MAPPINGS_JSON CLOB,
    CREATE_BY VARCHAR2(64 CHAR),
    CREATED_TIME TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP,
    UPDATED_BY VARCHAR2(64 CHAR),
    UPDATED_TIME TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP,
    DEL_FLAG CHAR(1 CHAR) DEFAULT '0' NOT NULL,
    CONSTRAINT PK_CONTRACT_TEMPLATE_MANAGE PRIMARY KEY (ID),
    CONSTRAINT UK_TEMPLATE_CODE UNIQUE (TEMPLATE_CODE)
);

CREATE INDEX IDX_TEMPLATE_NAME ON CONTRACT_TEMPLATE_MANAGE (TEMPLATE_NAME);
CREATE INDEX IDX_CONTRACT_TYPE ON CONTRACT_TEMPLATE_MANAGE (CONTRACT_TYPE);
CREATE INDEX IDX_STATUS ON CONTRACT_TEMPLATE_MANAGE (STATUS);

COMMENT ON TABLE CONTRACT_TEMPLATE_MANAGE IS '合同模板管理表';
COMMENT ON COLUMN CONTRACT_TEMPLATE_MANAGE.ID IS '主键ID';
COMMENT ON COLUMN CONTRACT_TEMPLATE_MANAGE.TEMPLATE_CODE IS '范本编号，后端自动生成';
COMMENT ON COLUMN CONTRACT_TEMPLATE_MANAGE.TEMPLATE_NAME IS '范本名称';
COMMENT ON COLUMN CONTRACT_TEMPLATE_MANAGE.SOURCE IS '范本类型：标准模板/推荐文本/示范文本/对方文本';
COMMENT ON COLUMN CONTRACT_TEMPLATE_MANAGE.CONTRACT_TYPE IS '合同类型';
COMMENT ON COLUMN CONTRACT_TEMPLATE_MANAGE.VERSION IS '版本号';
COMMENT ON COLUMN CONTRACT_TEMPLATE_MANAGE.STATUS IS '状态：draft/published/disabled';
COMMENT ON COLUMN CONTRACT_TEMPLATE_MANAGE.VARIABLE_COUNT IS '变量数量';
COMMENT ON COLUMN CONTRACT_TEMPLATE_MANAGE.DESCRIPTION IS '范本描述';
COMMENT ON COLUMN CONTRACT_TEMPLATE_MANAGE.USAGE_TIP IS '使用提示';
COMMENT ON COLUMN CONTRACT_TEMPLATE_MANAGE.TEMPLATE_FILE_NAME IS '模板文件名称';
COMMENT ON COLUMN CONTRACT_TEMPLATE_MANAGE.TEMPLATE_FILE_PATH IS '模板文件相对路径';
COMMENT ON COLUMN CONTRACT_TEMPLATE_MANAGE.PUBLISH_ORG_IDS_JSON IS '发布范围 orgId JSON 数组';
COMMENT ON COLUMN CONTRACT_TEMPLATE_MANAGE.PUBLISH_ORG_NAMES_JSON IS '发布范围 orgName JSON 数组';
COMMENT ON COLUMN CONTRACT_TEMPLATE_MANAGE.VARIABLE_MAPPINGS_JSON IS '变量映射 JSON 数组';
COMMENT ON COLUMN CONTRACT_TEMPLATE_MANAGE.CREATE_BY IS '创建人';
COMMENT ON COLUMN CONTRACT_TEMPLATE_MANAGE.CREATED_TIME IS '创建时间';
COMMENT ON COLUMN CONTRACT_TEMPLATE_MANAGE.UPDATED_BY IS '更新人';
COMMENT ON COLUMN CONTRACT_TEMPLATE_MANAGE.UPDATED_TIME IS '更新时间';
COMMENT ON COLUMN CONTRACT_TEMPLATE_MANAGE.DEL_FLAG IS '删除标记：0存在 2删除';

CREATE SEQUENCE SEQ_CONTRACT_TEMPLATE_MANAGE
    START WITH 1
    INCREMENT BY 1
    NOCACHE
    NOCYCLE;

CREATE SEQUENCE SEQ_CONTRACT_TEMPLATE_MANAGE INCREMENT BY 1 MINVALUE 1 MAXVALUE 9999999999999999999999999999 NOCYCLE CACHE 20 NOORDER
