Skip to content

Commit 74ab5cb

Browse files
authored
Merge pull request #178 from HelloWorld-AICC/feat/#177
[feat/#177] Create Center Status Batch
2 parents bc38eab + 0aa598c commit 74ab5cb

4 files changed

Lines changed: 26 additions & 1 deletion

File tree

src/main/java/com/example/helloworldmvc/HelloworldMvcApplication.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
import org.springframework.boot.autoconfigure.SpringBootApplication;
55
import org.springframework.cloud.openfeign.EnableFeignClients;
66
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
7+
import org.springframework.scheduling.annotation.EnableScheduling;
78

89
@SpringBootApplication
910
@EnableJpaAuditing
1011
@EnableFeignClients
12+
@EnableScheduling
1113
public class HelloworldMvcApplication {
1214

1315
public static void main(String[] args) {

src/main/java/com/example/helloworldmvc/domain/Center.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,7 @@ public class Center {
5353
@OneToOne(mappedBy = "center", cascade = CascadeType.ALL)
5454
private File file;
5555

56+
public void setStatus(CenterStatus status) {
57+
this.status = status;
58+
}
5659
}

src/main/java/com/example/helloworldmvc/service/CenterService.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,6 @@ public interface CenterService {
1717
Center getCenter(String userId, Long centerId);
1818

1919
Page<Center> getCenterListByDistance(double latitude, double longitude, Integer page, Integer size);
20+
21+
public void updateCenterStatus();
2022
}

src/main/java/com/example/helloworldmvc/service/CenterServiceImpl.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,19 @@
77
import com.example.helloworldmvc.domain.Counselor;
88
import com.example.helloworldmvc.domain.Language;
99
import com.example.helloworldmvc.domain.User;
10+
import com.example.helloworldmvc.domain.enums.CenterStatus;
1011
import com.example.helloworldmvc.domain.mapping.UserLanguage;
1112
import com.example.helloworldmvc.repository.*;
1213
import com.example.helloworldmvc.web.dto.CenterRequestDTO;
1314
import jakarta.transaction.Transactional;
1415
import lombok.RequiredArgsConstructor;
1516
import org.springframework.data.domain.Page;
1617
import org.springframework.data.domain.PageRequest;
18+
import org.springframework.scheduling.annotation.Scheduled;
1719
import org.springframework.stereotype.Service;
1820

21+
import java.time.LocalDateTime;
22+
import java.time.LocalTime;
1923
import java.util.List;
2024
import java.util.stream.Collectors;
2125

@@ -35,7 +39,6 @@ public Page<Center> getCenterListByDistance(double latitude, double longitude, I
3539
return centerRepository.findAllOrderByDistance(latitude, longitude, PageRequest.of(page, size));
3640
}
3741

38-
3942
@Override
4043
public Page<Counselor> getCounselorList(String userId, Long centerId, Integer page, Integer size) {
4144
User user = userRepository.findByEmail(userId).orElseThrow(() -> new GeneralException(ErrorStatus.USER_NOT_FOUND));
@@ -64,4 +67,19 @@ public Center getCenter(String userId, Long centerId) {
6467
userRepository.findByEmail(userId).orElseThrow(() -> new GeneralException(ErrorStatus.USER_NOT_FOUND));
6568
return centerRepository.findById(centerId).orElseThrow(() -> new GeneralException(ErrorStatus.CENTER_NOT_FOUND));
6669
}
70+
71+
@Override
72+
/* 2시간마다 스케쥴링 */
73+
@Scheduled(cron = "0 0 0/2 * * ?", zone = "Asia/Seoul")
74+
public void updateCenterStatus() {
75+
List<Center> centerList = centerRepository.findAll();
76+
LocalTime now = LocalTime.now();
77+
centerList.stream().map(center -> {
78+
if(now.isAfter(center.getOpened()) && now.isBefore(center.getClosed()) ){
79+
center.setStatus(CenterStatus.OPEN);
80+
}
81+
center.setStatus(CenterStatus.CLOSED);
82+
return centerRepository.save(center);
83+
});
84+
}
6785
}

0 commit comments

Comments
 (0)