-- MES 物料模块菜单与权限（JeecgBoot）
-- 执行前可按需改 ID；默认授权 admin 角色（role_id=f6817f48af4fb3af11b9e8bf182f618b）
SET NAMES utf8mb4;

-- 一级菜单：MES管理
INSERT INTO sys_permission(id, parent_id, name, url, component, component_name, menu_type, perms, perms_type, sort_no, is_route, is_leaf, hidden, status, del_flag, always_show, keep_alive, internal_or_external, create_by, create_time)
VALUES ('1860000000000000001', NULL, 'MES管理', '/mes', 'layouts/RouteView', 'MesRoot', 0, NULL, '1', 50, 0, 0, 0, '1', 0, 1, 0, 0, 'admin', NOW())
ON DUPLICATE KEY UPDATE
parent_id=VALUES(parent_id), name=VALUES(name), url=VALUES(url), component=VALUES(component), component_name=VALUES(component_name),
menu_type=VALUES(menu_type), perms=VALUES(perms), perms_type=VALUES(perms_type), sort_no=VALUES(sort_no),
is_route=VALUES(is_route), is_leaf=VALUES(is_leaf), hidden=VALUES(hidden), status=VALUES(status), del_flag=VALUES(del_flag),
always_show=VALUES(always_show), keep_alive=VALUES(keep_alive), internal_or_external=VALUES(internal_or_external);

-- 二级菜单：物料信息
INSERT INTO sys_permission(id, parent_id, name, url, component, component_name, menu_type, perms, perms_type, sort_no, is_route, is_leaf, hidden, status, del_flag, keep_alive, internal_or_external, create_by, create_time)
VALUES ('1860000000000000011', '1860000000000000001', '物料信息', '/mes/materialinfo', 'mes/materialinfo/index', 'MesMaterialList', 1, NULL, '1', 1, 1, 1, 0, '1', 0, 1, 0, 'admin', NOW())
ON DUPLICATE KEY UPDATE
parent_id=VALUES(parent_id), name=VALUES(name), url=VALUES(url), component=VALUES(component), component_name=VALUES(component_name),
menu_type=VALUES(menu_type), perms=VALUES(perms), perms_type=VALUES(perms_type), sort_no=VALUES(sort_no),
is_route=VALUES(is_route), is_leaf=VALUES(is_leaf), hidden=VALUES(hidden), status=VALUES(status), del_flag=VALUES(del_flag),
keep_alive=VALUES(keep_alive), internal_or_external=VALUES(internal_or_external);
INSERT INTO sys_permission(id,parent_id,name,menu_type,perms,perms_type,status,del_flag,create_by,create_time) VALUES
('1860000000000000012','1860000000000000011','新增',2,'mes:mes_material:add','1','1',0,'admin',NOW()),
('1860000000000000013','1860000000000000011','编辑',2,'mes:mes_material:edit','1','1',0,'admin',NOW()),
('1860000000000000014','1860000000000000011','删除',2,'mes:mes_material:delete','1','1',0,'admin',NOW()),
('1860000000000000015','1860000000000000011','批量删除',2,'mes:mes_material:deleteBatch','1','1',0,'admin',NOW()),
('1860000000000000016','1860000000000000011','导出',2,'mes:mes_material:exportXls','1','1',0,'admin',NOW()),
('1860000000000000017','1860000000000000011','导入',2,'mes:mes_material:importExcel','1','1',0,'admin',NOW())
ON DUPLICATE KEY UPDATE
parent_id=VALUES(parent_id), name=VALUES(name), menu_type=VALUES(menu_type), perms=VALUES(perms), perms_type=VALUES(perms_type),
status=VALUES(status), del_flag=VALUES(del_flag);

