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
  1700000251,
  0,
  '常驻活动',
  '/resident-activity/manager',
  1,
  'example',
  '0',
  '0',
  195,
  0,
  NULL,
  'ResidentActivityManager'
WHERE NOT EXISTS (
  SELECT 1 FROM sys_menu WHERE alias = 'ResidentActivityManager'
);

UPDATE sys_menu
SET
  parent_id = 0,
  menu_name = '常驻活动',
  path = '/resident-activity/manager',
  menu_type = 1,
  icon = 'example',
  create_user = '0',
  update_user = '0',
  sort = 195,
  status = 0,
  router = NULL
WHERE alias = 'ResidentActivityManager';

INSERT INTO sys_menu (
  id,
  parent_id,
  menu_name,
  path,
  menu_type,
  icon,
  create_user,
  update_user,
  sort,
  status,
  router,
  alias
)
SELECT
  1700000252,
  (SELECT id FROM sys_menu WHERE alias = 'ResidentActivityManager' LIMIT 1),
  '签到奖励配置',
  'resident-activity/sign-in-reward-config/index',
  2,
  'form',
  '0',
  '0',
  1,
  0,
  'sign-in-reward/config',
  'ResidentSignInRewardConfig'
WHERE NOT EXISTS (
  SELECT 1 FROM sys_menu WHERE alias = 'ResidentSignInRewardConfig'
);

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/sign-in-reward-config/index',
  menu_type = 2,
  icon = 'form',
  create_user = '0',
  update_user = '0',
  sort = 1,
  status = 0,
  router = 'sign-in-reward/config'
WHERE alias = 'ResidentSignInRewardConfig';

INSERT INTO sys_menu (
  id,
  parent_id,
  menu_name,
  path,
  menu_type,
  icon,
  create_user,
  update_user,
  sort,
  status,
  router,
  alias
)
SELECT
  1700000253,
  (SELECT id FROM sys_menu WHERE alias = 'ResidentActivityManager' LIMIT 1),
  '注册奖励配置',
  'resident-activity/register-reward-config/index',
  2,
  'form',
  '0',
  '0',
  2,
  0,
  'register-reward/config',
  'ResidentRegisterRewardConfig'
WHERE NOT EXISTS (
  SELECT 1 FROM sys_menu WHERE alias = 'ResidentRegisterRewardConfig'
);

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/register-reward-config/index',
  menu_type = 2,
  icon = 'form',
  create_user = '0',
  update_user = '0',
  sort = 2,
  status = 0,
  router = 'register-reward/config'
WHERE alias = 'ResidentRegisterRewardConfig';

INSERT INTO sys_menu (
  id,
  parent_id,
  menu_name,
  path,
  menu_type,
  icon,
  create_user,
  update_user,
  sort,
  status,
  router,
  alias
)
SELECT
  1700000254,
  (SELECT id FROM sys_menu WHERE alias = 'ResidentActivityManager' LIMIT 1),
  '邀请活动配置',
  'resident-activity/invite-config/index',
  2,
  'form',
  '0',
  '0',
  3,
  0,
  'invite/config',
  'ResidentInviteConfig'
WHERE NOT EXISTS (
  SELECT 1 FROM sys_menu WHERE alias = 'ResidentInviteConfig'
);

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/invite-config/index',
  menu_type = 2,
  icon = 'form',
  create_user = '0',
  update_user = '0',
  sort = 3,
  status = 0,
  router = 'invite/config'
WHERE alias = 'ResidentInviteConfig';

INSERT INTO sys_menu (
  id,
  parent_id,
  menu_name,
  path,
  menu_type,
  icon,
  create_user,
  update_user,
  sort,
  status,
  router,
  alias
)
SELECT
  1700000255,
  (SELECT id FROM sys_menu WHERE alias = 'ResidentActivityManager' LIMIT 1),
  '邀请活动列表',
  'resident-activity/invite-list/index',
  2,
  'form',
  '0',
  '0',
  4,
  0,
  'invite/list',
  'ResidentInviteList'
WHERE NOT EXISTS (
  SELECT 1 FROM sys_menu WHERE alias = 'ResidentInviteList'
);

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/invite-list/index',
  menu_type = 2,
  icon = 'form',
  create_user = '0',
  update_user = '0',
  sort = 4,
  status = 0,
  router = 'invite/list'
WHERE alias = 'ResidentInviteList';

INSERT INTO sys_menu (
  id,
  parent_id,
  menu_name,
  path,
  menu_type,
  icon,
  create_user,
  update_user,
  sort,
  status,
  router,
  alias
)
SELECT
  1700000256,
  (SELECT id FROM sys_menu WHERE alias = 'ResidentActivityManager' LIMIT 1),
  '周星',
  'resident-activity/week-star/index',
  2,
  'form',
  '0',
  '0',
  5,
  0,
  'specified-gift-weekly-rank',
  'ResidentSpecifiedGiftWeeklyRank'
