From 70fe8a0b5c4858090a75d7e4be7e2adef9e51c92 Mon Sep 17 00:00:00 2001
From: dujinkim <dujin.kim@dtsolution.co.kr>
Date: Thu, 2 Oct 2025 12:12:54 +0000
Subject: (임수민) S-ERP 테이블 2개 추가, INSERT 로직을 UPDATE 로 변경, 복합 pk
 로직 추가
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 components/s-erp-import/import-card.tsx | 43 ++++++++++++++++++++++++---------
 1 file changed, 31 insertions(+), 12 deletions(-)

(limited to 'components/s-erp-import/import-card.tsx')

diff --git a/components/s-erp-import/import-card.tsx b/components/s-erp-import/import-card.tsx
index 43f2aa35..21ac7f9e 100644
--- a/components/s-erp-import/import-card.tsx
+++ b/components/s-erp-import/import-card.tsx
@@ -1,7 +1,7 @@
 'use client'
 
 import * as React from 'react'
-import { importExcel } from '@/lib/s-erp-import/actions'
+import { importExcel, getSingleTableCount } from '@/lib/s-erp-import/actions'
 import { RPAInfo } from './rpa-info'
 
 export function ImportButton({ tableName, count }: { tableName: string; count: number }) {
@@ -17,27 +17,46 @@ export function ImportButton({ tableName, count }: { tableName: string; count: n
     if (!file) return
     setPending(true)
     try {
-      console.log(`[SAP Import] 시작: ${tableName}, 파일: ${file.name}, 크기: ${file.size} bytes`)
       const res = await importExcel(tableName, file)
       if (res?.success) {
-        setCurrentCount((c) => c + (res.inserted ?? 0))
-        console.log(`[SAP Import] 성공: ${tableName}, 입력 건수: ${res.inserted ?? 0}`)
-        setStatusMsg(`${res.inserted ?? 0}건`)
+        // UPSERT 모드와 기존 INSERT 모드 모두 지원
+        const processedCount = (res as any).processed ?? res.inserted ?? 0
+        
+        // UPSERT 모드인 경우 상세 정보 표시
+        if ((res as any).upsertMode) {
+          const inserted = (res as any).inserted ?? 0
+          const updated = (res as any).updated ?? 0
+          
+          // 실제 DB에서 현재 카운트를 다시 가져옴
+          try {
+            const actualCount = await getSingleTableCount(tableName)
+            setCurrentCount(actualCount)
+          } catch (err) {
+            console.error(`[SAP Import] DB 카운트 조회 실패:`, err)
+          }
+          
+          if (inserted > 0 && updated > 0) {
+            setStatusMsg(`신규 ${inserted}건, 업데이트 ${updated}건`)
+          } else if (inserted > 0) {
+            setStatusMsg(`${inserted}건 신규 추가`)
+          } else if (updated > 0) {
+            setStatusMsg(`${updated}건 업데이트`)
+          } else {
+            setStatusMsg(`변경사항 없음`)
+          }
+        } else {
+          // INSERT 모드에서는 기존 방식대로 카운트 증가
+          setCurrentCount((c) => c + processedCount)
+          setStatusMsg(`${processedCount}건`)
+        }
       } else {
         console.error(`[SAP Import] 실패: ${tableName}, 사유: ${res?.message ?? '임포트 실패'}`)
-        if ((res as any)?.unknownHeaders) {
-          console.error(`[SAP Import] 매핑 실패 헤더:`, (res as any).unknownHeaders)
-        }
-        if ((res as any)?.errors) {
-          console.error(`[SAP Import] 유효성 오류 상세:`, (res as any).errors)
-        }
         setStatusMsg(`데이터 저장에 실패하였습니다.`)
       }
     } catch (err: any) {
       console.error(`[SAP Import] 예외 발생: ${tableName}, 에러: ${err?.message ?? err}`)
       setStatusMsg(`데이터 저장에 실패하였습니다.`)
     } finally {
-      console.log(`[SAP Import] 종료: ${tableName}`)
       setPending(false)
       e.target.value = ''
     }
-- 
cgit v1.2.3

