-- 生产订单「拆分」按钮权限补丁
SET NAMES utf8mb4;

-- 优先按组件路径定位“生产订单”菜单
SET @prod_menu_id = (
  SELECT id
  FROM sys_permission
  WHERE component = 'mes/productionorderinfo/index'
    AND menu_type = 1
    AND del_flag = 0
  ORDER BY create_time ASC
  LIMIT 1
);

-- 兜底：按URL定位
SET @prod_menu_id = IFNULL(@prod_menu_id, (
  SELECT id
  FROM sys_permission
  WHERE url = '/mes/productionorderinfo'
    AND menu_type = 1
    AND del_flag = 0
  ORDER BY create_time ASC
  LIMIT 1
));

-- 再兜底：按名称定位
SET @prod_menu_id = IFNULL(@prod_menu_id, (
  SELECT id
  FROM sys_permission
  WHERE name = '生产订单'
    AND menu_type = 1
    AND del_flag = 0
  ORDER BY create_time ASC
  LIMIT 1
));

-- 若找不到页面菜单，回退到约定ID（你现有脚本中使用）
SET @prod_menu_id = IFNULL(@prod_menu_id, '1860000000000099511');

-- 写入/修复“拆分”按钮权限
INSERT INTO sys_permission (
  id, parent_id, name, menu_type, perms, perms_type, status, del_flag, create_by, create_time
) VALUES (
  '1860000000000099517', @prod_menu_id, '拆分', 2, 'xslmes:mes_xsl_production_order:split', '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', '1860000000000099517', NOW(), '127.0.0.1'
FROM dual
WHERE NOT EXISTS (
  SELECT 1
  FROM sys_role_permission
  WHERE role_id = 'f6817f48af4fb3af11b9e8bf182f618b'
    AND permission_id = '1860000000000099517'
);
