-- LMS 日计划菜单及按钮权限，按路径、组件和权限编码幂等插入。
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/dailyplan/index', 'LmsDailyPlan', 0, b'1', b'1', b'1', '1', NOW(), '1', NOW(), b'0'
FROM system_menu parent
WHERE parent.path = 'tube-library' AND parent.deleted = b'0'
  AND NOT EXISTS (SELECT 1 FROM system_menu WHERE component='lms/dailyplan/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/dailyplanwork/index', 'LmsDailyPlanWork', 0, b'1', b'1', b'1', '1', NOW(), '1', NOW(), b'0'
FROM system_menu parent
WHERE parent.path = 'tube-library' AND parent.deleted = b'0'
  AND NOT EXISTS (SELECT 1 FROM system_menu WHERE component='lms/dailyplanwork/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'
) permission
WHERE menu.component='lms/dailyplan/index' AND menu.deleted=b'0'
  AND NOT EXISTS (SELECT 1 FROM system_menu existing WHERE 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 2093000000000000120, '日计划作业操作', 'lms:daily-plan:operate', 3, 1, menu.id,
       '', '', '', '', 0, b'1', b'1', b'1', '1', NOW(), '1', NOW(), b'0'
FROM system_menu menu
WHERE menu.component='lms/dailyplanwork/index' AND menu.deleted=b'0'
  AND NOT EXISTS (SELECT 1 FROM system_menu WHERE permission='lms:daily-plan:operate' AND 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'
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'
  );
