package com.heyu.api.controller.car;


import com.aliyun.ocr20191230.models.RecognizeLicensePlateResponse;
import com.aliyun.ocr20191230.models.RecognizeLicensePlateResponseBody;
import com.heyu.api.alibaba.handle.common.text.ARecognizeLicensePlateHandle;
import com.heyu.api.alibaba.request.common.text.ARecognizeLicensePlateRequest;
import com.heyu.api.controller.BaseController;
import com.heyu.api.resp.car.RecognizeLicensePlateResp;
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;

/***
 * https://next.api.aliyun.com/api/ocr/2019-12-30/RecognizeLicensePlate?useCommon=true&tab=DOC&lang=JAVA&sdkStyle=dara
 *
 *车牌识别
 *
 * RecognizeLicensePlate
 *
 *
 * 车牌识别能力可以准确识别出图像中车牌位置，输出车牌位置坐标、车牌类型、车牌号码、车牌号码置信度、车牌置信度，共 5 个关键字段信息。
 *
 * 车牌识别能力目前支持的地域（Region）有上海和深圳，当您开通服务时选择的是上海地域，推荐使用上海地域的 OSS 链接，对于文件在本地或者非上海地域 OSS 链接的情况，请参见文件 URL 处理。当您开通服务时选择的是深圳地域时，仅支持深圳地域 OSS 链接进行调用。
 *
 * 示例值:
 * http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/ocr/RecognizeLicensePlate/cpsb1.jpg
 *
 *
 */

@Slf4j
@RestController
@RequestMapping("/car/license/plate")
@NotIntercept
public class RecognizeLicensePlateController extends BaseController {

    @Autowired
    private ARecognizeLicensePlateHandle aRecognizeLicensePlateHandle;

    @RequestMapping("/recognize")
    public R recognize(ARecognizeLicensePlateRequest request) {
        RecognizeLicensePlateResp resp = new RecognizeLicensePlateResp();
        ApiR<RecognizeLicensePlateResponse> aR = aRecognizeLicensePlateHandle.handle(request);
        if (aR.isSuccess() && isSuccessStatusCode(aR.getData().getStatusCode())) {
            RecognizeLicensePlateResponse response = aR.getData();
            RecognizeLicensePlateResponseBody responseBody = response.getBody();
            RecognizeLicensePlateResponseBody.RecognizeLicensePlateResponseBodyData responseBodyData = responseBody.getData();
            if (CollectionUtils.isNotEmpty(responseBodyData.getPlates())) {
                RecognizeLicensePlateResponseBody.RecognizeLicensePlateResponseBodyDataPlates responseBodyDataPlates = responseBodyData.getPlates().get(0);
                resp.setConfidence(responseBodyDataPlates.getConfidence());
                resp.setPlateTypeConfidence(responseBodyDataPlates.getPlateTypeConfidence());
                resp.setPlateType(responseBodyDataPlates.getPlateType());
                resp.setPlateNumber(responseBodyDataPlates.getPlateNumber());
                return R.ok().setData(resp);
            }
        }
        return R.ok();
    }

}
