package com.heyu.api.alibaba.handle.common.text;


import com.aliyun.ocr20191230.models.RecognizeVATInvoiceRequest;
import com.aliyun.ocr20191230.models.RecognizeVATInvoiceResponse;
import com.aliyun.teautil.models.RuntimeOptions;
import com.heyu.api.data.annotation.CustomPath;
import com.heyu.api.data.utils.StringUtils;
import com.heyu.api.alibaba.AlibabaBaseHandle;
import com.heyu.api.alibaba.request.common.text.ARecognizeVATInvoiceRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


/**
 * https://next.api.aliyun.com/api/ocr/2019-12-30/RecognizeVATInvoice
 * <p>
 * 增值税发票识别
 */
@Component
@Slf4j
@CustomPath("VATInvoice")
public class ARecognizeVATInvoiceHandle extends AlibabaBaseHandle<ARecognizeVATInvoiceRequest, RecognizeVATInvoiceResponse> {

    @Autowired
    private com.aliyun.ocr20191230.Client client;


    @Override
    public String check(ARecognizeVATInvoiceRequest aRecognizeVATInvoiceRequest) {
        String check = checkUrlBase64(aRecognizeVATInvoiceRequest);
        if (StringUtils.isNotBlank(check)) {
            return check;
        }

        if (StringUtils.isBlank(aRecognizeVATInvoiceRequest.getFileType())) {
            aRecognizeVATInvoiceRequest.setFileType("jpg");
        }

        return null;


    }

    @Override
    public RecognizeVATInvoiceResponse run(ARecognizeVATInvoiceRequest aRecognizeVATInvoiceRequest, RuntimeOptions runtime) throws Exception {
        RecognizeVATInvoiceRequest request = new RecognizeVATInvoiceRequest();
        request.setFileURL(aRecognizeVATInvoiceRequest.getImageUrl());
        request.setFileType(aRecognizeVATInvoiceRequest.getFileType());
        // 复制代码运行请自行打印 API 的返回值
        return client.recognizeVATInvoiceWithOptions(request, runtime);
    }
}
