-- ============================================================
-- 菜单全面修复脚本
-- 1. 修复所有乱码菜单名称（根据 perms 字段还原）
-- 2. 新增菜单移至主目录（parent_id=0），因 智慧报价(2000) 已停用
-- 3. 插入缺失的菜单条目
-- ============================================8===============

SET NAMES utf8mb4;

-- ════════════════════════════════════════════════════════════
-- 第一部分：修复所有乱码菜单名称
-- ════════════════════════════════════════════════════════════

-- bid 模块主菜单（parent_id=0 且无 perms 的父节点）
UPDATE sys_menu SET menu_name = '智慧报价' WHERE menu_id = 2000;

-- bid 模块子菜单
UPDATE sys_menu SET menu_name = '物料管理' WHERE perms = 'bid:material:list';
UPDATE sys_menu SET menu_name = '新增' WHERE perms = 'bid:material:add' AND menu_type = 'F';
UPDATE sys_menu SET menu_name = '编辑' WHERE perms = 'bid:material:edit' AND menu_type = 'F';
UPDATE sys_menu SET menu_name = '删除' WHERE perms = 'bid:material:remove' AND menu_type = 'F';
UPDATE sys_menu SET menu_name = '查询' WHERE perms = 'bid:material:query' AND menu_type = 'F';

UPDATE sys_menu SET menu_name = '供应商管理' WHERE perms = 'bid:supplier:list';
UPDATE sys_menu SET menu_name = '供应商报价' WHERE perms = 'bid:quotation:list';
UPDATE sys_menu SET menu_name = '报价请求' WHERE perms = 'bid:rfq:list';
UPDATE sys_menu SET menu_name = '智慧比价' WHERE perms = 'bid:comparison:list';
UPDATE sys_menu SET menu_name = '采购单' WHERE perms = 'bid:purchaseorder:list';
UPDATE sys_menu SET menu_name = '供应商评价' WHERE perms = 'bid:evaluation:list';
UPDATE sys_menu SET menu_name = '订单异议' WHERE perms = 'bid:objection:list';
UPDATE sys_menu SET menu_name = '交易记录' WHERE perms = 'bid:transaction:list';
UPDATE sys_menu SET menu_name = '租户管理' WHERE perms = 'bid:tenant:list';

-- 甲方报价单
UPDATE sys_menu SET menu_name = '甲方报价' WHERE perms = 'bid:clientquote:list';
UPDATE sys_menu SET menu_name = '新增报价' WHERE perms = 'bid:clientquote:add' AND menu_type = 'F';
UPDATE sys_menu SET menu_name = '编辑报价' WHERE perms = 'bid:clientquote:edit' AND menu_type = 'F';
UPDATE sys_menu SET menu_name = '删除报价' WHERE perms = 'bid:clientquote:remove' AND menu_type = 'F';

-- 统计分析
UPDATE sys_menu SET menu_name = '统计分析' WHERE perms = 'bid:report:list';
UPDATE sys_menu SET menu_name = '采购总览看板' WHERE perms = 'bid:report:dashboard';
UPDATE sys_menu SET menu_name = '采购成本分析' WHERE perms = 'bid:report:cost';
UPDATE sys_menu SET menu_name = '供应商绩效' WHERE perms = 'bid:report:supplier';

-- ════════════════════════════════════════════════════════════
-- 第二部分：修复我们的新菜单名称
-- ════════════════════════════════════════════════════════════

