package com.heyu.api.controller.tax;


import com.aliyun.ocr20191230.models.RecognizeQuotaInvoiceResponse;
import com.aliyun.ocr20191230.models.RecognizeQuotaInvoiceResponseBody;
import com.heyu.api.alibaba.handle.common.text.ARecognizeQuotaInvoiceHandle;
import com.heyu.api.alibaba.request.common.text.ARecognizeQuotaInvoiceRequest;
import com.heyu.api.baidu.handle.financial.BQuotaInvoiceHandle;
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.request.tax.RecognizeQuotaInvoiceReq;
import com.heyu.api.resp.tax.RecognizeQuotaInvoiceResp;
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/RecognizeQuotaInvoice?tab=DOC&lang=JAVA
 *
 *
 * 功能描述
 * 定额发票识别能力可以对定额发票上的发票号码、发票代码、发票金额进行结构化识别。
 */
@Slf4j
@RestController
@RequestMapping("/quota/invoice")
@NotIntercept
public class RecognizeQuotaInvoiceController extends BaseController {


    @Autowired
    private ARecognizeQuotaInvoiceHandle recognizeQuotaInvoiceHandle;

    @Autowired
    private BQuotaInvoiceHandle bQuotaInvoiceHandle;


    @RequestMapping("/recognize")
    @CacheResult
    public R recognize(RecognizeQuotaInvoiceReq request) {
        RecognizeQuotaInvoiceResp resp = new RecognizeQuotaInvoiceResp();





        ARecognizeQuotaInvoiceRequest aRecognizeQuotaInvoiceRequest = new ARecognizeQuotaInvoiceRequest();
        aRecognizeQuotaInvoiceRequest.setImageBase64(request.getImageBase64());
        aRecognizeQuotaInvoiceRequest.setImageUrl(request.getImageUrl());

        ApiR<RecognizeQuotaInvoiceResponse> aR = recognizeQuotaInvoiceHandle.handle(aRecognizeQuotaInvoiceRequest);
        if (aR.isSuccess() && isSuccessStatusCode(aR.getData().getStatusCode())) {

            RecognizeQuotaInvoiceResponseBody body = aR.getData().getBody();
            RecognizeQuotaInvoiceResponseBody.RecognizeQuotaInvoiceResponseBodyData responseBodyData = body.getData();
            RecognizeQuotaInvoiceResponseBody.RecognizeQuotaInvoiceResponseBodyDataContent responseBodyDataContent = responseBodyData.getContent();


            resp.setSumAmount(responseBodyDataContent.getSumAmount());
            resp.setInvoiceCode(responseBodyDataContent.getInvoiceCode());
            resp.setInvoiceNo(responseBodyDataContent.getInvoiceNo());
            resp.setInvoiceAmount(responseBodyDataContent.getInvoiceAmount());
            resp.setInvoiceDetails(responseBodyDataContent.getInvoiceDetails());

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


        }



        return R.ok();
    }


}