-- 设备管理目录：从 MES管理 下迁出，与 MES基础资料 同级（已执行旧版 mes-xsl-equipment-ledger-menu-permission 时用）
SET NAMES utf8mb4;

SET @mes_equip_root_parent = (
  SELECT `parent_id` FROM `sys_permission`
  WHERE `del_flag` = 0 AND `menu_type` = 0 AND `name` = 'MES基础资料'
  LIMIT 1
);
SET @mes_equip_root_parent = IFNULL(@mes_equip_root_parent, (
  SELECT `parent_id` FROM `sys_permission`
  WHERE `del_flag` = 0 AND `menu_type` = 0 AND `name` = 'MES资料'
  LIMIT 1
));

SET @mes_equip_root_sort = IFNULL((
  SELECT `sort_no` + 1 FROM `sys_permission`
  WHERE `del_flag` = 0 AND `menu_type` = 0 AND `name` IN ('MES基础资料', 'MES资料')
  ORDER BY `sort_no` DESC
  LIMIT 1
), 51);

UPDATE `sys_permission`
SET
  `parent_id` = @mes_equip_root_parent,
  `sort_no` = @mes_equip_root_sort,
  `menu_type` = 0,
  `is_leaf` = 0,
  `hidden` = 0,
  `status` = '1',
  `del_flag` = 0
WHERE `id` = '1860000000000000133';
