SET NAMES utf8mb4;

SET @team_manager_id := (
  SELECT id FROM sys_menu WHERE alias = 'TeamManager' LIMIT 1
);

UPDATE sys_menu
SET
  parent_id = @team_manager_id,
  menu_name = '区域配置',
  path = 'team/region-config/index',
  menu_type = 2,
  icon = 'form',
  update_user = '0',
  sort = 213,
  status = 0,
  router = 'region/config'
WHERE alias = 'SystemRegionConfig';

UPDATE sys_menu
SET
  parent_id = @team_manager_id,
  menu_name = '区域配置',
  path = 'team/region-config/index',
  menu_type = 2,
  icon = 'form',
  update_user = '0',
  sort = 213,
  status = 0,
  router = 'region/config',
  alias = 'SystemRegionConfig'
WHERE alias = 'RegionConfig'
  AND NOT EXISTS (
    SELECT 1
    FROM (
      SELECT id FROM sys_menu WHERE alias = 'SystemRegionConfig' LIMIT 1
    ) AS existing_region_config
  );

INSERT INTO sys_menu (
  id,
  parent_id,
  menu_name,
  path,
  menu_type,
  icon,
  create_user,
  update_user,
  sort,
  status,
  router,
  alias
)
SELECT
  next_id,
  @team_manager_id,
  '区域配置',
  'team/region-config/index',
  2,
  'form',
  '0',
  '0',
  213,
  0,
  'region/config',
  'SystemRegionConfig'
FROM (
  SELECT COALESCE(MAX(id), 1700000000) + 1 AS next_id
  FROM sys_menu
) AS menu_id
WHERE NOT EXISTS (
  SELECT 1 FROM sys_menu WHERE alias = 'SystemRegionConfig'
)
AND NOT EXISTS (
  SELECT 1 FROM sys_menu WHERE id = 1700000089
);
