summaryrefslogtreecommitdiff
path: root/lib/items-tech/table/top/import-item-handler.tsx
diff options
context:
space:
mode:
authordujinkim <dujin.kim@dtsolution.co.kr>2025-05-14 06:12:13 +0000
committerdujinkim <dujin.kim@dtsolution.co.kr>2025-05-14 06:12:13 +0000
commitd0d2eaa2de58a0c33e9a21604b126961403cd69e (patch)
treef66cd3c8d3a123ff04f800b4b868c573fab2da95 /lib/items-tech/table/top/import-item-handler.tsx
parent21d8148fc5b1234cd4523e66ccaa8971ad104560 (diff)
(최겸) 기술영업 조선, 해양Top, 해양 Hull 아이템 리스트 개발(CRUD, excel import/export/template)
Diffstat (limited to 'lib/items-tech/table/top/import-item-handler.tsx')
-rw-r--r--lib/items-tech/table/top/import-item-handler.tsx143
1 files changed, 143 insertions, 0 deletions
diff --git a/lib/items-tech/table/top/import-item-handler.tsx b/lib/items-tech/table/top/import-item-handler.tsx
new file mode 100644
index 00000000..de1638a8
--- /dev/null
+++ b/lib/items-tech/table/top/import-item-handler.tsx
@@ -0,0 +1,143 @@
+"use client"
+
+import { z } from "zod"
+import { createOffshoreTopItem } from "../../service"
+
+// 해양 TOP 기능(공종) 유형 enum
+const TOP_WORK_TYPES = ["TM", "TS", "TE", "TP"] as const;
+
+// 아이템 데이터 검증을 위한 Zod 스키마
+const itemSchema = z.object({
+ itemCode: z.string().min(1, "아이템 코드는 필수입니다"),
+ itemName: z.string().min(1, "아이템 명은 필수입니다"),
+ workType: z.enum(TOP_WORK_TYPES, {
+ required_error: "기능(공종)은 필수입니다",
+ }),
+ description: z.string().nullable().optional(),
+ itemList1: z.string().nullable().optional(),
+ itemList2: z.string().nullable().optional(),
+ itemList3: z.string().nullable().optional(),
+ itemList4: z.string().nullable().optional(),
+});
+
+interface ProcessResult {
+ successCount: number;
+ errorCount: number;
+ errors?: Array<{ row: number; message: string }>;
+}
+
+/**
+ * Excel 파일에서 가져온 해양 TOP 아이템 데이터 처리하는 함수
+ */
+export async function processTopFileImport(
+ jsonData: any[],
+ progressCallback?: (current: number, total: number) => void
+): Promise<ProcessResult> {
+ // 결과 카운터 초기화
+ let successCount = 0;
+ let errorCount = 0;
+ const errors: Array<{ row: number; message: string }> = [];
+
+ // 빈 행 등 필터링
+ const dataRows = jsonData.filter(row => {
+ // 빈 행 건너뛰기
+ if (Object.values(row).every(val => !val)) {
+ return false;
+ }
+ return true;
+ });
+
+ // 데이터 행이 없으면 빈 결과 반환
+ if (dataRows.length === 0) {
+ return { successCount: 0, errorCount: 0 };
+ }
+
+ // 각 행에 대해 처리
+ for (let i = 0; i < dataRows.length; i++) {
+ const row = dataRows[i];
+ const rowIndex = i + 1; // 사용자에게 표시할 행 번호는 1부터 시작
+
+ // 진행 상황 콜백 호출
+ if (progressCallback) {
+ progressCallback(i + 1, dataRows.length);
+ }
+
+ try {
+ // 필드 매핑 (한글/영문 필드명 모두 지원)
+ const itemCode = row["아이템 코드"] || row["itemCode"] || "";
+ const itemName = row["아이템 명"] || row["itemName"] || "";
+ const workType = row["기능(공종)"] || row["workType"] || "";
+ const description = row["설명"] || row["description"] || null;
+ const itemList1 = row["항목1"] || row["itemList1"] || null;
+ const itemList2 = row["항목2"] || row["itemList2"] || null;
+ const itemList3 = row["항목3"] || row["itemList3"] || null;
+ const itemList4 = row["항목4"] || row["itemList4"] || null;
+
+ // 데이터 정제
+ const cleanedRow = {
+ itemCode: typeof itemCode === 'string' ? itemCode.trim() : String(itemCode).trim(),
+ itemName: typeof itemName === 'string' ? itemName.trim() : String(itemName).trim(),
+ workType: typeof workType === 'string' ? workType.trim() : String(workType).trim(),
+ description: description ? (typeof description === 'string' ? description : String(description)) : null,
+ itemList1: itemList1 ? (typeof itemList1 === 'string' ? itemList1 : String(itemList1)) : null,
+ itemList2: itemList2 ? (typeof itemList2 === 'string' ? itemList2 : String(itemList2)) : null,
+ itemList3: itemList3 ? (typeof itemList3 === 'string' ? itemList3 : String(itemList3)) : null,
+ itemList4: itemList4 ? (typeof itemList4 === 'string' ? itemList4 : String(itemList4)) : null,
+ };
+
+ // 데이터 유효성 검사
+ const validationResult = itemSchema.safeParse(cleanedRow);
+
+ if (!validationResult.success) {
+ const errorMessage = validationResult.error.errors.map(
+ err => `${err.path.join('.')}: ${err.message}`
+ ).join(', ');
+
+ errors.push({ row: rowIndex, message: errorMessage });
+ errorCount++;
+ continue;
+ }
+
+ // 해양 TOP 아이템 생성
+ const result = await createOffshoreTopItem({
+ itemCode: cleanedRow.itemCode,
+ itemName: cleanedRow.itemName,
+ workType: cleanedRow.workType as "TM" | "TS" | "TE" | "TP",
+ description: cleanedRow.description,
+ itemList1: cleanedRow.itemList1,
+ itemList2: cleanedRow.itemList2,
+ itemList3: cleanedRow.itemList3,
+ itemList4: cleanedRow.itemList4,
+ });
+
+ if (result.success) {
+ successCount++;
+ } else {
+ errors.push({
+ row: rowIndex,
+ message: result.message || result.error || "알 수 없는 오류"
+ });
+ errorCount++;
+ }
+ } catch (error) {
+ console.error(`${rowIndex}행 처리 오류:`, error);
+ errors.push({
+ row: rowIndex,
+ message: error instanceof Error ? error.message : "알 수 없는 오류"
+ });
+ errorCount++;
+ }
+
+ // 비동기 작업 쓰로틀링
+ if (i % 5 === 0) {
+ await new Promise(resolve => setTimeout(resolve, 10));
+ }
+ }
+
+ // 처리 결과 반환
+ return {
+ successCount,
+ errorCount,
+ errors: errors.length > 0 ? errors : undefined
+ };
+}