-- 用途：`README`/操作手册 §4.0 标准单行批量跑完后，在电梯库核查写库是否与接口侧一致
-- 库：`cw-elevator-application`
-- 表：`image_rule_ref`（访客 person_id 维度楼层规则）
-- 访客清单：与本包约定的 9199000100000000001～9199000100000000025 完全一致；若现场改用其它 personId，请改下方 expected 子查询或直接用 `sql/人工核查最终楼层结果.sql`。

-- ------------------------------------------------------------------
-- A) 每名访客当前「有效」规则行数 + 最近一次更新时间（核对 JSON 里的 floor_count / 时间点）
-- ------------------------------------------------------------------
SELECT
  e.visitor_person_id,
  SUM(CASE WHEN irr.person_delete = 0 THEN 1 ELSE 0 END) AS active_row_count,
  MAX(CASE WHEN irr.person_delete = 0 THEN irr.last_update_time END) AS latest_active_update_ms
FROM (
  SELECT '9199000100000000001' AS visitor_person_id
  UNION ALL SELECT '9199000100000000002'
  UNION ALL SELECT '9199000100000000003'
  UNION ALL SELECT '9199000100000000004'
  UNION ALL SELECT '9199000100000000005'
  UNION ALL SELECT '9199000100000000006'
  UNION ALL SELECT '9199000100000000007'
  UNION ALL SELECT '9199000100000000008'
  UNION ALL SELECT '9199000100000000009'
  UNION ALL SELECT '9199000100000000010'
  UNION ALL SELECT '9199000100000000011'
  UNION ALL SELECT '9199000100000000012'
  UNION ALL SELECT '9199000100000000013'
  UNION ALL SELECT '9199000100000000014'
  UNION ALL SELECT '9199000100000000015'
  UNION ALL SELECT '9199000100000000016'
  UNION ALL SELECT '9199000100000000017'
  UNION ALL SELECT '9199000100000000018'
  UNION ALL SELECT '9199000100000000019'
  UNION ALL SELECT '9199000100000000020'
  UNION ALL SELECT '9199000100000000021'
  UNION ALL SELECT '9199000100000000022'
  UNION ALL SELECT '9199000100000000023'
  UNION ALL SELECT '9199000100000000024'
  UNION ALL SELECT '9199000100000000025'
) e
LEFT JOIN `cw-elevator-application`.`image_rule_ref` irr
  ON irr.person_id = e.visitor_person_id
GROUP BY e.visitor_person_id
ORDER BY e.visitor_person_id;

-- ------------------------------------------------------------------
-- B) 有效规则明细（与报告 `derived.floors` 比对 zone_id；按人+时间排序）
-- ------------------------------------------------------------------
SELECT
  irr.person_id,
  irr.zone_id,
  irr.zone_name,
  irr.person_delete,
  irr.business_id,
  irr.create_time,
  irr.last_update_time
FROM `cw-elevator-application`.`image_rule_ref` irr
WHERE irr.person_id BETWEEN '9199000100000000001' AND '9199000100000000025'
ORDER BY irr.person_id, irr.last_update_time DESC, irr.zone_id;