WHERE NOT EXISTS (
  SELECT 1 FROM sys_menu WHERE alias = 'ResidentSpecifiedGiftWeeklyRank'
);

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/week-star/index',
  menu_type = 2,
  icon = 'form',
  create_user = '0',
  update_user = '0',
  sort = 5,
  status = 0,
  router = 'specified-gift-weekly-rank'
WHERE alias = 'ResidentSpecifiedGiftWeeklyRank';

INSERT INTO sys_menu (
  id,
  parent_id,
  menu_name,
  path,
  menu_type,
  icon,
  create_user,
  update_user,
  sort,
  status,
  router,
  alias
)
SELECT
  1700000261,
  (SELECT id FROM sys_menu WHERE alias = 'ResidentActivityManager' LIMIT 1),
  'VIP',
  'resident-activity/vip-config/index',
  2,
  'form',
  '0',
  '0',
  6,
  0,
  'vip/config',
  'ResidentVipConfig'
WHERE NOT EXISTS (
  SELECT 1 FROM sys_menu WHERE alias = 'ResidentVipConfig'
);

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 = 'VIP',
  path = 'resident-activity/vip-config/index',
  menu_type = 2,
  icon = 'form',
  create_user = '0',
  update_user = '0',
  sort = 6,
  status = 0,
  router = 'vip/config'
WHERE alias = 'ResidentVipConfig';

INSERT INTO sys_menu (
  id,
  parent_id,
  menu_name,
  path,
  menu_type,
  icon,
  create_user,
  update_user,
  sort,
  status,
  router,
  alias
)
SELECT
  1700000262,
  (SELECT id FROM sys_menu WHERE alias = 'ResidentActivityManager' LIMIT 1),
  '充值奖励配置',
  'resident-activity/recharge-reward-config/index',
  2,
  'form',
  '0',
  '0',
  7,
  0,
  'recharge-reward/config',
  'ResidentRechargeRewardConfig'
WHERE NOT EXISTS (
  SELECT 1 FROM sys_menu WHERE alias = 'ResidentRechargeRewardConfig'
);

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/recharge-reward-config/index',
  menu_type = 2,
  icon = 'form',
  create_user = '0',
  update_user = '0',
  sort = 7,
  status = 0,
  router = 'recharge-reward/config'
WHERE alias = 'ResidentRechargeRewardConfig';

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_menu (
  id,
  parent_id,
  menu_name,
  path,
  menu_type,
  icon,
  create_user,
  update_user,
  sort,
  status,
  router,
  alias
)
SELECT
  1700000263,
  (SELECT id FROM sys_menu WHERE alias = 'ResidentActivityManager' LIMIT 1),
  '每日任务',
  'resident-activity/daily-task-config/index',
  2,
  'form',
  '0',
  '0',
  9,
  0,
  'daily-task/config',
  'ResidentDailyTaskConfig'
WHERE NOT EXISTS (
  SELECT 1 FROM sys_menu WHERE alias = 'ResidentDailyTaskConfig'
);

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/daily-task-config/index',
  menu_type = 2,
  icon = 'form',
  create_user = '0',
  update_user = '0',
  sort = 9,
  status = 0,
  router = 'daily-task/config'
WHERE alias = 'ResidentDailyTaskConfig';

INSERT INTO sys_menu (
  id,
  parent_id,
  menu_name,
  path,
  menu_type,
  icon,
  create_user,
  update_user,
  sort,
  status,
  router,
  alias
)
SELECT
  1700000258,
  0,
  '游戏管理',
  '/game-manager',
  1,
  'form',
  '0',
  '0',
  133,
  0,
  NULL,
  'OperateGameManager'
WHERE NOT EXISTS (
  SELECT 1 FROM sys_menu WHERE alias = 'OperateGameManager'
);

UPDATE sys_menu
SET
  parent_id = 0,
  menu_name = '游戏管理',
  path = '/game-manager',
  menu_type = 1,
  icon = 'form',
  create_user = '0',
  update_user = '0',
  sort = 133,
  status = 0,
  router = NULL
WHERE alias = 'OperateGameManager';

UPDATE sys_menu
SET
  parent_id = (
    SELECT game_manager.id
    FROM (
      SELECT id
      FROM sys_menu
      WHERE alias = 'OperateGameManager'
      LIMIT 1
    ) AS game_manager
  ),
  menu_name = '游戏列表',
  path = 'sys/game-manager/list/index',
  menu_type = 2,
  icon = 'form',
  create_user = '0',
  update_user = '0',
  sort = 1,
  status = 0,
  router = 'game-config'
WHERE alias = 'GameConfig';

