-- CSV "종량제_개발목록_20260127(전체 메뉴 - 1차정리).csv" 기반으로
-- site 타입(menu_type.mt_code='site')의 1차/2차 메뉴를 초기 시드하는 SQL 뼈대입니다.
--
-- 1행: 1차 메뉴명 리스트
-- 2행 이후: 각 열이 해당 1차 메뉴의 2차 메뉴명에 해당.
--
-- 실제 운영 시에는 CSV를 파싱해서 INSERT를 생성하는 스크립트를 사용하는 편이 낫고,
-- 아래 SQL은 구조 이해 및 수동 시드를 위한 참고용입니다.

-- 1) site용 mt_idx 조회 (예: @mt_site)
SET @mt_site := (SELECT mt_idx FROM menu_type WHERE mt_code = 'site' LIMIT 1);


-- 3) 2차 메뉴 예시
-- CSV의 2행 이후를 바탕으로, 각 1차 메뉴 아래에 2차 메뉴를 추가하는 방식의 예시입니다.
-- 아래 예시는 구조를 보여주기 위한 샘플이며, 실제 라우트(mm_link)는 구현 후에 맞게 수정해야 합니다.

-- 예: "기본정보관리" 하위 2차 메뉴들 (PWB-030100~ 등)
SET @parent_basic := (
  SELECT mm_idx FROM menu
  WHERE mt_idx = @mt_site AND lg_idx = 1 AND mm_pidx = 0 AND mm_name = '기본정보관리'
  LIMIT 1
);

INSERT INTO menu (mt_idx, lg_idx, mm_name, mm_link, mm_pidx, mm_dep, mm_num, mm_cnode, mm_level, mm_is_view)
SELECT @mt_site, 1, t.mm_name, '', @parent_basic, 1, t.mm_num, 0, '', 'Y'
FROM (
  SELECT 0 AS mm_num, '기본 코드 관리'       AS mm_name UNION ALL
  SELECT 1,           '단가 관리'                     UNION ALL
  SELECT 2,           '포장 단위 관리'               UNION ALL
  SELECT 3,           '판매 대행소 관리'             UNION ALL
  SELECT 4,           '담당자 관리'                   UNION ALL
  SELECT 5,           '업체 관리'                     UNION ALL
  SELECT 6,           '무료용 대상자 관리'           UNION ALL
  SELECT 7,           '지정 판매소 관리'             UNION ALL
  SELECT 8,           '지정 판매소 조회'             UNION ALL
  SELECT 9,           '지정 판매소 신규/취소 현황'   UNION ALL
  SELECT 10,          '지정판매소 바코드 출력'       UNION ALL
  SELECT 11,          'PC -> PDA로 자료 전송'        UNION ALL
  SELECT 12,          'PDA -> PC로 자료 전송'        UNION ALL
  SELECT 13,          '올린 자료 처리'               UNION ALL
  SELECT 14,          '시찰 처리'                     UNION ALL
  SELECT 15,          'PASSWORD 변경'                UNION ALL
  SELECT 16,          '환경 설정'                     UNION ALL
  SELECT 17,          '지정 판매소 현황'
) AS t
WHERE @parent_basic IS NOT NULL
  AND NOT EXISTS (
    SELECT 1 FROM menu m
    WHERE m.mt_idx = @mt_site
      AND m.lg_idx = 1
      AND m.mm_pidx = @parent_basic
      AND m.mm_name = t.mm_name
  );

-- 2) 1차: 발주 입고 관리 하위 메뉴
SET @parent_order := (
  SELECT mm_idx FROM menu
  WHERE mt_idx = @mt_site AND lg_idx = 1 AND mm_pidx = 0 AND mm_name = '발주 입고 관리'
  LIMIT 1
);

