diff --git a/batch/src/main/kotlin/com/few/batch/service/article/writer/WorkBookSubscriberWriter.kt b/batch/src/main/kotlin/com/few/batch/service/article/writer/WorkBookSubscriberWriter.kt index ae6346dd3..e88ac80c1 100644 --- a/batch/src/main/kotlin/com/few/batch/service/article/writer/WorkBookSubscriberWriter.kt +++ b/batch/src/main/kotlin/com/few/batch/service/article/writer/WorkBookSubscriberWriter.kt @@ -11,6 +11,7 @@ import com.few.email.service.article.dto.Content import com.few.email.service.article.dto.SendArticleEmailArgs import jooq.jooq_dsl.tables.* import org.jooq.DSLContext +import org.jooq.UpdateConditionStep import org.jooq.impl.DSL import org.springframework.stereotype.Component import org.springframework.transaction.annotation.Transactional @@ -201,24 +202,32 @@ class WorkBookSubscriberWriter( memberSuccessRecords[it.memberId] == true } - val successMemberIds = memberSuccessRecords.filter { it.value }.keys /** 이메일 전송에 성공한 구독자들의 진행률을 업데이트한다.*/ - dslContext.update(subscriptionT) - .set(subscriptionT.PROGRESS, subscriptionT.PROGRESS.add(1)) - .where(subscriptionT.MEMBER_ID.`in`(successMemberIds)) - .and(subscriptionT.TARGET_WORKBOOK_ID.`in`(targetWorkBookIds)) - .execute() + val successMemberIds = memberSuccessRecords.filter { it.value }.keys + val updateTargetMemberRecords = items.filter { it.memberId in successMemberIds } + val updateQueries = mutableListOf>() + for (updateTargetMemberRecord in updateTargetMemberRecords) { + updateQueries.add( + dslContext.update(subscriptionT) + .set(subscriptionT.PROGRESS, updateTargetMemberRecord.progress + 1) + .where(subscriptionT.MEMBER_ID.eq(updateTargetMemberRecord.memberId)) + .and(subscriptionT.TARGET_WORKBOOK_ID.eq(updateTargetMemberRecord.targetWorkBookId)) + ) + } + dslContext.batch(updateQueries).execute() /** 마지막 학습지를 받은 구독자들은 구독을 해지한다.*/ - // todo refactoring to batch update + val receiveLastDayQueries = mutableListOf>() for (receiveLastDayMember in receiveLastDayMembers) { - dslContext.update(subscriptionT) - .set(subscriptionT.DELETED_AT, LocalDateTime.now()) - .set(subscriptionT.UNSUBS_OPINION, "receive.all") - .where(subscriptionT.MEMBER_ID.eq(receiveLastDayMember.memberId)) - .and(subscriptionT.TARGET_WORKBOOK_ID.eq(receiveLastDayMember.targetWorkBookId)) - .execute() + receiveLastDayQueries.add( + dslContext.update(subscriptionT) + .set(subscriptionT.DELETED_AT, LocalDateTime.now()) + .set(subscriptionT.UNSUBS_OPINION, "receive.all") + .where(subscriptionT.MEMBER_ID.eq(receiveLastDayMember.memberId)) + .and(subscriptionT.TARGET_WORKBOOK_ID.eq(receiveLastDayMember.targetWorkBookId)) + ) } + dslContext.batch(receiveLastDayQueries).execute() return if (failRecords.isNotEmpty()) { mapOf("records" to memberSuccessRecords, "fail" to failRecords)