INSERT INTO sys_menu (
  id,
  parent_id,
  menu_name,
  path,
  menu_type,
  icon,
  create_user,
  update_user,
  sort,
  status,
  router,
  alias
)
SELECT
  1700000257,
  (
    SELECT game_manager.id
    FROM (
      SELECT id
      FROM sys_menu
      WHERE alias = 'OperateGameManager'
      LIMIT 1
    ) AS game_manager
  ),
  '百顺配置',
  'sys/game-manager/baishun-config/index',
  2,
  'form',
  '0',
  '0',
  2,
  0,
  'baishun-config',
  'OperateBaishunGameManage'
WHERE NOT EXISTS (
  SELECT 1 FROM sys_menu WHERE alias = 'OperateBaishunGameManage'
);

UPDATE sys_menu
SET
  parent_id = (
    SELECT game_manager.id
    FROM (
      SELECT id
      FROM sys_menu
      WHERE alias = 'OperateGameManager'
      LIMIT 1
    ) AS game_manager
  ),
  menu_name = '百顺配置',
  path = 'sys/game-manager/baishun-config/index',
  menu_type = 2,
  icon = 'form',
  create_user = '0',
  update_user = '0',
  sort = 2,
  status = 0,
  router = 'baishun-config'
WHERE alias = 'OperateBaishunGameManage';

INSERT INTO sys_menu (
  id,
  parent_id,
  menu_name,
  path,
  menu_type,
  icon,
  create_user,
  update_user,
  sort,
  status,
  router,
  alias
)
SELECT
  next_id,
  (
    SELECT game_manager.id
    FROM (
      SELECT id
      FROM sys_menu
      WHERE alias = 'OperateGameManager'
      LIMIT 1
    ) AS game_manager
  ),
  '热游配置',
  'operate/hotgame-config',
  2,
  'form',
  '0',
  '0',
  4,
  0,
  'hotgame-config',
  'OperateHotgameConfig'
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 = 'OperateHotgameConfig'
);

UPDATE sys_menu
SET
  parent_id = (
    SELECT game_manager.id
    FROM (
      SELECT id
      FROM sys_menu
      WHERE alias = 'OperateGameManager'
      LIMIT 1
    ) AS game_manager
  ),
  menu_name = '热游配置',
  path = 'operate/hotgame-config',
  menu_type = 2,
  icon = 'form',
  create_user = '0',
  update_user = '0',
  sort = 4,
  status = 0,
  router = 'hotgame-config'
WHERE alias = 'OperateHotgameConfig';

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 target_menu
  ON target_menu.alias IN (
    'ResidentActivityManager',
    'ResidentSignInRewardConfig',
    'ResidentRegisterRewardConfig',
    'ResidentInviteConfig',
    'ResidentInviteList',
    'ResidentSpecifiedGiftWeeklyRank',
    'ResidentVipConfig',
    'ResidentRechargeRewardConfig',
    'ResidentFirstRechargeRewardConfig',
    'ResidentDailyTaskConfig',
    'OperateGameManager',
    'GameConfig',
    'OperateBaishunGameManage',
    'OperateHotgameConfig'
  )
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 parent_role.menu_id = 1700000057
  AND existing_role_menu.id IS NULL;

INSERT INTO sys_role_menu (role_id, menu_id)
SELECT DISTINCT
  source_role.role_id,
  target_menu.id
FROM sys_role_menu AS source_role
JOIN sys_menu AS source_menu
  ON source_menu.id = source_role.menu_id
JOIN sys_menu AS target_menu
  ON target_menu.alias = 'OperateHotgameConfig'
LEFT JOIN sys_role_menu AS existing_role_menu
  ON existing_role_menu.role_id = source_role.role_id
  AND existing_role_menu.menu_id = target_menu.id
WHERE source_menu.alias IN ('OperateGameManager', 'GameConfig', 'OperateBaishunGameManage', 'OperateLingxianConfig')
  AND existing_role_menu.id IS NULL;

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 = 'ResidentDailyTaskConfig'
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;

INSERT INTO sys_menu (
  id,
  parent_id,
  menu_name,
  path,
  menu_type,
  icon,
  create_user,
  update_user,
  sort,
  status,
  router,
  alias
)
SELECT
  next_id,
  (SELECT id FROM sys_menu WHERE alias = 'ResidentActivityManager' LIMIT 1),
  'CP',
  'resident-activity/cp-config/index',
  2,
  'form',
  '0',
  '0',
  6,
  0,
  'cp/config',
  'ResidentCpConfig'
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 = 'ResidentCpConfig'
);

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 = 'CP',
  path = 'resident-activity/cp-config/index',
  menu_type = 2,
  icon = 'form',
  create_user = '0',
  update_user = '0',
  sort = 6,
  status = 0,
  router = 'cp/config'
WHERE alias = 'ResidentCpConfig';

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 = 'ResidentCpConfig'
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;

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;