INSERT INTO menu (mt_idx, lg_idx, mm_name, mm_link, mm_pidx, mm_dep, mm_num, mm_cnode, mm_level, mm_is_view)
SELECT @mt_site, 1, t.mm_name, '', @parent_order, 1, t.mm_num, 0, '', 'Y'
FROM (
  SELECT 0 AS mm_num, '발주 등록'          AS mm_name UNION ALL
  SELECT 1,           '발주 변경'                   UNION ALL
  SELECT 2,           'LOT-No 디스켓 불출'         UNION ALL
  SELECT 3,           '발주 현황'                   UNION ALL
  SELECT 4,           '발주 입고[스캐너]'          UNION ALL
  SELECT 5,           '일괄입고'                   UNION ALL
  SELECT 6,           '입고 현황'
) AS t
WHERE @parent_order IS NOT NULL
  AND NOT EXISTS (
    SELECT 1 FROM menu m
    WHERE m.mt_idx = @mt_site
      AND m.lg_idx = 1
      AND m.mm_pidx = @parent_order
      AND m.mm_name = t.mm_name
  );

-- 3) 1차: 불출 관리 하위 메뉴
SET @parent_issue := (
  SELECT mm_idx FROM menu
  WHERE mt_idx = @mt_site AND lg_idx = 1 AND mm_pidx = 0 AND mm_name = '불출 관리'
  LIMIT 1
);

INSERT INTO menu (mt_idx, lg_idx, mm_name, mm_link, mm_pidx, mm_dep, mm_num, mm_cnode, mm_level, mm_is_view)
SELECT @mt_site, 1, t.mm_name, '', @parent_issue, 1, t.mm_num, 0, '', 'Y'
FROM (
  SELECT 0 AS mm_num, '무료 불출 현황'   AS mm_name UNION ALL
  SELECT 1,           '무료용 불출 처리'          UNION ALL
  SELECT 2,           '무료용 불출 취소'
) AS t
WHERE @parent_issue IS NOT NULL
  AND NOT EXISTS (
    SELECT 1 FROM menu m
    WHERE m.mt_idx = @mt_site
      AND m.lg_idx = 1
      AND m.mm_pidx = @parent_issue
      AND m.mm_name = t.mm_name
  );

-- 4) 1차: 재고 관리 하위 메뉴
SET @parent_inventory := (
  SELECT mm_idx FROM menu
  WHERE mt_idx = @mt_site AND lg_idx = 1 AND mm_pidx = 0 AND mm_name = '재고 관리'
  LIMIT 1
);

INSERT INTO menu (mt_idx, lg_idx, mm_name, mm_link, mm_pidx, mm_dep, mm_num, mm_cnode, mm_level, mm_is_view)
SELECT @mt_site, 1, t.mm_name, '', @parent_inventory, 1, t.mm_num, 0, '', 'Y'
FROM (
  SELECT 0 AS mm_num, '재고 현황'     AS mm_name UNION ALL
  SELECT 1,           '실사 관리'               UNION ALL
  SELECT 2,           '실사 선별'               UNION ALL
  SELECT 3,           '실사 선별 조회'         UNION ALL
  SELECT 4,           '실사 등록'               UNION ALL
  SELECT 5,           '실사 재고 조회'         UNION ALL
  SELECT 6,           '실사 오류 조회'         UNION ALL
  SELECT 7,           '실사 선별 취소'         UNION ALL
  SELECT 8,           '실사 등록 취소'
) AS t
WHERE @parent_inventory IS NOT NULL
  AND NOT EXISTS (
    SELECT 1 FROM menu m
    WHERE m.mt_idx = @mt_site
      AND m.lg_idx = 1
      AND m.mm_pidx = @parent_inventory
      AND m.mm_name = t.mm_name
  );

-- 5) 1차: 판매 관리 하위 메뉴
SET @parent_sales := (
  SELECT mm_idx FROM menu
  WHERE mt_idx = @mt_site AND lg_idx = 1 AND mm_pidx = 0 AND mm_name = '판매 관리'
  LIMIT 1
);

