-- 账户库管理菜单
-- 挂载到“信息维护”目录下，可重复执行

SET @parent_menu_id = (
    SELECT menu_id
    FROM sys_menu
    WHERE menu_name = '信息维护'
      AND parent_id = 0
    LIMIT 1
);

INSERT INTO sys_menu (
    menu_name,
    parent_id,
    order_num,
    path,
    component,
    is_frame,
    is_cache,
    menu_type,
    visible,
    status,
    perms,
    icon,
    create_by,
    create_time,
    update_by,
    update_time,
    remark
)
SELECT
    '账户库管理',
    @parent_menu_id,
    9,
    'accountInfo',
    'ccdiAccountInfo/index',
    1,
    0,
    'C',
    '0',
    '0',
    'ccdi:accountInfo:list',
    'documentation',
    'admin',
    NOW(),
    '',
    NULL,
    '账户库管理菜单'
FROM dual
WHERE @parent_menu_id IS NOT NULL
  AND NOT EXISTS (
      SELECT 1
      FROM sys_menu
      WHERE parent_id = @parent_menu_id
        AND path = 'accountInfo'
  );

SET @menu_id = (
    SELECT menu_id
    FROM sys_menu
    WHERE parent_id = @parent_menu_id
      AND path = 'accountInfo'
    LIMIT 1
);

INSERT INTO sys_menu (
    menu_name,
    parent_id,
    order_num,
    path,
    component,
    is_frame,
    is_cache,
    menu_type,
    visible,
    status,
    perms,
    icon,
    create_by,
    create_time,
    remark
)
SELECT '账户查询', @menu_id, 1, '', '', 1, 0, 'F', '0', '0', 'ccdi:accountInfo:query', '#', 'admin', NOW(), ''
FROM dual
WHERE @menu_id IS NOT NULL
  AND NOT EXISTS (
      SELECT 1
      FROM sys_menu
      WHERE parent_id = @menu_id
        AND perms = 'ccdi:accountInfo:query'
  );

INSERT INTO sys_menu (
    menu_name,
    parent_id,
    order_num,
    path,
    component,
    is_frame,
    is_cache,
    menu_type,
    visible,
    status,
    perms,
    icon,
    create_by,
    create_time,
    remark
)
SELECT '账户新增', @menu_id, 2, '', '', 1, 0, 'F', '0', '0', 'ccdi:accountInfo:add', '#', 'admin', NOW(), ''
FROM dual
WHERE @menu_id IS NOT NULL
  AND NOT EXISTS (
      SELECT 1
      FROM sys_menu
      WHERE parent_id = @menu_id
        AND perms = 'ccdi:accountInfo:add'
  );

INSERT INTO sys_menu (
    menu_name,
    parent_id,
    order_num,
    path,
    component,
    is_frame,
    is_cache,
    menu_type,
    visible,
    status,
    perms,
    icon,
    create_by,
    create_time,
    remark
)
SELECT '账户修改', @menu_id, 3, '', '', 1, 0, 'F', '0', '0', 'ccdi:accountInfo:edit', '#', 'admin', NOW(), ''
FROM dual
WHERE @menu_id IS NOT NULL
  AND NOT EXISTS (
      SELECT 1
      FROM sys_menu
      WHERE parent_id = @menu_id
        AND perms = 'ccdi:accountInfo:edit'
  );

INSERT INTO sys_menu (
    menu_name,
    parent_id,
    order_num,
    path,
    component,
    is_frame,
    is_cache,
    menu_type,
    visible,
    status,
    perms,
    icon,
    create_by,
    create_time,
    remark
)
SELECT '账户删除', @menu_id, 4, '', '', 1, 0, 'F', '0', '0', 'ccdi:accountInfo:remove', '#', 'admin', NOW(), ''
FROM dual
WHERE @menu_id IS NOT NULL
  AND NOT EXISTS (
      SELECT 1
      FROM sys_menu
      WHERE parent_id = @menu_id
        AND perms = 'ccdi:accountInfo:remove'
  );