UPDATE sys_menu
SET menu_name = CONVERT(0xE5B8B8E9A9BBE6B4BBE58AA8 USING utf8mb4)
WHERE alias = 'ResidentActivityManager';

UPDATE sys_menu
SET menu_name = CONVERT(0xE7ADBEE588B0E5A596E58AB1E9858DE7BDAE USING utf8mb4)
WHERE alias = 'ResidentSignInRewardConfig';

UPDATE sys_menu
SET menu_name = CONVERT(0xE6B3A8E5868CE5A596E58AB1E9858DE7BDAE USING utf8mb4)
WHERE alias = 'ResidentRegisterRewardConfig';

UPDATE sys_menu
SET menu_name = CONVERT(0xE98280E8AFB7E6B4BBE58AA8E9858DE7BDAE USING utf8mb4)
WHERE alias = 'ResidentInviteConfig';

UPDATE sys_menu
SET menu_name = CONVERT(0xE98280E8AFB7E6B4BBE58AA8E58897E8A1A8 USING utf8mb4)
WHERE alias = 'ResidentInviteList';

UPDATE sys_menu
SET menu_name = CONVERT(0xE591A8E6989F USING utf8mb4)
WHERE alias = 'ResidentSpecifiedGiftWeeklyRank';

UPDATE sys_menu
SET menu_name = 'VIP'
WHERE alias = 'ResidentVipConfig';

UPDATE sys_menu
SET menu_name = CONVERT(0xE58585E580BCE5A596E58AB1E9858DE7BDAE USING utf8mb4)
WHERE alias = 'ResidentRechargeRewardConfig';

UPDATE sys_menu
SET menu_name = CONVERT(0xE9A696E586B2E5A596E58AB1 USING utf8mb4)
WHERE alias = 'ResidentFirstRechargeRewardConfig';

UPDATE sys_menu
SET menu_name = CONVERT(0xE6AF8FE697A5E4BBBBE58AA1 USING utf8mb4)
WHERE alias = 'ResidentDailyTaskConfig';

UPDATE sys_menu
SET menu_name = CONVERT(0xE6B8B8E6888FE7AEA1E79086 USING utf8mb4)
WHERE alias = 'OperateGameManager';

UPDATE sys_menu
SET menu_name = CONVERT(0xE6B8B8E6888FE58897E8A1A8 USING utf8mb4)
WHERE alias = 'GameConfig';

UPDATE sys_menu
SET menu_name = CONVERT(0xE799BEE9A1BAE9858DE7BDAE USING utf8mb4)
WHERE alias = 'OperateBaishunGameManage';

UPDATE sys_menu
SET menu_name = CONVERT(0xE783ADE6B8B8E9858DE7BDAE USING utf8mb4)
WHERE alias = 'OperateHotgameConfig';

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

INSERT INTO sys_menu (
  id,
  parent_id,
  menu_name,
  path,
  menu_type,
  icon,
  create_user,
  update_user,
  sort,
  status,
  router,
  alias
)
SELECT
  next_id,
  @resident_activity_id,
  CONVERT(0xE7BAA2E58C85 USING utf8mb4),
  'resident-activity/voice-room-red-packet',
  2,
  'form',
  '0',
  '0',
  9,
  0,
  'voice-room-red-packet',
  'ResidentVoiceRoomRedPacket'
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 = 'ResidentVoiceRoomRedPacket'
);

UPDATE sys_menu
SET
  parent_id = @resident_activity_id,
  menu_name = CONVERT(0xE7BAA2E58C85 USING utf8mb4),
  path = 'resident-activity/voice-room-red-packet',
  menu_type = 2,
  icon = 'form',
  create_user = '0',
  update_user = '0',
  sort = 9,
  status = 0,
  router = 'voice-room-red-packet'
WHERE alias = 'ResidentVoiceRoomRedPacket';

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 = 'ResidentVoiceRoomRedPacket'
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;

INSERT INTO sys_menu (
  id,
  parent_id,
  menu_name,
  path,
  menu_type,
  icon,
  create_user,
  update_user,
  sort,
  status,
  router,
  alias
)
SELECT
  next_id,
  @resident_activity_id,
  '火箭',
  'resident-activity/voice-room-rocket',
  2,
  'form',
  '0',
  '0',
  10,
  0,
  'voice-room-rocket',
  'ResidentVoiceRoomRocket'
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 = 'ResidentVoiceRoomRocket'
);

UPDATE sys_menu
SET
  parent_id = @resident_activity_id,
  menu_name = '火箭',
  path = 'resident-activity/voice-room-rocket',
  menu_type = 2,
  icon = 'form',
  create_user = '0',
  update_user = '0',
  sort = 10,
  status = 0,
  router = 'voice-room-rocket'
WHERE alias = 'ResidentVoiceRoomRocket';

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 = 'ResidentVoiceRoomRocket'
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;
