-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
6 changed files
with
146 additions
and
5 deletions.
There are no files selected for viewing
101 changes: 101 additions & 0 deletions
101
Server/src/main/java/com/server/global/batch/adjustment/AdjustmentJobConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,105 @@ | ||
package com.server.global.batch.adjustment; | ||
|
||
import com.server.domain.adjustment.domain.Adjustment; | ||
import com.server.domain.adjustment.repository.AdjustmentRepository; | ||
import com.server.domain.member.entity.Member; | ||
import com.server.domain.video.entity.Video; | ||
import com.server.module.firmbank.FirmBankService; | ||
import com.server.module.firmbank.response.AdjustmentResult; | ||
import org.springframework.batch.core.Job; | ||
import org.springframework.batch.core.Step; | ||
import org.springframework.batch.core.configuration.annotation.*; | ||
import org.springframework.batch.item.ItemProcessor; | ||
import org.springframework.batch.item.ItemWriter; | ||
import org.springframework.batch.item.database.JpaPagingItemReader; | ||
import org.springframework.batch.item.database.builder.JpaPagingItemReaderBuilder; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import javax.persistence.EntityManagerFactory; | ||
import java.time.LocalDateTime; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
@Configuration | ||
@EnableBatchProcessing | ||
public class AdjustmentJobConfig { | ||
|
||
private final JobBuilderFactory jobBuilderFactory; | ||
private final StepBuilderFactory stepBuilderFactory; | ||
private final EntityManagerFactory entityManagerFactory; | ||
private final AdjustmentRepository adjustmentRepository; | ||
private final FirmBankService firmBankService; | ||
|
||
public AdjustmentJobConfig(JobBuilderFactory jobBuilderFactory, | ||
StepBuilderFactory stepBuilderFactory, | ||
EntityManagerFactory entityManagerFactory, AdjustmentRepository adjustmentRepository, FirmBankService firmBankService) { | ||
this.jobBuilderFactory = jobBuilderFactory; | ||
this.stepBuilderFactory = stepBuilderFactory; | ||
this.entityManagerFactory = entityManagerFactory; | ||
this.adjustmentRepository = adjustmentRepository; | ||
this.firmBankService = firmBankService; | ||
} | ||
|
||
@Bean(name = "adjustmentJob") | ||
public Job adjustmentJob() throws Exception { | ||
return jobBuilderFactory.get("adjustmentJob") | ||
.start(adjustmentStep()) | ||
.build(); | ||
} | ||
|
||
@Bean | ||
@JobScope | ||
public Step adjustmentStep() { | ||
return stepBuilderFactory.get("adjustmentStep") | ||
.<Member, Adjustment>chunk(1000) | ||
.reader(memberReader()) | ||
.processor(adjustmentProcessor()) | ||
.writer(adjustmentWriter()) | ||
.build(); | ||
} | ||
|
||
@Bean | ||
@StepScope | ||
public JpaPagingItemReader<Member> memberReader() { | ||
|
||
return new JpaPagingItemReaderBuilder<Member>() | ||
.name("JpaPagingItemReader") | ||
.entityManagerFactory(entityManagerFactory) | ||
.queryString("SELECT m FROM Member m " + | ||
"JOIN FETCH m.account a") | ||
.pageSize(1000) // 페이징 사이즈를 1000 으로 설정 -> 누락되는거 없이 한번에 들고오자는 생각 | ||
.build(); | ||
} | ||
|
||
@Bean | ||
@StepScope | ||
public ItemProcessor<Member, Adjustment> adjustmentProcessor() { | ||
|
||
LocalDateTime monthAgo = LocalDateTime.now().minusMonths(1); | ||
int year = monthAgo.getYear(); | ||
int month = monthAgo.getMonthValue(); | ||
|
||
return member -> { | ||
|
||
Integer amount = adjustmentRepository.calculateAmount(member.getMemberId(), month, year); | ||
if(amount == null || amount == 0) return null; | ||
|
||
AdjustmentResult result = firmBankService.adjustment(member.getAccount().getName(), | ||
member.getAccount().getAccount(), | ||
member.getAccount().getBank(), | ||
amount); | ||
|
||
return Adjustment.createAdjustment(year, month, member, amount, result.getStatus(), result.getReason()); | ||
}; | ||
} | ||
|
||
// Writer 설정 - 처리된 결과 데이터를 데이터베이스에 저장하는 역할 | ||
@Bean | ||
@StepScope | ||
public ItemWriter<Adjustment> adjustmentWriter() { | ||
|
||
return adjustments -> adjustmentRepository.saveAll((List<Adjustment>) adjustments); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 3 additions & 1 deletion
4
Server/src/main/java/com/server/module/firmbank/FirmBankService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
package com.server.module.firmbank; | ||
|
||
import com.server.module.firmbank.response.AdjustmentResult; | ||
|
||
public interface FirmBankService { | ||
|
||
String adjustment(String name, String account, String bank, int amount); | ||
AdjustmentResult adjustment(String name, String account, String bank, int amount); | ||
} |
9 changes: 7 additions & 2 deletions
9
Server/src/main/java/com/server/module/firmbank/MockFirmBankService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,17 @@ | ||
package com.server.module.firmbank; | ||
|
||
import com.server.domain.adjustment.domain.AdjustmentStatus; | ||
import com.server.module.firmbank.response.AdjustmentResult; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class MockFirmBankService implements FirmBankService { | ||
|
||
@Override | ||
public String adjustment(String name, String account, String bank, int amount) { | ||
return "OK"; | ||
public AdjustmentResult adjustment(String name, String account, String bank, int amount) { | ||
return AdjustmentResult.builder() | ||
.status(AdjustmentStatus.ADJUSTED) | ||
.reason(AdjustmentStatus.ADJUSTED.getDescription()) | ||
.build(); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
Server/src/main/java/com/server/module/firmbank/response/AdjustmentResult.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.server.module.firmbank.response; | ||
|
||
import com.server.domain.adjustment.domain.AdjustmentStatus; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
@Builder | ||
public class AdjustmentResult { | ||
|
||
private AdjustmentStatus status; | ||
private String reason; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -104,4 +104,4 @@ decorator: | |
enable-logging: false | ||
|
||
warmup: | ||
is-completed: true | ||
is-completed: false |