summaryrefslogtreecommitdiff
path: root/lib/tech-vendor-possible-items/table/excel-import.tsx
blob: 743879b36b4d63a3a1b3b779d773a056ff58c0c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
"use client";

import * as ExcelJS from 'exceljs';
import { ImportTechVendorPossibleItemData, ImportResult, importTechVendorPossibleItems } from '../service';
import { saveAs } from "file-saver";
import { decryptWithServerAction } from "@/components/drm/drmUtils"
import { toast } from 'sonner';

export interface ExcelImportResult extends ImportResult {
  errorFileUrl?: string;
}

/**
 * Excel 파일에서 tech vendor possible items 데이터를 읽고 import (새로운 스키마 버전)
 */
export async function importTechVendorPossibleItemsFromExcel(
  file: File
): Promise<ExcelImportResult> {

  try {
      // DRM 복호화 처리 - 서버 액션 직접 호출
      let arrayBuffer: ArrayBuffer;
      try {
        toast.info("파일 복호화 중...");
        arrayBuffer = await decryptWithServerAction(file);
      } catch (decryptError) {
        console.error("파일 복호화 실패, 원본 파일 사용:", decryptError);
        toast.warning("파일 복호화에 실패하여 원본 파일을 사용합니다.");
        // 복호화 실패 시 원본 파일 사용
        arrayBuffer = await file.arrayBuffer();
      }
      // ExcelJS 워크북 로드
      const workbook = new ExcelJS.Workbook();
      await workbook.xlsx.load(arrayBuffer);

    // 첫 번째 워크시트에서 데이터 읽기
    const worksheet = workbook.getWorksheet(1);
    if (!worksheet) {
      return {
        success: false,
        totalRows: 0,
        successCount: 0,
        failedRows: [{ row: 0, error: "워크시트를 찾을 수 없습니다." }],
      };
    }

    const data: ImportTechVendorPossibleItemData[] = [];

    // 데이터 행 읽기 (헤더 제외)
    // 새로운 스키마: 벤더이메일, 아이템코드, 공종, 선종, 아이템리스트, 서브아이템리스트
    worksheet.eachRow((row, rowNumber) => {
      if (rowNumber === 1) return; // 헤더 건너뛰기

      const vendorEmail = row.getCell(1).value?.toString()?.trim(); // 필수
      const itemCode = row.getCell(2).value?.toString()?.trim(); // 필수
      const workType = row.getCell(3).value?.toString()?.trim(); // 선택
      const shipTypes = row.getCell(4).value?.toString()?.trim(); // 선택
      const itemList = row.getCell(5).value?.toString()?.trim(); // 선택
      const subItemList = row.getCell(6).value?.toString()?.trim(); // 선택
      const vendorCode = row.getCell(7).value?.toString()?.trim(); // 선택 (호환성)

      // 빈 행 건너뛰기
      if (!vendorEmail && !itemCode && !workType && !shipTypes && !itemList && !subItemList && !vendorCode) {
        return;
      }

      // 필수 필드 체크: 벤더이메일, 아이템코드
      if (!vendorEmail || !itemCode) {
        // 불완전한 데이터도 포함하여 에러 처리
        data.push({
          vendorEmail: vendorEmail || '',
          itemCode: itemCode || '',
          workType: workType || undefined,
          shipTypes: shipTypes || undefined,
          itemList: itemList || undefined,
          subItemList: subItemList || undefined,
          vendorCode: vendorCode || undefined,
        });
        return;
      }

      // 완전한 데이터 추가
      data.push({
        vendorEmail,
        itemCode,
        workType: workType || undefined,
        shipTypes: shipTypes || undefined,
        itemList: itemList || undefined,
        subItemList: subItemList || undefined,
        vendorCode: vendorCode || undefined,
      });
    });

    if (data.length === 0) {
      return {
        success: false,
        totalRows: 0,
        successCount: 0,
        failedRows: [{ row: 0, error: "가져올 데이터가 없습니다. 템플릿 형식을 확인하세요." }],
      };
    }

    // 서비스를 통해 import 실행
    const result = await importTechVendorPossibleItems(data);

    // 실패한 항목이 있으면 오류 파일 생성
    if (result.failedRows.length > 0) {
      const errorFileUrl = await createErrorExcelFile(result.failedRows);
      return {
        ...result,
        errorFileUrl,
      };
    }

    return result;
  } catch (error) {
    console.error("Excel import 중 오류:", error);
    return {
      success: false,
      totalRows: 0,
      successCount: 0,
      failedRows: [
        {
          row: 0,
          error: error instanceof Error ? error.message : "파일 처리 중 오류가 발생했습니다. 파일 형식을 확인하세요.",
        },
      ],
    };
  }
}

/**
 * 실패한 항목들을 포함한 오류 Excel 파일 생성 (새로운 스키마 버전)
 */
