Skip to content

Commit

Permalink
Merge pull request #32 from softeerbootcamp4th/fix/arrival-tests
Browse files Browse the repository at this point in the history
[Fix] 선착순 플래그 변수 및 테스트코드 수정
  • Loading branch information
eckrin authored Aug 26, 2024
2 parents 1ed33db + 43f708b commit 3c3f391
Show file tree
Hide file tree
Showing 11 changed files with 226 additions and 129 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,19 @@ public interface ArrivalEventReleaseService {

CompletableFuture<ArrivalApplicationResponseDto> applyEvent(AuthInfo authInfo);

public LocalDateTime getStartTime();
void setMaxArrival(int val);

void setStartTime(LocalDateTime val);

void setStartDate(Boolean val);

int getMaxArrival();

void setCheckFlag(boolean flag);

LocalDateTime getStartTime();

LocalDateTime getStartTimeStatic();

boolean getStartDate();
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ public class ArrivalEventReleaseServiceJavaImpl implements ArrivalEventReleaseSe

private final ArrivalUserRepository arrivalUserRepository;

private static int MAX_ARRIVAL = 100; // default
private int MAX_ARRIVAL = 100; // default
private boolean CHECK = false;
private static LocalDateTime START_TIME = LocalDateTime.of(LocalDate.now(), LocalTime.of(0, 0));
private static boolean START_DATE = true;
private LocalDateTime START_TIME = LocalDateTime.of(LocalDate.now(), LocalTime.of(13, 0));
private boolean START_DATE = true;

private static AtomicInteger count = new AtomicInteger(1);
private static ConcurrentHashMap<String, Integer> hashMap = new ConcurrentHashMap<>();
Expand Down Expand Up @@ -77,32 +77,36 @@ public CompletableFuture<ArrivalApplicationResponseDto> applyEvent(AuthInfo auth
});
}

