-- 新增 日志流水清理 菜单，并初始化日志保留天数配置
-- 说明：执行本脚本后，请在“角色授权”里给对应角色勾选新菜单和“查看”权限。

INSERT INTO sys_config(name, code, value, type, status, select_type)
SELECT '日志流水清理保留天数', 'logCleanupExpireDays', '180', 1, 1, 'develop'
FROM dual
WHERE NOT EXISTS (
    SELECT 1
    FROM sys_config
    WHERE code = 'logCleanupExpireDays'
);

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

INSERT INTO sys_resource(code, name, resource_id, level, sort, status)
SELECT 'logCleanup/logCleanup.html', '日志流水清理', @log_cleanup_parent_id, 2, 995, 1
FROM dual
WHERE @log_cleanup_parent_id IS NOT NULL
  AND NOT EXISTS (
    SELECT 1
    FROM sys_resource
    WHERE code = 'logCleanup/logCleanup.html' AND level = 2
  );

UPDATE sys_resource
SET name = '日志流水清理',
    resource_id = @log_cleanup_parent_id,
    level = 2,
    sort = 995,
    status = 1
WHERE code = 'logCleanup/logCleanup.html' AND level = 2;

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

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

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

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