CREATE TABLE IF NOT EXISTS audit_flow (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    tenant_id BIGINT UNSIGNED NOT NULL,
    flow_code VARCHAR(64) NOT NULL,
    flow_name VARCHAR(128) NOT NULL,
    status VARCHAR(32) NOT NULL DEFAULT 'ENABLED',
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    UNIQUE KEY uk_tenant_flow_code (tenant_id, flow_code)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS audit_flow_node (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    flow_id BIGINT UNSIGNED NOT NULL,
    node_code VARCHAR(32) NOT NULL,
    node_name VARCHAR(64) NOT NULL,
    sort_no INT NOT NULL,
    status VARCHAR(32) NOT NULL DEFAULT 'ENABLED',
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    UNIQUE KEY uk_flow_node_code (flow_id, node_code),
    KEY idx_flow_sort (flow_id, sort_no)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

INSERT INTO audit_flow (id, tenant_id, flow_code, flow_name, status)
VALUES (1, 1, 'DEFAULT', '默认三审流程', 'ENABLED')
ON DUPLICATE KEY UPDATE flow_name = VALUES(flow_name), status = VALUES(status);

INSERT INTO audit_flow_node (id, flow_id, node_code, node_name, sort_no, status)
VALUES
    (1, 1, 'INIT_REVIEW', '初审', 1, 'ENABLED'),
    (2, 1, 'RE_REVIEW', '复审', 2, 'ENABLED'),
    (3, 1, 'FINAL_REVIEW', '终审', 3, 'ENABLED')
ON DUPLICATE KEY UPDATE node_name = VALUES(node_name), sort_no = VALUES(sort_no), status = VALUES(status);
