diff --git a/cs25-batch/src/main/java/com/example/cs25batch/batch/service/TodayQuizService.java b/cs25-batch/src/main/java/com/example/cs25batch/batch/service/TodayQuizService.java index 6b2057f8..7ded3640 100644 --- a/cs25-batch/src/main/java/com/example/cs25batch/batch/service/TodayQuizService.java +++ b/cs25-batch/src/main/java/com/example/cs25batch/batch/service/TodayQuizService.java @@ -34,7 +34,7 @@ public class TodayQuizService { private final MailLogRepository mailLogRepository; private final SesMailService mailService; - @Transactional + @Transactional(readOnly = true) public Quiz getTodayQuizBySubscription(Subscription subscription) { // 1. 구독자 정보 및 카테고리 조회 Long parentCategoryId = subscription.getCategory().getId(); // 대분류 ID @@ -55,6 +55,10 @@ public Quiz getTodayQuizBySubscription(Subscription subscription) { ? QuizFormatType.SUBJECTIVE : QuizFormatType.MULTIPLE_CHOICE; + QuizFormatType alterType = isEssayDay + ? QuizFormatType.MULTIPLE_CHOICE + : QuizFormatType.SUBJECTIVE; + // 3. 정답률 기반 난이도 바운더리 설정 List allowedDifficulties = getAllowedDifficulties(accuracy); @@ -64,7 +68,7 @@ public Quiz getTodayQuizBySubscription(Subscription subscription) { // 7. 필터링 조건으로 문제 조회(대분류, 난이도, 내가푼문제 제외, 제외할 카테고리 제외하고, 문제 타입 전부 조건으로) - Quiz todayQuiz = quizRepository.findAvailableQuizzesUnderParentCategory( + /* Quiz todayQuiz = quizRepository.findAvailableQuizzesUnderParentCategory( parentCategoryId, allowedDifficulties, sentQuizIds, @@ -82,8 +86,17 @@ public Quiz getTodayQuizBySubscription(Subscription subscription) { targetType, 0 ); - } + }*/ + + int[] offsetsToTry = (offset > 0) ? new int[]{offset, 0} : new int[]{0}; + QuizFormatType[] typesToTry = new QuizFormatType[]{targetType, alterType}; + + Quiz todayQuiz = tryFindQuiz( + parentCategoryId, allowedDifficulties, sentQuizIds, typesToTry, offsetsToTry + ); + if (todayQuiz == null) { + log.info("subscription_id {} - 문제 출제 실패, targetType {}", subscriptionId, targetType); throw new QuizException(QuizExceptionCode.QUIZ_VALIDATION_FAILED_ERROR); } @@ -103,6 +116,22 @@ private List getAllowedDifficulties(double accuracy) { } } + private Quiz tryFindQuiz(Long parentCategoryId, + List difficulties, + Set sentQuizIds, + QuizFormatType[] types, + int[] offsets) { + for (QuizFormatType type : types) { + for (int off : offsets) { + Quiz q = quizRepository.findAvailableQuizzesUnderParentCategory( + parentCategoryId, difficulties, sentQuizIds, type, off + ); + if (q != null) return q; + } + } + return null; + } + // private double calculateAccuracy(List answers) { // if (answers.isEmpty()) { // return 100.0;