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

import com.github.microservice.app.stream.StreamConsumer;
import com.github.microservice.auth.client.content.ResultContent;
import com.github.microservice.auth.client.content.ResultState;
import com.zhongshu.payment.client.model.order.v2.GoodsResultModel;
import com.zhongshu.payment.client.model.order.v2.OrderStremModel;
import com.zhongshu.payment.client.service.v2.OrderService2;
import com.zhongshu.payment.client.type.JobFlowMap;
import com.zhongshu.payment.client.type.JobFlowType;
import com.zhongshu.payment.client.type.OrderType;
import com.zswl.cloud.shop.client.dto.life.OperateStockDto;
import com.zswl.cloud.springBtach.server.core.job.standard.StandarDeliveryJob;
import lombok.SneakyThrows;
import lombok.extern.log4j.Log4j2;
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.stereotype.Component;
import org.springframework.util.Assert;

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

@Log4j2
@Component("orderStreamConsumer")
public class OrderStream extends StreamConsumer<OrderStremModel> {

    @Autowired
    OrderService2 orderService2;

    @Autowired
    StandarDeliveryJob standarDeliveryJob;

    @Autowired
    ApplicationContext applicationContext;

    @Resource
    private JobExplorer jobExplorer;

    @Resource
    private JobLauncher jobLauncher;

    @SneakyThrows
    @Override
    public void accept(OrderStremModel orderStremModel) {
        ResultContent<List<GoodsResultModel>> listResultContent = orderService2.goodsDetail(orderStremModel.getOrderNo());

        for (GoodsResultModel goodsResultModel : listResultContent.getContent()) {

            if (orderStremModel.getOrderType().equals(OrderType.HAVE_PAID)) {
                ThreadLocal<GoodsResultModel> parameter = standarDeliveryJob.getParameter();
                parameter.set(goodsResultModel);
                JobFlowMap jobFlowMap = goodsResultModel.getJobFlowMap();
                Job job = (Job) applicationContext.getBean(jobFlowMap.getJobMapping().get(JobFlowType.Delivery));
                JobParameters parameters = new JobParametersBuilder(jobExplorer)
                        .getNextJobParameters(job)
                        .toJobParameters();
                jobLauncher.run(job, parameters).getExitStatus();
                ThreadLocal<ResultContent> resultContent = standarDeliveryJob.getResultContent();
                Assert.isTrue(resultContent.get().getState().equals(ResultState.Success), "发货失败");
                orderService2.delivery(goodsResultModel.getId());
            }else if (OrderType.WAIT_PAYMENT.equals(orderStremModel.getOrderType())) {
                //todo 暂存
            }


        }


    }
}
