-- ============================================================
-- 审批流程定义表增加 TenantId 字段（按租户匹配流程）
-- NULL = 全局（所有租户可用），非 NULL = 仅该租户可用
-- ============================================================
SET @has_col := (SELECT COUNT(*) FROM information_schema.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'ApprovalFlow' AND COLUMN_NAME = 'TenantId');
SET @ddl := IF(@has_col = 0,
  'ALTER TABLE ApprovalFlow ADD COLUMN TenantId BIGINT NULL COMMENT ''租户Id（NULL=全局，非NULL=仅该租户可用）''',
  'SELECT 1');
PREPARE p FROM @ddl; EXECUTE p; DEALLOCATE PREPARE p;

SET @has_idx := (SELECT COUNT(DISTINCT INDEX_NAME) FROM information_schema.STATISTICS
  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'ApprovalFlow' AND INDEX_NAME = 'idx_approvalflow_biztype_tenant');
SET @ddl2 := IF(@has_idx = 0,
  'CREATE INDEX idx_approvalflow_biztype_tenant ON ApprovalFlow (BizType, TenantId, IsPublished)',
  'SELECT 1');
PREPARE p2 FROM @ddl2; EXECUTE p2; DEALLOCATE PREPARE p2;
