From d2bd623d9a2431f8d4045a143eb06cf626864f17 Mon Sep 17 00:00:00 2001 From: leeheejin Date: Fri, 5 Dec 2025 11:59:11 +0900 Subject: [PATCH] =?UTF-8?q?=EA=B3=B5=EC=B0=A8/=EC=9A=B4=ED=96=89=20~=20?= =?UTF-8?q?=EC=9A=B4=ED=96=89=EC=95=8C=EB=A6=BC=EA=B9=8C=EC=A7=80=20?= =?UTF-8?q?=EA=B1=B8=EB=A6=B0=20=EA=B1=B0=EB=A6=AC,=20=EC=8B=9C=EA=B0=84?= =?UTF-8?q?=20=EA=B8=B0=EB=A1=9D=ED=95=98=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/lib/utils/buttonActions.ts | 108 ++++++++++++++++++++++++---- 1 file changed, 95 insertions(+), 13 deletions(-) diff --git a/frontend/lib/utils/buttonActions.ts b/frontend/lib/utils/buttonActions.ts index b5ebdc12..7c1545d8 100644 --- a/frontend/lib/utils/buttonActions.ts +++ b/frontend/lib/utils/buttonActions.ts @@ -3619,9 +3619,88 @@ export class ButtonActionExecutor { const tripStats = await this.calculateTripStats(tripId); console.log("πŸ“Š μš΄ν–‰ 톡계:", tripStats); - // μš΄ν–‰ 톡계λ₯Ό vehicle_location_history의 λ§ˆμ§€λ§‰ λ ˆμ½”λ“œμ— μ—…λ°μ΄νŠΈν•˜κ±°λ‚˜ - // 별도 ν…Œμ΄λΈ”μ— μ €μž₯ν•  수 있음 + // μš΄ν–‰ 톡계λ₯Ό 두 ν…Œμ΄λΈ”μ— μ €μž₯ if (tripStats) { + const distanceMeters = Math.round(tripStats.totalDistanceKm * 1000); // km β†’ m + const timeMinutes = tripStats.totalTimeMinutes; + const userId = this.trackingUserId || context.userId; + + console.log("πŸ’Ύ μš΄ν–‰ 톡계 DB μ €μž₯ μ‹œλ„:", { + tripId, + userId, + distanceMeters, + timeMinutes, + startTime: tripStats.startTime, + endTime: tripStats.endTime, + }); + + const { apiClient } = await import("@/lib/api/client"); + + // 1️⃣ vehicle_location_history λ§ˆμ§€λ§‰ λ ˆμ½”λ“œμ— 톡계 μ €μž₯ (이λ ₯용) + try { + const lastRecordResponse = await apiClient.post(`/table-management/tables/vehicle_location_history/data`, { + page: 1, + size: 1, + search: { trip_id: tripId }, + sortBy: "recorded_at", + sortOrder: "desc", + autoFilter: true, + }); + + const lastRecordData = lastRecordResponse.data?.data?.data || lastRecordResponse.data?.data?.rows || []; + if (lastRecordData.length > 0) { + const lastRecordId = lastRecordData[0].id; + console.log("πŸ“ λ§ˆμ§€λ§‰ λ ˆμ½”λ“œ ID:", lastRecordId); + + const historyUpdates = [ + { field: "trip_distance", value: distanceMeters }, + { field: "trip_time", value: timeMinutes }, + { field: "trip_start", value: tripStats.startTime }, + { field: "trip_end", value: tripStats.endTime }, + ]; + + for (const update of historyUpdates) { + await apiClient.put(`/dynamic-form/update-field`, { + tableName: "vehicle_location_history", + keyField: "id", + keyValue: lastRecordId, + updateField: update.field, + updateValue: update.value, + }); + } + console.log("βœ… vehicle_location_history 톡계 μ €μž₯ μ™„λ£Œ"); + } else { + console.warn("⚠️ trip_id에 ν•΄λ‹Ήν•˜λŠ” λ ˆμ½”λ“œλ₯Ό 찾을 수 μ—†μŒ:", tripId); + } + } catch (historyError) { + console.warn("⚠️ vehicle_location_history μ €μž₯ μ‹€νŒ¨:", historyError); + } + + // 2️⃣ vehicles ν…Œμ΄λΈ”μ—λ„ λ§ˆμ§€λ§‰ μš΄ν–‰ 톡계 μ—…λ°μ΄νŠΈ (μ΅œμ‹  μ •λ³΄μš©) + if (userId) { + try { + const vehicleUpdates = [ + { field: "last_trip_distance", value: distanceMeters }, + { field: "last_trip_time", value: timeMinutes }, + { field: "last_trip_start", value: tripStats.startTime }, + { field: "last_trip_end", value: tripStats.endTime }, + ]; + + for (const update of vehicleUpdates) { + await apiClient.put(`/dynamic-form/update-field`, { + tableName: "vehicles", + keyField: "user_id", + keyValue: userId, + updateField: update.field, + updateValue: update.value, + }); + } + console.log("βœ… vehicles ν…Œμ΄λΈ” 톡계 μ—…λ°μ΄νŠΈ μ™„λ£Œ"); + } catch (vehicleError) { + console.warn("⚠️ vehicles ν…Œμ΄λΈ” μ €μž₯ μ‹€νŒ¨:", vehicleError); + } + } + // 이벀트둜 톡계 전달 (UIμ—μ„œ ν‘œμ‹œμš©) window.dispatchEvent(new CustomEvent("tripCompleted", { detail: { @@ -3699,28 +3778,31 @@ export class ButtonActionExecutor { endTime: string | null; } | null> { try { - // vehicle_location_historyμ—μ„œ ν•΄λ‹Ή trip의 λͺ¨λ“  μœ„μΉ˜ 쑰회 (직접 fetch μ‚¬μš©) - const token = typeof window !== "undefined" ? localStorage.getItem("authToken") || "" : ""; + // vehicle_location_historyμ—μ„œ ν•΄λ‹Ή trip의 λͺ¨λ“  μœ„μΉ˜ 쑰회 + const { apiClient } = await import("@/lib/api/client"); - const response = await fetch(`/api/dynamic-form/list/vehicle_location_history?trip_id=${encodeURIComponent(tripId)}&pageSize=10000&sortBy=recorded_at&sortOrder=asc`, { - headers: { - Authorization: `Bearer ${token}`, - }, + const response = await apiClient.post(`/table-management/tables/vehicle_location_history/data`, { + page: 1, + size: 10000, + search: { trip_id: tripId }, + sortBy: "recorded_at", + sortOrder: "asc", }); - if (!response.ok) { - console.log("πŸ“Š 톡계 계산: API 응닡 μ‹€νŒ¨", response.status); + if (!response.data?.success) { + console.log("πŸ“Š 톡계 계산: API 응닡 μ‹€νŒ¨"); return null; } - const result = await response.json(); + // 응닡 ν˜•μ‹: data.data.data λ˜λŠ” data.data.rows + const rows = response.data?.data?.data || response.data?.data?.rows || []; - if (!result.success || !result.data?.rows?.length) { + if (!rows.length) { console.log("πŸ“Š 톡계 계산: 데이터 μ—†μŒ"); return null; } - const locations = result.data.rows; + const locations = rows; console.log(`πŸ“Š 톡계 계산: ${locations.length}개 μœ„μΉ˜ 데이터`); // μ‹œκ°„ 계산