-- 1.0.386 verify: UAT_FUNCTION_USER 应已获得 sysOrg:list 按钮权限。
--
-- 约定：AutoVersionUpdate 取每条语句「首行首列」当布尔断言，必须为真。
--   返回 0 行会得到 NULL 判假并中断启动，故断言一律返回标量真值。
-- 环境容错：角色不存在的库上主脚本为空操作，断言同样短路为真。

-- 1) 角色已绑定 sysOrg:list 菜单
SELECT IF(
  (SELECT COUNT(*) FROM SysRole WHERE Code = 'UAT_FUNCTION_USER' AND Status = 1) = 0,
  1,
  (SELECT COUNT(*)
     FROM SysRoleMenu rm
     JOIN SysRole r ON r.Id = rm.RoleId
     JOIN SysMenu m ON m.Id = rm.MenuId
    WHERE r.Code = 'UAT_FUNCTION_USER'
      AND m.Permission = 'sysOrg:list'
      AND m.Type = 3)
) AS ok_binding;

-- 2) 无重复绑定（幂等）
SELECT IF(
  (SELECT COUNT(*) FROM (
     SELECT rm.RoleId, rm.MenuId
       FROM SysRoleMenu rm
       JOIN SysRole r ON r.Id = rm.RoleId
      WHERE r.Code = 'UAT_FUNCTION_USER'
      GROUP BY rm.RoleId, rm.MenuId
     HAVING COUNT(*) > 1
   ) dup) = 0,
  1, 0
) AS ok_no_duplicate;
