diff --git a/src/main/java/com/hsu/shimpyoo/domain/hospital/repository/HospitalVisitRepository.java b/src/main/java/com/hsu/shimpyoo/domain/hospital/repository/HospitalVisitRepository.java index 118966d..2c8104b 100644 --- a/src/main/java/com/hsu/shimpyoo/domain/hospital/repository/HospitalVisitRepository.java +++ b/src/main/java/com/hsu/shimpyoo/domain/hospital/repository/HospitalVisitRepository.java @@ -5,9 +5,16 @@ import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; +import java.time.LocalDateTime; import java.util.List; +import java.util.Optional; @Repository public interface HospitalVisitRepository extends JpaRepository { List findByUserId(User userId); + + + // 사용자 ID로 미래의 방문 일정을 조회하고, 방문 시간을 오름차순으로 정렬하여 가장 빠른 일정 반환 + Optional findFirstByUserIdAndVisitTimeAfterOrderByVisitTimeAsc(User userId, LocalDateTime now); + } diff --git a/src/main/java/com/hsu/shimpyoo/domain/hospital/service/HospitalService.java b/src/main/java/com/hsu/shimpyoo/domain/hospital/service/HospitalService.java index e999cc5..e947d25 100644 --- a/src/main/java/com/hsu/shimpyoo/domain/hospital/service/HospitalService.java +++ b/src/main/java/com/hsu/shimpyoo/domain/hospital/service/HospitalService.java @@ -10,4 +10,5 @@ public interface HospitalService { ResponseEntity> setVisitHospital(HospitalVisitSetRequestDto hospitalVisitSetRequestDto); ResponseEntity> getAllHospitalVisit(); ResponseEntity> getOneHospitalVisit(Long hospitalVisitId); + ResponseEntity> getTimeLeftHospitalVisit(); } diff --git a/src/main/java/com/hsu/shimpyoo/domain/hospital/service/HospitalServiceImpl.java b/src/main/java/com/hsu/shimpyoo/domain/hospital/service/HospitalServiceImpl.java index 11e6083..313d794 100644 --- a/src/main/java/com/hsu/shimpyoo/domain/hospital/service/HospitalServiceImpl.java +++ b/src/main/java/com/hsu/shimpyoo/domain/hospital/service/HospitalServiceImpl.java @@ -4,10 +4,7 @@ import com.hsu.shimpyoo.domain.hospital.entity.HospitalVisit; import com.hsu.shimpyoo.domain.hospital.repository.HospitalRepository; import com.hsu.shimpyoo.domain.hospital.repository.HospitalVisitRepository; -import com.hsu.shimpyoo.domain.hospital.web.dto.HospitalSearchRequestDto; -import com.hsu.shimpyoo.domain.hospital.web.dto.HospitalSearchResponseDto; -import com.hsu.shimpyoo.domain.hospital.web.dto.HospitalVisitDto; -import com.hsu.shimpyoo.domain.hospital.web.dto.HospitalVisitSetRequestDto; +import com.hsu.shimpyoo.domain.hospital.web.dto.*; import com.hsu.shimpyoo.domain.user.entity.User; import com.hsu.shimpyoo.domain.user.repository.UserRepository; import com.hsu.shimpyoo.global.response.CustomAPIResponse; @@ -20,6 +17,8 @@ import org.springframework.transaction.annotation.Transactional; import org.springframework.web.server.ResponseStatusException; +import java.time.LocalDateTime; +import java.time.temporal.ChronoUnit; import java.util.List; import java.util.Optional; import java.util.stream.Collectors; @@ -138,5 +137,46 @@ public ResponseEntity> getOneHospitalVisit(Long hospitalVis return ResponseEntity.status(HttpStatus.OK).body(res); } + @Override + public ResponseEntity> getTimeLeftHospitalVisit() { + // 사용자 존재 확인 + Optional isExistUser=userRepository.findByLoginId(SecurityContextHolder.getContext().getAuthentication().getName()); + if(isExistUser.isEmpty()){ + throw new ResponseStatusException(HttpStatus.NOT_FOUND,"존재하지 않는 사용자입니다."); + } + + LocalDateTime now = LocalDateTime.now(); // 현재 시간 + + // 현재보다 미래인 일정 중에서 가장 빠른 방문 일정을 찾음 + Optional firstHospitalVisit = + hospitalVisitRepository.findFirstByUserIdAndVisitTimeAfterOrderByVisitTimeAsc(isExistUser.get(), now); + + // 방문 일정이 없다면, 그에 맞는 응답을 반환 + if(firstHospitalVisit.isEmpty()){ + CustomAPIResponse res=CustomAPIResponse.createSuccess(200, null, "설정한 병원 방문 일정이 없습니다."); + return ResponseEntity.status(HttpStatus.OK).body(res); + } + + // 가장 빠른 방문 시간 + LocalDateTime firstVisitTime=firstHospitalVisit.get().getVisitTime(); + + int leftDay = (int) ChronoUnit.DAYS.between(now, firstVisitTime); + int leftHour = (int) (ChronoUnit.HOURS.between(now, firstVisitTime) % 24); + int leftMinute = (int) (ChronoUnit.MINUTES.between(now, firstVisitTime) % 60); + + + HospitalVisitTimeLeftDto hospitalVisitTimeLeftDto=HospitalVisitTimeLeftDto.builder() + .day(leftDay) + .hour(leftHour) + .minute(leftMinute) + .build(); + + + CustomAPIResponse res=CustomAPIResponse.createSuccess(200, hospitalVisitTimeLeftDto, + "병원 방문까지 남은 시간이 조회되었습니다."); + return ResponseEntity.status(HttpStatus.OK).body(res); + + } + } diff --git a/src/main/java/com/hsu/shimpyoo/domain/hospital/web/controller/HospitalController.java b/src/main/java/com/hsu/shimpyoo/domain/hospital/web/controller/HospitalController.java index 617035a..f997437 100644 --- a/src/main/java/com/hsu/shimpyoo/domain/hospital/web/controller/HospitalController.java +++ b/src/main/java/com/hsu/shimpyoo/domain/hospital/web/controller/HospitalController.java @@ -52,4 +52,10 @@ public ResponseEntity> getOneHospitalVisit(@RequestParam Lo ResponseEntity> result=hospitalService.getOneHospitalVisit(hospitalVisitId); return result; } + + @GetMapping("/getTimeLeft") + public ResponseEntity> getTimeLeft(){ + ResponseEntity> result=hospitalService.getTimeLeftHospitalVisit(); + return result; + } } diff --git a/src/main/java/com/hsu/shimpyoo/domain/hospital/web/dto/HospitalVisitTimeLeftDto.java b/src/main/java/com/hsu/shimpyoo/domain/hospital/web/dto/HospitalVisitTimeLeftDto.java new file mode 100644 index 0000000..d961722 --- /dev/null +++ b/src/main/java/com/hsu/shimpyoo/domain/hospital/web/dto/HospitalVisitTimeLeftDto.java @@ -0,0 +1,20 @@ +package com.hsu.shimpyoo.domain.hospital.web.dto; + +import lombok.*; + +@Getter +@Setter +@AllArgsConstructor +@NoArgsConstructor +@Builder +public class HospitalVisitTimeLeftDto { + // 며칠 남았는지 + private int day; + + // 몇시간 남았는지 + private int hour; + + // 몇분 남았는지 + private int minute; + +}