From 0aa598ce626f35d8026c3dcb555c3fbee7a6f6d5 Mon Sep 17 00:00:00 2001 From: ls-rain Date: Tue, 4 Nov 2025 21:50:56 +0900 Subject: [PATCH] [feat/#177] Create Center Status Batch --- .../HelloworldMvcApplication.java | 2 ++ .../example/helloworldmvc/domain/Center.java | 3 +++ .../helloworldmvc/service/CenterService.java | 2 ++ .../service/CenterServiceImpl.java | 20 ++++++++++++++++++- 4 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/example/helloworldmvc/HelloworldMvcApplication.java b/src/main/java/com/example/helloworldmvc/HelloworldMvcApplication.java index 6166e6c..48c1b80 100644 --- a/src/main/java/com/example/helloworldmvc/HelloworldMvcApplication.java +++ b/src/main/java/com/example/helloworldmvc/HelloworldMvcApplication.java @@ -4,10 +4,12 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.data.jpa.repository.config.EnableJpaAuditing; +import org.springframework.scheduling.annotation.EnableScheduling; @SpringBootApplication @EnableJpaAuditing @EnableFeignClients +@EnableScheduling public class HelloworldMvcApplication { public static void main(String[] args) { diff --git a/src/main/java/com/example/helloworldmvc/domain/Center.java b/src/main/java/com/example/helloworldmvc/domain/Center.java index bb20294..13025b9 100644 --- a/src/main/java/com/example/helloworldmvc/domain/Center.java +++ b/src/main/java/com/example/helloworldmvc/domain/Center.java @@ -53,4 +53,7 @@ public class Center { @OneToOne(mappedBy = "center", cascade = CascadeType.ALL) private File file; + public void setStatus(CenterStatus status) { + this.status = status; + } } diff --git a/src/main/java/com/example/helloworldmvc/service/CenterService.java b/src/main/java/com/example/helloworldmvc/service/CenterService.java index d318792..270e307 100644 --- a/src/main/java/com/example/helloworldmvc/service/CenterService.java +++ b/src/main/java/com/example/helloworldmvc/service/CenterService.java @@ -17,4 +17,6 @@ public interface CenterService { Center getCenter(String userId, Long centerId); Page
getCenterListByDistance(double latitude, double longitude, Integer page, Integer size); + + public void updateCenterStatus(); } diff --git a/src/main/java/com/example/helloworldmvc/service/CenterServiceImpl.java b/src/main/java/com/example/helloworldmvc/service/CenterServiceImpl.java index 5880cc6..5dd8a92 100644 --- a/src/main/java/com/example/helloworldmvc/service/CenterServiceImpl.java +++ b/src/main/java/com/example/helloworldmvc/service/CenterServiceImpl.java @@ -7,6 +7,7 @@ import com.example.helloworldmvc.domain.Counselor; import com.example.helloworldmvc.domain.Language; import com.example.helloworldmvc.domain.User; +import com.example.helloworldmvc.domain.enums.CenterStatus; import com.example.helloworldmvc.domain.mapping.UserLanguage; import com.example.helloworldmvc.repository.*; import com.example.helloworldmvc.web.dto.CenterRequestDTO; @@ -14,8 +15,11 @@ import lombok.RequiredArgsConstructor; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; +import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; +import java.time.LocalDateTime; +import java.time.LocalTime; import java.util.List; import java.util.stream.Collectors; @@ -35,7 +39,6 @@ public Page
getCenterListByDistance(double latitude, double longitude, I return centerRepository.findAllOrderByDistance(latitude, longitude, PageRequest.of(page, size)); } - @Override public Page getCounselorList(String userId, Long centerId, Integer page, Integer size) { User user = userRepository.findByEmail(userId).orElseThrow(() -> new GeneralException(ErrorStatus.USER_NOT_FOUND)); @@ -64,4 +67,19 @@ public Center getCenter(String userId, Long centerId) { userRepository.findByEmail(userId).orElseThrow(() -> new GeneralException(ErrorStatus.USER_NOT_FOUND)); return centerRepository.findById(centerId).orElseThrow(() -> new GeneralException(ErrorStatus.CENTER_NOT_FOUND)); } + + @Override + /* 2시간마다 스케쥴링 */ + @Scheduled(cron = "0 0 0/2 * * ?", zone = "Asia/Seoul") + public void updateCenterStatus() { + List
centerList = centerRepository.findAll(); + LocalTime now = LocalTime.now(); + centerList.stream().map(center -> { + if(now.isAfter(center.getOpened()) && now.isBefore(center.getClosed()) ){ + center.setStatus(CenterStatus.OPEN); + } + center.setStatus(CenterStatus.CLOSED); + return centerRepository.save(center); + }); + } }