-- 提成结算业绩：独立页面 fans/commission-settlement + stats.commissionSettlement/* 接口权限
-- 与业绩看板 yeji 同父菜单（pid = 业绩看板菜单 id 293）；幂等

INSERT INTO `zyt_system_menu`
(`pid`, `type`, `name`, `icon`, `sort`, `perms`, `paths`, `component`, `selected`, `params`, `is_show`, `is_disable`, `create_time`, `update_time`)
SELECT
    y.pid,
    'C',
    '提成结算业绩',
    '',
    96,
    'fans/commission-settlement',
    '/fans/commission-settlement',
    'fans/commission-settlement',
    '',
    '',
    1,
    0,
    UNIX_TIMESTAMP(),
    UNIX_TIMESTAMP()
FROM (SELECT pid FROM `zyt_system_menu` WHERE id = 293 LIMIT 1) AS y
WHERE EXISTS (SELECT 1 FROM `zyt_system_menu` WHERE id = 293)
  AND NOT EXISTS (
      SELECT 1 FROM `zyt_system_menu` m WHERE m.component = 'fans/commission-settlement'
  );

SET @commission_cs_pid := (
    SELECT id FROM `zyt_system_menu` WHERE component = 'fans/commission-settlement' ORDER BY id DESC LIMIT 1
);

INSERT INTO `zyt_system_menu`
(`pid`, `type`, `name`, `icon`, `sort`, `perms`, `paths`, `component`, `selected`, `params`, `is_show`, `is_disable`, `create_time`, `update_time`)
SELECT
    @commission_cs_pid,
    'A',
    '提成结算-汇总',
    '',
    1,
    'stats.commissionSettlement/overview',
    '',
    '',
    '',
    '',
    1,
    0,
    UNIX_TIMESTAMP(),
    UNIX_TIMESTAMP()
FROM (SELECT 1 AS `_`) AS `_exec`
WHERE @commission_cs_pid IS NOT NULL
  AND NOT EXISTS (
      SELECT 1 FROM `zyt_system_menu` m
      WHERE m.pid = @commission_cs_pid AND m.perms = 'stats.commissionSettlement/overview'
  );

INSERT INTO `zyt_system_menu`
(`pid`, `type`, `name`, `icon`, `sort`, `perms`, `paths`, `component`, `selected`, `params`, `is_show`, `is_disable`, `create_time`, `update_time`)
SELECT
    @commission_cs_pid,
    'A',
    '提成结算-部门下拉',
    '',
    2,
    'stats.commissionSettlement/deptOptions',
    '',
    '',
    '',
    '',
    1,
    0,
    UNIX_TIMESTAMP(),
    UNIX_TIMESTAMP()
FROM (SELECT 1 AS `_`) AS `_exec`
WHERE @commission_cs_pid IS NOT NULL
  AND NOT EXISTS (
      SELECT 1 FROM `zyt_system_menu` m
      WHERE m.pid = @commission_cs_pid AND m.perms = 'stats.commissionSettlement/deptOptions'
  );

INSERT INTO `zyt_system_menu`
(`pid`, `type`, `name`, `icon`, `sort`, `perms`, `paths`, `component`, `selected`, `params`, `is_show`, `is_disable`, `create_time`, `update_time`)
SELECT
    @commission_cs_pid,
    'A',
    '提成结算-渠道下拉',
    '',
    3,
    'stats.commissionSettlement/channelOptions',
    '',
    '',
    '',
    '',
    1,
    0,
    UNIX_TIMESTAMP(),
    UNIX_TIMESTAMP()
FROM (SELECT 1 AS `_`) AS `_exec`
WHERE @commission_cs_pid IS NOT NULL
  AND NOT EXISTS (
      SELECT 1 FROM `zyt_system_menu` m
      WHERE m.pid = @commission_cs_pid AND m.perms = 'stats.commissionSettlement/channelOptions'
  );