INSERT INTO menu (mt_idx, lg_idx, mm_name, mm_link, mm_pidx, mm_dep, mm_num, mm_cnode, mm_level, mm_is_view)
SELECT @mt_site, 1, t.mm_name, '', @parent_sales, 1, t.mm_num, 0, '', 'Y'
FROM (
  SELECT 0 AS mm_num, '전화 접수'         AS mm_name UNION ALL
  SELECT 1,           '전화 접수 관리'             UNION ALL
  SELECT 2,           '지정 판매소 판매'           UNION ALL
  SELECT 3,           '지정 판매소 반품'           UNION ALL
  SELECT 4,           '지정 판매소 판매 취소'      UNION ALL
  SELECT 5,           '지정 판매소 반품 취소'
) AS t
WHERE @parent_sales IS NOT NULL
  AND NOT EXISTS (
    SELECT 1 FROM menu m
    WHERE m.mt_idx = @mt_site
      AND m.lg_idx = 1
      AND m.mm_pidx = @parent_sales
      AND m.mm_name = t.mm_name
  );

-- 6) 1차: 판매 현황 하위 메뉴
SET @parent_sales_stats := (
  SELECT mm_idx FROM menu
  WHERE mt_idx = @mt_site AND lg_idx = 1 AND mm_pidx = 0 AND mm_name = '판매 현황'
  LIMIT 1
);

INSERT INTO menu (mt_idx, lg_idx, mm_name, mm_link, mm_pidx, mm_dep, mm_num, mm_cnode, mm_level, mm_is_view)
SELECT @mt_site, 1, t.mm_name, '', @parent_sales_stats, 1, t.mm_num, 0, '', 'Y'
FROM (
  SELECT 0 AS mm_num, '지정 판매소 일 판매대장'          AS mm_name UNION ALL
  SELECT 1,           '지정 판매소 기간별 판매대장'               UNION ALL
  SELECT 2,           '일계표'                                   UNION ALL
  SELECT 3,           '기간별 판매현황[일집계]'                  UNION ALL
  SELECT 4,           '기간별 판매현황[기간집계]'                UNION ALL
  SELECT 5,           '년 판매 현황'                             UNION ALL
  SELECT 6,           '지정 판매소 별 판매현황(수량)'            UNION ALL
  SELECT 7,           '지정 판매소 별 판매현황(금액)'            UNION ALL
  SELECT 8,           '지정판매소별 거래현황'                    UNION ALL
  SELECT 9,           '홈텍스 처리'
) AS t
WHERE @parent_sales_stats IS NOT NULL
  AND NOT EXISTS (
    SELECT 1 FROM menu m
    WHERE m.mt_idx = @mt_site
      AND m.lg_idx = 1
      AND m.mm_pidx = @parent_sales_stats
      AND m.mm_name = t.mm_name
  );

-- 7) 1차: 봉투 수불 관리 하위 메뉴
SET @parent_flow := (
  SELECT mm_idx FROM menu
  WHERE mt_idx = @mt_site AND lg_idx = 1 AND mm_pidx = 0 AND mm_name = '봉투 수불 관리'
  LIMIT 1
);

INSERT INTO menu (mt_idx, lg_idx, mm_name, mm_link, mm_pidx, mm_dep, mm_num, mm_cnode, mm_level, mm_is_view)
SELECT @mt_site, 1, t.mm_name, '', @parent_flow, 1, t.mm_num, 0, '', 'Y'
FROM (
  SELECT 0 AS mm_num, '기타 입출고'            AS mm_name UNION ALL
  SELECT 1,           '기간별 봉투 수불 현황'          UNION ALL
  SELECT 2,           '일일 봉투 수불 현황'            UNION ALL
  SELECT 3,           '반품/파기 현황'                 UNION ALL
  SELECT 4,           '쓰레기 봉투 수급 계획'          UNION ALL
  SELECT 5,           'LOT 수불 조회'
) AS t
WHERE @parent_flow IS NOT NULL
  AND NOT EXISTS (
    SELECT 1 FROM menu m
    WHERE m.mt_idx = @mt_site
      AND m.lg_idx = 1
      AND m.mm_pidx = @parent_flow
      AND m.mm_name = t.mm_name
  );