-- 二级菜单：单位信息
INSERT INTO sys_permission(id, parent_id, name, url, component, component_name, menu_type, perms, perms_type, sort_no, is_route, is_leaf, hidden, status, del_flag, keep_alive, internal_or_external, create_by, create_time)
VALUES ('1860000000000000021', '1860000000000000001', '单位信息', '/mes/materialunit', 'mes/materialunit/index', 'MesUnitList', 1, NULL, '1', 2, 1, 1, 0, '1', 0, 1, 0, 'admin', NOW())
ON DUPLICATE KEY UPDATE
parent_id=VALUES(parent_id), name=VALUES(name), url=VALUES(url), component=VALUES(component), component_name=VALUES(component_name),
menu_type=VALUES(menu_type), perms=VALUES(perms), perms_type=VALUES(perms_type), sort_no=VALUES(sort_no),
is_route=VALUES(is_route), is_leaf=VALUES(is_leaf), hidden=VALUES(hidden), status=VALUES(status), del_flag=VALUES(del_flag),
keep_alive=VALUES(keep_alive), internal_or_external=VALUES(internal_or_external);
INSERT INTO sys_permission(id,parent_id,name,menu_type,perms,perms_type,status,del_flag,create_by,create_time) VALUES
('1860000000000000022','1860000000000000021','新增',2,'mes:mes_unit:add','1','1',0,'admin',NOW()),
('1860000000000000023','1860000000000000021','编辑',2,'mes:mes_unit:edit','1','1',0,'admin',NOW()),
('1860000000000000024','1860000000000000021','删除',2,'mes:mes_unit:delete','1','1',0,'admin',NOW()),
('1860000000000000025','1860000000000000021','批量删除',2,'mes:mes_unit:deleteBatch','1','1',0,'admin',NOW()),
('1860000000000000026','1860000000000000021','导出',2,'mes:mes_unit:exportXls','1','1',0,'admin',NOW()),
('1860000000000000027','1860000000000000021','导入',2,'mes:mes_unit:importExcel','1','1',0,'admin',NOW())
ON DUPLICATE KEY UPDATE
parent_id=VALUES(parent_id), name=VALUES(name), menu_type=VALUES(menu_type), perms=VALUES(perms), perms_type=VALUES(perms_type),
status=VALUES(status), del_flag=VALUES(del_flag);

-- 二级菜单：物料分类
INSERT INTO sys_permission(id, parent_id, name, url, component, component_name, menu_type, perms, perms_type, sort_no, is_route, is_leaf, hidden, status, del_flag, keep_alive, internal_or_external, create_by, create_time)
VALUES ('1860000000000000031', '1860000000000000001', '物料分类', '/mes/materialcategory', 'mes/materialcategory/index', 'MesMaterialCategoryList', 1, NULL, '1', 3, 1, 1, 0, '1', 0, 1, 0, 'admin', NOW())
ON DUPLICATE KEY UPDATE
parent_id=VALUES(parent_id), name=VALUES(name), url=VALUES(url), component=VALUES(component), component_name=VALUES(component_name),
menu_type=VALUES(menu_type), perms=VALUES(perms), perms_type=VALUES(perms_type), sort_no=VALUES(sort_no),
is_route=VALUES(is_route), is_leaf=VALUES(is_leaf), hidden=VALUES(hidden), status=VALUES(status), del_flag=VALUES(del_flag),
keep_alive=VALUES(keep_alive), internal_or_external=VALUES(internal_or_external);
INSERT INTO sys_permission(id,parent_id,name,menu_type,perms,perms_type,status,del_flag,create_by,create_time) VALUES
('1860000000000000032','1860000000000000031','新增',2,'mes:mes_material_category:add','1','1',0,'admin',NOW()),
('1860000000000000033','1860000000000000031','编辑',2,'mes:mes_material_category:edit','1','1',0,'admin',NOW()),
('1860000000000000034','1860000000000000031','删除',2,'mes:mes_material_category:delete','1','1',0,'admin',NOW()),
('1860000000000000035','1860000000000000031','批量删除',2,'mes:mes_material_category:deleteBatch','1','1',0,'admin',NOW()),
('1860000000000000036','1860000000000000031','导出',2,'mes:mes_material_category:exportXls','1','1',0,'admin',NOW()),
('1860000000000000037','1860000000000000031','导入',2,'mes:mes_material_category:importExcel','1','1',0,'admin',NOW())
ON DUPLICATE KEY UPDATE
parent_id=VALUES(parent_id), name=VALUES(name), menu_type=VALUES(menu_type), perms=VALUES(perms), perms_type=VALUES(perms_type),
status=VALUES(status), del_flag=VALUES(del_flag);