async function createErrorExcelFile(
  failedRows: ImportResult['failedRows']
): Promise<string> {
  try {
    const workbook = new ExcelJS.Workbook();
    const worksheet = workbook.addWorksheet('Import 오류 목록');

    // 헤더 설정 (새로운 스키마에 맞춰)
    worksheet.columns = [
      { header: '행 번호', key: 'row', width: 10 },
      { header: '벤더이메일', key: 'vendorEmail', width: 30 },
      { header: '아이템코드', key: 'itemCode', width: 20 },
      { header: '공종', key: 'workType', width: 15 },
      { header: '선종', key: 'shipTypes', width: 20 },
      { header: '아이템리스트', key: 'itemList', width: 30 },
      { header: '서브아이템리스트', key: 'subItemList', width: 30 },
      { header: '벤더코드', key: 'vendorCode', width: 15 },
      { header: '오류 내용', key: 'error', width: 60 },
      { header: '해결 방법', key: 'solution', width: 40 },
    ];

    // 헤더 스타일
    const headerRow = worksheet.getRow(1);
    headerRow.eachCell((cell) => {
      cell.fill = {
        type: 'pattern',
        pattern: 'solid',
        fgColor: { argb: 'FFFF6B6B' }
      };
      cell.font = {
        bold: true,
        color: { argb: 'FFFFFFFF' }
      };
      cell.border = {
        top: { style: 'thin' },
        left: { style: 'thin' },
        bottom: { style: 'thin' },
        right: { style: 'thin' }
      };
    });

    // 오류 데이터 추가
    failedRows.forEach((item) => {
      let solution = '시스템 관리자에게 문의하세요';
      
      if (item.error.includes('벤더 이메일')) {
        solution = '올바른 이메일 형식으로 등록된 벤더 이메일인지 확인하세요';
      } else if (item.error.includes('아이템 코드')) {
        solution = '아이템 코드가 누락되었거나 잘못된 형식입니다';
      } else if (item.error.includes('이미 존재')) {
        solution = '중복된 조합입니다. 기존 데이터를 확인하세요';
      } else if (item.error.includes('찾을 수 없습니다')) {
        solution = '벤더 이메일이 시스템에 등록되어 있는지 확인하세요';
      }

      const row = worksheet.addRow({
        row: item.row,
        vendorEmail: item.vendorEmail || '누락',
        itemCode: item.itemCode || '누락',
        workType: item.workType || '',
        shipTypes: item.shipTypes || '',
        itemList: item.itemList || '',
        subItemList: item.subItemList || '',
        vendorCode: item.vendorCode || '',
        error: item.error,
        solution: solution,
      });

      row.eachCell((cell) => {
        cell.border = {
          top: { style: 'thin' },
          left: { style: 'thin' },
          bottom: { style: 'thin' },
          right: { style: 'thin' }
        };
      });
    });

    // 안내사항 추가 (새로운 스키마에 맞춰)
    const instructionSheet = workbook.addWorksheet('오류 해결 가이드');
    const instructions = [
      ['📋 새로운 스키마 Import 가이드', ''],
      ['', ''],
      ['📌 필수 필드:', ''],
      ['   • 벤더이메일: 시스템에 등록된 벤더의 이메일 주소', ''],
      ['   • 아이템코드: 처리할 아이템의 코드', ''],
      ['', ''],
      ['📌 선택 필드:', ''],
      ['   • 공종: 작업 유형 (예: 용접, 도장, 기계 등)', ''],
      ['   • 선종: 선박 유형 (예: 컨테이너선, 벌크선, 탱커 등)', ''],
      ['   • 아이템리스트: 아이템에 대한 상세 설명', ''],
      ['   • 서브아이템리스트: 세부 아이템들에 대한 설명', ''],
      ['   • 벤더코드: 호환성을 위한 선택 필드', ''],
      ['', ''],
      ['🔍 오류 유형별 해결 방법:', ''],
      ['', ''],
      ['1. 벤더 이메일 오류:', ''],
      ['   • 올바른 이메일 형식 확인 (예: vendor@example.com)', ''],
      ['   • 시스템에 등록된 벤더 이메일인지 확인', ''],
      ['   • 벤더 관리 메뉴에서 등록 상태 확인', ''],
      ['', ''],
      ['2. 아이템 코드 오류:', ''],
      ['   • 아이템 코드가 누락되지 않았는지 확인', ''],
      ['   • 특수문자나 공백이 포함되지 않았는지 확인', ''],
      ['', ''],
      ['3. 중복 오류:', ''],
      ['   • 동일한 벤더 + 아이템코드 + 공종 + 선종 조합', ''],
      ['   • 기존 데이터 확인 후 중복 제거', ''],
      ['', ''],
      ['📞 추가 문의: 시스템 관리자', ''],
    ];

    instructions.forEach((rowData, index) => {
      const row = instructionSheet.addRow(rowData);
      if (index === 0) {
        row.getCell(1).font = { bold: true, size: 14, color: { argb: 'FF1F4E79' } };
      } else if (rowData[0]?.includes('📌') || rowData[0]?.includes('🔍')) {
        row.getCell(1).font = { bold: true, color: { argb: 'FF1F4E79' } };
      } else if (rowData[0]?.includes(':')) {
        row.getCell(1).font = { bold: true, color: { argb: 'FF1F4E79' } };
      }
    });

    instructionSheet.getColumn(1).width = 60;

    // 파일 생성 및 다운로드
    const buffer = await workbook.xlsx.writeBuffer();
    const blob = new Blob([buffer], { 
      type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" 
    });
    
    const fileName = `Import_오류_${new Date().toISOString().split('T')[0]}_${Date.now()}.xlsx`;
    saveAs(blob, fileName);
    
    return fileName;
  } catch (error) {
    console.error("오류 파일 생성 중 오류:", error);
    return '';
  }
}