From caa01b321311de3983fb8bcf65bb20a6c047cf57 Mon Sep 17 00:00:00 2001 From: joonhoekim <26rote@gmail.com> Date: Tue, 9 Sep 2025 10:40:11 +0000 Subject: (김준회) 자재그룹코드 및 자재그룹명에 대해 별도 테이블 생성, 동기화 로직 작성(일회성 사용이며 수신시점에는 자동저장하므로 추후 사용 불필요), 자재그룹 선택기를 변경사항에 맞춰 업데이트, 자재그룹명은 MAKTX 로 김학의 프로 답변에 따라 변경 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/material-groups/sync-button.tsx | 240 +++++++++++++++++++++++++++++ 1 file changed, 240 insertions(+) create mode 100644 components/material-groups/sync-button.tsx (limited to 'components/material-groups') diff --git a/components/material-groups/sync-button.tsx b/components/material-groups/sync-button.tsx new file mode 100644 index 00000000..7dc7da8f --- /dev/null +++ b/components/material-groups/sync-button.tsx @@ -0,0 +1,240 @@ +"use client"; + +import * as React from "react"; +import { Button } from "@/components/ui/button"; +import { Badge } from "@/components/ui/badge"; +import { + Dialog, + DialogContent, + DialogDescription, + DialogHeader, + DialogTitle, + DialogTrigger +} from "@/components/ui/dialog"; +import { + RefreshCw, + Database, + CheckCircle, + AlertCircle, + Clock, + TrendingUp +} from "lucide-react"; +import { useToast } from "@/hooks/use-toast"; +import { + syncMaterialGroupMaster, + getMaterialGroupSyncStatus, + type SyncResult +} from "@/lib/material-groups/sync-service"; + +export function MaterialGroupSyncButton() { + const [isOpen, setIsOpen] = React.useState(false); + const [isLoading, setIsLoading] = React.useState(false); + const [syncStatus, setSyncStatus] = React.useState<{ + totalMDGRecords: number; + totalMasterRecords: number; + lastSyncDate?: string; + } | null>(null); + const [lastSyncResult, setLastSyncResult] = React.useState(null); + const { toast } = useToast(); + + // 다이얼로그 열릴 때 상태 정보 로드 + React.useEffect(() => { + if (isOpen) { + loadSyncStatus(); + } + }, [isOpen]); + + const loadSyncStatus = async () => { + try { + const status = await getMaterialGroupSyncStatus(); + if (status.success && status.data) { + setSyncStatus(status.data); + } + } catch (error) { + console.error("동기화 상태 로드 실패:", error); + } + }; + + const handleSync = async () => { + setIsLoading(true); + try { + const result = await syncMaterialGroupMaster(); + setLastSyncResult(result); + + if (result.success) { + toast({ + title: "동기화 완료", + description: result.message, + variant: "default", + }); + // 상태 다시 로드 + await loadSyncStatus(); + } else { + toast({ + title: "동기화 실패", + description: result.message, + variant: "destructive", + }); + } + } catch (error) { + toast({ + title: "동기화 오류", + description: error instanceof Error ? error.message : "알 수 없는 오류가 발생했습니다.", + variant: "destructive", + }); + } finally { + setIsLoading(false); + } + }; + + const formatDate = (dateString: string) => { + return new Date(dateString).toLocaleString('ko-KR', { + year: 'numeric', + month: '2-digit', + day: '2-digit', + hour: '2-digit', + minute: '2-digit' + }); + }; + + const getSyncBadgeVariant = () => { + if (!syncStatus) return "secondary"; + if (syncStatus.totalMDGRecords === syncStatus.totalMasterRecords) { + return "default"; // 동기화됨 + } + return "destructive"; // 동기화 필요 + }; + + return ( + + + + + + + + + + 자재그룹 마스터 동기화 + + + MDG 테이블로부터 자재그룹 마스터 데이터를 동기화합니다. + + + +
+ {/* 현재 상태 */} + {syncStatus && ( +
+

+ + 현재 상태 +

+ +
+
+ MDG 자재그룹: +
{syncStatus.totalMDGRecords.toLocaleString()}개
+
+
+ 마스터 테이블: +
{syncStatus.totalMasterRecords.toLocaleString()}개
+
+
+ + {syncStatus.lastSyncDate && ( +
+ + + 최근 동기화: + +
{formatDate(syncStatus.lastSyncDate)}
+
+ )} + +
+ + {syncStatus.totalMDGRecords === syncStatus.totalMasterRecords + ? "동기화됨" + : "동기화 필요"} + +
+
+ )} + + {/* 마지막 동기화 결과 */} + {lastSyncResult && ( +
+

+ {lastSyncResult.success ? ( + + ) : ( + + )} + 마지막 동기화 결과 +

+ +

+ {lastSyncResult.message} +

+ + {lastSyncResult.data && ( +
+
+
+ {lastSyncResult.data.newRecords} +
+
신규
+
+
+
+ {lastSyncResult.data.updatedRecords} +
+
업데이트
+
+
+
+ {lastSyncResult.data.skippedRecords} +
+
건너뜀
+
+
+ )} +
+ )} + + {/* 동기화 실행 버튼 */} + + +

+ * 동기화는 기존 데이터를 업데이트하며 새로운 자재그룹을 추가합니다. +

+
+
+
+ ); +} -- cgit v1.2.3