public static void setMaxArrival(int val) {
public void setMaxArrival(int val) {
MAX_ARRIVAL = val;
}

public static void setStartTime(LocalDateTime val) {
public void setStartTime(LocalDateTime val) {
START_TIME = val;
}

public static void setStartDate(Boolean val) {
public void setStartDate(Boolean val) {
START_DATE = val;
}

public static int getMaxArrival() {
return MAX_ARRIVAL;
public void setCheckFlag(boolean flag) {
CHECK = flag;
}

public int getMaxArrival() {
return MAX_ARRIVAL;
}

public LocalDateTime getStartTime() {
return START_TIME;
}

public static LocalDateTime getStartTimeStatic(){
public LocalDateTime getStartTimeStatic(){
return START_TIME;
}

public static boolean getStartDate() {
public boolean getStartDate() {
return START_DATE;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ public class ArrivalEventReleaseServiceRedisImpl implements ArrivalEventReleaseS

private final String ARRIVAL_SET = "arrivalset";
private boolean CHECK = false;
private static int MAX_ARRIVAL = 100; // default
private static LocalDateTime START_TIME = LocalDateTime.of(LocalDate.now(), LocalTime.of(0, 0));
private static boolean START_DATE = true;
private int MAX_ARRIVAL = 100; // default
private LocalDateTime START_TIME = LocalDateTime.of(LocalDate.now(), LocalTime.of(13, 0));
private boolean START_DATE = true;

/**
* 비동기로 Redis 호출하는 메서드
Expand Down Expand Up @@ -87,31 +87,35 @@ public CompletableFuture<ArrivalApplicationResponseDto> applyEvent(AuthInfo auth
});
}

public static void setMaxArrival(int val) {
public void setMaxArrival(int val) {
MAX_ARRIVAL = val;
}

public static void setStartTime(LocalDateTime val) {
public void setStartTime(LocalDateTime val) {
START_TIME = val;
}

public static void setStartDate(Boolean val) {
public void setStartDate(Boolean val) {
START_DATE = val;
}

public static int getMaxArrival() {
public void setCheckFlag(boolean flag) {
CHECK = flag;
}

public int getMaxArrival() {
return MAX_ARRIVAL;
}

public LocalDateTime getStartTime() {
return START_TIME;
}

public static LocalDateTime getStartTimeStatic(){
public LocalDateTime getStartTimeStatic(){
return START_TIME;
}

public static boolean getStartDate() {
public boolean getStartDate() {
return START_DATE;
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
package com.softeer.podoarrival.event.service;

import com.softeer.podoarrival.event.exception.EventTypeNotExistsException;
import com.softeer.podoarrival.event.model.dto.ArrivalApplicationResponseDto;
import com.softeer.podoarrival.event.model.entity.Event;
import com.softeer.podoarrival.event.model.entity.EventReward;
import com.softeer.podoarrival.event.model.entity.EventType;
import com.softeer.podoarrival.event.repository.EventRepository;
import com.softeer.podoarrival.event.repository.EventRewardRepository;
import com.softeer.podoarrival.event.repository.EventTypeRepository;
import com.softeer.podoarrival.security.AuthInfo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;

import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;
import java.util.concurrent.CompletableFuture;

@Slf4j
Expand All @@ -17,6 +28,9 @@
public class ArrivalEventService {

private final ArrivalEventReleaseService arrivalEventReleaseServiceRedisImpl;
private final EventTypeRepository eventTypeRepository;
private final EventRewardRepository eventRewardRepository;
private final EventRepository eventRepository;

/**
* 선착순 응모용 service.
Expand All @@ -42,4 +56,52 @@ public Flux<Long> streamLeftSecondsToEventTime() {
return Math.max(seconds, 0);
});
}

@Scheduled(cron = "0 25 03 * * *")
public void setArrivalEventInformation() {
// Check Flag 초기화
arrivalEventReleaseServiceRedisImpl.setCheckFlag(false);

// START DATE, START TIME 초기화
arrivalEventReleaseServiceRedisImpl.setStartDate(true);
arrivalEventReleaseServiceRedisImpl.setStartTime(LocalDateTime.of(LocalDate.now(), LocalTime.of(13, 0)));

// 시작일자, 이벤트 종류만 고려하여 이벤트 추출
LocalDateTime startOfDay = LocalDate.now().atStartOfDay();
LocalDateTime endOfDay = startOfDay.plusDays(1).minusNanos(1);

// 선착순 이벤트
EventType eventType = eventTypeRepository.findById(1L).orElseThrow(() -> new EventTypeNotExistsException("이벤트 타입이 존재하지 않습니다."));
Event findEvent = eventRepository.findFirstByEventTypeAndStartAtBetween(eventType, startOfDay, endOfDay);

if(findEvent == null) {
log.warn("오늘 날짜에 이벤트가 없습니다.");
arrivalEventReleaseServiceRedisImpl.setMaxArrival(0);
return;
}

// 찾은 이벤트에 해당하는 reward개수 조회
int rewardCount = 0;
List<EventReward> eventRewards = eventRewardRepository.findAllByEvent(findEvent);
for (EventReward eventReward : eventRewards) {
rewardCount += eventReward.getNumWinners();
}

// 찾은 이벤트에 해당하는 반복 시간 확인
LocalTime repeatTime = findEvent.getRepeatTime();

// 찾은 이벤트에 해당하는 반복 요일 확인 및 저장
String repeatDate = findEvent.getRepeatDay();
int today = startOfDay.getDayOfWeek().getValue();
if(repeatDate.length() >= 7 && repeatDate.charAt(today - 1) == '1') {
arrivalEventReleaseServiceRedisImpl.setStartDate(true);
}else{
arrivalEventReleaseServiceRedisImpl.setStartDate(false);
}

// service에 이벤트 내용 저장
arrivalEventReleaseServiceRedisImpl.setMaxArrival(rewardCount);

arrivalEventReleaseServiceRedisImpl.setStartTime(LocalDateTime.of(LocalDate.now(), repeatTime));
}
}
Loading

0 comments on commit 3c3f391

Please sign in to comment.