-- ═══════════════════════════════════════════════════════════════════
-- 菜单重组：按业务模块分组到统一父菜单 + 新增审批配置
-- ═══════════════════════════════════════════════════════════════════
SET NAMES utf8mb4;

-- ───────────────────────────────────────────────
-- 1. 新建一级业务父菜单（M 类型 = 目录）
-- ───────────────────────────────────────────────
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
  (2100, '基础数据', 0, 10, 'basedata', NULL, 1, 0, 'M', '0', '0', '', 'tree-table', 'admin', NOW()),
  (2110, '报价采购', 0, 20, 'quote',    NULL, 1, 0, 'M', '0', '0', '', 'money',      'admin', NOW()),
  (2120, '订单履约', 0, 30, 'fulfill',  NULL, 1, 0, 'M', '0', '0', '', 'shopping',   'admin', NOW()),
  (2130, '系统配置', 0, 90, 'bizconfig',NULL, 1, 0, 'M', '0', '0', '', 'tool',       'admin', NOW());

-- 统计分析 2019 已存在，保持 order_num
UPDATE sys_menu SET order_num = 40 WHERE menu_id = 2019;

-- ───────────────────────────────────────────────
-- 2. 已有子菜单按业务归类到新父菜单
--    用 perms 标识 ，避免依赖具体 menu_id
-- ───────────────────────────────────────────────

-- 基础数据
UPDATE sys_menu SET parent_id = 2100, order_num = 1 WHERE perms = 'bid:material:list';
UPDATE sys_menu SET parent_id = 2100, order_num = 2 WHERE perms = 'bid:category:list';
UPDATE sys_menu SET parent_id = 2100, order_num = 3 WHERE perms = 'bid:client:list';
UPDATE sys_menu SET parent_id = 2100, order_num = 4 WHERE perms = 'bid:supplier:list';

-- 报价采购
UPDATE sys_menu SET parent_id = 2110, order_num = 1 WHERE perms = 'bid:rfq:list';
UPDATE sys_menu SET parent_id = 2110, order_num = 2 WHERE perms = 'bid:quotation:list';
UPDATE sys_menu SET parent_id = 2110, order_num = 3 WHERE perms = 'bid:comparison:list';
UPDATE sys_menu SET parent_id = 2110, order_num = 4 WHERE perms = 'bid:purchaseorder:list';
UPDATE sys_menu SET parent_id = 2110, order_num = 5 WHERE perms = 'bid:clientquote:list';

-- 订单履约
UPDATE sys_menu SET parent_id = 2120, order_num = 1 WHERE perms = 'bid:order:pending';
UPDATE sys_menu SET parent_id = 2120, order_num = 2 WHERE perms = 'bid:order:transit';
UPDATE sys_menu SET parent_id = 2120, order_num = 3 WHERE perms = 'bid:order:history';
UPDATE sys_menu SET parent_id = 2120, order_num = 4 WHERE perms = 'bid:order:closeDate';
UPDATE sys_menu SET parent_id = 2120, order_num = 5 WHERE perms = 'bid:objection:list';
UPDATE sys_menu SET parent_id = 2120, order_num = 6 WHERE perms = 'bid:clientDelivery:list';

-- 兼容旧 perms：order:list（订单履约旧索引页）若存在，放履约组顶部
UPDATE sys_menu SET parent_id = 2120, order_num = 0 WHERE perms = 'bid:order:list';

-- 系统配置 / 业务配置
UPDATE sys_menu SET parent_id = 2130, order_num = 2 WHERE perms = 'bid:evaluation:list';
UPDATE sys_menu SET parent_id = 2130, order_num = 3 WHERE perms = 'bid:transaction:list';
UPDATE sys_menu SET parent_id = 2130, order_num = 4 WHERE perms = 'bid:operationlog:list';
UPDATE sys_menu SET parent_id = 2130, order_num = 5 WHERE perms = 'bid:tenant:list';

-- ───────────────────────────────────────────────
-- 3. 新增：审批配置（挂在系统配置下）
-- ───────────────────────────────────────────────
DELETE FROM sys_menu WHERE perms = 'bid:approval:list';
INSERT 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 (2131, '审批配置', 2130, 1, 'approval', 'bid/approval/index', 1, 0, 'C', '0', '0', 'bid:approval:list', 'tree', 'admin', NOW());

-- 审批配置按钮权限
DELETE FROM sys_menu WHERE perms IN ('bid:approval:edit','bid:approval:query');
INSERT INTO sys_menu(menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time)
VALUES
  ('查询', 2131, 1, '', NULL, 1, 0, 'F', '0', '0', 'bid:approval:query', '#', 'admin', NOW()),
  ('编辑', 2131, 2, '', NULL, 1, 0, 'F', '0', '0', 'bid:approval:edit',  '#', 'admin', NOW());

-- 给 admin 角色（role_id=1）授权
INSERT IGNORE INTO sys_role_menu(role_id, menu_id)
SELECT 1, menu_id FROM sys_menu
WHERE perms IN ('bid:approval:list','bid:approval:query','bid:approval:edit')
   OR menu_id IN (2100, 2110, 2120, 2130);

-- ───────────────────────────────────────────────
-- 4. 验证：列出当前一级菜单
-- ───────────────────────────────────────────────
SELECT menu_id, menu_name, order_num, menu_type, perms
FROM sys_menu
WHERE parent_id = 0 AND visible = '0' AND status = '0'
ORDER BY order_num;
