package com.heyu.api.controller.tax;


import com.aliyun.ocr20191230.models.RecognizeTicketInvoiceResponse;
import com.aliyun.ocr20191230.models.RecognizeTicketInvoiceResponseBody;
import com.heyu.api.alibaba.handle.common.text.ARecognizeTicketInvoiceHandle;
import com.heyu.api.alibaba.request.common.text.ARecognizeTicketInvoiceRequest;
import com.heyu.api.controller.BaseController;
import com.heyu.api.request.tax.RecognizeTicketInvoiceReq;
import com.heyu.api.resp.tax.RecognizeTicketInvoiceResp;
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 lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/***
 * https://next.api.aliyun.com/api/ocr/2019-12-30/RecognizeTicketInvoice
 *
 * 增值税发票卷票识别 RecognizeTicketInvoice
 */


@Slf4j
@RestController
@RequestMapping("/ticket/invoice")
@NotIntercept
public class RecognizeTicketInvoiceController extends BaseController {


    @Autowired
    private ARecognizeTicketInvoiceHandle recognizeTicketInvoiceHandle;

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


        RecognizeTicketInvoiceResp resp = new RecognizeTicketInvoiceResp();

        ARecognizeTicketInvoiceRequest
                recognizeTicketInvoiceRequest = new ARecognizeTicketInvoiceRequest();
        recognizeTicketInvoiceRequest.setImageBase64(request.getImageBase64());
        recognizeTicketInvoiceRequest.setImageUrl(request.getImageUrl());
        ApiR<RecognizeTicketInvoiceResponse> aR = recognizeTicketInvoiceHandle.handle(recognizeTicketInvoiceRequest);
        if (aR.isSuccess() && isSuccessStatusCode(aR.getData().getStatusCode())) {
            RecognizeTicketInvoiceResponse response = aR.getData();

            RecognizeTicketInvoiceResponseBody responseBody = response.getBody();
            RecognizeTicketInvoiceResponseBody.RecognizeTicketInvoiceResponseBodyData responseBodyData = responseBody.getData();
            List<RecognizeTicketInvoiceResponseBody.RecognizeTicketInvoiceResponseBodyDataResults> results = responseBodyData.getResults();

            if (CollectionUtils.isNotEmpty(results)) {
                RecognizeTicketInvoiceResponseBody.RecognizeTicketInvoiceResponseBodyDataResults responseBodyDataResults = results.get(0);
                RecognizeTicketInvoiceResponseBody.RecognizeTicketInvoiceResponseBodyDataResultsContent responseBodyDataResultsContent = responseBodyDataResults.getContent();

                resp.setInvoiceCode(responseBodyDataResultsContent.getInvoiceCode());
                resp.setInvoiceNumber(responseBodyDataResultsContent.getInvoiceNumber());
                resp.setInvoiceDate(responseBodyDataResultsContent.getInvoiceDate());
                resp.setAntiFakeCode(responseBodyDataResultsContent.getAntiFakeCode());
                resp.setPayeeName(responseBodyDataResultsContent.getPayeeName());
                resp.setPayerRegisterNo(responseBodyDataResultsContent.getPayerRegisterNo());
                resp.setSumAmount(responseBodyDataResultsContent.getSumAmount());
                resp.setType(responseBodyDataResults.getType());
                return R.ok().setData(resp);
            }


        }


        return R.ok();
    }


}
