-- 自动/人工小料计划维护 菜单与权限（挂载到 MES密炼工程，兼容 MES管理）
SET NAMES utf8mb4;

SET @plan_parent_id = (
  SELECT id
  FROM sys_permission
  WHERE name = 'MES密炼工程' AND menu_type = 0 AND del_flag = 0
  ORDER BY create_time ASC
  LIMIT 1
);
SET @plan_parent_id = IFNULL(@plan_parent_id, (
  SELECT id
  FROM sys_permission
  WHERE url = '/mes' AND menu_type = 0 AND del_flag = 0
  ORDER BY create_time ASC
  LIMIT 1
));
SET @plan_parent_id = IFNULL(@plan_parent_id, '1860000000000000001');

-- 自动小料计划维护
INSERT INTO sys_permission
(`id`,`parent_id`,`name`,`url`,`component`,`component_name`,`menu_type`,`perms`,`perms_type`,`sort_no`,`always_show`,`icon`,`is_route`,`is_leaf`,`keep_alive`,`hidden`,`hide_tab`,`description`,`status`,`del_flag`,`create_by`,`create_time`,`rule_flag`,`internal_or_external`)
VALUES
('1900000000000000740',@plan_parent_id,'自动小料计划维护','/mes/autosmallmaterialplanmaintain','mes/autosmallmaterialplanmaintain/index','MesXslAutoSmallMaterialPlanMaintainList',1,NULL,'1',98,0,'ant-design:table-outlined',1,1,1,0,0,'自动小料计划维护',1,0,'admin',NOW(),0,0)
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`),`sort_no`=VALUES(`sort_no`),
`is_route`=VALUES(`is_route`),`is_leaf`=VALUES(`is_leaf`),`keep_alive`=VALUES(`keep_alive`),`icon`=VALUES(`icon`),
`status`=VALUES(`status`),`hidden`=VALUES(`hidden`),`del_flag`=VALUES(`del_flag`);

INSERT INTO sys_permission
(`id`,`parent_id`,`name`,`menu_type`,`perms`,`perms_type`,`sort_no`,`status`,`del_flag`,`create_by`,`create_time`)
VALUES
('1900000000000000741','1900000000000000740','整表保存',2,'xslmes:mes_xsl_auto_small_material_plan_maintain:saveAll','1',1,'1',0,'admin',NOW())
ON DUPLICATE KEY UPDATE
`name`=VALUES(`name`),`perms`=VALUES(`perms`),`sort_no`=VALUES(`sort_no`),`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`,`always_show`,`icon`,`is_route`,`is_leaf`,`keep_alive`,`hidden`,`hide_tab`,`description`,`status`,`del_flag`,`create_by`,`create_time`,`rule_flag`,`internal_or_external`)
VALUES
('1900000000000000750',@plan_parent_id,'人工小料计划维护','/mes/manualsmallmaterialplanmaintain','mes/manualsmallmaterialplanmaintain/index','MesXslManualSmallMaterialPlanMaintainList',1,NULL,'1',99,0,'ant-design:table-outlined',1,1,1,0,0,'人工小料计划维护',1,0,'admin',NOW(),0,0)
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`),`sort_no`=VALUES(`sort_no`),
`is_route`=VALUES(`is_route`),`is_leaf`=VALUES(`is_leaf`),`keep_alive`=VALUES(`keep_alive`),`icon`=VALUES(`icon`),
`status`=VALUES(`status`),`hidden`=VALUES(`hidden`),`del_flag`=VALUES(`del_flag`);

INSERT INTO sys_permission
(`id`,`parent_id`,`name`,`menu_type`,`perms`,`perms_type`,`sort_no`,`status`,`del_flag`,`create_by`,`create_time`)
VALUES
('1900000000000000751','1900000000000000750','整表保存',2,'xslmes:mes_xsl_manual_small_material_plan_maintain:saveAll','1',1,'1',0,'admin',NOW())
ON DUPLICATE KEY UPDATE
`name`=VALUES(`name`),`perms`=VALUES(`perms`),`sort_no`=VALUES(`sort_no`),`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 ('1900000000000000740','1900000000000000741','1900000000000000750','1900000000000000751')
  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 parent_id=@plan_parent_id,url='/mes/autosmallmaterialplanmaintain',component='mes/autosmallmaterialplanmaintain/index',
    component_name='MesXslAutoSmallMaterialPlanMaintainList',menu_type=1,is_route=1,is_leaf=1,hidden=0,status='1',del_flag=0,redirect=NULL
WHERE id='1900000000000000740' OR name='自动小料计划维护' OR url='/mes/autosmallmaterialplanmaintain';

UPDATE sys_permission
SET parent_id=@plan_parent_id,url='/mes/manualsmallmaterialplanmaintain',component='mes/manualsmallmaterialplanmaintain/index',
    component_name='MesXslManualSmallMaterialPlanMaintainList',menu_type=1,is_route=1,is_leaf=1,hidden=0,status='1',del_flag=0,redirect=NULL
WHERE id='1900000000000000750' OR name='人工小料计划维护' OR url='/mes/manualsmallmaterialplanmaintain';
