-- LMS 日计划菜单及按钮权限，固定基础数据使用预留的 2093 号段。
-- 兼容已经执行过旧组件路径的环境，优先修正固定菜单标识，也兼容按旧 component 匹配的数据。
UPDATE system_menu
SET component = 'lms/daily-plan/management/index',
    component_name = 'LmsDailyPlanManagement',
    updater = '1',
    update_time = NOW()
WHERE deleted = b'0'
  AND (id = 2093000000000000100 OR component = 'lms/dailyplan/index');

UPDATE system_menu
SET component = 'lms/daily-plan/work/index',
    component_name = 'LmsDailyPlanWork',
    updater = '1',
    update_time = NOW()
WHERE deleted = b'0'
  AND (id = 2093000000000000101 OR component = 'lms/dailyplanwork/index');

INSERT INTO system_menu
    (id, name, permission, type, sort, parent_id, path, icon, component, component_name,
     status, visible, keep_alive, always_show, creator, create_time, updater, update_time, deleted)
SELECT 2093000000000000100, '日计划管理', '', 2, 20, parent.id, 'daily-plan', '',
       'lms/daily-plan/management/index', 'LmsDailyPlanManagement', 0, b'1', b'1', b'1', '1', NOW(), '1', NOW(), b'0'
FROM system_menu parent
JOIN system_menu lms ON lms.id = parent.parent_id AND lms.path = '/lms' AND lms.deleted = b'0'
WHERE parent.path = 'tube-library' AND parent.deleted = b'0'
  AND NOT EXISTS (
      SELECT 1 FROM system_menu
      WHERE (id=2093000000000000100 OR component='lms/daily-plan/management/index') AND deleted=b'0'
  );

INSERT INTO system_menu
    (id, name, permission, type, sort, parent_id, path, icon, component, component_name,
     status, visible, keep_alive, always_show, creator, create_time, updater, update_time, deleted)
SELECT 2093000000000000101, '日计划作业', '', 2, 30, parent.id, 'daily-plan-work', '',
       'lms/daily-plan/work/index', 'LmsDailyPlanWork', 0, b'1', b'1', b'1', '1', NOW(), '1', NOW(), b'0'
FROM system_menu parent
JOIN system_menu lms ON lms.id = parent.parent_id AND lms.path = '/lms' AND lms.deleted = b'0'
WHERE parent.path = 'tube-library' AND parent.deleted = b'0'
  AND NOT EXISTS (
      SELECT 1 FROM system_menu
      WHERE (id=2093000000000000101 OR component='lms/daily-plan/work/index') AND deleted=b'0'
  );

-- 查询、新增、修改、启动和排程权限归属于日计划管理。
INSERT INTO system_menu
    (id, name, permission, type, sort, parent_id, path, icon, component, component_name,
     status, visible, keep_alive, always_show, creator, create_time, updater, update_time, deleted)
SELECT 2093000000000000110 + permission.sort, permission.name, permission.code, 3, permission.sort,
       menu.id, '', '', '', '', 0, b'1', b'1', b'1', '1', NOW(), '1', NOW(), b'0'
FROM system_menu menu
CROSS JOIN (
    SELECT 1 sort, '日计划查询' name, 'lms:daily-plan:query' code
    UNION ALL SELECT 2, '日计划新增', 'lms:daily-plan:create'
    UNION ALL SELECT 3, '日计划修改', 'lms:daily-plan:update'
    UNION ALL SELECT 4, '日计划启动', 'lms:daily-plan:start'
    UNION ALL SELECT 5, '日计划排程', 'lms:daily-plan:schedule'
    UNION ALL SELECT 6, '日计划策略', 'lms:daily-plan:strategy'
    UNION ALL SELECT 7, '日计划操作', 'lms:daily-plan:operate'
) permission
WHERE menu.id = COALESCE(
        (SELECT fixed.id FROM system_menu fixed WHERE fixed.id=2093000000000000100 AND fixed.deleted=b'0'),
        (SELECT MIN(fallback.id) FROM system_menu fallback
         WHERE fallback.component='lms/daily-plan/management/index' AND fallback.deleted=b'0')
      )
  AND menu.deleted=b'0'
  AND NOT EXISTS (
      SELECT 1 FROM system_menu existing
      WHERE existing.parent_id=menu.id AND existing.permission=permission.code AND existing.deleted=b'0'
  );

-- 作业页面独立挂载自身实际使用的按钮权限，避免仅授权作业菜单时按钮全部不可见。
INSERT INTO system_menu
    (id, name, permission, type, sort, parent_id, path, icon, component, component_name,
     status, visible, keep_alive, always_show, creator, create_time, updater, update_time, deleted)
SELECT permission.id, permission.name, permission.code, 3, permission.sort, menu.id,
       '', '', '', '', 0, b'1', b'1', b'1', '1', NOW(), '1', NOW(), b'0'
