-- ============================================================================
-- 1.0.435 verify：按钮权限已移出业务页面、且被回归删除的授权已恢复。
--
-- 约定：每条语句取「首行首列」当布尔断言；返回 0 行会判假并中断启动，故一律返回标量。
-- 口径：全部钉死精确 Id / TenantId / RoleId，不使用跨租户 RoleCode COUNT。
-- 环境容错：非本项目夹具库上主脚本为空操作，断言同步短路为真。
-- ============================================================================

-- 1) 「库存查询」必须重新成为叶子（其下不得再挂任何子节点）
--    这是本次回归的直接触发条件：一旦它有未被持有的子节点，就会在角色保存时被删除。
SELECT IF(
  (SELECT COUNT(*) FROM SysMenu WHERE Id = 1329015030003) = 0,
  1,
  (SELECT COUNT(*) = 0 FROM SysMenu WHERE Pid = 1329015030003)
) AS ok_stock_query_is_leaf_again;

-- 2) 6 个按钮权限全部已挂到隐藏管理目录下
SELECT IF(
  (SELECT COUNT(*) FROM SysMenu WHERE Id = 1329015030100) = 0,
  1,
  (SELECT COUNT(*) = 6 FROM SysMenu
    WHERE Type = 3 AND Pid = 1329015030100
      AND Permission LIKE 's5InventoryInbound:%')
) AS ok_buttons_moved_under_admin_dir;

-- 3) 隐藏管理目录本身：Type=1 / 隐藏 / 启用 / 挂在 S5 物料仓储下
SELECT IF(
  (SELECT COUNT(*) FROM SysMenu WHERE Id = 1329015030100) = 0,
  1,
  (SELECT COUNT(*) = 1 FROM SysMenu
    WHERE Id = 1329015030100 AND Type = 1 AND IsHide = 1 AND Status = 1 AND Pid = 1321000006000)
) AS ok_admin_dir_shape;

-- 4) 该隐藏目录不得授予任何角色（授予业务角色会把管理能力泄漏出去）
SELECT (
  SELECT COUNT(*) = 0 FROM SysRoleMenu WHERE MenuId = 1329015030100
) AS ok_admin_dir_not_granted_to_any_role;

-- 5) UAT_FUNCTION_USER 的「库存查询」授权已恢复，且精确 1 条
SELECT IF(
  (SELECT COUNT(*) FROM SysRole WHERE Id = 838258976469061 AND TenantId = 838257186181189) = 0,
  1,
  (SELECT COUNT(*) = 1
     FROM SysRoleMenu rm
     JOIN SysRole r ON r.Id = rm.RoleId
    WHERE rm.RoleId = 838258976469061
      AND rm.MenuId = 1329015030003
      AND r.TenantId = 838257186181189)
) AS ok_uat_stock_query_restored;

-- 6) 权限点仍未扩散到任何角色（1.0.433 的安全不变量不得因本次搬迁被破坏）
SELECT (
  SELECT COUNT(*) = 0
    FROM SysRoleMenu rm
    JOIN SysMenu m ON m.Id = rm.MenuId
   WHERE m.Type = 3 AND m.Permission LIKE 's5InventoryInbound:%'
) AS ok_no_role_holds_inbound_permission;

-- 7) 6 个权限点仍然登记且启用（搬迁不得影响 JwtHandler 的命中）
SELECT IF(
  (SELECT COUNT(*) FROM SysMenu WHERE Permission LIKE 's5InventoryInbound:%') = 0,
  1,
  (SELECT COUNT(*) = 6 FROM SysMenu
    WHERE Type = 3 AND Status = 1 AND Permission LIKE 's5InventoryInbound:%')
) AS ok_six_permissions_still_registered;
