-- iWork 流程日志表（达梦数据库）
-- 合并了流程创建日志和回调日志
CREATE TABLE system_iwork_workflow_log (
    id BIGINT NOT NULL,
    request_id VARCHAR(128) NOT NULL,
    workflow_id BIGINT,
    business_code VARCHAR(128),
    biz_callback_key VARCHAR(255),
    raw_request VARCHAR(2000),
    status VARCHAR(32),
    callback_status INTEGER,
    retry_count INTEGER DEFAULT 0,
    max_retry INTEGER,
    last_error_message VARCHAR(512),
    raw_callback VARCHAR(2000),
    last_callback_time TIMESTAMP,
    creator VARCHAR(64) DEFAULT '',
    create_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updater VARCHAR(64) DEFAULT '',
    update_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    deleted SMALLINT NOT NULL DEFAULT 0,
    tenant_id BIGINT NOT NULL DEFAULT 0,
    PRIMARY KEY (id)
);

-- 添加注释
COMMENT ON TABLE system_iwork_workflow_log IS 'iWork 流程日志';
COMMENT ON COLUMN system_iwork_workflow_log.id IS '主键';
COMMENT ON COLUMN system_iwork_workflow_log.request_id IS 'iWork 请求编号';
COMMENT ON COLUMN system_iwork_workflow_log.workflow_id IS '流程模板 ID';
COMMENT ON COLUMN system_iwork_workflow_log.business_code IS '业务编码';
COMMENT ON COLUMN system_iwork_workflow_log.biz_callback_key IS '业务回调标识';
COMMENT ON COLUMN system_iwork_workflow_log.raw_request IS '创建请求原文';
COMMENT ON COLUMN system_iwork_workflow_log.status IS '流程状态';
COMMENT ON COLUMN system_iwork_workflow_log.callback_status IS '回调处理状态';
COMMENT ON COLUMN system_iwork_workflow_log.retry_count IS '已重试次数';
COMMENT ON COLUMN system_iwork_workflow_log.max_retry IS '最大重试次数';
COMMENT ON COLUMN system_iwork_workflow_log.last_error_message IS '最后错误信息';
COMMENT ON COLUMN system_iwork_workflow_log.raw_callback IS '回调原文';
COMMENT ON COLUMN system_iwork_workflow_log.last_callback_time IS '最近回调时间';
COMMENT ON COLUMN system_iwork_workflow_log.creator IS '创建者';
COMMENT ON COLUMN system_iwork_workflow_log.create_time IS '创建时间';
COMMENT ON COLUMN system_iwork_workflow_log.updater IS '更新者';
COMMENT ON COLUMN system_iwork_workflow_log.update_time IS '更新时间';
COMMENT ON COLUMN system_iwork_workflow_log.deleted IS '是否删除';
COMMENT ON COLUMN system_iwork_workflow_log.tenant_id IS '租户编号';

-- 创建唯一索引
CREATE UNIQUE INDEX uk_iwork_workflow_log_request_id ON system_iwork_workflow_log(request_id);

-- 创建普通索引
-- CREATE INDEX idx_iwork_workflow_log_business_code ON system_iwork_workflow_log(business_code);
-- CREATE INDEX idx_iwork_workflow_log_biz_callback_key ON system_iwork_workflow_log(biz_callback_key);
