-- 1.0.384 verify: UATAdminA 应绑定 UAT_FUNCTION_USER 并能解析出菜单。

-- 1) 目标账号角色与可解析菜单数
--    期望：roles = 4（原 3 个 SMART_OPS_* + 新增 UAT_FUNCTION_USER），role_menus > 0
SELECT u.Account,
       u.AccountType,
       u.TenantId,
       COUNT(DISTINCT ur.RoleId) AS roles,
       COUNT(DISTINCT rm.MenuId) AS role_menus
FROM SysUser u
LEFT JOIN SysUserRole ur ON ur.UserId = u.Id
LEFT JOIN SysRoleMenu rm ON rm.RoleId = ur.RoleId
WHERE u.Account = 'UATAdminA'
GROUP BY u.Id, u.Account, u.AccountType, u.TenantId;

-- 2) 与同租户对照账号横向比对
--    期望：UATAdminA 的 role_menus 与其余 5 个账号一致（308）
SELECT u.Account,
       COUNT(DISTINCT rm.MenuId) AS role_menus
FROM SysUser u
LEFT JOIN SysUserRole ur ON ur.UserId = u.Id
LEFT JOIN SysRoleMenu rm ON rm.RoleId = ur.RoleId
WHERE u.TenantId = (SELECT TenantId FROM SysUser WHERE Account = 'UATAdminA')
GROUP BY u.Id, u.Account
ORDER BY u.Account;

-- 3) 幂等性：重复执行不应产生重复绑定
--    期望：0 行
SELECT UserId, RoleId, COUNT(*) AS dup
FROM SysUserRole
WHERE UserId = (SELECT Id FROM SysUser WHERE Account = 'UATAdminA')
GROUP BY UserId, RoleId
HAVING COUNT(*) > 1;
