package com.heyu.api.controller.tax;


import com.aliyun.ocr20191230.models.RecognizeVINCodeResponse;
import com.aliyun.ocr20191230.models.RecognizeVINCodeResponseBody;
import com.heyu.api.alibaba.handle.common.text.ARecognizeVINCodeHandle;
import com.heyu.api.alibaba.request.common.text.ARecognizeVINCodeRequest;
import com.heyu.api.baidu.handle.traffic.BVinCodeHandle;
import com.heyu.api.baidu.request.traffic.BVinCodeRequest;
import com.heyu.api.baidu.response.traffic.BVinCodeResp;
import com.heyu.api.controller.BaseController;
import com.heyu.api.request.tax.ARecognizeVATInvoiceReq;
import com.heyu.api.resp.tax.RecognizeVINCodeResp;
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.ArrayList;
import java.util.List;

/***
 *https://next.api.aliyun.com/api/ocr/2019-12-30/RecognizeVINCode?tab=DEMO&lang=JAVA
 *
 *
 * VIN码识别
 *
 * RecognizeVINCode
 *功能描述
 *
 *
 *
 * VIN 码识别能力可以识别车辆 VIN 码，输出车辆 VIN 码数值。
 *
 *
 *
 *
 *https://console.bce.baidu.com/support/?_=1740411162437&timestamp=1740411253251#/api?product=AI&project=%E6%96%87%E5%AD%97%E8%AF%86%E5%88%AB&parent=%E4%BA%A4%E9%80%9A%E5%9C%BA%E6%99%AFOCR&api=rest%2F2.0%2Focr%2Fv1%2Fvin_code&method=post
 *
 *
 *
 */
@Slf4j
@RestController
@RequestMapping("/vin/invoice")
@NotIntercept
public class RecognizeVINCodeController extends BaseController {


    @Autowired
    private ARecognizeVINCodeHandle arecognizeVINCodeHandle;


    @Autowired
    private BVinCodeHandle bVinCodeHandle;


    @RequestMapping("/recognize")
    @CacheResult
    public R recognize(ARecognizeVATInvoiceReq request) {
        List<RecognizeVINCodeResp> respList = new ArrayList<>();

        BVinCodeRequest bVinCodeRequest = new BVinCodeRequest();
        bVinCodeRequest.setImageBase64(request.getImageBase64());
        bVinCodeRequest.setImageUrl(request.getImageUrl());

        ApiR<BVinCodeResp> bR = bVinCodeHandle.handle(bVinCodeRequest);
        if (bR.isSuccess()) {
            BVinCodeResp bVinCodeResp = bR.getData();
            List<BVinCodeResp.WordsResultDTO> wordsResultDTOS = bVinCodeResp.getWordsResult();
            if (CollectionUtils.isNotEmpty(wordsResultDTOS)) {
                for (BVinCodeResp.WordsResultDTO wordsResultDTO : wordsResultDTOS) {
                    RecognizeVINCodeResp response = new RecognizeVINCodeResp();
                    response.setVinCode(wordsResultDTO.getWords());
                    respList.add(response);
                }
            }
            return R.ok().setData(respList);
        }

        ARecognizeVINCodeRequest aRecognizeVINCodeRequest = new ARecognizeVINCodeRequest();
        aRecognizeVINCodeRequest.setImageUrl(request.getImageUrl());
        aRecognizeVINCodeRequest.setImageBase64(request.getImageBase64());

        ApiR<RecognizeVINCodeResponse> aR = arecognizeVINCodeHandle.handle(aRecognizeVINCodeRequest);
        if (aR.isSuccess() && isSuccessStatusCode(aR.getData().getStatusCode())) {
            RecognizeVINCodeResponseBody responseBody = aR.getData().getBody();
            RecognizeVINCodeResponseBody.RecognizeVINCodeResponseBodyData responseBodyData = responseBody.getData();
            RecognizeVINCodeResp resp = new RecognizeVINCodeResp();

            resp.setVinCode(responseBodyData.getVinCode());

            respList.add(resp);


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

}


























