Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package kernel360.trackyweb.dashboard.application;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -89,22 +90,28 @@ public DashboardStatisticsResponse getStatistics(String bizUuid) {
* @return 구역 : 차량 수 map
*/
@Transactional(readOnly = true)
public Map<String, Integer> getGeoData() {
List<GpsHistoryEntity> gpsList = dashGpsHistoryProvider.findLatestGpsByMdn();
public Map<String, List<String>> getGeoData(String bizUuid) {

List<GpsHistoryEntity> gpsList = dashGpsHistoryProvider.findLatestGps(bizUuid);

log.info("gpsList: {} ", gpsList);

Map<String, Integer> provinceCountMap = new HashMap<>();
Map<String, List<String>> provinceCountMap = new HashMap<>();

for (GpsHistoryEntity gps : gpsList) {
// DB에 저장된 위도/경도는 정수형이므로 소수로 변환 필요
double lat = gps.getLat() / 1_000_000.0;
double lon = gps.getLon() / 1_000_000.0;

String province = provinceMatcher.findProvince(lon, lat);
String mdn = gps.getDrive().getCar().getMdn();

provinceCountMap.put(province, provinceCountMap.getOrDefault(province, 0) + 1);
provinceCountMap
.computeIfAbsent(province, k -> new ArrayList<>())
.add(mdn);
}

log.info("처리된 GPS 데이터: {}개", gpsList.size());
log.info("provinceCountMap: {}", provinceCountMap);
return provinceCountMap;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class DashGpsHistoryProvider {

private final DashGpsHistoryRepository dashGpsHistoryRepository;

public List<GpsHistoryEntity> findLatestGpsByMdn() {
return dashGpsHistoryRepository.findLatestGpsByMdn();
public List<GpsHistoryEntity> findLatestGps(String bizUuid) {
return dashGpsHistoryRepository.getLatestGps(bizUuid);
}
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,10 @@
package kernel360.trackyweb.dashboard.infrastructure.repository;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import kernel360.trackycore.core.domain.entity.GpsHistoryEntity;
import kernel360.trackycore.core.infrastructure.repository.GpsHistoryRepository;

@Repository
public interface DashGpsHistoryRepository extends GpsHistoryRepository {

@Query(value = """
SELECT *
FROM (
SELECT g.*, d.mdn,
ROW_NUMBER() OVER (PARTITION BY d.mdn ORDER BY g.created_at DESC) AS rn
FROM gpshistory g
JOIN drive d ON g.drive_id = d.id
) t
WHERE t.rn = 1
""", nativeQuery = true)
List<GpsHistoryEntity> findLatestGpsByMdn();
public interface DashGpsHistoryRepository extends GpsHistoryRepository, DashGpsHistoryRepositoryCustom {

}
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
package kernel360.trackyweb.dashboard.infrastructure.repository;

import static kernel360.trackycore.core.domain.entity.QBizEntity.*;
import static kernel360.trackycore.core.domain.entity.QCarEntity.*;
import static kernel360.trackycore.core.domain.entity.QDriveEntity.*;
import static kernel360.trackycore.core.domain.entity.QGpsHistoryEntity.*;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.querydsl.core.types.dsl.Expressions;
import com.querydsl.jpa.JPAExpressions;
import com.querydsl.jpa.impl.JPAQueryFactory;

import kernel360.trackycore.core.domain.entity.GpsHistoryEntity;
import kernel360.trackycore.core.domain.entity.QDriveEntity;
import kernel360.trackycore.core.domain.entity.QGpsHistoryEntity;
import lombok.RequiredArgsConstructor;

@Repository
Expand All @@ -22,29 +21,32 @@ public class DashGpsHistoryRepositoryCustomImpl implements DashGpsHistoryReposit

private final JPAQueryFactory queryFactory;

/**
* 대시보드 차량 위치 지도 - 업체별 최신 GPS 조회
* @param bizUuid
* @return
*/
@Override
public List<GpsHistoryEntity> getLatestGps(String bizUuid) {
QGpsHistoryEntity gSub = new QGpsHistoryEntity("gSub");
QDriveEntity dSub = new QDriveEntity("dSub");

return queryFactory
.selectFrom(gpsHistoryEntity)
.join(gpsHistoryEntity.drive, driveEntity).fetchJoin()
.select(gpsHistoryEntity)
.from(gpsHistoryEntity)
.join(gpsHistoryEntity.drive, driveEntity)
.where(
Expressions.list(
driveEntity.car.mdn,
gpsHistoryEntity.oTime
).in(
// bizUuid 필터링 + MDN별 최신 drive.id 서브쿼리
gpsHistoryEntity.drive.id.in(
JPAExpressions
.select(
dSub.car.mdn,
gSub.oTime.max()
)
.from(gSub)
.join(gSub.drive, dSub)
.groupBy(dSub.car.mdn)
.select(driveEntity.id.max())
.from(driveEntity)
.join(driveEntity.car, carEntity)
.join(carEntity.biz, bizEntity)
.where(bizEntity.bizUuid.eq(bizUuid))
.groupBy(carEntity.mdn)
)
)
.groupBy(driveEntity.id, driveEntity.car.mdn)
.orderBy(gpsHistoryEntity.driveSeq.desc())
.fetch();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@ public ApiResponse<String> updateStatusToReturn(@PathVariable String rentUuid) {
}

@GetMapping("/geo")
public ApiResponse<Map<String, Integer>> getGeoData() {
Map<String, Integer> geoMap = dashBoardService.getGeoData();
public ApiResponse<Map<String, List<String>>> getGeoData(
@Schema(hidden = true) @AuthenticationPrincipal MemberPrincipal memberPrincipal
) {
Map<String, List<String>> geoMap = dashBoardService.getGeoData(memberPrincipal.bizUuid());
return ApiResponse.success(geoMap);
}
}