-- 将 输送路径策略管理 菜单挂载到：基础资料（优先）或开发专用
-- 说明：执行本脚本后，请在“角色授权”里给对应角色勾选新菜单和“查看”权限。

SET @station_path_policy_parent_id := COALESCE(
  (
    SELECT id
    FROM sys_resource
    WHERE code = 'base' AND level = 1
    ORDER BY id
    LIMIT 1
  ),
  (
    SELECT id
    FROM sys_resource
    WHERE code = 'develop' AND level = 1
    ORDER BY id
    LIMIT 1
  )
);

INSERT INTO sys_resource(code, name, resource_id, level, sort, status)
SELECT 'stationPathPolicy/stationPathPolicy.html', '输送路径策略', @station_path_policy_parent_id, 2, 996, 1
FROM dual
WHERE @station_path_policy_parent_id IS NOT NULL
  AND NOT EXISTS (
    SELECT 1
    FROM sys_resource
    WHERE code = 'stationPathPolicy/stationPathPolicy.html' AND level = 2
  );

UPDATE sys_resource
SET name = '输送路径策略',
    resource_id = @station_path_policy_parent_id,
    level = 2,
    sort = 996,
    status = 1
WHERE code = 'stationPathPolicy/stationPathPolicy.html' AND level = 2;

SET @station_path_policy_id := (
  SELECT id
  FROM sys_resource
  WHERE code = 'stationPathPolicy/stationPathPolicy.html' AND level = 2
  ORDER BY id
  LIMIT 1
);

INSERT INTO sys_resource(code, name, resource_id, level, sort, status)
SELECT 'stationPathPolicy/stationPathPolicy.html#view', '查看', @station_path_policy_id, 3, 1, 1
FROM dual
WHERE @station_path_policy_id IS NOT NULL
  AND NOT EXISTS (
    SELECT 1
    FROM sys_resource
    WHERE code = 'stationPathPolicy/stationPathPolicy.html#view' AND level = 3
  );

UPDATE sys_resource
SET name = '查看',
    resource_id = @station_path_policy_id,
    level = 3,
    sort = 1,
    status = 1
WHERE code = 'stationPathPolicy/stationPathPolicy.html#view' AND level = 3;

SELECT id, code, name, resource_id, level, sort, status
FROM sys_resource
WHERE code IN (
  'stationPathPolicy/stationPathPolicy.html',
  'stationPathPolicy/stationPathPolicy.html#view'
)
ORDER BY level, sort, id;
