package com.zswl.cloud.springBtach.server.core.config;

import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * @author wy
 */
@Configuration
public class OrderBatchConfig {

    @Resource
    private StepBuilderFactory stepBuilderFactory;

    @Bean
    public Tasklet orderTaskletA(){
        return new Tasklet() {
            @Override
            public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
                // todo 具体业务调用或代码
                System.out.println("----------------OrderBatchConfig-taskletA---------------" + chunkContext.getStepContext().getJobParameters().get("name"));
                return RepeatStatus.FINISHED;
            }
        };
    }

    @Bean
    public Tasklet orderTaskletB(){
        return new Tasklet() {
            @Override
            public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
                // todo 具体业务调用或代码
                System.out.println("------------OrderBatchConfig-taskletB-------------------");
                return RepeatStatus.FINISHED;
            }
        };
    }

    @Bean
    public Tasklet orderTaskletC(){
        return new Tasklet() {
            @Override
            public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
                // todo 具体业务调用或代码
                System.out.println("----------------OrderBatchConfig-taskletC---------------");
                return RepeatStatus.FINISHED;
            }
        };
    }

    @Bean
    public Step orderStepA(){
        return stepBuilderFactory.get("orderStepA")
                .tasklet(orderTaskletA())
                .build();
    }

    @Bean
    public Step orderStepB() {
        return stepBuilderFactory.get("orderStepB")
                .tasklet(orderTaskletB())
                .build();
    }

    @Bean
    public Step orderStepC(){
        return stepBuilderFactory.get("orderStepC")
                .tasklet(orderTaskletC())
                .build();
    }

}
