diff options
Diffstat (limited to 'app/api/(S-ERP)/(MDG)/IF_MDZ_EVCP_DEPARTMENT_CODE/route.ts')
| -rw-r--r-- | app/api/(S-ERP)/(MDG)/IF_MDZ_EVCP_DEPARTMENT_CODE/route.ts | 54 |
1 files changed, 23 insertions, 31 deletions
diff --git a/app/api/(S-ERP)/(MDG)/IF_MDZ_EVCP_DEPARTMENT_CODE/route.ts b/app/api/(S-ERP)/(MDG)/IF_MDZ_EVCP_DEPARTMENT_CODE/route.ts index 28757fb5..fb54dff3 100644 --- a/app/api/(S-ERP)/(MDG)/IF_MDZ_EVCP_DEPARTMENT_CODE/route.ts +++ b/app/api/(S-ERP)/(MDG)/IF_MDZ_EVCP_DEPARTMENT_CODE/route.ts @@ -16,9 +16,12 @@ import { processNestedArray, createErrorResponse, createSuccessResponse, - replaceSubTableData, withSoapLogging -} from "@/lib/soap/mdg/utils"; +} from "@/lib/soap/utils"; +import { + bulkUpsert, + bulkReplaceSubTableData +} from "@/lib/soap/batch-utils"; // 스키마에서 직접 타입 추론 type DeptData = typeof DEPARTMENT_CODE_CMCTB_DEPT_MDG.$inferInsert; @@ -187,40 +190,29 @@ function transformDepartmentData(deptData: DeptXML[]): ProcessedDepartmentData[] * @param processedDepts 변환된 DEPARTMENT 데이터 배열 */ async function saveToDatabase(processedDepts: ProcessedDepartmentData[]) { - console.log(`데이터베이스 저장 시작: ${processedDepts.length}개 부서 데이터`); - + console.log(`데이터베이스(배치) 저장 시작: ${processedDepts.length}개 부서 데이터`); try { await db.transaction(async (tx) => { - for (const deptData of processedDepts) { - const { dept, deptnms, compnms, corpnms } = deptData; - - if (!dept.DEPTCD) { - console.warn('부서코드(DEPTCD)가 없는 항목 발견, 건너뜁니다.'); - continue; - } + const deptRows = processedDepts + .map((d) => d.dept) + .filter((d): d is DeptData => !!d.DEPTCD); - // 1. Department 테이블 Upsert (최상위 테이블 - unique 필드: DEPTCD) - await tx.insert(DEPARTMENT_CODE_CMCTB_DEPT_MDG) - .values(dept) - .onConflictDoUpdate({ - target: DEPARTMENT_CODE_CMCTB_DEPT_MDG.DEPTCD, - set: { - ...dept, - updatedAt: new Date(), - } - }); - - // 2. 하위 테이블들 처리 - FK(DEPTCD) 기준 전체 삭제 후 재삽입 - // 전체 데이터셋 기반 처리로 데이터 일관성 확보 - await Promise.all([ - replaceSubTableData(tx, DEPARTMENT_CODE_CMCTB_DEPT_MDG_DEPTNM, deptnms, 'DEPTCD', dept.DEPTCD), - replaceSubTableData(tx, DEPARTMENT_CODE_CMCTB_DEPT_MDG_COMPNM, compnms, 'DEPTCD', dept.DEPTCD), - replaceSubTableData(tx, DEPARTMENT_CODE_CMCTB_DEPT_MDG_CORPNM, corpnms, 'DEPTCD', dept.DEPTCD) - ]); - } + const deptcds = deptRows.map((d) => d.DEPTCD as string); + + const deptnms = processedDepts.flatMap((d) => d.deptnms); + const compnms = processedDepts.flatMap((d) => d.compnms); + const corpnms = processedDepts.flatMap((d) => d.corpnms); + + await bulkUpsert(tx, DEPARTMENT_CODE_CMCTB_DEPT_MDG, deptRows, 'DEPTCD'); + + await Promise.all([ + bulkReplaceSubTableData(tx, DEPARTMENT_CODE_CMCTB_DEPT_MDG_DEPTNM, deptnms, DEPARTMENT_CODE_CMCTB_DEPT_MDG_DEPTNM.DEPTCD, deptcds), + bulkReplaceSubTableData(tx, DEPARTMENT_CODE_CMCTB_DEPT_MDG_COMPNM, compnms, DEPARTMENT_CODE_CMCTB_DEPT_MDG_COMPNM.DEPTCD, deptcds), + bulkReplaceSubTableData(tx, DEPARTMENT_CODE_CMCTB_DEPT_MDG_CORPNM, corpnms, DEPARTMENT_CODE_CMCTB_DEPT_MDG_CORPNM.DEPTCD, deptcds), + ]); }); - console.log(`데이터베이스 저장 완료: ${processedDepts.length}개 부서`); + console.log(`데이터베이스(배치) 저장 완료: ${processedDepts.length}개 부서`); return true; } catch (error) { console.error('데이터베이스 저장 중 오류 발생:', error); |
