feat(repeat-screen-modal): 집계 저장 및 채번 규칙 값 저장 기능 추가
- RepeatScreenModal 집계 결과를 연관 테이블에 저장하는 기능 추가 - ButtonPrimary 저장 시 채번 규칙 값(shipment_plan_no) 함께 저장 - _repeatScreenModal_* 데이터 감지 시 메인 테이블 중복 저장 방지 - 기존 행 수정 모드(_isEditing) 지원 - AggregationSaveConfig 타입 및 ConfigPanel UI 추가
This commit is contained in:
@@ -5,6 +5,7 @@ import { toast } from "sonner";
|
||||
import { screenApi } from "@/lib/api/screen";
|
||||
import { DynamicFormApi } from "@/lib/api/dynamicForm";
|
||||
import { ImprovedButtonActionExecutor } from "@/lib/utils/improvedButtonActionExecutor";
|
||||
import { apiClient } from "@/lib/api/client";
|
||||
import type { ExtendedControlContext } from "@/types/control-management";
|
||||
|
||||
/**
|
||||
@@ -663,11 +664,122 @@ export class ButtonActionExecutor {
|
||||
}
|
||||
}
|
||||
|
||||
saveResult = await DynamicFormApi.saveFormData({
|
||||
screenId,
|
||||
tableName,
|
||||
data: dataWithUserInfo,
|
||||
});
|
||||
// 🆕 v3.9: RepeatScreenModal의 외부 테이블 데이터 저장 처리
|
||||
const repeatScreenModalKeys = Object.keys(context.formData).filter((key) =>
|
||||
key.startsWith("_repeatScreenModal_") && key !== "_repeatScreenModal_aggregations"
|
||||
);
|
||||
|
||||
// RepeatScreenModal 데이터가 있으면 해당 테이블에 대한 메인 저장은 건너뜀
|
||||
const repeatScreenModalTables = repeatScreenModalKeys.map((key) => key.replace("_repeatScreenModal_", ""));
|
||||
const shouldSkipMainSave = repeatScreenModalTables.includes(tableName);
|
||||
|
||||
if (shouldSkipMainSave) {
|
||||
console.log(`⏭️ [handleSave] ${tableName} 메인 저장 건너뜀 (RepeatScreenModal에서 처리)`);
|
||||
saveResult = { success: true, message: "RepeatScreenModal에서 처리" };
|
||||
} else {
|
||||
saveResult = await DynamicFormApi.saveFormData({
|
||||
screenId,
|
||||
tableName,
|
||||
data: dataWithUserInfo,
|
||||
});
|
||||
}
|
||||
|
||||
if (repeatScreenModalKeys.length > 0) {
|
||||
console.log("📦 [handleSave] RepeatScreenModal 데이터 저장 시작:", repeatScreenModalKeys);
|
||||
|
||||
// 🆕 formData에서 채번 규칙으로 생성된 값들 추출 (예: shipment_plan_no)
|
||||
const numberingFields: Record<string, any> = {};
|
||||
for (const [fieldKey, value] of Object.entries(context.formData)) {
|
||||
// _numberingRuleId로 끝나는 키가 있으면 해당 필드는 채번 규칙 값
|
||||
if (context.formData[`${fieldKey}_numberingRuleId`]) {
|
||||
numberingFields[fieldKey] = value;
|
||||
}
|
||||
}
|
||||
console.log("📦 [handleSave] 채번 규칙 필드:", numberingFields);
|
||||
|
||||
for (const key of repeatScreenModalKeys) {
|
||||
const targetTable = key.replace("_repeatScreenModal_", "");
|
||||
const rows = context.formData[key] as any[];
|
||||
|
||||
if (!Array.isArray(rows) || rows.length === 0) continue;
|
||||
|
||||
console.log(`📦 [handleSave] ${targetTable} 테이블 저장:`, rows);
|
||||
|
||||
for (const row of rows) {
|
||||
const { _isNew, _targetTable, id, ...dataToSave } = row;
|
||||
|
||||
// 사용자 정보 추가 + 채번 규칙 값 병합
|
||||
const dataWithMeta = {
|
||||
...dataToSave,
|
||||
...numberingFields, // 채번 규칙 값 (shipment_plan_no 등)
|
||||
created_by: context.userId,
|
||||
updated_by: context.userId,
|
||||
company_code: context.companyCode,
|
||||
};
|
||||
|
||||
try {
|
||||
if (_isNew) {
|
||||
// INSERT
|
||||
console.log(`📝 [handleSave] ${targetTable} INSERT:`, dataWithMeta);
|
||||
const insertResult = await apiClient.post(
|
||||
`/table-management/tables/${targetTable}/add`,
|
||||
dataWithMeta
|
||||
);
|
||||
console.log(`✅ [handleSave] ${targetTable} INSERT 완료:`, insertResult.data);
|
||||
} else if (id) {
|
||||
// UPDATE
|
||||
const originalData = { id };
|
||||
const updatedData = { ...dataWithMeta, id };
|
||||
console.log(`📝 [handleSave] ${targetTable} UPDATE:`, { originalData, updatedData });
|
||||
const updateResult = await apiClient.put(
|
||||
`/table-management/tables/${targetTable}/edit`,
|
||||
{ originalData, updatedData }
|
||||
);
|
||||
console.log(`✅ [handleSave] ${targetTable} UPDATE 완료:`, updateResult.data);
|
||||
}
|
||||
} catch (error: any) {
|
||||
console.error(`❌ [handleSave] ${targetTable} 저장 실패:`, error.response?.data || error.message);
|
||||
// 개별 실패는 전체 저장을 중단하지 않음
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 🆕 v3.9: RepeatScreenModal 집계 저장 처리
|
||||
const aggregationConfigs = context.formData._repeatScreenModal_aggregations as Array<{
|
||||
resultField: string;
|
||||
aggregatedValue: number;
|
||||
targetTable: string;
|
||||
targetColumn: string;
|
||||
joinKey: { sourceField: string; targetField: string };
|
||||
sourceValue: any;
|
||||
}>;
|
||||
|
||||
if (aggregationConfigs && aggregationConfigs.length > 0) {
|
||||
console.log("📊 [handleSave] 집계 저장 시작:", aggregationConfigs);
|
||||
|
||||
for (const config of aggregationConfigs) {
|
||||
const { targetTable, targetColumn, joinKey, aggregatedValue, sourceValue } = config;
|
||||
|
||||
try {
|
||||
const originalData = { [joinKey.targetField]: sourceValue };
|
||||
const updatedData = {
|
||||
[targetColumn]: aggregatedValue,
|
||||
[joinKey.targetField]: sourceValue,
|
||||
};
|
||||
|
||||
console.log(`📊 [handleSave] ${targetTable}.${targetColumn} = ${aggregatedValue} (조인: ${joinKey.sourceField} = ${sourceValue})`);
|
||||
|
||||
const updateResult = await apiClient.put(
|
||||
`/table-management/tables/${targetTable}/edit`,
|
||||
{ originalData, updatedData }
|
||||
);
|
||||
console.log(`✅ [handleSave] ${targetTable} 집계 저장 완료:`, updateResult.data);
|
||||
} catch (error: any) {
|
||||
console.error(`❌ [handleSave] ${targetTable} 집계 저장 실패:`, error.response?.data || error.message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!saveResult.success) {
|
||||
|
||||
Reference in New Issue
Block a user