package com.jianshui.invoice.task;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jianshui.common.core.domain.entity.Companyservice;
import com.jianshui.common.utils.StringUtils;
import com.jianshui.common.utils.spring.SpringUtils;
import com.jianshui.framework.manager.AsyncManager;
import com.jianshui.invoice.domain.BillInfo;
import com.jianshui.invoice.domain.Invoice;
import com.jianshui.invoice.domain.Redinfo;
import com.jianshui.invoice.domain.dto.DownloadRedInfoDTO;
import com.jianshui.invoice.domain.dto.HXResponse;
import com.jianshui.invoice.domain.dto.QueryBillInfoDTO;
import com.jianshui.invoice.mapper.InvoiceMapper;
import com.jianshui.invoice.mapper.RedinfoMapper;
import com.jianshui.invoice.service.IInvoiceApiService;
import com.jianshui.queue.utils.RedisQueueUtil;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.TimerTask;

/**
 * @Description 红字信息结果申请查询
 * @Author 巩权林
 * @Date 2022/4/16 00:21
 **/
@Slf4j
public class QueryRedInfoTask extends TimerTask {

    private BillInfo finalBillInfo;
    private Redinfo redinfo;
    private Companyservice companyservice;
    private IInvoiceApiService invoiceApiService;


    public QueryRedInfoTask(BillInfo billInfo, Redinfo redInfo, Companyservice companyservice, IInvoiceApiService invoiceApiService) {
        this.finalBillInfo = billInfo;
        this.redinfo = redInfo;
        this.companyservice = companyservice;
        this.invoiceApiService = invoiceApiService;
    }

    @SneakyThrows
    @Override
    public void run() {
        RedinfoMapper redinfoMapper = SpringUtils.getBean(RedinfoMapper.class);
        Thread.sleep(10 * 1000); // 睡眠10秒再去查询
        log.info("【定时任务】【红字信息发票查询】开始查询发票：{}", JSONObject.toJSONString(this.redinfo));
        DownloadRedInfoDTO queryBillInfoDTO = new DownloadRedInfoDTO();
        queryBillInfoDTO.setBuyerTaxNo(this.redinfo.getBuyerTaxnum());
        queryBillInfoDTO.setBillInfoNo(this.redinfo.getRedInfoNo());
        invoiceApiService.downloadRedInfo(queryBillInfoDTO, this.companyservice);

        DownloadRedInfoDTO downloadRedInfoDTO = new DownloadRedInfoDTO();
        Redinfo queryRedinfo = new Redinfo();
        queryRedinfo.setRedInfoNo(downloadRedInfoDTO.getBillInfoNo());
        queryRedinfo.setCompanyId(companyservice.getCompanyid());
        QueryWrapper<Redinfo> tempQueryWrapper = new QueryWrapper<>();
        tempQueryWrapper.last(" limit 1 ");
        Redinfo tempRedInfoResult = redinfoMapper.selectOne(tempQueryWrapper);
        if (tempRedInfoResult == null) {
            log.error("【定时任务】【红字信息发票查询】红字信息不存在:{}", JSONObject.toJSONString(this.redinfo));
        }
        // 获取发票
        // Invoice invoice = invoiceMapper.selectBySystemOrderNo(this.companyservice.getCompanyid(), this.finalBillInfo.getSystemOrderno());
        // if (invoice == null || StringUtils.equalsAny(Integer.toString(invoice.getState()), "0", "20")) {
        //     log.error("发票信息不存在或还在开票中，再次进入查询");
        //     AsyncManager.me().execute(new QueryRedInfoTask(this.finalBillInfo, this.redinfo, companyservice, this.invoiceApiService));
        // }

        RedisQueueUtil.build().setData(redinfo).onQueue("invoice_redinfo_callback_consumer").retry(0).dispatch();
    }
}
