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

import com.zswl.cloud.springBtach.server.core.event.JobEvent;
import lombok.SneakyThrows;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.explore.JobExplorer;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

@Component
public class JobListener implements ApplicationListener<JobEvent> {


    @Autowired
    private ApplicationContext applicationContext;


    @Resource
    private JobLauncher jobLauncher;

    @Resource
    private JobExplorer jobExplorer;


    @Override
    @SneakyThrows
    public void onApplicationEvent(JobEvent event) {

       Map<String,Object> source  =(Map) event.getSource();






        if (source.get("goodsCode").equals("81") && source.get("type").equals("deliver")){
            Job job = (Job) this.applicationContext.getBean("xx_hxJob");
            //todo 触发核销流程
            JobParameters parameters = new JobParametersBuilder(jobExplorer)
                    .getNextJobParameters(job)
                    .toJobParameters();
            jobLauncher.run(job, parameters).getExitStatus();
        }


        //todo 触发分账 -> 分账策略 ->



    }
}
