Skip to content

Commit

Permalink
[BE]✨ feat : 정산 배치작업 구성 merge
Browse files Browse the repository at this point in the history
[BE]✨ feat : 정산 배치작업 구성 #494
  • Loading branch information
hobeen-kim authored Oct 2, 2023
2 parents 0259a4c + ae5eac5 commit 6bff9bf
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 5 deletions.
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,20 @@
public class CancelVideoScheduler {

private final Job cancelVideoJob;
private final Job adjustmentJob;
private final JobLauncher jobLauncher;

public CancelVideoScheduler(@Qualifier("cancelVideoJob") Job cancelVideoJob,
@Qualifier("adjustmentJob") Job adjustmentJob,
JobLauncher jobLauncher) {
this.cancelVideoJob = cancelVideoJob;
this.adjustmentJob = adjustmentJob;
this.jobLauncher = jobLauncher;
}

//매일 새벽 1시에 실행
@Scheduled(cron = "0 0 1 * * *")
public void executeJob() {
public void executeVideoJob() {
try {
jobLauncher.run(cancelVideoJob,
new JobParametersBuilder()
Expand All @@ -38,4 +41,19 @@ public void executeJob() {
e.printStackTrace();
}
}

//매달 15일 새벽 2시에 실행 (14일 후 취소하지 못하므로)
@Scheduled(cron = "0 0 2 15 * *")
public void executeAdjustmentJob() {
try {
jobLauncher.run(cancelVideoJob,
new JobParametersBuilder()
.addString("time", LocalDateTime.now().toString())
.toJobParameters() //Job 실행 시 필요한 파라미터 설정
);
} catch (JobExecutionException e) {
log.error(e.getMessage() + " 으로 인한 정산 배치작업 실패");
e.printStackTrace();
}
}
}
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);
}
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();
}
}
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;
}
2 changes: 1 addition & 1 deletion Server/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,4 @@ decorator:
enable-logging: false

warmup:
is-completed: true
is-completed: false

0 comments on commit 6bff9bf

Please sign in to comment.