ALTER TABLE meeting ADD COLUMN invoice_config_json VARCHAR(1000) DEFAULT NULL COMMENT '会议发票动态模块配置JSON';

-- 增加发票动态配置权限
SET @next_permission_id := (SELECT IFNULL(MAX(id), 0) + 1 FROM permission);
INSERT INTO permission (id, permission_code, permission_name, module)
SELECT @next_permission_id, 'meeting.invoice.config', '配置会议发票模块', 'meeting'
FROM dual
WHERE NOT EXISTS (SELECT 1 FROM permission WHERE permission_code = 'meeting.invoice.config');

-- 为单位管理员(TENANT_ADMIN)默认绑定该权限
SET @next_role_permission_id := (SELECT IFNULL(MAX(id), 0) FROM role_permission);
INSERT INTO role_permission (id, tenant_id, role_id, permission_id)
SELECT
    (@next_role_permission_id := @next_role_permission_id + 1) AS id,
    r.tenant_id,
    r.id AS role_id,
    p.id AS permission_id
FROM role r
JOIN permission p ON p.permission_code = 'meeting.invoice.config'
WHERE r.role_code = 'TENANT_ADMIN'
  AND r.is_deleted = 0
  AND NOT EXISTS (
    SELECT 1
    FROM role_permission rp
    WHERE rp.tenant_id = r.tenant_id
      AND rp.role_id = r.id
      AND rp.permission_id = p.id
  );
