diff --git a/src/hooks/floorPlan/estimate/useEstimateController.js b/src/hooks/floorPlan/estimate/useEstimateController.js index 3f7120d8..264b9732 100644 --- a/src/hooks/floorPlan/estimate/useEstimateController.js +++ b/src/hooks/floorPlan/estimate/useEstimateController.js @@ -10,6 +10,8 @@ import { useRouter, useSearchParams } from 'next/navigation' import { FloorPlanContext } from '@/app/floor-plan/FloorPlanProvider' import { QcastContext } from '@/app/QcastProvider' import { useSwal } from '@/hooks/useSwal' +import { POLYGON_TYPE } from '@/common/common' +import { logger } from '@/util/logger' // Constants const ESTIMATE_API_ENDPOINT = '/api/estimate' // API 엔드포인트 정의 @@ -33,7 +35,7 @@ export const useEstimateController = (planNo, flag) => { const { getMessage } = useMessage() - const { promiseGet, post, promisePost } = useAxios(globalLocaleState) + const { promiseGet, post, promisePost, get } = useAxios(globalLocaleState) const [isLoading, setIsLoading] = useState(false) const { estimateContextState, setEstimateContextState } = useContext(FloorPlanContext) @@ -386,6 +388,36 @@ export const useEstimateController = (planNo, flag) => { } } + // 견적 상세 저장 후 저장된 plan JSON 에서 회로 payload 를 만들어 CONN_VOL_W(접속용량) 백필을 요청한다. + const backfillConnVol = async (objectNo, planNo) => { + try { + const list = await get({ url: `/api/canvas-management/canvas-statuses/by-object/${objectNo}` }) + const target = (list || []).find((it) => String(it.planNo) === String(planNo)) || (list || [])[0] + const raw = target?.canvasStatus?.replace(/##/g, '"').replace(/∠/g, '∠').replace(/°/g, '°') + const objects = raw ? JSON.parse(raw).objects || [] : [] + + const moduleObjs = objects.filter((o) => o.name === POLYGON_TYPE.MODULE) + const moduleList = moduleObjs.map((m) => ({ circuit: m.circuitNumber, specification: m.moduleInfo?.wpOut })) + if (moduleList.length === 0) return + + // PCS 유닛 번호 = circuit 첫 숫자. distinct 개수 = 유닛 수, 오름차순 = 유닛 index + const unitNumbers = [...new Set(moduleObjs.map((m) => parseInt(String(m.circuitNumber ?? '').match(/\d+/)?.[0], 10)).filter((n) => n > 0))].sort( + (a, b) => a - b + ) + // PCS 데이터는 surfaces[0] 에만 실려 있다. + const surface0 = objects.find((o) => o.name === POLYGON_TYPE.MODULE_SETUP_SURFACE) + const pcsList = (surface0?.pcses || []).filter((pcs) => pcs?.pcsItemId) + const circuitItemList = unitNumbers.map((unit) => ({ itemId: pcsList[unit - 1]?.pcsItemId })) + + await promisePost({ + url: `${ESTIMATE_API_ENDPOINT}/update-conn-vol`, + data: { objectNo, planNo, circuitItemList, roofSurfaceList: [{ moduleList }] }, + }) + } catch (e) { + logger.log('[UPDATE-CONN-VOL] 백필 호출 실패', e?.message) + } + } + const realSave = async (fileList) => { setEstimateContextState({pricingFlag: false}) @@ -451,9 +483,11 @@ export const useEstimateController = (planNo, flag) => { //2. 상세데이터 저장 try { setIsGlobalLoading(true) - await promisePost({ url: `${ESTIMATE_API_ENDPOINT}/save-estimate`, data: estimateData }).then((res) => { + await promisePost({ url: `${ESTIMATE_API_ENDPOINT}/save-estimate`, data: estimateData }).then(async (res) => { if (res.status === 201) { estimateData.newFileList = [] + // 인증용량이 CONN_VOL_W 로 계산되므로, /detail 재조회(fetchSetting) 전에 백필을 먼저 끝낸다. + await backfillConnVol(estimateData.objectNo, estimateData.planNo) swalFire({ text: getMessage('estimate.detail.save.alertMsg'), type: 'alert' }) fetchSetting(estimateData.objectNo, estimateData.planNo) }