package com.jianshui.invoice.task;

import com.alibaba.fastjson.JSONObject;
import com.jianshui.common.core.domain.entity.Companyservice;
import com.jianshui.common.core.redis.RedisCache;
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.dto.QueryBillInfoDTO;
import com.jianshui.invoice.mapper.InvoiceMapper;
import com.jianshui.invoice.service.IInvoiceApiService;
import com.jianshui.queue.utils.RedisQueueUtil;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import lombok.extern.slf4j.XSlf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.TimerTask;

/**
 * @Description 发票查询任务
 * @Author 巩权林
 * @Date 2022/4/16 00:21
 **/
@Slf4j
public class QueryInvoiceTask extends TimerTask {

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


    public QueryInvoiceTask(BillInfo billInfo, Companyservice companyservice, IInvoiceApiService invoiceApiService) {
        this.finalBillInfo = billInfo;
        this.companyservice = companyservice;
        this.invoiceApiService = invoiceApiService;
    }

    @SneakyThrows
    @Override
    public void run() {
        RedisCache redisCache = SpringUtils.getBean(RedisCache.class);
        InvoiceMapper invoiceMapper = SpringUtils.getBean(InvoiceMapper.class);
        Thread.sleep(10 * 1000); // 睡眠10秒再去查询
        log.info("【定时任务】【发票查询】开始查询发票：{}", JSONObject.toJSONString(this.finalBillInfo));
        QueryBillInfoDTO queryBillInfoDTO = new QueryBillInfoDTO();
        queryBillInfoDTO.setFpqqlsh(this.finalBillInfo.getSystemOrderno());
        invoiceApiService.queryInvoice(queryBillInfoDTO, this.companyservice);

        // 获取发票
        Invoice invoice = invoiceMapper.selectBySystemOrderNo(this.companyservice.getCompanyid(), this.finalBillInfo.getSystemOrderno());
        if (invoice == null || StringUtils.equalsAny(Integer.toString(invoice.getState()), "0", "20")) {
            // 查询下失败队列，是否有次数
            String keys = "invoice_query_times_" + this.companyservice.getCompanyid() + "_" + this.finalBillInfo.getSystemOrderno();
            Integer queryTimes = redisCache.getCacheObject(keys);
            // 查询重试次数超过20次，就不查了
            if (queryTimes != null && queryTimes > 20) {
                log.error("【定时任务】【发票查询】发票信息始终不存在，不再进行查询：{}", JSONObject.toJSONString(this.finalBillInfo));
                redisCache.deleteObject(keys);
                return;
            }
            if (queryTimes == null) {
                queryTimes = 0;
            }
            queryTimes += 1;
            redisCache.setCacheObject(keys, queryTimes);
            log.error("【定时任务】【发票查询】发票信息不存在或还在开票中，再次进入查询：{}", JSONObject.toJSONString(this.finalBillInfo));
            AsyncManager.me().execute(new QueryInvoiceTask(this.finalBillInfo, companyservice, this.invoiceApiService));
        }

        RedisQueueUtil.build().setData(invoice).onQueue("invoice_add_callback_consumer").retry(0).dispatch();
    }
}