UPDATE sys_menu SET menu_name = '订单履约' WHERE perms = 'bid:order:list';
UPDATE sys_menu SET menu_name = '待发订单' WHERE perms = 'bid:order:pending';
UPDATE sys_menu SET menu_name = '历史订单' WHERE perms = 'bid:order:history';
UPDATE sys_menu SET menu_name = '结单时间管理' WHERE perms = 'bid:order:closeDate';
UPDATE sys_menu SET menu_name = '甲方客户' WHERE perms = 'bid:client:list';
UPDATE sys_menu SET menu_name = '编辑客户' WHERE perms = 'bid:client:edit' AND menu_type = 'F';
UPDATE sys_menu SET menu_name = '删除客户' WHERE perms = 'bid:client:remove' AND menu_type = 'F';
UPDATE sys_menu SET menu_name = '结单录入' WHERE perms = 'bid:order:closeDate:edit' AND menu_type = 'F';
UPDATE sys_menu SET menu_name = '操作记录' WHERE perms = 'bid:operationlog:list';

-- ════════════════════════════════════════════════════════════
-- 第三部分：将新菜单移至主目录（parent_id=0）
-- 因为 智慧报价(2000) 已停用，其下的子菜单不可见
-- ════════════════════════════════════════════════════════════

-- 订单履约(2023) → 根目录，排序20
UPDATE sys_menu SET parent_id = 0, order_num = 20 WHERE menu_id = 2023;
-- 甲方客户(2028) → 根目录，排序21
UPDATE sys_menu SET parent_id = 0, order_num = 21 WHERE menu_id = 2028;
-- 操作记录(2029) → 根目录，排序22
UPDATE sys_menu SET parent_id = 0, order_num = 22 WHERE menu_id = 2029;

-- ════════════════════════════════════════════════════════════
-- 第四部分：插入缺失的菜单条目
-- ════════════════════════════════════════════════════════════

-- 在途订单 (2025)
INSERT IGNORE INTO sys_menu(menu_id, menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time)
VALUES(2025, '在途订单', 2023, 2, 'transit', 'bid/order/transit', 1, 0, 'C', '0', '0', 'bid:order:transit', 'truck', 'admin', NOW());

-- 新增客户按钮 (2030)
INSERT IGNORE INTO sys_menu(menu_id, menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time)
VALUES(2030, '新增客户', 2028, 1, '#', NULL, 1, 0, 'F', '0', '0', 'bid:client:add', '#', 'admin', NOW());

-- 查询客户按钮 (2033)
INSERT IGNORE INTO sys_menu(menu_id, menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time)
VALUES(2033, '查询客户', 2028, 4, '#', NULL, 1, 0, 'F', '0', '0', 'bid:client:query', '#', 'admin', NOW());

-- 订单查询按钮 (2034)
INSERT IGNORE INTO sys_menu(menu_id, menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time)
VALUES(2034, '订单查询', 2024, 1, '#', NULL, 1, 0, 'F', '0', '0', 'bid:order:query', '#', 'admin', NOW());

-- 订单状态变更按钮 (2035)
INSERT IGNORE INTO sys_menu(menu_id, menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time)
VALUES(2035, '订单状态变更', 2024, 2, '#', NULL, 1, 0, 'F', '0', '0', 'bid:order:status', '#', 'admin', NOW());

-- 操作记录查询按钮 (2037)
INSERT IGNORE INTO sys_menu(menu_id, menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time)
VALUES(2037, '操作记录查询', 2029, 1, '#', NULL, 1, 0, 'F', '0', '0', 'bid:operationlog:query', '#', 'admin', NOW());

-- ════════════════════════════════════════════════════════════
-- 第五部分：角色-菜单关联（确保 admin 有权限）
-- ════════════════════════════════════════════════════════════

INSERT IGNORE INTO sys_role_menu (role_id, menu_id)
SELECT 1, menu_id FROM sys_menu WHERE menu_id IN (2025, 2030, 2033, 2034, 2035, 2037);

-- ════════════════════════════════════════════════════════════
-- 验证
-- ════════════════════════════════════════════════════════════

SELECT menu_id, menu_name, parent_id, perms, order_num,
  CASE WHEN status='0' THEN '正常' ELSE '停用' END AS status
FROM sys_menu WHERE menu_id BETWEEN 2000 AND 2037
ORDER BY menu_id;
