summaryrefslogtreecommitdiff
path: root/lib/items-tech/table/top
diff options
context:
space:
mode:
Diffstat (limited to 'lib/items-tech/table/top')
-rw-r--r--lib/items-tech/table/top/import-item-handler.tsx271
-rw-r--r--lib/items-tech/table/top/item-excel-template.tsx218
2 files changed, 239 insertions, 250 deletions
diff --git a/lib/items-tech/table/top/import-item-handler.tsx b/lib/items-tech/table/top/import-item-handler.tsx
index 0a163791..4f34cff2 100644
--- a/lib/items-tech/table/top/import-item-handler.tsx
+++ b/lib/items-tech/table/top/import-item-handler.tsx
@@ -1,141 +1,130 @@
-"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().optional(),
- workType: z.enum(TOP_WORK_TYPES, {
- required_error: "기능(공종)은 필수입니다",
- }),
- itemList: z.string().nullable().optional(),
- subItemList: z.string().nullable().optional(),
-});
-
-interface ProcessResult {
- successCount: number;
- errorCount: number;
- errors: Array<{ row: number; message: string; itemCode?: string; workType?: string }>;
-}
-
-/**
- * Excel 파일에서 가져온 해양 TOP 아이템 데이터 처리하는 함수
- */
-export async function processTopFileImport(
- jsonData: Record<string, unknown>[],
- 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, errors: [] };
- }
-
- // 각 행에 대해 처리
- 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 workType = row["기능(공종)"] || row["workType"] || "";
- const itemList = row["자재명"] || row["itemList"] || null;
- const subItemList = row["자재명(상세)"] || row["subItemList"] || null;
-
- // 데이터 정제
- const cleanedRow = {
- itemCode: typeof itemCode === 'string' ? itemCode.trim() : String(itemCode).trim(),
- workType: typeof workType === 'string' ? workType.trim() : String(workType).trim(),
- itemList: itemList ? (typeof itemList === 'string' ? itemList : String(itemList)) : null,
- subItemList: subItemList ? (typeof subItemList === 'string' ? subItemList : String(subItemList)) : 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,
- itemCode: cleanedRow.itemCode,
- workType: cleanedRow.workType
- });
- errorCount++;
- continue;
- }
-
- // 해양 TOP 아이템 생성
- const result = await createOffshoreTopItem({
- itemCode: cleanedRow.itemCode,
- workType: cleanedRow.workType as "TM" | "TS" | "TE" | "TP",
- itemList: cleanedRow.itemList,
- subItemList: cleanedRow.subItemList,
- });
-
- if (result.success) {
- successCount++;
- } else {
- errors.push({
- row: rowIndex,
- message: result.message || result.error || "알 수 없는 오류",
- itemCode: cleanedRow.itemCode,
- workType: cleanedRow.workType
- });
- errorCount++;
- }
- } catch (error) {
- console.error(`${rowIndex}행 처리 오류:`, error);
-
- // cleanedRow가 정의되지 않은 경우를 처리
- const itemCode = row["자재 그룹"] || row["itemCode"] || "";
- const workType = row["기능(공종)"] || row["workType"] || "";
-
- errors.push({
- row: rowIndex,
- message: error instanceof Error ? error.message : "알 수 없는 오류",
- itemCode: typeof itemCode === 'string' ? itemCode.trim() : String(itemCode).trim(),
- workType: typeof workType === 'string' ? workType.trim() : String(workType).trim()
- });
- errorCount++;
- }
-
- // 비동기 작업 쓰로틀링
- if (i % 5 === 0) {
- await new Promise(resolve => setTimeout(resolve, 10));
- }
- }
-
- // 처리 결과 반환
- return {
- successCount,
- errorCount,
- errors
- };
-}
+"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().optional(),
+ workType: z.enum(TOP_WORK_TYPES, {
+ required_error: "기능(공종)은 필수입니다",
+ }),
+ itemList: z.string().nullable().optional(),
+ subItemList: z.string().nullable().optional(),
+});
+
+interface ProcessResult {
+ successCount: number;
+ errorCount: number;
+ errors: Array<{ row: number; message: string }>;
+}
+
+/**
+ * Excel 파일에서 가져온 해양 TOP 아이템 데이터 처리하는 함수
+ */
+export async function processTopFileImport(
+ jsonData: Record<string, unknown>[],
+ 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, errors: [] };
+ }
+
+ // 각 행에 대해 처리
+ 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 workType = row["기능(공종)"] || row["workType"] || "";
+ const itemList = row["자재명"] || row["itemList"] || null;
+ const subItemList = row["자재명(상세)"] || row["subItemList"] || null;
+
+ // 데이터 정제
+ const cleanedRow = {
+ itemCode: typeof itemCode === 'string' ? itemCode.trim() : String(itemCode).trim(),
+ workType: typeof workType === 'string' ? workType.trim() : String(workType).trim(),
+ itemList: itemList ? (typeof itemList === 'string' ? itemList : String(itemList)) : null,
+ subItemList: subItemList ? (typeof subItemList === 'string' ? subItemList : String(subItemList)) : 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,
+ workType: cleanedRow.workType as "TM" | "TS" | "TE" | "TP",
+ itemList: cleanedRow.itemList,
+ subItemList: cleanedRow.subItemList,
+ });
+
+ 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
+ };
+}
diff --git a/lib/items-tech/table/top/item-excel-template.tsx b/lib/items-tech/table/top/item-excel-template.tsx
index b67d91be..9121d70f 100644
--- a/lib/items-tech/table/top/item-excel-template.tsx
+++ b/lib/items-tech/table/top/item-excel-template.tsx
@@ -1,109 +1,109 @@
-import * as ExcelJS from 'exceljs';
-import { saveAs } from "file-saver";
-
-
-/**
- * 해양 TOP 아이템 데이터 가져오기를 위한 Excel 템플릿 파일 생성 및 다운로드
- */
-export async function exportTopItemTemplate() {
- // 워크북 생성
- const workbook = new ExcelJS.Workbook();
- workbook.creator = 'Offshore TOP Item Management System';
- workbook.created = new Date();
-
- // 워크시트 생성
- const worksheet = workbook.addWorksheet('해양 TOP 아이템');
-
- // 컬럼 헤더 정의 및 스타일 적용
- worksheet.columns = [
- { header: '자재 그룹', key: 'itemCode', width: 15 },
- { header: '기능(공종)', key: 'workType', width: 15 },
- { header: '자재명', key: 'itemList', width: 20 },
- { header: '자재명(상세)', key: 'subItemList', width: 20 },
-
- ];
-
- // 헤더 스타일 적용
- const headerRow = worksheet.getRow(1);
- headerRow.font = { bold: true };
- headerRow.fill = {
- type: 'pattern',
- pattern: 'solid',
- fgColor: { argb: 'FFE0E0E0' }
- };
- headerRow.alignment = { vertical: 'middle', horizontal: 'center' };
-
- // 테두리 스타일 적용
- headerRow.eachCell((cell) => {
- cell.border = {
- top: { style: 'thin' },
- left: { style: 'thin' },
- bottom: { style: 'thin' },
- right: { style: 'thin' }
- };
- });
-
- // 샘플 데이터 추가
- const sampleData = [
- {
- itemCode: 'TOP001',
- workType: 'TM',
- itemList: '항목1 샘플 데이터',
- subItemList: '항목2 샘플 데이터',
- },
- {
- itemCode: 'TOP002',
- workType: 'TS',
- itemList: '항목1 샘플 데이터',
- subItemList: '항목2 샘플 데이터',
- }
-
- ];
-
- // 데이터 행 추가
- sampleData.forEach(item => {
- worksheet.addRow(item);
- });
-
- // 데이터 행 스타일 적용
- worksheet.eachRow((row, rowNumber) => {
- if (rowNumber > 1) { // 헤더를 제외한 데이터 행
- row.eachCell((cell) => {
- cell.border = {
- top: { style: 'thin' },
- left: { style: 'thin' },
- bottom: { style: 'thin' },
- right: { style: 'thin' }
- };
- });
- }
- });
-
-
- // 워크시트 보호 (선택적)
- worksheet.protect('', {
- selectLockedCells: true,
- selectUnlockedCells: true,
- formatColumns: true,
- formatRows: true,
- insertColumns: false,
- insertRows: true,
- insertHyperlinks: false,
- deleteColumns: false,
- deleteRows: true,
- sort: true,
- autoFilter: true,
- pivotTables: false
- });
-
- try {
- // 워크북을 Blob으로 변환
- const buffer = await workbook.xlsx.writeBuffer();
- const blob = new Blob([buffer], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
- saveAs(blob, 'offshore-top-item-template.xlsx');
- return true;
- } catch (error) {
- console.error('Excel 템플릿 생성 오류:', error);
- throw error;
- }
-}
+import * as ExcelJS from 'exceljs';
+import { saveAs } from "file-saver";
+
+
+/**
+ * 해양 TOP 아이템 데이터 가져오기를 위한 Excel 템플릿 파일 생성 및 다운로드
+ */
+export async function exportTopItemTemplate() {
+ // 워크북 생성
+ const workbook = new ExcelJS.Workbook();
+ workbook.creator = 'Offshore TOP Item Management System';
+ workbook.created = new Date();
+
+ // 워크시트 생성
+ const worksheet = workbook.addWorksheet('해양 TOP 아이템');
+
+ // 컬럼 헤더 정의 및 스타일 적용
+ worksheet.columns = [
+ { header: '자재 그룹', key: 'itemCode', width: 15 },
+ { header: '기능(공종)', key: 'workType', width: 15 },
+ { header: '자재명', key: 'itemList', width: 20 },
+ { header: '자재명(상세)', key: 'subItemList', width: 20 },
+
+ ];
+
+ // 헤더 스타일 적용
+ const headerRow = worksheet.getRow(1);
+ headerRow.font = { bold: true };
+ headerRow.fill = {
+ type: 'pattern',
+ pattern: 'solid',
+ fgColor: { argb: 'FFE0E0E0' }
+ };
+ headerRow.alignment = { vertical: 'middle', horizontal: 'center' };
+
+ // 테두리 스타일 적용
+ headerRow.eachCell((cell) => {
+ cell.border = {
+ top: { style: 'thin' },
+ left: { style: 'thin' },
+ bottom: { style: 'thin' },
+ right: { style: 'thin' }
+ };
+ });
+
+ // 샘플 데이터 추가
+ const sampleData = [
+ {
+ itemCode: 'TOP001',
+ workType: 'TM',
+ itemList: '항목1 샘플 데이터',
+ subItemList: '항목2 샘플 데이터',
+ },
+ {
+ itemCode: 'TOP002',
+ workType: 'TS',
+ itemList: '항목1 샘플 데이터',
+ subItemList: '항목2 샘플 데이터',
+ }
+
+ ];
+
+ // 데이터 행 추가
+ sampleData.forEach(item => {
+ worksheet.addRow(item);
+ });
+
+ // 데이터 행 스타일 적용
+ worksheet.eachRow((row, rowNumber) => {
+ if (rowNumber > 1) { // 헤더를 제외한 데이터 행
+ row.eachCell((cell) => {
+ cell.border = {
+ top: { style: 'thin' },
+ left: { style: 'thin' },
+ bottom: { style: 'thin' },
+ right: { style: 'thin' }
+ };
+ });
+ }
+ });
+
+
+ // 워크시트 보호 (선택적)
+ worksheet.protect('', {
+ selectLockedCells: true,
+ selectUnlockedCells: true,
+ formatColumns: true,
+ formatRows: true,
+ insertColumns: false,
+ insertRows: true,
+ insertHyperlinks: false,
+ deleteColumns: false,
+ deleteRows: true,
+ sort: true,
+ autoFilter: true,
+ pivotTables: false
+ });
+
+ try {
+ // 워크북을 Blob으로 변환
+ const buffer = await workbook.xlsx.writeBuffer();
+ const blob = new Blob([buffer], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
+ saveAs(blob, 'offshore-top-item-template.xlsx');
+ return true;
+ } catch (error) {
+ console.error('Excel 템플릿 생성 오류:', error);
+ throw error;
+ }
+}