공차/운행 ~ 운행알림까지 걸린 거리, 시간 기록하기
This commit is contained in:
parent
ccf8bd3284
commit
d2bd623d9a
|
|
@ -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}개 위치 데이터`);
|
||||
|
||||
// 시간 계산
|
||||
|
|
|
|||
Loading…
Reference in New Issue