package com.heyu.api.controller.certificate;


import com.heyu.api.baidu.handle.certificate.BOverseasPassportHandle;
import com.heyu.api.baidu.request.certificate.BOverseasPassportRequest;
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.MapUtils;
import com.heyu.api.data.utils.R;
import com.heyu.api.resp.certificate.OverseasPassportResp;
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;

import java.util.Map;


/**
 * https://console.bce.baidu.com/support/?_=1740219852952&timestamp=1740308634888#/api?product=AI&project=%E6%96%87%E5%AD%97%E8%AF%86%E5%88%AB&parent=%E5%8D%A1%E8%AF%81OCR&api=rest%2F2.0%2Focr%2Fv1%2Foverseas_passport&method=post
 * <p>
 * <p>
 * 护照识别（港澳台地区及境外）
 * <p>
 * 接口描述
 * 支持对港澳台地区及境外护照进行结构化识别，包括MRZCode1、MRZCode2、出生日期、国家码、国籍、姓名拼音、性别、护照号、护照类型、有效期，10 个关键字段。
 */
@Slf4j
@RestController
@RequestMapping("/overseasPassport")
@NotIntercept
public class OverseasPassportController extends BaseController {


    @Autowired
    private BOverseasPassportHandle bOverseasPassportHandle;


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

        OverseasPassportResp resp = new OverseasPassportResp();


        ApiR<Map> bR = bOverseasPassportHandle.handle(request);
        if (bR.isSuccess()) {

            Map<String, Object> data = bR.getData();

            resp.setMRZCode2(MapUtils.getByExpr(data, "words_result.MRZCode2.[0].word"));                // MRZCode2
            resp.setPassportType(MapUtils.getByExpr(data, "words_result.护照类型.[0].word"));        //护照类型
            resp.setPassportNo(MapUtils.getByExpr(data, "words_result.护照号.[0].word"));      //护照号
            resp.setPassportDate(MapUtils.getByExpr(data, "words_result.出生日期.[0].word"));        //出生日期
            resp.setPassportCountryCode(MapUtils.getByExpr(data, "words_result.国家码.[0].word"));     //国家码
            resp.setNationality(MapUtils.getByExpr(data, "words_result.国籍.[0].word"));     //国籍
            resp.setNamePinYin(MapUtils.getByExpr(data, "words_result.姓名拼音.[0].word"));      //姓名拼音
            resp.setMRZCode1(MapUtils.getByExpr(data, "words_result.MRZCode1.[0].word"));            //MRZCode1
            resp.setGender(MapUtils.getByExpr(data, "words_result.性别.[0].word")); //性别名称：男、女
            resp.setValidDate(MapUtils.getByExpr(data, "words_result.有效期.[0].word"));            //有效期

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


        return R.error();
    }


}
