|
| 1 | +package com.loopers.batch.job.ranking.processor; |
| 2 | + |
| 3 | +import com.loopers.batch.domain.ranking.WeeklyRanking; |
| 4 | +import com.loopers.dto.RankedProduct; |
| 5 | +import lombok.extern.slf4j.Slf4j; |
| 6 | +import org.springframework.batch.core.configuration.annotation.StepScope; |
| 7 | +import org.springframework.batch.item.ItemProcessor; |
| 8 | +import org.springframework.beans.factory.annotation.Value; |
| 9 | +import org.springframework.stereotype.Component; |
| 10 | + |
| 11 | +import java.time.DayOfWeek; |
| 12 | +import java.time.LocalDate; |
| 13 | +import java.time.format.DateTimeFormatter; |
| 14 | +import java.util.concurrent.atomic.AtomicInteger; |
| 15 | + |
| 16 | +@Slf4j |
| 17 | +@StepScope |
| 18 | +@Component |
| 19 | +public class WeeklyRankingProcessor implements ItemProcessor<RankedProduct, WeeklyRanking> { |
| 20 | + |
| 21 | + @Value("#{jobParameters['startDate']}") |
| 22 | + private String startDateStr; |
| 23 | + |
| 24 | + @Value("#{jobParameters['endDate']}") |
| 25 | + private String endDateStr; |
| 26 | + |
| 27 | + private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyyMMdd"); |
| 28 | + |
| 29 | + private final AtomicInteger rankCounter = new AtomicInteger(0); |
| 30 | + |
| 31 | + @Override |
| 32 | + public WeeklyRanking process(RankedProduct item) { |
| 33 | + LocalDate weekStart = parseStartDate(); |
| 34 | + LocalDate weekEnd = parseEndDate(); |
| 35 | + |
| 36 | + int rank = rankCounter.incrementAndGet(); |
| 37 | + |
| 38 | + return WeeklyRanking.create( |
| 39 | + rank, |
| 40 | + item.productId(), |
| 41 | + item.score(), |
| 42 | + weekStart, |
| 43 | + weekEnd |
| 44 | + ); |
| 45 | + } |
| 46 | + |
| 47 | + private LocalDate parseStartDate() { |
| 48 | + if (startDateStr != null && !startDateStr.isBlank()) { |
| 49 | + return LocalDate.parse(startDateStr, DATE_FORMATTER); |
| 50 | + } |
| 51 | + return LocalDate.now().with(DayOfWeek.MONDAY); |
| 52 | + } |
| 53 | + |
| 54 | + private LocalDate parseEndDate() { |
| 55 | + if (endDateStr != null && !endDateStr.isBlank()) { |
| 56 | + return LocalDate.parse(endDateStr, DATE_FORMATTER); |
| 57 | + } |
| 58 | + return LocalDate.now().with(DayOfWeek.SUNDAY); |
| 59 | + } |
| 60 | +} |
0 commit comments