diff --git a/Event/src/main/java/com/pawith/event/christmas/ChristmasEventCloseHandler.java b/Event/src/main/java/com/pawith/event/christmas/ChristmasEventCloseHandler.java index e56a9dfa..12252d7f 100644 --- a/Event/src/main/java/com/pawith/event/christmas/ChristmasEventCloseHandler.java +++ b/Event/src/main/java/com/pawith/event/christmas/ChristmasEventCloseHandler.java @@ -15,7 +15,7 @@ @Component public class ChristmasEventCloseHandler extends AbstractBatchSchedulingHandler { private static final Integer BATCH_SIZE = 100; - private static final String CRON_EXPRESSION = "0 0 0 26 12 *"; + private static final String CRON_EXPRESSION = "0 0 0 2 1 *"; private final ChristmasCategoryRepository christmasCategoryRepository; private final CategoryRepository categoryRepository; diff --git a/Event/src/main/java/com/pawith/event/christmas/ChristmasEventService.java b/Event/src/main/java/com/pawith/event/christmas/ChristmasEventService.java index 18169eb4..d3106ecc 100644 --- a/Event/src/main/java/com/pawith/event/christmas/ChristmasEventService.java +++ b/Event/src/main/java/com/pawith/event/christmas/ChristmasEventService.java @@ -3,9 +3,13 @@ import com.pawith.event.christmas.entity.ChristmasCategory; import com.pawith.event.christmas.repository.ChristmasCategoryRepository; import com.pawith.tododomain.entity.*; -import com.pawith.tododomain.repository.*; +import com.pawith.tododomain.repository.AssignRepository; +import com.pawith.tododomain.repository.CategoryRepository; +import com.pawith.tododomain.repository.RegisterRepository; +import com.pawith.tododomain.repository.TodoRepository; import com.pawith.tododomain.service.RegisterQueryService; import lombok.RequiredArgsConstructor; +import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; @@ -18,12 +22,8 @@ public class ChristmasEventService { private static final String EVENT_CATEGORY_NAME = "크리스마스"; private static final String EVENT_TODO_DESCRIPTION = "포잇스마스 이벤트 참여하기"; - private static final List EVENT_TODO_SCHEDULED_DATE_LIST = List.of( - LocalDate.of(2023, 12, 23), - LocalDate.of(2023, 12, 24), - LocalDate.of(2023, 12, 25) - ); - + private static final LocalDate EVENT_START_DATE = LocalDate.of(2023, 12, 23); + private static final LocalDate EVENT_END_DATE = LocalDate.of(2024, 1, 2); private static final Long EVENT_CREATOR_ID = -1L; private final CategoryRepository categoryRepository; @@ -47,7 +47,9 @@ public void publishEvent(TodoTeam todoTeam){ final List allRegisters = registerRepository.findAllByTodoTeamId(todoTeam.getId()); final List eventTodoList = new ArrayList<>(); final List eventAssignList = new ArrayList<>(); - EVENT_TODO_SCHEDULED_DATE_LIST + + final List eventDateList = EVENT_START_DATE.datesUntil(EVENT_END_DATE).toList(); + eventDateList .forEach(eventDate -> { final Todo todo = Todo.builder() .category(saved) @@ -83,8 +85,49 @@ public void addNewRegisterAtChristmasEventTodo(Long todoTeamId, Long userId){ }); } + @Scheduled(cron = "0 0 0 26 12 *") + public void addNewTodoAtChristmasEventCategory(){ + final List allChristmasCategory = christmasCategoryRepository.findAll(); + List todoList = new ArrayList<>(); + List assignList = new ArrayList<>(); + allChristmasCategory.forEach(christmasCategory ->{ + final Long categoryId = christmasCategory.getCategoryId(); + categoryRepository.findById(categoryId).ifPresent(category -> { + final List eventDateList = new ArrayList<>(EVENT_START_DATE.datesUntil(EVENT_END_DATE).toList()); + final TodoTeam todoTeam = category.getTodoTeam(); + final List allRegisters = registerRepository.findAllByTodoTeamId(todoTeam.getId()); + todoRepository.findTodoListByCreatorIdAndTodoTeamIdQuery(EVENT_CREATOR_ID, todoTeam.getId()) + .forEach(todo -> { + final LocalDate scheduledDate = todo.getScheduledDate(); + eventDateList.remove(scheduledDate); + }); + + eventDateList.forEach(eventDate -> { + final Todo todo = Todo.builder() + .category(category) + .creatorId(EVENT_CREATOR_ID) + .description(EVENT_TODO_DESCRIPTION) + .scheduledDate(eventDate) + .build(); + todoList.add(todo); + List allAssign = allRegisters.stream() + .filter(Register::isRegistered) + .map(register -> Assign.builder() + .todo(todo) + .register(register) + .build()) + .toList(); + assignList.addAll(allAssign); + }); + }); + }); + todoRepository.saveAll(todoList); + assignRepository.saveAll(assignList); + } + public Boolean isEventDay(){ - return EVENT_TODO_SCHEDULED_DATE_LIST.contains(LocalDate.now()); + final LocalDate now = LocalDate.now(); + return now.isAfter(EVENT_START_DATE) && now.isBefore(EVENT_END_DATE); } }