-- 将 设备网络分析 菜单挂载到：日志报表（优先）或开发专用
-- 说明：执行本脚本后，请在“角色授权”里给对应角色勾选新菜单和“查看”权限。

SET @device_ping_parent_id := COALESCE(
  (
    SELECT id
    FROM sys_resource
    WHERE code = 'logReport' 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 'devicePingLog/devicePingLog.html', '设备网络分析', @device_ping_parent_id, 2, 997, 1
FROM dual
WHERE @device_ping_parent_id IS NOT NULL
  AND NOT EXISTS (
    SELECT 1
    FROM sys_resource
    WHERE code = 'devicePingLog/devicePingLog.html' AND level = 2
  );

UPDATE sys_resource
SET name = '设备网络分析',
    resource_id = @device_ping_parent_id,
    level = 2,
    sort = 997,
    status = 1
WHERE code = 'devicePingLog/devicePingLog.html' AND level = 2;

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

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

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

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