package com.heyu.api.controller.car;

import com.aliyun.ocr20191230.models.RecognizeDriverLicenseResponse;
import com.aliyun.ocr20191230.models.RecognizeDriverLicenseResponseBody;
import com.heyu.api.alibaba.handle.common.text.ARecognizeDriverLicenseHandle;
import com.heyu.api.alibaba.request.common.text.ARecognizeDriverLicenseRequest;
import com.heyu.api.controller.BaseController;
import com.heyu.api.resp.car.RecognizeDriverLicenseBackResp;
import com.heyu.api.resp.car.RecognizeDriverLicenseFaceResp;
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.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/RecognizeDriverLicense?tab=DEMO&lang=JAVA
 * <p>
 * 驾驶证识别
 * <p>
 * RecognizeDriverLicense
 *
 * 驾驶证识别能力可以识别驾驶证首页和副页关键字段内容，包括：档案编号、姓名、有效期时长、性别、发证日期、驾驶证号、驾驶证准驾车型、有效期开始时间、地址，共 9 个关键字段信息。
 *
 *
 *
 *
 * https://console.bce.baidu.com/support/?_=1740219852952&timestamp=1740325062124#/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%2Fdriving_license&method=post
 *
 *
 * 驾驶证识别
 *
 *
 *
 *
 *
 */
@Slf4j
@RestController
@RequestMapping("/driver/license")
@NotIntercept
public class RecognizeDriverLicenseController extends BaseController {


    @Autowired
    private ARecognizeDriverLicenseHandle aRecognizeDriverLicenseHandle;






    @RequestMapping("/recognize")
    public R recognize(ARecognizeDriverLicenseRequest request) {

        RecognizeDriverLicenseFaceResp faceResp = new RecognizeDriverLicenseFaceResp();
        RecognizeDriverLicenseBackResp backResp = new RecognizeDriverLicenseBackResp();
        ApiR<RecognizeDriverLicenseResponse> aR = aRecognizeDriverLicenseHandle.handle(request);


        if (aR.isSuccess()) {
            RecognizeDriverLicenseResponse response = aR.getData();
            if (isSuccessStatusCode(response.getStatusCode())) {
                RecognizeDriverLicenseResponseBody.RecognizeDriverLicenseResponseBodyData
                        responseBodyData = response.getBody().getData();

                if ("face".equals(request.getSide())) {
                    RecognizeDriverLicenseResponseBody.RecognizeDriverLicenseResponseBodyDataFaceResult
                            faceResult = responseBodyData.getFaceResult();

                    faceResp.setVehicleType(faceResult.getVehicleType());
                    faceResp.setIssueDate(faceResult.getIssueDate());
                    faceResp.setEndDate(faceResult.getEndDate());
                    faceResp.setGender(faceResult.getGender());
                    faceResp.setAddress(faceResult.getAddress());
                    faceResp.setStartDate(faceResult.getStartDate());
                    faceResp.setLicenseNumber(faceResult.getLicenseNumber());
                    faceResp.setNationality(faceResult.getNationality());
                    faceResp.setIssueUnit(faceResult.getIssueUnit());
                    faceResp.setNationality(faceResult.getNationality());
                    faceResp.setBirthDate(faceResult.getBirthDate());
                    return R.ok().setData(faceResp);
                }
                if ("back".equals(request.getSide())) {
                    RecognizeDriverLicenseResponseBody.RecognizeDriverLicenseResponseBodyDataBackResult
                            backResult = responseBodyData.getBackResult();

                    backResp.setArchiveNumber(backResult.getArchiveNumber());
                    backResp.setName(backResult.getName());
                    backResp.setCardNumber(backResult.getCardNumber());
                    backResp.setRecord(backResult.getRecord());
                    return R.ok().setData(backResp);
                }
            }
        }


        return R.error("识别失败");
    }

}
