@@ -5,56 +5,62 @@ import gogo.gogobetting.domain.batch.detail.persistence.BatchDetailRepository
55import gogo.gogobetting.domain.batch.root.event.MatchBatchEvent
66import gogo.gogobetting.domain.batch.root.event.StudentBettingDto
77import gogo.gogobetting.domain.batch.root.persistence.BatchRepository
8- import gogo.gogobetting.domain.betting.result.persistence.BettingResult
9- import gogo.gogobetting.domain.betting.result.persistence.BettingResultRepository
108import gogo.gogobetting.domain.betting.root.persistence.BettingRepository
119import gogo.gogobetting.global.kafka.publisher.BatchPublisher
10+ import gogo.gogobetting.infra.batch.dto.BettingResultJdbcDto
11+ import gogo.gogobetting.infra.batch.dto.BettingRow
1212import org.slf4j.LoggerFactory
1313import org.springframework.batch.core.StepExecution
1414import org.springframework.batch.core.annotation.AfterStep
1515import org.springframework.batch.core.annotation.BeforeStep
1616import org.springframework.batch.core.configuration.annotation.StepScope
1717import org.springframework.batch.item.Chunk
1818import org.springframework.batch.item.ItemWriter
19+ import org.springframework.batch.item.database.JdbcBatchItemWriter
1920import org.springframework.beans.factory.annotation.Value
20- import org.springframework.context.ApplicationEventPublisher
2121import org.springframework.data.repository.findByIdOrNull
2222import org.springframework.stereotype.Component
23- import java.util.*
23+ import java.util.UUID
24+ import kotlin.math.ceil
2425
2526@Component(" springBatchBettingWriter" )
2627@StepScope
27- class BettingWriter (
28- private val bettingResultRepository : BettingResultRepository ,
28+ class BettingResultWriter (
29+ private val jdbcWriter : JdbcBatchItemWriter < BettingResultJdbcDto > ,
2930 private val batchDetailRepository : BatchDetailRepository ,
3031 private val batchRepository : BatchRepository ,
3132 private val bettingRepository : BettingRepository ,
32- private val applicationEventPublisher : ApplicationEventPublisher ,
3333 private val batchPublisher : BatchPublisher ,
34- ) : ItemWriter<BettingResult> {
35-
36- @Value(" #{jobParameters['matchId']}" )
37- private val matchId: Long = 0
38-
39- @Value(" #{jobParameters['winTeamId']}" )
40- private val winTeamId: Long = 0
41-
42- @Value(" #{jobParameters['aTeamScore']}" )
43- private val aTeamScore: Int = 0
44-
45- @Value(" #{jobParameters['bTeamScore']}" )
46- private val bTeamScore: Int = 0
34+ @Value(" #{jobParameters['winTeamId']}" ) private val winTeamId : Long ,
35+ @Value(" #{jobParameters['bettingOdds']}" ) private val bettingOdds : Double ,
36+ @Value(" #{jobParameters['matchId']}" ) private val matchId : Long ,
37+ @Value(" #{jobParameters['aTeamScore']}" ) private val aTeamScore : Int ,
38+ @Value(" #{jobParameters['bTeamScore']}" ) private val bTeamScore : Int
39+ ) : ItemWriter<BettingRow> {
4740
4841 private var batchId: Long = 0
49- private val accumulatedItems = mutableListOf<BettingResult >()
50-
51- private val log = LoggerFactory .getLogger(this ::class .java.simpleName)
42+ private val accumulated = mutableListOf<BettingResultJdbcDto >()
43+ private val log = LoggerFactory .getLogger(this ::class .java)
5244
5345 @BeforeStep
5446 fun beforeStep (stepExecution : StepExecution ) {
55- val jobExecution = stepExecution.jobExecution
56- accumulatedItems.clear()
57- batchId = jobExecution.executionContext[" batchId" ]!! as Long
47+ batchId = stepExecution.jobExecution.executionContext[" batchId" ] as Long
48+ accumulated.clear()
49+ }
50+
51+ override fun write (items : Chunk <out BettingRow >) {
52+ val dtoList = items.map { row ->
53+ val isPredicted = row.predictedWinTeamId == winTeamId
54+ val earnedPoint = if (isPredicted) ceil(row.point * bettingOdds).toLong() + row.point else 0L
55+ BettingResultJdbcDto (
56+ bettingId = row.id,
57+ earnedPoint = earnedPoint,
58+ isPredicted = isPredicted,
59+ )
60+ }
61+
62+ jdbcWriter.write(Chunk (dtoList))
63+ accumulated.addAll(dtoList)
5864 }
5965
6066 @AfterStep
@@ -70,7 +76,7 @@ class BettingWriter(
7076 )
7177 )
7278
73- // val successList = accumulatedItems .filter { it.isPredicted }
79+ // val successList = accumulated .filter { it.isPredicted }
7480// .map {
7581// val studentId = bettingRepository.findByIdOrNull(it.bettingId)!!.studentId
7682// StudentBettingDto(studentId, it.earnedPoint)
@@ -89,9 +95,4 @@ class BettingWriter(
8995// log.info("published betting batch application event: {}", event.id)
9096// batchPublisher.publishBettingBatchEvent(event)
9197 }
92-
93- override fun write (items : Chunk <out BettingResult >) {
94- bettingResultRepository.saveAll(items)
95- accumulatedItems.addAll(items)
96- }
9798}
0 commit comments