-- ============================================================
-- Organize tenant_sys_menu template (master DB: ylrz_saas)
-- Backup first: CREATE TABLE tenant_sys_menu_bak AS SELECT * FROM tenant_sys_menu;
-- ============================================================

-- 1) Top-level visible module order (saasadminui top nav)
UPDATE tenant_sys_menu SET order_num = 0  WHERE menu_id = 32644; -- index
UPDATE tenant_sys_menu SET order_num = 1  WHERE menu_id = 32361; -- qw
UPDATE tenant_sys_menu SET order_num = 2  WHERE menu_id = 32380; -- wx
UPDATE tenant_sys_menu SET order_num = 3  WHERE menu_id = 32347; -- crm
UPDATE tenant_sys_menu SET order_num = 4  WHERE menu_id = 32357; -- member
UPDATE tenant_sys_menu SET order_num = 5  WHERE menu_id = 32351; -- his
UPDATE tenant_sys_menu SET order_num = 6  WHERE menu_id = 32369; -- store
UPDATE tenant_sys_menu SET order_num = 7  WHERE menu_id = 32353; -- live
UPDATE tenant_sys_menu SET order_num = 8  WHERE menu_id = 32345; -- course
UPDATE tenant_sys_menu SET order_num = 9  WHERE menu_id = 32348; -- fastGpt
UPDATE tenant_sys_menu SET order_num = 10 WHERE menu_id = 32355; -- lobster
UPDATE tenant_sys_menu SET order_num = 11 WHERE menu_id = 32331; -- ad
UPDATE tenant_sys_menu SET order_num = 12 WHERE menu_id = 32372; -- system
UPDATE tenant_sys_menu SET order_num = 13 WHERE menu_id = 32339; -- bill
UPDATE tenant_sys_menu SET order_num = 14 WHERE menu_id = 32341; -- calendar
UPDATE tenant_sys_menu SET order_num = 15 WHERE menu_id = 32368; -- statistics
UPDATE tenant_sys_menu SET order_num = 16 WHERE menu_id = 32379; -- watch
UPDATE tenant_sys_menu SET order_num = 17 WHERE menu_id = 35300; -- other

-- Hide empty placeholder module
UPDATE tenant_sys_menu SET visible = '1', status = '0'
WHERE menu_id = 35129;

-- Hide company root by id (path may have been corrupted by prior runs)
UPDATE tenant_sys_menu SET visible = '1', status = '0'
WHERE menu_id = 32344;

-- 2) Hide platform-only root menus (should not be assigned to tenants)
UPDATE tenant_sys_menu
SET visible = '1', status = '0'
WHERE parent_id = 0
  AND path IN (
    'admin', 'saas', 'proxy', 'tenant', 'monitor', 'moduleUsage',
    'sysUser', 'tool', 'saler', 'company', 'qwechat', 'qwExternalContact',
    'storeOrderOfflineItem', 'FastGptExtUserTag', 'addressBook', 'adv',
    'aiChatQuality', 'aiSipCall', 'aiob', 'baidu', 'callRecord', 'chat',
    'courseFinishTemp', 'food', 'gw', 'hisStore', 'liveData', 'medical',
    'taskStatistics', 'todo', 'user', 'users', 'shop'
  );

-- Hide menus whose component points to super-admin pages
UPDATE tenant_sys_menu
SET visible = '1', status = '0'
WHERE component LIKE 'admin/%';

UPDATE tenant_sys_menu SET visible = '1', status = '0' WHERE menu_id = 29228;

-- 3) Fix known wrong component paths for tenant UI
UPDATE tenant_sys_menu SET component = 'crm/customer/index'
WHERE menu_id = 29355 AND component = 'admin/crm/index';

UPDATE tenant_sys_menu
SET component = SUBSTRING(component, 7)
WHERE component LIKE 'admin/%';

-- 4) Verification queries
-- SELECT menu_id, menu_name, order_num, path, visible
-- FROM tenant_sys_menu WHERE parent_id = 0 AND menu_type = 'M' AND visible = '0'
-- ORDER BY order_num, menu_id;

-- SELECT menu_id, menu_name, component, visible
-- FROM tenant_sys_menu WHERE component LIKE 'admin/%';

-- SELECT COUNT(*) total,
--        SUM(parent_id = 0) root_count,
--        SUM(parent_id = 0 AND visible = '0') visible_root_count
-- FROM tenant_sys_menu;

-- Next step: run organize_tenant_sys_menu_subtree.sql for system/qw/store hierarchy
