-- ============================================================================
-- STEP 1 合同评审周期菜单 · VERIFY
--
-- 执行契约（AutoVersionUpdate.RunVerifyScriptIfExists）：每条语句经
-- SqlQuerySingle<object> 取首行首列并要求 IsTruthy == true。故：
--   ① 禁止 SET @var（返回 null 即判失败）—— 租户/角色一律内联子查询；
--   ② 每条语句返回「通过为真」的标量；
--   ③ 文件必须以最后一条 SQL 的 ; 结束，末尾不得残留 comment-only 块
--      （SqlScriptSplitter 会把它切成一条纯注释语句 → null → false Failed）。
-- ============================================================================

-- 菜单本身存在且字段口径正确
SELECT COUNT(*) = 1 FROM SysMenu WHERE Id = 1329002000004 AND Pid = 1329002000000 AND Type = 2 AND Status = 1 AND OrderNo = 4 AND Path = '/aidop/s0/sales/contract-review-cycle' AND Component = '/aidop/s0/sales/ContractReviewCycleList';

-- 路由唯一（未产生重复菜单）
SELECT COUNT(*) = 1 FROM SysMenu WHERE Path = '/aidop/s0/sales/contract-review-cycle';

-- 租户可见性
SELECT COUNT(*) = 1 FROM SysTenantMenu tm JOIN SysMenu m ON m.Id = tm.MenuId WHERE m.Path = '/aidop/s0/sales/contract-review-cycle' AND tm.TenantId = (SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id = t.OrgId WHERE o.Code = 'UATTEST_CHL' AND t.Status = 1 LIMIT 1);

-- 角色授权（UAT_FUNCTION_USER）
SELECT COUNT(*) = 1 FROM SysRoleMenu rm JOIN SysMenu m ON m.Id = rm.MenuId JOIN SysRole r ON r.Id = rm.RoleId WHERE m.Path = '/aidop/s0/sales/contract-review-cycle' AND r.Code = 'UAT_FUNCTION_USER' AND r.TenantId = (SELECT t.Id FROM SysTenant t JOIN SysOrg o ON o.Id = t.OrgId WHERE o.Code = 'UATTEST_CHL' AND t.Status = 1 LIMIT 1);

-- UATAdminA 经其角色可达该菜单（端到端可达性）
SELECT COUNT(*) = 1 FROM SysUser u JOIN SysUserRole ur ON ur.UserId = u.Id JOIN SysRoleMenu rm ON rm.RoleId = ur.RoleId JOIN SysMenu m ON m.Id = rm.MenuId WHERE u.Account = 'UATAdminA' AND m.Path = '/aidop/s0/sales/contract-review-cycle';

-- 产销建模子菜单总数由 6 增至 7，且 OrderNo 无重复
SELECT COUNT(*) = 7 FROM SysMenu WHERE Pid = 1329002000000 AND Type = 2;
SELECT COUNT(DISTINCT OrderNo) = COUNT(*) FROM SysMenu WHERE Pid = 1329002000000 AND Type = 2;

-- 回归：不得误伤其它 S0 菜单（S0 Type=2 菜单总数 58 → 59）
SELECT COUNT(*) = 59 FROM SysMenu WHERE Path LIKE '/aidop/s0%' AND Type = 2;
