-- =============================================================
-- 线下库 klp-oa-test 菜单对齐修复
-- 将质量评审菜单从"质量管理"下移到"评审流程"目录下
-- 并补充次品钢卷、O级卷菜单
-- =============================================================
SET NAMES utf8mb4;

-- ==================== 1. 清理旧的质量评审菜单 ====================
DELETE FROM sys_role_menu WHERE menu_id IN (2100000000000000036,2100000000000000037,2100000000000000038,2100000000000000039,2100000000000000040,2100000000000000041,2100000000000000042,2100000000000000043);
DELETE FROM sys_menu WHERE menu_id IN (2100000000000000036,2100000000000000037,2100000000000000038,2100000000000000039,2100000000000000040,2100000000000000041,2100000000000000042,2100000000000000043);

-- 计算可用菜单ID
SET @max_id = (SELECT COALESCE(MAX(menu_id), 0) FROM sys_menu);

-- ==================== 2. 创建"评审流程"目录（挂在质量管理下） ====================
SET @parent_quality = 2068954238598967297; -- 质量管理
SET @id_review_dir = @max_id + 1;
INSERT IGNORE INTO sys_menu (menu_id, menu_name, parent_id, order_num, path, is_frame, is_cache, menu_type, visible, status, icon, create_by, create_time, update_by, update_time)
VALUES (@id_review_dir, '评审流程', @parent_quality, 5, 'review', 1, 0, 'M', '0', '0', 'guide', 'admin', NOW(), 'admin', NOW());

-- ==================== 3. 在评审流程下创建子菜单 ====================
SET @parent = @id_review_dir;
SET @id = @max_id + 2;

-- 次品钢卷 (order=4)
INSERT IGNORE INTO sys_menu (menu_id, menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, icon, create_by, create_time)
VALUES (@id, '次品钢卷', @parent, 4, 'rubbish', 'wms/coil/views/scrap', 1, 0, 'C', '0', '0', 'checkbox', 'admin', NOW());
SET @id = @id + 1;

-- O级卷 (order=5)
INSERT IGNORE INTO sys_menu (menu_id, menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, icon, create_by, create_time)
VALUES (@id, 'O级卷', @parent, 5, 'jishu', 'wms/coil/views/jishu', 1, 0, 'C', '0', '0', 'checkbox', 'admin', NOW());
SET @id = @id + 1;

-- 质量评审 (order=6)
SET @id_review_menu = @id;
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 (@id_review_menu, '质量评审', @parent, 6, 'qualityReview', 'mes/qc/qualityReview/index', 1, 0, 'C', '0', '0', 'qc:qualityReview:list', 'guide', 'admin', NOW());
SET @id = @id + 1;

-- 质量评审待审批 (order=7, 隐藏)
SET @id_review_todo = @id;
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 (@id_review_todo, '质量评审待审批', @parent, 7, 'qualityReviewTodo', 'mes/qc/qualityReview/todo', 1, 0, 'C', '1', '0', 'qc:qualityReview:approve', 'checkbox', 'admin', NOW());
SET @id = @id + 1;

-- ==================== 4. 按钮权限（挂在质量评审菜单下） ====================
INSERT INTO sys_menu (menu_id, menu_name, parent_id, order_num, menu_type, visible, status, perms, create_by, create_time)
VALUES (@id, '质量评审新增', @id_review_menu, 1, 'F', '0', '0', 'qc:qualityReview:add', 'admin', NOW()); SET @id = @id + 1;
INSERT INTO sys_menu (menu_id, menu_name, parent_id, order_num, menu_type, visible, status, perms, create_by, create_time)
VALUES (@id, '质量评审修改', @id_review_menu, 2, 'F', '0', '0', 'qc:qualityReview:edit', 'admin', NOW()); SET @id = @id + 1;
INSERT INTO sys_menu (menu_id, menu_name, parent_id, order_num, menu_type, visible, status, perms, create_by, create_time)
VALUES (@id, '质量评审删除', @id_review_menu, 3, 'F', '0', '0', 'qc:qualityReview:delete', 'admin', NOW()); SET @id = @id + 1;
INSERT INTO sys_menu (menu_id, menu_name, parent_id, order_num, menu_type, visible, status, perms, create_by, create_time)
VALUES (@id, '质量评审提交送审', @id_review_menu, 4, 'F', '0', '0', 'qc:qualityReview:submit', 'admin', NOW()); SET @id = @id + 1;
INSERT INTO sys_menu (menu_id, menu_name, parent_id, order_num, menu_type, visible, status, perms, create_by, create_time)
VALUES (@id, '质量评审审批', @id_review_menu, 5, 'F', '0', '0', 'qc:qualityReview:approve', 'admin', NOW()); SET @id = @id + 1;
INSERT INTO sys_menu (menu_id, menu_name, parent_id, order_num, menu_type, visible, status, perms, create_by, create_time)
VALUES (@id, '质量评审执行改判', @id_review_menu, 6, 'F', '0', '0', 'qc:qualityReview:execute', 'admin', NOW());

-- ==================== 5. 授权admin角色 ====================
INSERT IGNORE INTO sys_role_menu (role_id, menu_id) VALUES
(1, @parent_quality), (1, @id_review_dir),
(1, @id_review_menu), (1, @id_review_todo);
-- 按钮权限
INSERT IGNORE INTO sys_role_menu (role_id, menu_id) SELECT 1, menu_id FROM sys_menu WHERE parent_id = @id_review_menu AND menu_type = 'F';

-- ==================== 6. 验证 ====================
SELECT 'Dev align OK' AS result;