-- 二级菜单：ERP映射
INSERT INTO sys_permission(id, parent_id, name, url, component, component_name, menu_type, perms, perms_type, sort_no, is_route, is_leaf, hidden, status, del_flag, keep_alive, internal_or_external, create_by, create_time)
VALUES ('1860000000000000041', '1860000000000000001', 'ERP映射', '/mes/materialerpmap', 'mes/materialerpmap/index', 'MesMaterialErpMapList', 1, NULL, '1', 4, 1, 1, 0, '1', 0, 1, 0, 'admin', NOW())
ON DUPLICATE KEY UPDATE
parent_id=VALUES(parent_id), name=VALUES(name), url=VALUES(url), component=VALUES(component), component_name=VALUES(component_name),
menu_type=VALUES(menu_type), perms=VALUES(perms), perms_type=VALUES(perms_type), sort_no=VALUES(sort_no),
is_route=VALUES(is_route), is_leaf=VALUES(is_leaf), hidden=VALUES(hidden), status=VALUES(status), del_flag=VALUES(del_flag),
keep_alive=VALUES(keep_alive), internal_or_external=VALUES(internal_or_external);
INSERT INTO sys_permission(id,parent_id,name,menu_type,perms,perms_type,status,del_flag,create_by,create_time) VALUES
('1860000000000000042','1860000000000000041','新增',2,'mes:mes_material_erp_map:add','1','1',0,'admin',NOW()),
('1860000000000000043','1860000000000000041','编辑',2,'mes:mes_material_erp_map:edit','1','1',0,'admin',NOW()),
('1860000000000000044','1860000000000000041','删除',2,'mes:mes_material_erp_map:delete','1','1',0,'admin',NOW()),
('1860000000000000045','1860000000000000041','批量删除',2,'mes:mes_material_erp_map:deleteBatch','1','1',0,'admin',NOW()),
('1860000000000000046','1860000000000000041','导出',2,'mes:mes_material_erp_map:exportXls','1','1',0,'admin',NOW()),
('1860000000000000047','1860000000000000041','导入',2,'mes:mes_material_erp_map:importExcel','1','1',0,'admin',NOW())
ON DUPLICATE KEY UPDATE
parent_id=VALUES(parent_id), name=VALUES(name), menu_type=VALUES(menu_type), perms=VALUES(perms), perms_type=VALUES(perms_type),
status=VALUES(status), del_flag=VALUES(del_flag);

-- admin 角色授权
INSERT INTO sys_role_permission(id, role_id, permission_id, operate_date, operate_ip)
SELECT REPLACE(UUID(),'-',''), 'f6817f48af4fb3af11b9e8bf182f618b', p.id, NOW(), '127.0.0.1'
FROM sys_permission p
WHERE p.id IN (
  '1860000000000000001',
  '1860000000000000011','1860000000000000012','1860000000000000013','1860000000000000014','1860000000000000015','1860000000000000016','1860000000000000017',
  '1860000000000000021','1860000000000000022','1860000000000000023','1860000000000000024','1860000000000000025','1860000000000000026','1860000000000000027',
  '1860000000000000031','1860000000000000032','1860000000000000033','1860000000000000034','1860000000000000035','1860000000000000036','1860000000000000037',
  '1860000000000000041','1860000000000000042','1860000000000000043','1860000000000000044','1860000000000000045','1860000000000000046','1860000000000000047'
) AND NOT EXISTS (
  SELECT 1
  FROM sys_role_permission rp
  WHERE rp.role_id = 'f6817f48af4fb3af11b9e8bf182f618b'
    AND rp.permission_id = p.id
);

-- ======================================================
-- 修复项：菜单组件引用错误导致页面空白
-- ======================================================
UPDATE sys_permission
SET component = 'layouts/RouteView',
    is_leaf = 0,
    is_route = 0,
    redirect = NULL,
    hidden = 0,
    menu_type = 0
WHERE id = '1860000000000000001';

UPDATE sys_permission
SET is_leaf = 1,
    is_route = 1,
    hidden = 0,
    menu_type = 1
WHERE id IN (
  '1860000000000000011',
  '1860000000000000021',
  '1860000000000000031',
  '1860000000000000041'
);
