refactor: 디버깅 로그 제거 및 코드 정리
변경사항: - handleBatchSave의 모든 console.log 제거 - 핵심 로직만 유지 (데이터 매핑, 조합 생성, 저장) - 코드 가독성 향상 제거된 로그: - modalDataStore 데이터 확인 로그 - parentDataMapping 설정 로그 - 품목/그룹 처리 로그 - 조합 생성/병합 로그 - 데이터 소스 상세 로그 - 저장 요청/결과 로그 유지된 기능: - Zustand modalDataStore에서 부모 데이터 가져오기 - 무한 깊이 모달 지원 - 완전히 설정 기반 parentDataMapping - 카티션 곱 조합 생성 - 하드코딩 없는 동적 매핑
This commit is contained in:
parent
762ab8e684
commit
d4895c363c
|
|
@ -563,20 +563,6 @@ export class ButtonActionExecutor {
|
|||
}
|
||||
});
|
||||
|
||||
console.log(`🔍 [handleBatchSave] 부모 데이터:`, {
|
||||
hasParentData: Object.keys(parentData).length > 0,
|
||||
parentDataKeys: Object.keys(parentData),
|
||||
parentDataFull: parentData,
|
||||
selectedRowsData,
|
||||
originalData,
|
||||
modalDataStoreKeys: Object.keys(modalDataStore),
|
||||
modalDataStoreDetails: Object.fromEntries(
|
||||
Object.entries(modalDataStore).map(([key, data]) => [
|
||||
key,
|
||||
{ count: Array.isArray(data) ? data.length : 1, hasData: !!data }
|
||||
])
|
||||
),
|
||||
});
|
||||
|
||||
// 각 SelectedItemsDetailInput 컴포넌트의 데이터 처리
|
||||
for (const key of selectedItemsKeys) {
|
||||
|
|
@ -587,24 +573,13 @@ export class ButtonActionExecutor {
|
|||
fieldGroups: Record<string, Array<{ id: string; [key: string]: any }>>;
|
||||
}>;
|
||||
|
||||
console.log(`📦 [handleBatchSave] ${key} 처리 중 (${items.length}개 품목)`);
|
||||
|
||||
// 🆕 이 컴포넌트의 parentDataMapping 설정 가져오기
|
||||
const componentConfig = context.componentConfigs?.[key];
|
||||
const parentDataMapping = componentConfig?.parentDataMapping || [];
|
||||
|
||||
console.log(`🔍 [handleBatchSave] parentDataMapping 설정:`, {
|
||||
componentId: key,
|
||||
hasComponentConfig: !!componentConfig,
|
||||
hasMapping: parentDataMapping.length > 0,
|
||||
mappings: parentDataMapping,
|
||||
sourceTable: componentConfig?.sourceTable,
|
||||
});
|
||||
|
||||
// 🆕 각 품목의 그룹 간 조합(카티션 곱) 생성
|
||||
for (const item of items) {
|
||||
const groupKeys = Object.keys(item.fieldGroups);
|
||||
console.log(`🔍 [handleBatchSave] 품목 처리: ${item.id} (${groupKeys.length}개 그룹)`);
|
||||
|
||||
// 각 그룹의 항목 배열 가져오기
|
||||
const groupArrays = groupKeys.map(groupKey => ({
|
||||
|
|
@ -612,10 +587,6 @@ export class ButtonActionExecutor {
|
|||
entries: item.fieldGroups[groupKey] || []
|
||||
}));
|
||||
|
||||
console.log(`📊 [handleBatchSave] 그룹별 항목 수:`,
|
||||
groupArrays.map(g => `${g.groupKey}: ${g.entries.length}개`).join(", ")
|
||||
);
|
||||
|
||||
// 카티션 곱 계산 함수
|
||||
const cartesianProduct = (arrays: any[][]): any[][] => {
|
||||
if (arrays.length === 0) return [[]];
|
||||
|
|
@ -633,8 +604,6 @@ export class ButtonActionExecutor {
|
|||
const entryArrays = groupArrays.map(g => g.entries);
|
||||
const combinations = cartesianProduct(entryArrays);
|
||||
|
||||
console.log(`🔢 [handleBatchSave] 생성된 조합 수: ${combinations.length}개`);
|
||||
|
||||
// 각 조합을 개별 레코드로 저장
|
||||
for (let i = 0; i < combinations.length; i++) {
|
||||
const combination = combinations[i];
|
||||
|
|
@ -644,75 +613,38 @@ export class ButtonActionExecutor {
|
|||
|
||||
// 1. parentDataMapping 설정이 있으면 적용
|
||||
if (parentDataMapping.length > 0) {
|
||||
console.log(` 🔗 [parentDataMapping] 매핑 시작 (${parentDataMapping.length}개 매핑)`);
|
||||
|
||||
for (const mapping of parentDataMapping) {
|
||||
// sourceTable을 기준으로 데이터 소스 결정
|
||||
let sourceData: any;
|
||||
|
||||
// 🔍 sourceTable과 실제 데이터 테이블 비교
|
||||
// - modalDataStore는 모든 이전 화면의 누적 데이터 (예: 거래처, 품목)
|
||||
// - item.originalData는 현재 선택된 항목 데이터 (예: 품목 테이블)
|
||||
|
||||
// 원본 데이터 테이블명 확인 (sourceTable이 config에 명시되어 있음)
|
||||
const sourceTableName = mapping.sourceTable;
|
||||
|
||||
// 현재 선택된 항목의 테이블 = config.sourceTable
|
||||
const selectedItemTable = componentConfig?.sourceTable;
|
||||
|
||||
if (sourceTableName === selectedItemTable) {
|
||||
// 선택된 항목 데이터 사용
|
||||
sourceData = item.originalData;
|
||||
console.log(` 📦 소스: 선택된 항목 데이터 (${sourceTableName})`);
|
||||
} else {
|
||||
// 🆕 modalDataStore에서 해당 테이블 데이터 가져오기
|
||||
const tableData = modalDataStore[sourceTableName];
|
||||
if (tableData && Array.isArray(tableData) && tableData.length > 0) {
|
||||
sourceData = tableData[0]; // 첫 번째 항목 사용
|
||||
console.log(` 🌐 소스: modalDataStore (${sourceTableName})`);
|
||||
sourceData = tableData[0];
|
||||
} else {
|
||||
// 폴백: 이전 화면 데이터 사용
|
||||
sourceData = parentData;
|
||||
console.log(` 👤 소스: 이전 화면 데이터 (${sourceTableName}) [폴백]`);
|
||||
}
|
||||
}
|
||||
|
||||
const sourceValue = sourceData[mapping.sourceField];
|
||||
|
||||
console.log(` 🔍 데이터 소스 상세:`, {
|
||||
sourceTable: sourceTableName,
|
||||
selectedItemTable,
|
||||
isFromSelectedItem: sourceTableName === selectedItemTable,
|
||||
sourceDataKeys: Object.keys(sourceData),
|
||||
sourceField: mapping.sourceField,
|
||||
sourceValue,
|
||||
targetField: mapping.targetField
|
||||
});
|
||||
|
||||
if (sourceValue !== undefined && sourceValue !== null) {
|
||||
mappedData[mapping.targetField] = sourceValue;
|
||||
console.log(` ✅ [${sourceTableName}] ${mapping.sourceField} → ${mapping.targetField}: ${sourceValue}`);
|
||||
} else if (mapping.defaultValue !== undefined) {
|
||||
mappedData[mapping.targetField] = mapping.defaultValue;
|
||||
console.log(` ⚠️ [${sourceTableName}] ${mapping.sourceField} 없음, 기본값 사용 → ${mapping.targetField}: ${mapping.defaultValue}`);
|
||||
} else {
|
||||
console.log(` ⚠️ [${sourceTableName}] ${mapping.sourceField} 없음, 건너뜀`);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// 🔧 parentDataMapping 설정이 없는 경우 기본 매핑 (하위 호환성)
|
||||
console.log(` ⚠️ [parentDataMapping] 설정 없음, 기본 매핑 적용`);
|
||||
|
||||
// 기본 item_id 매핑 (item.originalData의 id)
|
||||
if (item.originalData.id) {
|
||||
mappedData.item_id = item.originalData.id;
|
||||
console.log(` ✅ [기본] item_id 매핑: ${item.originalData.id}`);
|
||||
}
|
||||
|
||||
// 기본 customer_id 매핑 (parentData의 id 또는 customer_id)
|
||||
if (parentData.id || parentData.customer_id) {
|
||||
mappedData.customer_id = parentData.customer_id || parentData.id;
|
||||
console.log(` ✅ [기본] customer_id 매핑: ${mappedData.customer_id}`);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -727,32 +659,15 @@ export class ButtonActionExecutor {
|
|||
// 원본 데이터로 시작 (매핑된 데이터 사용)
|
||||
let mergedData = { ...mappedData };
|
||||
|
||||
console.log(`🔍 [handleBatchSave] 조합 ${i + 1}/${combinations.length} 병합 시작:`, {
|
||||
originalDataKeys: Object.keys(item.originalData),
|
||||
mappedDataKeys: Object.keys(mappedData),
|
||||
combinationLength: combination.length
|
||||
});
|
||||
|
||||
// 각 그룹의 항목 데이터를 순차적으로 병합
|
||||
for (let j = 0; j < combination.length; j++) {
|
||||
const entry = combination[j];
|
||||
const { id, ...entryData } = entry; // id 제외
|
||||
|
||||
console.log(` 🔸 그룹 ${j + 1} 데이터 병합:`, entryData);
|
||||
|
||||
mergedData = { ...mergedData, ...entryData };
|
||||
}
|
||||
|
||||
console.log(`📝 [handleBatchSave] 조합 ${i + 1}/${combinations.length} 최종 데이터:`, mergedData);
|
||||
|
||||
// 🆕 조합 저장 시 id 필드 제거 (각 조합이 독립된 새 레코드가 되도록)
|
||||
// originalData의 id는 원본 품목의 ID이므로, 새로운 customer_item_mapping 레코드 생성 시 제거 필요
|
||||
const { id: _removedId, ...dataWithoutId } = mergedData;
|
||||
|
||||
console.log(`🔧 [handleBatchSave] 조합 ${i + 1}/${combinations.length} id 제거됨:`, {
|
||||
removedId: _removedId,
|
||||
hasId: 'id' in dataWithoutId
|
||||
});
|
||||
|
||||
// 사용자 정보 추가
|
||||
if (!context.userId) {
|
||||
|
|
@ -763,20 +678,13 @@ export class ButtonActionExecutor {
|
|||
const companyCodeValue = context.companyCode || "";
|
||||
|
||||
const dataWithUserInfo = {
|
||||
...dataWithoutId, // id가 제거된 데이터 사용
|
||||
...dataWithoutId,
|
||||
writer: dataWithoutId.writer || writerValue,
|
||||
created_by: writerValue,
|
||||
updated_by: writerValue,
|
||||
company_code: dataWithoutId.company_code || companyCodeValue,
|
||||
};
|
||||
|
||||
console.log(`💾 [handleBatchSave] 조합 ${i + 1}/${combinations.length} 저장 요청:`, {
|
||||
itemId: item.id,
|
||||
combinationIndex: i + 1,
|
||||
totalCombinations: combinations.length,
|
||||
data: dataWithUserInfo
|
||||
});
|
||||
|
||||
// INSERT 실행
|
||||
const { DynamicFormApi } = await import("@/lib/api/dynamicForm");
|
||||
const saveResult = await DynamicFormApi.saveFormData({
|
||||
|
|
@ -787,19 +695,13 @@ export class ButtonActionExecutor {
|
|||
|
||||
if (saveResult.success) {
|
||||
successCount++;
|
||||
console.log(`✅ [handleBatchSave] 조합 ${i + 1}/${combinations.length} 저장 성공!`, {
|
||||
savedId: saveResult.data?.id,
|
||||
itemId: item.id
|
||||
});
|
||||
} else {
|
||||
failCount++;
|
||||
errors.push(`품목 ${item.id} > 조합 ${i + 1}: ${saveResult.message}`);
|
||||
console.error(`❌ [handleBatchSave] 조합 ${i + 1}/${combinations.length} 저장 실패:`, saveResult.message);
|
||||
}
|
||||
} catch (error: any) {
|
||||
failCount++;
|
||||
errors.push(`품목 ${item.id} > 조합 ${i + 1}: ${error.message}`);
|
||||
console.error(`❌ [handleBatchSave] 조합 ${i + 1}/${combinations.length} 저장 오류:`, error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue