-- 规则引擎模块核心表结构（DM8）
-- 如果需要重建表，请在执行前备份现有数据

DROP TABLE IF EXISTS rule_release_record;
DROP TABLE IF EXISTS rule_business_relation;
DROP TABLE IF EXISTS rule_business;
DROP TABLE IF EXISTS rule_chain_dependency;
DROP TABLE IF EXISTS rule_chain;
DROP TABLE IF EXISTS rule_definition;

CREATE TABLE rule_definition (
    id BIGINT IDENTITY(1,1) PRIMARY KEY,
    code VARCHAR(128) NOT NULL,
    name VARCHAR(128) NOT NULL,
    type SMALLINT NOT NULL,
    dsl CLOB,
    script_language VARCHAR(64),
    bean_ref VARCHAR(128),
    config_json CLOB,
    status SMALLINT DEFAULT 0 NOT NULL,
    version VARCHAR(64),
    remark VARCHAR(512),
    creator VARCHAR(64) DEFAULT '' NOT NULL,
    create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
    updater VARCHAR(64) DEFAULT '' NOT NULL,
    update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
    deleted TINYINT DEFAULT 0 NOT NULL,
    tenant_id BIGINT DEFAULT 0 NOT NULL
);

COMMENT ON TABLE rule_definition IS '规则定义表';
COMMENT ON COLUMN rule_definition.id IS '规则定义编号';
COMMENT ON COLUMN rule_definition.code IS '规则编码';
COMMENT ON COLUMN rule_definition.name IS '规则名称';
COMMENT ON COLUMN rule_definition.type IS '规则类型';
COMMENT ON COLUMN rule_definition.dsl IS 'LiteFlow DSL 脚本';
COMMENT ON COLUMN rule_definition.script_language IS '脚本语言';
COMMENT ON COLUMN rule_definition.bean_ref IS 'Spring Bean 引用';
COMMENT ON COLUMN rule_definition.config_json IS '规则配置 JSON';
COMMENT ON COLUMN rule_definition.status IS '规则状态';
COMMENT ON COLUMN rule_definition.version IS '规则版本号';
COMMENT ON COLUMN rule_definition.remark IS '备注';
COMMENT ON COLUMN rule_definition.creator IS '创建者';
COMMENT ON COLUMN rule_definition.create_time IS '创建时间';
COMMENT ON COLUMN rule_definition.updater IS '更新者';
COMMENT ON COLUMN rule_definition.update_time IS '更新时间';
COMMENT ON COLUMN rule_definition.deleted IS '是否删除';
COMMENT ON COLUMN rule_definition.tenant_id IS '租户编号';

CREATE UNIQUE INDEX uk_rule_definition_code_tenant ON rule_definition (code, tenant_id);

CREATE TABLE rule_chain (
    id BIGINT IDENTITY(1,1) PRIMARY KEY,
    code VARCHAR(128) NOT NULL,
    name VARCHAR(128) NOT NULL,
    description VARCHAR(512),
    structure_json CLOB,
    liteflow_dsl CLOB,
    status SMALLINT DEFAULT 0 NOT NULL,
    version VARCHAR(64),
    remark VARCHAR(512),
    creator VARCHAR(64) DEFAULT '' NOT NULL,
    create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
    updater VARCHAR(64) DEFAULT '' NOT NULL,
    update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
    deleted TINYINT DEFAULT 0 NOT NULL,
    tenant_id BIGINT DEFAULT 0 NOT NULL
);

COMMENT ON TABLE rule_chain IS '规则链表';
COMMENT ON COLUMN rule_chain.id IS '规则链编号';
COMMENT ON COLUMN rule_chain.code IS '规则链编码';
COMMENT ON COLUMN rule_chain.name IS '规则链名称';
COMMENT ON COLUMN rule_chain.description IS '规则链描述';
COMMENT ON COLUMN rule_chain.structure_json IS '链路结构 JSON';
COMMENT ON COLUMN rule_chain.liteflow_dsl IS 'LiteFlow DSL 内容';
COMMENT ON COLUMN rule_chain.status IS '规则链状态';
COMMENT ON COLUMN rule_chain.version IS '版本号';
COMMENT ON COLUMN rule_chain.remark IS '备注';
COMMENT ON COLUMN rule_chain.creator IS '创建者';
COMMENT ON COLUMN rule_chain.create_time IS '创建时间';
COMMENT ON COLUMN rule_chain.updater IS '更新者';
COMMENT ON COLUMN rule_chain.update_time IS '更新时间';
COMMENT ON COLUMN rule_chain.deleted IS '是否删除';
COMMENT ON COLUMN rule_chain.tenant_id IS '租户编号';

