-- 收货库存导出权限
-- 说明：
-- 1. 在“收货库存”菜单下补充独立导出按钮权限
-- 2. 权限标识为 manager:warehouseAreasItem:export
-- 3. 该权限覆盖同步导出、异步导出任务创建、任务状态查询、导出文件下载

SET @tenant_id := 1;

SET @warehouse_areas_item_menu_id := (
  SELECT id
  FROM sys_menu
  WHERE deleted = 0
    AND tenant_id = @tenant_id
    AND type = 0
    AND (
      route = '/manager/warehouseAreasItem'
      OR component = 'warehouseAreasItem'
      OR name = 'menu.warehouseAreasItem'
    )
  ORDER BY id
  LIMIT 1
);

INSERT INTO sys_menu (
  name,
  parent_id,
  parent_name,
  path,
  path_name,
  route,
  component,
  brief,
  code,
  type,
  authority,
  icon,
  sort,
  meta,
  tenant_id,
  status,
  deleted,
  create_time,
  create_by,
  update_time,
  update_by,
  memo
)
SELECT
  'Export 收货库存',
  @warehouse_areas_item_menu_id,
  'menu.warehouseAreasItem',
  NULL,
  NULL,
  NULL,
  NULL,
  '收货库存导出权限',
  NULL,
  1,
  'manager:warehouseAreasItem:export',
  NULL,
  20,
  NULL,
  @tenant_id,
  1,
  0,
  NOW(),
  1,
  NOW(),
  1,
  '收货库存同步导出、异步导出任务与下载'
FROM dual
WHERE @warehouse_areas_item_menu_id IS NOT NULL
  AND NOT EXISTS (
    SELECT 1
    FROM sys_menu
    WHERE deleted = 0
      AND tenant_id = @tenant_id
      AND authority = 'manager:warehouseAreasItem:export'
  );
