-- 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);

-- 二级菜单：密炼物料信息
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 ('1860000000000000051', '1860000000000000001', '密炼物料信息', '/mes/mixermaterialinfo', 'mes/mixermaterialinfo/index', 'MesMixerMaterialList', 1, NULL, '1', 5, 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
('1860000000000000052','1860000000000000051','新增',2,'mes:mes_mixer_material:add','1','1',0,'admin',NOW()),
('1860000000000000053','1860000000000000051','编辑',2,'mes:mes_mixer_material:edit','1','1',0,'admin',NOW()),
('1860000000000000054','1860000000000000051','删除',2,'mes:mes_mixer_material:delete','1','1',0,'admin',NOW()),
('1860000000000000055','1860000000000000051','批量删除',2,'mes:mes_mixer_material:deleteBatch','1','1',0,'admin',NOW()),
('1860000000000000056','1860000000000000051','导出',2,'mes:mes_mixer_material:exportXls','1','1',0,'admin',NOW()),
('1860000000000000057','1860000000000000051','导入',2,'mes:mes_mixer_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);

-- 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',
  '1860000000000000051','1860000000000000052','1860000000000000053','1860000000000000054','1860000000000000055','1860000000000000056','1860000000000000057'
) 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',
  '1860000000000000051'
);

-- ======================================================
-- 新增菜单：原材料检验项目
-- ======================================================
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 ('1860000000000000061', '1860000000000000001', '原材料检验项目', '/mes/rawmaterialinspectitem', 'mes/material/MesRawMaterialInspectItemList', 'MesRawMaterialInspectItemList', 1, NULL, '1', 6, 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
('1860000000000000062','1860000000000000061','新增',2,'mes:mes_raw_material_inspect_item:add','1','1',0,'admin',NOW()),
('1860000000000000063','1860000000000000061','编辑',2,'mes:mes_raw_material_inspect_item:edit','1','1',0,'admin',NOW()),
('1860000000000000064','1860000000000000061','删除',2,'mes:mes_raw_material_inspect_item:delete','1','1',0,'admin',NOW()),
('1860000000000000065','1860000000000000061','批量删除',2,'mes:mes_raw_material_inspect_item:deleteBatch','1','1',0,'admin',NOW()),
('1860000000000000066','1860000000000000061','导出',2,'mes:mes_raw_material_inspect_item:exportXls','1','1',0,'admin',NOW()),
('1860000000000000067','1860000000000000061','导入',2,'mes:mes_raw_material_inspect_item: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 ('1860000000000000071', '1860000000000000001', '原材料检验标准', '/mes/rawmaterialinspectstd', 'mes/material/MesRawMaterialInspectStdList', 'MesRawMaterialInspectStdList', 1, NULL, '1', 7, 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
('1860000000000000072','1860000000000000071','新增',2,'mes:mes_raw_material_inspect_std:add','1','1',0,'admin',NOW()),
('1860000000000000073','1860000000000000071','编辑',2,'mes:mes_raw_material_inspect_std:edit','1','1',0,'admin',NOW()),
('1860000000000000074','1860000000000000071','删除',2,'mes:mes_raw_material_inspect_std:delete','1','1',0,'admin',NOW()),
('1860000000000000075','1860000000000000071','批量删除',2,'mes:mes_raw_material_inspect_std:deleteBatch','1','1',0,'admin',NOW()),
('1860000000000000076','1860000000000000071','启用停用',2,'mes:mes_raw_material_inspect_std:enable','1','1',0,'admin',NOW()),
('1860000000000000077','1860000000000000071','导出',2,'mes:mes_raw_material_inspect_std:exportXls','1','1',0,'admin',NOW()),
('1860000000000000078','1860000000000000071','导入',2,'mes:mes_raw_material_inspect_std: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 (
  '1860000000000000061','1860000000000000062','1860000000000000063','1860000000000000064','1860000000000000065','1860000000000000066','1860000000000000067',
  '1860000000000000071','1860000000000000072','1860000000000000073','1860000000000000074','1860000000000000075','1860000000000000076','1860000000000000077','1860000000000000078'
) AND NOT EXISTS (
  SELECT 1
  FROM sys_role_permission rp
  WHERE rp.role_id = 'f6817f48af4fb3af11b9e8bf182f618b'
    AND rp.permission_id = p.id
);