CREATE UNIQUE INDEX uk_rule_chain_code_tenant ON rule_chain (code, tenant_id);
CREATE INDEX idx_rule_chain_status ON rule_chain (status);

CREATE TABLE rule_chain_dependency (
    id BIGINT IDENTITY(1,1) PRIMARY KEY,
    parent_chain_id BIGINT NOT NULL,
    child_rule_id BIGINT NOT NULL,
    link_type SMALLINT NOT NULL,
    order_index INTEGER,
    parallel_group VARCHAR(64),
    condition_expr VARCHAR(512),
    config_json CLOB,
    creator VARCHAR(64) DEFAULT '' NOT NULL,
    create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
    updater VARCHAR(64) DEFAULT '' NOT NULL,
    update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
    deleted TINYINT DEFAULT 0 NOT NULL,
    tenant_id BIGINT DEFAULT 0 NOT NULL
);

COMMENT ON TABLE rule_chain_dependency IS '规则链依赖表';
COMMENT ON COLUMN rule_chain_dependency.id IS '依赖编号';
COMMENT ON COLUMN rule_chain_dependency.parent_chain_id IS '父规则链编号';
COMMENT ON COLUMN rule_chain_dependency.child_rule_id IS '引用的规则定义编号';
COMMENT ON COLUMN rule_chain_dependency.link_type IS '节点类型';
COMMENT ON COLUMN rule_chain_dependency.order_index IS '执行顺序';
COMMENT ON COLUMN rule_chain_dependency.parallel_group IS '并行组标识';
COMMENT ON COLUMN rule_chain_dependency.condition_expr IS '条件表达式';
COMMENT ON COLUMN rule_chain_dependency.config_json IS '节点配置 JSON';
COMMENT ON COLUMN rule_chain_dependency.creator IS '创建者';
COMMENT ON COLUMN rule_chain_dependency.create_time IS '创建时间';
COMMENT ON COLUMN rule_chain_dependency.updater IS '更新者';
COMMENT ON COLUMN rule_chain_dependency.update_time IS '更新时间';
COMMENT ON COLUMN rule_chain_dependency.deleted IS '是否删除';
COMMENT ON COLUMN rule_chain_dependency.tenant_id IS '租户编号';

CREATE INDEX idx_rule_chain_dependency_parent ON rule_chain_dependency (parent_chain_id);
CREATE INDEX idx_rule_chain_dependency_child ON rule_chain_dependency (child_rule_id);

CREATE TABLE rule_business (
    id BIGINT IDENTITY(1,1) PRIMARY KEY,
    business VARCHAR(128) NOT NULL,
    rule_chain_id BIGINT,
    override_strategy SMALLINT DEFAULT 0 NOT NULL,
    locked TINYINT DEFAULT 0 NOT NULL,
    effective_version VARCHAR(64),
    config_json CLOB,
    remark VARCHAR(512),
    creator VARCHAR(64) DEFAULT '' NOT NULL,
    create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
    updater VARCHAR(64) DEFAULT '' NOT NULL,
    update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
    deleted TINYINT DEFAULT 0 NOT NULL,
    tenant_id BIGINT DEFAULT 0 NOT NULL
);

COMMENT ON TABLE rule_business IS '业务规则绑定表';
COMMENT ON COLUMN rule_business.id IS '业务绑定编号';
COMMENT ON COLUMN rule_business.business IS '业务标识';
COMMENT ON COLUMN rule_business.rule_chain_id IS '绑定的规则链编号';
COMMENT ON COLUMN rule_business.override_strategy IS '覆盖策略';
COMMENT ON COLUMN rule_business.locked IS '是否锁定';
COMMENT ON COLUMN rule_business.effective_version IS '生效版本';
COMMENT ON COLUMN rule_business.config_json IS '业务配置 JSON';
COMMENT ON COLUMN rule_business.remark IS '备注';
COMMENT ON COLUMN rule_business.creator IS '创建者';
COMMENT ON COLUMN rule_business.create_time IS '创建时间';
COMMENT ON COLUMN rule_business.updater IS '更新者';
COMMENT ON COLUMN rule_business.update_time IS '更新时间';
COMMENT ON COLUMN rule_business.deleted IS '是否删除';
COMMENT ON COLUMN rule_business.tenant_id IS '租户编号';