INSERT INTO sys_menu (
    menu_name,
    parent_id,
    order_num,
    path,
    component,
    is_frame,
    is_cache,
    menu_type,
    visible,
    status,
    perms,
    icon,
    create_by,
    create_time,
    remark
)
SELECT '账户导入', @menu_id, 5, '', '', 1, 0, 'F', '0', '0', 'ccdi:accountInfo:import', '#', 'admin', NOW(), ''
FROM dual
WHERE @menu_id IS NOT NULL
  AND NOT EXISTS (
      SELECT 1
      FROM sys_menu
      WHERE parent_id = @menu_id
        AND perms = 'ccdi:accountInfo:import'
  );

INSERT INTO sys_role_menu (role_id, menu_id)
SELECT 1, @menu_id
FROM dual
WHERE @menu_id IS NOT NULL
  AND NOT EXISTS (
      SELECT 1
      FROM sys_role_menu
      WHERE role_id = 1
        AND menu_id = @menu_id
  );

INSERT INTO sys_role_menu (role_id, menu_id)
SELECT 1,
       (
           SELECT menu_id
           FROM sys_menu
           WHERE parent_id = @menu_id
             AND perms = 'ccdi:accountInfo:query'
           LIMIT 1
       )
FROM dual
WHERE @menu_id IS NOT NULL
  AND NOT EXISTS (
      SELECT 1
      FROM sys_role_menu
      WHERE role_id = 1
        AND menu_id = (
            SELECT menu_id
            FROM sys_menu
            WHERE parent_id = @menu_id
              AND perms = 'ccdi:accountInfo:query'
            LIMIT 1
        )
  );

INSERT INTO sys_role_menu (role_id, menu_id)
SELECT 1,
       (
           SELECT menu_id
           FROM sys_menu
           WHERE parent_id = @menu_id
             AND perms = 'ccdi:accountInfo:import'
           LIMIT 1
       )
FROM dual
WHERE @menu_id IS NOT NULL
  AND NOT EXISTS (
      SELECT 1
      FROM sys_role_menu
      WHERE role_id = 1
        AND menu_id = (
            SELECT menu_id
            FROM sys_menu
            WHERE parent_id = @menu_id
              AND perms = 'ccdi:accountInfo:import'
            LIMIT 1
        )
  );

INSERT INTO sys_role_menu (role_id, menu_id)
SELECT 1,
       (
           SELECT menu_id
           FROM sys_menu
           WHERE parent_id = @menu_id
             AND perms = 'ccdi:accountInfo:add'
           LIMIT 1
       )
FROM dual
WHERE @menu_id IS NOT NULL
  AND NOT EXISTS (
      SELECT 1
      FROM sys_role_menu
      WHERE role_id = 1
        AND menu_id = (
            SELECT menu_id
            FROM sys_menu
            WHERE parent_id = @menu_id
              AND perms = 'ccdi:accountInfo:add'
            LIMIT 1
        )
  );

INSERT INTO sys_role_menu (role_id, menu_id)
SELECT 1,
       (
           SELECT menu_id
           FROM sys_menu
           WHERE parent_id = @menu_id
             AND perms = 'ccdi:accountInfo:edit'
           LIMIT 1
       )
FROM dual
WHERE @menu_id IS NOT NULL
  AND NOT EXISTS (
      SELECT 1
      FROM sys_role_menu
      WHERE role_id = 1
        AND menu_id = (
            SELECT menu_id
            FROM sys_menu
            WHERE parent_id = @menu_id
              AND perms = 'ccdi:accountInfo:edit'
            LIMIT 1
        )
  );

INSERT INTO sys_role_menu (role_id, menu_id)
SELECT 1,
       (
           SELECT menu_id
           FROM sys_menu
           WHERE parent_id = @menu_id
             AND perms = 'ccdi:accountInfo:remove'
           LIMIT 1
       )
FROM dual
WHERE @menu_id IS NOT NULL
  AND NOT EXISTS (
      SELECT 1
      FROM sys_role_menu
      WHERE role_id = 1
        AND menu_id = (
            SELECT menu_id
            FROM sys_menu
            WHERE parent_id = @menu_id
              AND perms = 'ccdi:accountInfo:remove'
            LIMIT 1
        )
  );
