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

import cn.hutool.json.JSONObject;
import com.github.microservice.auth.client.content.ResultContent;
import com.github.microservice.auth.client.content.ResultState;
import com.github.microservice.auth.security.helper.AuthHelper;
import com.zhongshu.payment.client.model.order.v2.CreateOrder2Model;
import com.zhongshu.payment.client.model.order.v2.GoodsResultModel;
import com.zhongshu.payment.client.model.order.v2.VerificationModel;
import com.zhongshu.payment.client.model.payment.SubOrdersConfirmResponseModel;
import com.zhongshu.payment.client.service.v2.OrderService2;
import com.zhongshu.payment.client.service.v2.PaymentService2;
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
@Configuration
@Log4j2
public class StandarVerificationJob {

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

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

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

    @Autowired
    PaymentService2 paymentService2;
    @Resource
    private StepBuilderFactory stepBuilderFactory;

    @Resource
    private JobBuilderFactory jobBuilderFactory;

    @Autowired
    OrderService2 orderService2;

    @Autowired
    AuthHelper authHelper;


    public Step standardVerificationStep() {
        return stepBuilderFactory.get("standardVerificationStep")
                .tasklet((contribution, chunkContext) -> {

                    getResultContent().remove();

                    VerificationModel verificationModel = getParameter().get();
                    String enterPriseId = authHelper.getEnterPriseId();

                    ResultContent<GoodsResultModel> result = orderService2.qrGoods(verificationModel.getId());
                    if (!result.getState().equals(ResultState.Success)) {
                        stop.set(true);
                        getResultContent().set(result);
                        return RepeatStatus.FINISHED;
                    }
                    JSONObject goodsInfo = result.getContent().getGoodsInfo();
                    Integer validEnable = (Integer) goodsInfo.get("validEnable");
                    String validRang = String.valueOf(goodsInfo.get("validRang"));
                    switch (validEnable) {
                        case 1:
                            break;
                        case 2:
                            if (!validRang.equals(enterPriseId)) {
                                getResultContent().set(ResultContent.build(ResultState.Fail, "请到指定店铺核销"));
                                stop.set(true);
                                return RepeatStatus.FINISHED;
                            }
                            break;
                        case 3:
                            if (!result.getContent().getShopId().equals(enterPriseId)) {
                                getResultContent().set(ResultContent.build(ResultState.Fail, "请到指定店铺核销"));
                                stop.set(true);
                                return RepeatStatus.FINISHED;
                            }
                            break;
                    }


                    ResultContent<SubOrdersConfirmResponseModel> resultContent = paymentService2.subOrdersConfirm(verificationModel.getId(), verificationModel.getShopId());
                    if (!resultContent.getState().equals(ResultState.Success)) {
                        getResultContent().set(resultContent);
                        return RepeatStatus.FINISHED;
                    }
                    getResultContent().set(orderService2.verification(verificationModel));
                    return RepeatStatus.FINISHED;
                })
                .build();
    }

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