-- 8) 1차: 통계 분석 관리 하위 메뉴
SET @parent_analytics := (
  SELECT mm_idx FROM menu
  WHERE mt_idx = @mt_site AND lg_idx = 1 AND mm_pidx = 0 AND mm_name = '통계 분석 관리'
  LIMIT 1
);

INSERT INTO menu (mt_idx, lg_idx, mm_name, mm_link, mm_pidx, mm_dep, mm_num, mm_cnode, mm_level, mm_is_view)
SELECT @mt_site, 1, t.mm_name, '', @parent_analytics, 1, t.mm_num, 0, '', 'Y'
FROM (
  SELECT 0 AS mm_num, '전년 대비 판매 분석'     AS mm_name UNION ALL
  SELECT 1,           '월별 판매 추이 분석'              UNION ALL
  SELECT 2,           '계절별 판매 추이 분석'
) AS t
WHERE @parent_analytics IS NOT NULL
  AND NOT EXISTS (
    SELECT 1 FROM menu m
    WHERE m.mt_idx = @mt_site
      AND m.lg_idx = 1
      AND m.mm_pidx = @parent_analytics
      AND m.mm_name = t.mm_name
  );

-- 9) 1차: 창 하위 메뉴
SET @parent_window := (
  SELECT mm_idx FROM menu
  WHERE mt_idx = @mt_site AND lg_idx = 1 AND mm_pidx = 0 AND mm_name = '창'
  LIMIT 1
);

INSERT INTO menu (mt_idx, lg_idx, mm_name, mm_link, mm_pidx, mm_dep, mm_num, mm_cnode, mm_level, mm_is_view)
SELECT @mt_site, 1, t.mm_name, '', @parent_window, 1, t.mm_num, 0, '', 'Y'
FROM (
  SELECT 0 AS mm_num, 'PDA 수정'       AS mm_name UNION ALL
  SELECT 1,           '바둑판식 배열'           UNION ALL
  SELECT 2,           '계단식 배열'             UNION ALL
  SELECT 3,           '계층식 배열'
) AS t
WHERE @parent_window IS NOT NULL
  AND NOT EXISTS (
    SELECT 1 FROM menu m
    WHERE m.mt_idx = @mt_site
      AND m.lg_idx = 1
      AND m.mm_pidx = @parent_window
      AND m.mm_name = t.mm_name
  );

-- 10) 1차: 도움말 하위 메뉴
SET @parent_help := (
  SELECT mm_idx FROM menu
  WHERE mt_idx = @mt_site AND lg_idx = 1 AND mm_pidx = 0 AND mm_name = '도움말'
  LIMIT 1
);

INSERT INTO menu (mt_idx, lg_idx, mm_name, mm_link, mm_pidx, mm_dep, mm_num, mm_cnode, mm_level, mm_is_view)
SELECT @mt_site, 1, t.mm_name, '', @parent_help, 1, t.mm_num, 0, '', 'Y'
FROM (
  SELECT 0 AS mm_num, '도움말 항목'   AS mm_name UNION ALL
  SELECT 1,           '원격 요청'               UNION ALL
  SELECT 2,           'pda 리셋'                UNION ALL
  SELECT 3,           '번호알기'                UNION ALL
  SELECT 4,           'Data Backup'            UNION ALL
  SELECT 5,           '컴포트 설정'             UNION ALL
  SELECT 6,           'Version 정보'           UNION ALL
  SELECT 7,           '종료'                   UNION ALL
  SELECT 8,           '1GBMS'
) AS t
WHERE @parent_help IS NOT NULL
  AND NOT EXISTS (
    SELECT 1 FROM menu m
    WHERE m.mt_idx = @mt_site
      AND m.lg_idx = 1
      AND m.mm_pidx = @parent_help
      AND m.mm_name = t.mm_name
  );

