SET NAMES utf8mb4;

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

INSERT INTO `sys_menu` (
  `id`, `parent_id`, `menu_name`, `path`, `menu_type`, `icon`, `create_user`, `update_user`,
  `sort`, `status`, `router`, `alias`
)
SELECT
  1700000500, @team_parent_menu_id, '经理列表', 'team/manager/index', 2, 'form',
  '0', '0', 245, 0, 'team/manager', 'TeamManagerList'
WHERE @team_parent_menu_id IS NOT NULL
  AND NOT EXISTS (
    SELECT 1 FROM `sys_menu` WHERE `alias` = 'TeamManagerList'
  );

UPDATE `sys_menu`
SET
  `parent_id` = @team_parent_menu_id,
  `menu_name` = '经理列表',
  `path` = 'team/manager/index',
  `menu_type` = 2,
  `icon` = 'form',
  `create_user` = '0',
  `update_user` = '0',
  `sort` = 245,
  `status` = 0,
  `router` = 'team/manager'
WHERE `alias` = 'TeamManagerList'
  AND @team_parent_menu_id IS NOT NULL;

INSERT INTO `sys_menu` (
  `id`, `parent_id`, `menu_name`, `path`, `menu_type`, `icon`, `create_user`, `update_user`,
  `sort`, `status`, `router`, `alias`
)
SELECT *
FROM (
  SELECT 1700000501 AS `id`, 0 AS `parent_id`, 'team:manager:list:query:all' AS `menu_name`,
         NULL AS `path`, 3 AS `menu_type`, NULL AS `icon`, '0' AS `create_user`, '0' AS `update_user`,
         39 AS `sort`, 0 AS `status`, NULL AS `router`, 'team:manager:list:query:all' AS `alias`
  UNION ALL
  SELECT 1700000502, 0, 'team:manager:list:add', NULL, 3, NULL, '0', '0', 38, 0, NULL, 'team:manager:list:add'
  UNION ALL
  SELECT 1700000503, 0, 'team:manager:list:edit', NULL, 3, NULL, '0', '0', 37, 0, NULL, 'team:manager:list:edit'
  UNION ALL
  SELECT 1700000504, 0, 'team:manager:list:del', NULL, 3, NULL, '0', '0', 36, 0, NULL, 'team:manager:list:del'
) seed
WHERE NOT EXISTS (
  SELECT 1 FROM `sys_menu` existing WHERE existing.`alias` = seed.`alias`
);

UPDATE `sys_menu`
SET
  `parent_id` = 0,
  `path` = NULL,
  `menu_type` = 3,
  `icon` = NULL,
  `create_user` = '0',
  `update_user` = '0',
  `status` = 0,
  `router` = NULL,
  `menu_name` = `alias`,
  `sort` = CASE `alias`
    WHEN 'team:manager:list:query:all' THEN 39
    WHEN 'team:manager:list:add' THEN 38
    WHEN 'team:manager:list:edit' THEN 37
    WHEN 'team:manager:list:del' THEN 36
    ELSE `sort`
  END
WHERE `alias` IN (
  'team:manager:list:query:all',
  'team:manager:list:add',
  'team:manager:list:edit',
  'team:manager:list:del'
);

INSERT INTO `sys_role_menu` (`role_id`, `menu_id`)
SELECT DISTINCT source.`role_id`, target_menu.`id`
FROM `sys_role_menu` source
JOIN `sys_menu` source_menu ON source_menu.`id` = source.`menu_id`
JOIN `sys_menu` target_menu ON target_menu.`alias` = 'TeamManagerList'
WHERE source_menu.`alias` = 'TeamMember'
  AND NOT EXISTS (
    SELECT 1 FROM `sys_role_menu` target
    WHERE target.`role_id` = source.`role_id`
      AND target.`menu_id` = target_menu.`id`
  );

INSERT INTO `sys_role_menu` (`role_id`, `menu_id`)
SELECT DISTINCT source.`role_id`, target_menu.`id`
FROM `sys_role_menu` source
JOIN `sys_menu` source_menu ON source_menu.`id` = source.`menu_id`
JOIN `sys_menu` target_menu ON target_menu.`alias` = CASE source_menu.`alias`
  WHEN 'team:list:query:all' THEN 'team:manager:list:query:all'
  WHEN 'team:list:add' THEN 'team:manager:list:add'
  WHEN 'team:list:edit' THEN 'team:manager:list:edit'
  WHEN 'team:list:del' THEN 'team:manager:list:del'
END
WHERE source_menu.`alias` IN (
  'team:list:query:all',
  'team:list:add',
  'team:list:edit',
  'team:list:del'
)
  AND NOT EXISTS (
    SELECT 1 FROM `sys_role_menu` target
    WHERE target.`role_id` = source.`role_id`
      AND target.`menu_id` = target_menu.`id`
  );
