-- 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 2092000000000000100, '管芯库管理', '', 1, 10, parent.id, 'tube-library', '',
       '', '', 0, b'1', b'1', b'1', '1', NOW(), '1', NOW(), b'0'
FROM system_menu parent
WHERE parent.path = '/lms'
  AND parent.deleted = b'0'
  AND NOT EXISTS (
      SELECT 1 FROM system_menu
      WHERE parent_id = parent.id AND path = 'tube-library' 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 2092000000000000101, '备货区域', '', 2, 10, parent.id, 'stocking-ivt', '',
       'lms/stockingivt/index', 'LmsStockingIvt', 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/stockingivt/index' AND deleted = b'0'
  );

-- 兼容已经初始化过备货区域菜单的环境，将其迁移到“管芯库管理”目录下。
UPDATE system_menu menu
JOIN system_menu parent
  ON parent.path = 'tube-library'
 AND parent.deleted = b'0'
SET menu.parent_id = parent.id,
    menu.sort = 1,
    menu.updater = '1',
    menu.update_time = NOW()
WHERE menu.component = 'lms/stockingivt/index'
  AND menu.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 2092000000000000102, '备货区域查询', 'lms:stocking-ivt:query', 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/stockingivt/index'
  AND menu.deleted = b'0'
  AND NOT EXISTS (
      SELECT 1 FROM system_menu
      WHERE permission = 'lms:stocking-ivt:query' 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 2092000000000000103, '备货区域修改', 'lms:stocking-ivt:update', 3, 2, menu.id, '', '',
       '', '', 0, b'1', b'1', b'1', '1', NOW(), '1', NOW(), b'0'
FROM system_menu menu
WHERE menu.component = 'lms/stockingivt/index'
  AND menu.deleted = b'0'
  AND NOT EXISTS (
      SELECT 1 FROM system_menu
      WHERE permission = 'lms:stocking-ivt:update' AND deleted = b'0'
  );

-- 已授权备货区域的角色同步获得父目录权限。
INSERT INTO system_role_menu
    (role_id, menu_id, creator, create_time, updater, update_time, deleted, tenant_id)
SELECT 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.component = 'lms/stockingivt/index'
 AND child.deleted = b'0'
JOIN system_menu parent
  ON 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'
  );
