Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ public interface EmergencyHistoryRepository extends JpaRepository<EmergencyHisto
select
eh
from EmergencyHistory eh
join NormalUser n
LEFT JOIN eh.user n
where n.id = :normalId
and eh.createdAt >= :firstDay
""")
List<EmergencyHistory> findAllByNormalId(
@Param("normalId") Long normalId,
@Param("firstDay")LocalDateTime firstDay
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Repository
Expand All @@ -19,5 +19,5 @@ public interface SpeechRepository extends JpaRepository<Speech, Long> {
""")
List<Speech> findAllByNormalUserId(
@Param("userId") Long normalId,
@Param("date") LocalDate date);
@Param("date") LocalDateTime date);
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class StaticsServiceImpl implements StaticsService {
@Override
public StaticsRes getNormalUsersStatics(Long normalId) {
// 선택된 normal_user의 PK를 통하여 모든 발화 이력을 조회
List<Speech> pickSpeech = speechRepository.findAllByNormalUserId(normalId, LocalDateTime.now().minusDays(7).toLocalDate());
List<Speech> pickSpeech = speechRepository.findAllByNormalUserId(normalId, LocalDateTime.now().minusDays(7).toLocalDate().atStartOfDay());
LocalDateTime lastDay = LocalDateTime.now().minusDays(6).toLocalDate().atStartOfDay();

// 최근 7일간 사용한 시각을 통한 개수 카운팅
Expand All @@ -50,7 +50,14 @@ public StaticsRes getNormalUsersStatics(Long normalId) {
.toList();

// normal_user가 좋아하는 상위 5개의 즐겨찾기 조회
List<Favorite> top5Favorites = favoriteRepository.findTop5ByNormalUserIdOrderByCountDesc(normalId);
List<StaticsRes.Favorites> top5Favorites = favoriteRepository.findTop5ByNormalUserIdOrderByCountDesc(normalId)
.stream()
.map(f -> new StaticsRes.Favorites(
f.getFavoriteId(),
f.getSentence(),
f.getCount()
))
.toList();

// 사용한 시각과 같은 날짜의 사용 장소를 카운트
Map<String, Long> usedPlace = pickSpeech.stream()
Expand All @@ -68,7 +75,7 @@ public StaticsRes getNormalUsersStatics(Long normalId) {

// 긴급호출 사용 이력을 조회
// 단, 모든 기록을 조회할 시, 쿼리 시간이 길어짐을 생각하여 최근 7일만 조회
List<EmergencyHistory> userEmergencyHistories = ehRepository.findAllByNormalId(LocalDateTime.now());
List<EmergencyHistory> userEmergencyHistories = ehRepository.findAllByNormalId(normalId, LocalDateTime.now());
List<StaticsRes.History> parsedEmergencyHistory = userEmergencyHistories.stream()
.filter(s -> s.getCreatedAt().isAfter(lastDay))
.map(h -> new StaticsRes.History(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@

public record StaticsRes(
List<UsedCount> howManyUsed,
List<Favorite> top5Used,
List<Favorites> top5Used,
Map<String, Long> usedPlace,
Map<String, Long> usedWhen,
List<History> histories
) {
public record Favorites (
Long favoriteId,
String sentence,
Integer count
) {}
public record UsedCount(
String date,
Long value
Expand Down