ALTER TABLE template
    ADD COLUMN biz_scene VARCHAR(64) NOT NULL DEFAULT 'MEETING_RECOMMEND' AFTER meeting_id;

CREATE TABLE IF NOT EXISTS template_flow_link (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    tenant_id BIGINT UNSIGNED NOT NULL,
    scene_code VARCHAR(64) NOT NULL,
    template_id BIGINT UNSIGNED NOT NULL,
    is_deleted TINYINT(1) NOT NULL DEFAULT 0,
    created_by BIGINT UNSIGNED NOT NULL DEFAULT 0,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_by BIGINT UNSIGNED NOT NULL DEFAULT 0,
    updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    UNIQUE KEY uk_tenant_scene (tenant_id, scene_code),
    KEY idx_tenant_template (tenant_id, template_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

INSERT INTO template_flow_link (tenant_id, scene_code, template_id, created_by, updated_by)
SELECT t.tenant_id, 'MEETING_RECOMMEND', t.id, 0, 0
FROM template t
WHERE t.tenant_id = 1 AND t.status='PUBLISHED'
ORDER BY t.id DESC
LIMIT 1
ON DUPLICATE KEY UPDATE template_id = VALUES(template_id), updated_at = CURRENT_TIMESTAMP;

INSERT INTO permission (id, permission_code, permission_name, module)
VALUES
    (1050, 'template.flow.link', '模板流程联动绑定', 'template')
ON DUPLICATE KEY UPDATE permission_name = VALUES(permission_name), module = VALUES(module);

INSERT INTO role_permission (id, tenant_id, role_id, permission_id)
VALUES
    (51, 1, 101, 1050)
ON DUPLICATE KEY UPDATE permission_id = VALUES(permission_id);