FROM system_menu menu
CROSS JOIN (
    SELECT 2093000000000000120 id, 1 sort, '日计划作业操作' name, 'lms:daily-plan:operate' code
    UNION ALL SELECT 2093000000000000121, 2, '日计划作业查询', 'lms:daily-plan:query'
    UNION ALL SELECT 2093000000000000122, 3, '日计划作业启动', 'lms:daily-plan:start'
    UNION ALL SELECT 2093000000000000123, 4, '日计划作业排程', 'lms:daily-plan:schedule'
    UNION ALL SELECT 2093000000000000124, 5, '日计划作业策略', 'lms:daily-plan:strategy'
) permission
WHERE menu.id = COALESCE(
        (SELECT fixed.id FROM system_menu fixed WHERE fixed.id=2093000000000000101 AND fixed.deleted=b'0'),
        (SELECT MIN(fallback.id) FROM system_menu fallback
         WHERE fallback.component='lms/daily-plan/work/index' AND fallback.deleted=b'0')
      )
  AND menu.deleted=b'0'
  AND NOT EXISTS (
      SELECT 1 FROM system_menu existing
      WHERE existing.parent_id=menu.id AND existing.permission=permission.code AND existing.deleted=b'0'
  );

-- 已授权日计划作业菜单的角色只自动获得查询权限，避免迁移过程扩大操作权限。
INSERT INTO system_role_menu
    (role_id, menu_id, creator, create_time, updater, update_time, deleted, tenant_id)
SELECT DISTINCT work_role.role_id, query_button.id, '1', NOW(), '1', NOW(), b'0', work_role.tenant_id
FROM system_role_menu work_role
JOIN system_menu work_menu ON work_menu.id=work_role.menu_id
    AND work_menu.component='lms/daily-plan/work/index' AND work_menu.deleted=b'0'
JOIN system_menu query_button ON query_button.parent_id=work_menu.id
    AND query_button.permission='lms:daily-plan:query' AND query_button.deleted=b'0'
WHERE work_role.deleted=b'0'
  AND NOT EXISTS (
      SELECT 1 FROM system_role_menu existing
      WHERE existing.role_id=work_role.role_id AND existing.menu_id=query_button.id
        AND existing.tenant_id=work_role.tenant_id AND existing.deleted=b'0'
  );

-- 其他作业按钮仅映射角色迁移前已拥有的同名权限，不因新增作业菜单而提权。
INSERT INTO system_role_menu
    (role_id, menu_id, creator, create_time, updater, update_time, deleted, tenant_id)
SELECT DISTINCT work_role.role_id, work_button.id, '1', NOW(), '1', NOW(), b'0', work_role.tenant_id
FROM system_role_menu work_role
JOIN system_menu work_menu ON work_menu.id=work_role.menu_id
    AND work_menu.component='lms/daily-plan/work/index' AND work_menu.deleted=b'0'
JOIN system_menu work_button ON work_button.parent_id=work_menu.id AND work_button.type=3
    AND work_button.permission IN (
        'lms:daily-plan:start', 'lms:daily-plan:operate',
        'lms:daily-plan:schedule', 'lms:daily-plan:strategy'
    ) AND work_button.deleted=b'0'
JOIN system_role_menu source_role ON source_role.role_id=work_role.role_id
    AND source_role.tenant_id=work_role.tenant_id AND source_role.deleted=b'0'
JOIN system_menu source_button ON source_button.id=source_role.menu_id
    AND source_button.permission=work_button.permission AND source_button.deleted=b'0'
    AND source_button.id<>work_button.id
WHERE work_role.deleted=b'0'
  AND NOT EXISTS (
      SELECT 1 FROM system_role_menu existing
      WHERE existing.role_id=work_role.role_id AND existing.menu_id=work_button.id
        AND existing.tenant_id=work_role.tenant_id AND existing.deleted=b'0'
  );

-- 已授权管芯库现有子菜单的角色同步获得管芯库父目录权限。
INSERT INTO system_role_menu
    (role_id, menu_id, creator, create_time, updater, update_time, deleted, tenant_id)
SELECT DISTINCT child_role.role_id, parent.id, '1', NOW(), '1', NOW(), b'0', child_role.tenant_id
FROM system_role_menu child_role
JOIN system_menu child ON child.id=child_role.menu_id AND child.deleted=b'0'
JOIN system_menu parent ON parent.id=child.parent_id AND parent.path='tube-library' AND parent.deleted=b'0'
JOIN system_menu lms ON lms.id=parent.parent_id AND lms.path='/lms' AND lms.deleted=b'0'
WHERE child_role.deleted=b'0'
  AND NOT EXISTS (
      SELECT 1 FROM system_role_menu parent_role
      WHERE parent_role.role_id=child_role.role_id AND parent_role.menu_id=parent.id
        AND parent_role.tenant_id=child_role.tenant_id AND parent_role.deleted=b'0'
  );
