package com.heyu.api.controller.tax;


import com.aliyun.ocr20191230.models.RecognizeVATInvoiceResponse;
import com.aliyun.ocr20191230.models.RecognizeVATInvoiceResponseBody;
import com.heyu.api.alibaba.handle.common.text.ARecognizeVATInvoiceHandle;
import com.heyu.api.alibaba.request.common.text.ARecognizeVATInvoiceRequest;
import com.heyu.api.baidu.handle.financial.BVatInvoiceHandle;
import com.heyu.api.controller.BaseController;
import com.heyu.api.data.annotation.CacheResult;
import com.heyu.api.data.annotation.NotIntercept;
import com.heyu.api.data.utils.ApiR;
import com.heyu.api.data.utils.R;
import com.heyu.api.resp.tax.RecognizeVATInvoiceResp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/****
 *https://next.api.aliyun.com/api/ocr/2019-12-30/RecognizeVATInvoice?tab=DEMO&lang=JAVA
 *
 *
 * 增值税发票识别 RecognizeVATInvoice
 *
 *
 * 功能描述
 * 增值税发票识别能力可以识别增值税发票（电子发票和纸质发票）关键字段内容，包括：校验码、复核人、开票人、发票代码、收款人等。
 *
 *
 *
 *
 *
 *
 * https://console.bce.baidu.com/support/?_=1740494347073&timestamp=1740497736043#/api?product=AI&project=%E6%96%87%E5%AD%97%E8%AF%86%E5%88%AB&parent=%E8%B4%A2%E5%8A%A1%E7%A5%A8%E6%8D%AEOCR&api=rest%2F2.0%2Focr%2Fv1%2Fvat_invoice&method=post
 * <p>
 * 增值税发票识别
 *
 *
 */
@Slf4j
@RestController
@RequestMapping("/vat/invoice")
@NotIntercept
public class RecognizeVATInvoiceController extends BaseController {


    @Autowired
    private ARecognizeVATInvoiceHandle arRecognizeVATInvoiceHandle;


    @Autowired
    private BVatInvoiceHandle bVatInvoiceHandle;

    @RequestMapping("/recognize")
    @CacheResult
    public R recognize(ARecognizeVATInvoiceRequest request) {


        RecognizeVATInvoiceResp resp = new RecognizeVATInvoiceResp();

        ApiR<RecognizeVATInvoiceResponse> aR = arRecognizeVATInvoiceHandle.handle(request);
        if (aR.isSuccess() && isSuccessStatusCode(aR.getData().getStatusCode())) {
            RecognizeVATInvoiceResponse response = aR.getData();
            RecognizeVATInvoiceResponseBody responseBody = response.getBody();
            RecognizeVATInvoiceResponseBody.RecognizeVATInvoiceResponseBodyData responseBodyData = responseBody.getData();
            RecognizeVATInvoiceResponseBody.RecognizeVATInvoiceResponseBodyDataContent content = responseBodyData.getContent();

            resp.setPayerAddress(content.getPayerAddress());
            resp.setPayeeRegisterNo(content.getPayeeRegisterNo());
            resp.setPayeeBankName(content.getPayeeBankName());
            resp.setInvoiceNo(content.getInvoiceNo());
            resp.setPayerRegisterNo(content.getPayerRegisterNo());
            resp.setChecker(content.getChecker());
            resp.setTaxAmount(content.getTaxAmount());
            resp.setInvoiceCode(content.getInvoiceCode());
            resp.setWithoutTaxAmount(content.getWithoutTaxAmount());
            resp.setInvoiceAmount(content.getInvoiceAmount());
            resp.setAntiFakeCode(content.getAntiFakeCode());
            resp.setPayerName(content.getPayerName());
            resp.setPayee(content.getPayee());
            resp.setSumAmount(content.getSumAmount());
            resp.setPayerBankName(content.getPayerBankName());
            resp.setClerk(content.getClerk());
            resp.setPayeeName(content.getPayeeName());
            resp.setPayeeAddress(content.getPayeeAddress());
            resp.setInvoiceCode(content.getInvoiceCode());
            resp.setItemName(content.getItemName());

            return R.ok().setData(resp);
        }


        return R.ok();
    }


}
