-- 物料打印模板左侧独立菜单 + 权限按钮
-- 说明：
-- 1. 在“基础信息”下新增“物料打印模板”独立菜单
-- 2. 菜单路由为 /basic-info/matnr-print-template，组件键为 matnrPrintTemplate
-- 3. 若此前已执行旧版“挂在物料页下的按钮权限”脚本，重复执行本脚本会自动迁移按钮到新菜单下

SET @tenant_id := 1;

SET @basic_info_menu_id := COALESCE(
  (
    SELECT parent_id
    FROM sys_menu
    WHERE deleted = 0
      AND tenant_id = @tenant_id
      AND type = 0
      AND (
        route = '/basic-info/wh-mat'
        OR component IN ('matnr', 'whMat')
        OR name = 'menu.matnr'
      )
    ORDER BY id
    LIMIT 1
  ),
  (
    SELECT id
    FROM sys_menu
    WHERE deleted = 0
      AND tenant_id = @tenant_id
      AND type = 0
      AND (
        route = '/basic-info'
        OR component = 'basicInfo'
        OR name = 'menu.basicInfo'
      )
    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
  'menu.matnrPrintTemplate',
  @basic_info_menu_id,
  'menu.basicInfo',
  'matnr-print-template',
  'matnrPrintTemplate',
  '/basic-info/matnr-print-template',
  'matnrPrintTemplate',
  '物料打印模板独立菜单',
  NULL,
  0,
  NULL,
  'ri:price-tag-3-line',
  96,
  NULL,
  @tenant_id,
  1,
  0,
  NOW(),
  1,
  NOW(),
  1,
  '物料打印模板左侧菜单'
FROM dual
WHERE @basic_info_menu_id IS NOT NULL
  AND NOT EXISTS (
    SELECT 1
    FROM sys_menu
    WHERE deleted = 0
      AND tenant_id = @tenant_id
      AND type = 0
      AND (
        route = '/basic-info/matnr-print-template'
        OR component = 'matnrPrintTemplate'
        OR name = 'menu.matnrPrintTemplate'
      )
  );

SET @template_menu_id := (
  SELECT id
  FROM sys_menu
  WHERE deleted = 0
    AND tenant_id = @tenant_id
    AND type = 0
    AND (
      route = '/basic-info/matnr-print-template'
      OR component = 'matnrPrintTemplate'
      OR name = 'menu.matnrPrintTemplate'
    )
  ORDER BY id
  LIMIT 1
);

UPDATE sys_menu
SET
  name = 'menu.matnrPrintTemplate',
  parent_id = @basic_info_menu_id,
  parent_name = 'menu.basicInfo',
  path = 'matnr-print-template',
  path_name = 'matnrPrintTemplate',
  route = '/basic-info/matnr-print-template',
  component = 'matnrPrintTemplate',
  brief = '物料打印模板独立菜单',
  type = 0,
  authority = NULL,
  icon = 'ri:price-tag-3-line',
  sort = 96,
  status = 1,
  update_time = NOW(),
  update_by = 1,
  memo = '物料打印模板左侧菜单'
WHERE @template_menu_id IS NOT NULL
  AND id = @template_menu_id;

UPDATE sys_menu
SET
  parent_id = @template_menu_id,
  parent_name = 'menu.matnrPrintTemplate',
  update_time = NOW(),
  update_by = 1
WHERE @template_menu_id IS NOT NULL
  AND deleted = 0
  AND tenant_id = @tenant_id
  AND authority IN (
    'manager:matnrPrintTemplate:list',
    'manager:matnrPrintTemplate:save',
    'manager:matnrPrintTemplate:update',
    'manager:matnrPrintTemplate:remove'
  );

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
  'Query 物料打印模板',
  @template_menu_id,
  'menu.matnrPrintTemplate',
  NULL,
  NULL,
  NULL,
  NULL,
  '物料打印模板读取权限',
  NULL,
  1,
  'manager:matnrPrintTemplate:list',
  NULL,
  10,
  NULL,
  @tenant_id,
  1,
  0,
  NOW(),
  1,
  NOW(),
  1,
  '物料打印模板列表、详情、默认模板、打印读取'
FROM dual
WHERE @template_menu_id IS NOT NULL
  AND NOT EXISTS (
    SELECT 1
    FROM sys_menu
    WHERE deleted = 0
      AND tenant_id = @tenant_id
      AND authority = 'manager:matnrPrintTemplate:list'
  );

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
  'Create 物料打印模板',
  @template_menu_id,
  'menu.matnrPrintTemplate',
  NULL,
  NULL,
  NULL,
  NULL,
  '物料打印模板新增权限',
  NULL,
  1,
  'manager:matnrPrintTemplate:save',
  NULL,
  11,
  NULL,
  @tenant_id,
  1,
  0,
  NOW(),
  1,
  NOW(),
  1,
  '物料打印模板新增'
FROM dual
WHERE @template_menu_id IS NOT NULL
  AND NOT EXISTS (
    SELECT 1
    FROM sys_menu
    WHERE deleted = 0
      AND tenant_id = @tenant_id
      AND authority = 'manager:matnrPrintTemplate:save'
  );

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
  'Update 物料打印模板',
  @template_menu_id,
  'menu.matnrPrintTemplate',
  NULL,
  NULL,
  NULL,
  NULL,
  '物料打印模板修改权限',
  NULL,
  1,
  'manager:matnrPrintTemplate:update',
  NULL,
  12,
  NULL,
  @tenant_id,
  1,
  0,
  NOW(),
  1,
  NOW(),
  1,
  '物料打印模板编辑、设默认'
FROM dual
WHERE @template_menu_id IS NOT NULL
  AND NOT EXISTS (
    SELECT 1
    FROM sys_menu
    WHERE deleted = 0
      AND tenant_id = @tenant_id
      AND authority = 'manager:matnrPrintTemplate:update'
  );

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
  'Delete 物料打印模板',
  @template_menu_id,
  'menu.matnrPrintTemplate',
  NULL,
  NULL,
  NULL,
  NULL,
  '物料打印模板删除权限',
  NULL,
  1,
  'manager:matnrPrintTemplate:remove',
  NULL,
  13,
  NULL,
  @tenant_id,
  1,
  0,
  NOW(),
  1,
  NOW(),
  1,
  '物料打印模板删除'
FROM dual
WHERE @template_menu_id IS NOT NULL
  AND NOT EXISTS (
    SELECT 1
    FROM sys_menu
    WHERE deleted = 0
      AND tenant_id = @tenant_id
      AND authority = 'manager:matnrPrintTemplate:remove'
  );
