package com.heyu.api.controller.certificate;


import com.heyu.api.baidu.handle.certificate.BPassportHandle;
import com.heyu.api.baidu.request.certificate.BPassportRequest;
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.PassportResp;
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=1740308374343#/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%2Fpassport&method=post
 * <p>
 * <p>
 * 护照识别
 * <p>
 * <p>
 * 接口描述
 * 支持对中国大陆护照个人资料页所有15个字段进行结构化识别，包括国家码、护照号、姓名、姓名拼音、性别、出生地点、出生日期、签发地点（不支持境外签发地）、签发日期、有效期、签发机关、护照类型、国籍、MRZCode1、MRZCode2。
 */
@Slf4j
@RestController
@RequestMapping("/passport")
@NotIntercept
public class PassportController extends BaseController {


    @Autowired
    private BPassportHandle bPassportHandle;

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

        PassportResp resp = new PassportResp();
        ApiR<Map> bR = bPassportHandle.handle(request);

        if (bR.isSuccess()) {
            Map<String, Object> data = bR.getData();
            resp.setPassportType(MapUtils.getByExpr(data, "words_result.护照类型.words"));        //护照类型
            resp.setPassportCountryCode(MapUtils.getByExpr(data, "words_result.国家码.words"));     //国家码
            resp.setPassportIssueAddress(MapUtils.getByExpr(data, "words_result.护照签发地点.words"));        //护照签发地点
            resp.setMRZCode2(MapUtils.getByExpr(data, "words_result.MRZCode2.words"));                // MRZCode2
            resp.setValidDateEnd(MapUtils.getByExpr(data, "words_result.有效期至.words"));            //有效期
            resp.setIssueUnit(MapUtils.getByExpr(data, "words_result.签发机关.words"));           // 签发机关
            resp.setMRZCode1(MapUtils.getByExpr(data, "words_result.MRZCode1.words"));            //MRZCode1
            resp.setPassportNo(MapUtils.getByExpr(data, "words_result.护照号码.words"));      //护照号
            resp.setBirthAddress(MapUtils.getByExpr(data, "words_result.出生地点.words"));        //出生地点
            resp.setName(MapUtils.getByExpr(data, "words_result.姓名.words"));      //姓名
            resp.setNamePinYin(MapUtils.getByExpr(data, "words_result.姓名拼音.words"));      //姓名拼音
            resp.setNationality(MapUtils.getByExpr(data, "words_result.国籍.words"));     //国籍
            resp.setBirthDay(MapUtils.getByExpr(data, "words_result.生日.words"));            //生日
            resp.setGender(MapUtils.getByExpr(data, "words_result.性别.words")); //性别名称：男、女
            return R.ok().setData(resp);
        }

        return R.error();
    }
}
