package com.zswl.cloud.springBtach.server.core.job.standard;

import com.github.microservice.auth.client.content.ResultContent;
import com.zhongshu.payment.client.model.order.v2.GoodsResultModel;
import com.zhongshu.payment.client.service.v2.OrderService2;
import com.zswl.cloud.shop.client.dto.life.OperateStockDto;
import com.zswl.cloud.shop.client.ret.ResultState;
import com.zswl.cloud.shop.client.service.GoodsService;
import lombok.Data;
import lombok.extern.log4j.Log4j2;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

@Data
@Log4j2
@Configuration
public class StandarDeliveryJob {

    ThreadLocal<ResultContent> resultContent = new ThreadLocal<>();

    ThreadLocal<GoodsResultModel> parameter = new ThreadLocal<>();

    ThreadLocal<Boolean> stop = new ThreadLocal<>();

    @Resource
    private StepBuilderFactory stepBuilderFactory;

    @Resource
    private JobBuilderFactory jobBuilderFactory;

    @Autowired
    GoodsService goodsService;

    @Autowired
    OrderService2 orderService2;


    public Step standardDeliveryStep() {
        return stepBuilderFactory.get("standardDeliveryStep")
                .tasklet((contribution, chunkContext) -> {
                    GoodsResultModel goodsResultModel = parameter.get();
                    OperateStockDto operateStockDto = new OperateStockDto();
                    operateStockDto.setUserId(goodsResultModel.getUserId());
                    operateStockDto.setOperate(3);// 使用库存
                    operateStockDto.setShopId(goodsResultModel.getShopId());
                    operateStockDto.setGoodsId(goodsResultModel.getGoodsId());
                    com.zswl.cloud.shop.client.ret.ResultContent operateStock = goodsService.operateStock(operateStockDto);
                    if (!operateStock.getState().equals(ResultState.Success)) {
                        resultContent.set(ResultContent.build(com.github.microservice.auth.client.content.ResultState.Fail));
                        return RepeatStatus.FINISHED;
                    }

                    resultContent.set(ResultContent.build(com.github.microservice.auth.client.content.ResultState.Success));
                    return RepeatStatus.FINISHED;

                })
                .build();
    }

    @Bean
    public Job standardDeliveryJob() {
        return getJobBuilderFactory()
                .get("standardDeliveryJob")
                .start(standardDeliveryStep())
                .incrementer(new RunIdIncrementer())
                .build();
    }

}

