-- 租户访客楼层策略最小核查 SQL（执行前确认数据库实例为电梯业务库）
-- 目标租户：广发基金 business_id = 2524639890ba4f2cba9ba1a4eeaa4015

-- 1) 表结构与索引确认
SHOW CREATE TABLE tenant_visitor_floor_policy;

-- 2) 广发租户策略主检查
SELECT id,
       business_id,
       policy_type,
       allow_zone_ids,
       building_id,
       enabled,
       policy_version,
       updated_at,
       remark
FROM tenant_visitor_floor_policy
WHERE business_id = '2524639890ba4f2cba9ba1a4eeaa4015'
ORDER BY updated_at DESC, policy_version DESC;

-- 3) 同租户是否存在多条默认策略（building_id 为空）
SELECT business_id, COUNT(*) AS cnt
FROM tenant_visitor_floor_policy
WHERE business_id = '2524639890ba4f2cba9ba1a4eeaa4015'
  AND (building_id IS NULL OR building_id = '')
GROUP BY business_id
HAVING COUNT(*) > 1;

-- 4) enabled 与 policy_type 分布
SELECT policy_type, enabled, COUNT(*) AS cnt
FROM tenant_visitor_floor_policy
WHERE business_id = '2524639890ba4f2cba9ba1a4eeaa4015'
GROUP BY policy_type, enabled
ORDER BY cnt DESC;

-- 5) allow_zone_ids 是否包含 28F zone_id
SELECT id, allow_zone_ids
FROM tenant_visitor_floor_policy
WHERE business_id = '2524639890ba4f2cba9ba1a4eeaa4015'
  AND enabled = 1
  AND policy_type = 'INTERSECT_ALLOWLIST'
  AND allow_zone_ids LIKE '%605560545117995008%';