CREATE UNIQUE INDEX uk_rule_business_tenant ON rule_business (business, tenant_id);
CREATE INDEX idx_rule_business_chain ON rule_business (rule_chain_id);

CREATE TABLE rule_business_relation (
    id BIGINT IDENTITY(1,1) PRIMARY KEY,
    parent_business VARCHAR(128) NOT NULL,
    child_business VARCHAR(128) NOT NULL,
    sort INTEGER DEFAULT 0 NOT NULL,
    creator VARCHAR(64) DEFAULT '' NOT NULL,
    create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
    updater VARCHAR(64) DEFAULT '' NOT NULL,
    update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
    deleted TINYINT DEFAULT 0 NOT NULL,
    tenant_id BIGINT DEFAULT 0 NOT NULL
);

COMMENT ON TABLE rule_business_relation IS '业务继承关系表';
COMMENT ON COLUMN rule_business_relation.id IS '继承关系编号';
COMMENT ON COLUMN rule_business_relation.parent_business IS '父业务标识';
COMMENT ON COLUMN rule_business_relation.child_business IS '子业务标识';
COMMENT ON COLUMN rule_business_relation.sort IS '排序';
COMMENT ON COLUMN rule_business_relation.creator IS '创建者';
COMMENT ON COLUMN rule_business_relation.create_time IS '创建时间';
COMMENT ON COLUMN rule_business_relation.updater IS '更新者';
COMMENT ON COLUMN rule_business_relation.update_time IS '更新时间';
COMMENT ON COLUMN rule_business_relation.deleted IS '是否删除';
COMMENT ON COLUMN rule_business_relation.tenant_id IS '租户编号';

CREATE UNIQUE INDEX uk_rule_business_relation_child ON rule_business_relation (child_business, tenant_id);
CREATE INDEX idx_rule_business_relation_parent ON rule_business_relation (parent_business);

CREATE TABLE rule_release_record (
    id BIGINT IDENTITY(1,1) PRIMARY KEY,
    business VARCHAR(128) NOT NULL,
    chain_id VARCHAR(255) NOT NULL,
    chain_code VARCHAR(128),
    version VARCHAR(64) NOT NULL,
    status SMALLINT DEFAULT 0 NOT NULL,
    release_user_id BIGINT,
    release_user_name VARCHAR(128),
    release_time TIMESTAMP,
    remark VARCHAR(512),
    creator VARCHAR(64) DEFAULT '' NOT NULL,
    create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
    updater VARCHAR(64) DEFAULT '' NOT NULL,
    update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
    deleted TINYINT DEFAULT 0 NOT NULL,
    tenant_id BIGINT DEFAULT 0 NOT NULL
);

COMMENT ON TABLE rule_release_record IS '规则发布记录表';
COMMENT ON COLUMN rule_release_record.id IS '发布记录编号';
COMMENT ON COLUMN rule_release_record.business IS '业务标识';
COMMENT ON COLUMN rule_release_record.chain_id IS '生成链路标识';
COMMENT ON COLUMN rule_release_record.chain_code IS '规则链编码';
COMMENT ON COLUMN rule_release_record.version IS '发布版本';
COMMENT ON COLUMN rule_release_record.status IS '发布状态';
COMMENT ON COLUMN rule_release_record.release_user_id IS '发布人编号';
COMMENT ON COLUMN rule_release_record.release_user_name IS '发布人名称';
COMMENT ON COLUMN rule_release_record.release_time IS '发布时间';
COMMENT ON COLUMN rule_release_record.remark IS '备注';
COMMENT ON COLUMN rule_release_record.creator IS '创建者';
COMMENT ON COLUMN rule_release_record.create_time IS '创建时间';
COMMENT ON COLUMN rule_release_record.updater IS '更新者';
COMMENT ON COLUMN rule_release_record.update_time IS '更新时间';
COMMENT ON COLUMN rule_release_record.deleted IS '是否删除';
COMMENT ON COLUMN rule_release_record.tenant_id IS '租户编号';

CREATE INDEX idx_rule_release_business_time ON rule_release_record (business, release_time);
CREATE UNIQUE INDEX uk_rule_release_business_version ON rule_release_record (business, version, tenant_id);
