-- 订单打印模板菜单与权限
-- 说明：
-- 1. 在“订单管理”下新增“订单打印模板”独立菜单
-- 2. 菜单路由为 /orders/order-print-template，组件键为 orderPrintTemplate
-- 3. 提供列表、保存、更新、删除四个权限点

SET @tenant_id := 1;

SET @orders_menu_id := COALESCE(
  (
    SELECT parent_id
    FROM sys_menu
    WHERE deleted = 0
      AND tenant_id = @tenant_id
      AND type = 0
      AND (
        route = '/orders/asn-order'
        OR component = 'asnOrder'
        OR name = 'menu.asnOrder'
      )
    ORDER BY id
    LIMIT 1
  ),
  (
    SELECT id
    FROM sys_menu
    WHERE deleted = 0
      AND tenant_id = @tenant_id
      AND type = 0
      AND (
        route = '/orders'
        OR component = 'orders'
        OR name = 'menu.orders'
      )
    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.orderPrintTemplate',
  @orders_menu_id,
  'menu.orders',
  'order-print-template',
  'orderPrintTemplate',
  '/orders/order-print-template',
  'orderPrintTemplate',
  '订单打印模板独立菜单',
  NULL,
  0,
  NULL,
  'ri:printer-line',
  97,
  NULL,
  @tenant_id,
  1,
  0,
  NOW(),
  1,
  NOW(),
  1,
  '订单打印模板左侧菜单'
FROM dual
WHERE @orders_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 = '/orders/order-print-template'
        OR component = 'orderPrintTemplate'
        OR name = 'menu.orderPrintTemplate'
      )
  );

SET @template_menu_id := (
  SELECT id
  FROM sys_menu
  WHERE deleted = 0
    AND tenant_id = @tenant_id
    AND type = 0
    AND (
      route = '/orders/order-print-template'
      OR component = 'orderPrintTemplate'
      OR name = 'menu.orderPrintTemplate'
    )
  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
  'Query 订单打印模板',
  @template_menu_id,
  'menu.orderPrintTemplate',
  NULL,
  NULL,
  NULL,
  NULL,
  '订单打印模板读取权限',
  NULL,
  1,
  'manager:orderPrintTemplate: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:orderPrintTemplate: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.orderPrintTemplate',
  NULL,
  NULL,
  NULL,
  NULL,
  '订单打印模板新增权限',
  NULL,
  1,
  'manager:orderPrintTemplate: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:orderPrintTemplate: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.orderPrintTemplate',
  NULL,
  NULL,
  NULL,
  NULL,
  '订单打印模板更新权限',
  NULL,
  1,
  'manager:orderPrintTemplate: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:orderPrintTemplate: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.orderPrintTemplate',
  NULL,
  NULL,
  NULL,
  NULL,
  '订单打印模板删除权限',
  NULL,
  1,
  'manager:orderPrintTemplate: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:orderPrintTemplate:remove'
  );
