SET NAMES utf8mb4;

INSERT INTO sys_menu (
  id,
  parent_id,
  menu_name,
  path,
  menu_type,
  icon,
  create_user,
  update_user,
  sort,
  status,
  router,
  alias
)
SELECT
  1700000264,
  (SELECT id FROM sys_menu WHERE alias = 'ResidentActivityManager' LIMIT 1),
  '首冲奖励',
  'resident-activity/first-recharge-reward-config/index',
  2,
  'form',
  '0',
  '0',
  8,
  0,
  'first-recharge-reward/config',
  'ResidentFirstRechargeRewardConfig'
WHERE NOT EXISTS (
  SELECT 1 FROM sys_menu WHERE alias = 'ResidentFirstRechargeRewardConfig'
);

UPDATE sys_menu
SET
  parent_id = (
    SELECT resident_menu.id
    FROM (
      SELECT id
      FROM sys_menu
      WHERE alias = 'ResidentActivityManager'
      LIMIT 1
    ) AS resident_menu
  ),
  menu_name = '首冲奖励',
  path = 'resident-activity/first-recharge-reward-config/index',
  menu_type = 2,
  icon = 'form',
  create_user = '0',
  update_user = '0',
  sort = 8,
  status = 0,
  router = 'first-recharge-reward/config'
WHERE alias = 'ResidentFirstRechargeRewardConfig';

INSERT INTO sys_role_menu (role_id, menu_id)
SELECT DISTINCT
  parent_role.role_id,
  target_menu.id
FROM sys_role_menu AS parent_role
JOIN sys_menu AS parent_menu
  ON parent_menu.id = parent_role.menu_id
  AND parent_menu.alias = 'ResidentActivityManager'
JOIN sys_menu AS target_menu
  ON target_menu.alias = 'ResidentFirstRechargeRewardConfig'
LEFT JOIN sys_role_menu AS existing_role_menu
  ON existing_role_menu.role_id = parent_role.role_id
  AND existing_role_menu.menu_id = target_menu.id
WHERE existing_role_menu.id IS NULL;
