package com.heyu.api.controller.certificate;


import com.heyu.api.baidu.handle.certificate.BHkMacauTaiwanExitentrypermitHandle;
import com.heyu.api.baidu.request.certificate.BHkMacauTaiwanExitentrypermitRequest;
import com.heyu.api.data.annotation.CacheResult;
import com.heyu.api.data.annotation.NotIntercept;
import com.heyu.api.data.constants.ApiConstants;
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.request.certificate.BHkMacauReturnPassportReq;
import com.heyu.api.resp.certificate.BHkMacauReturnPassportBackResp;
import com.heyu.api.resp.certificate.BHkMacauReturnPassportFrontResp;
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=1740313880415#/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%2Fhk_macau_taiwan_exitentrypermit&method=post
 * <p>
 * <p>
 * 港澳台证件识别
 * <p>
 * 接口描述
 * 支持识别4类港澳台出入境证件，包含港澳通行证正/反面、台湾通行证正/反面、台胞证（台湾居民来往大陆通行证）正/反面、返乡证（港澳居民来往内地通行证）正/反面，支持识别以上4类证件的全部字段信息。
 * <p>
 * <p>
 * hk_mc_return_passport_front：返乡证正面
 * hk_mc_return_passport_back：返乡证反面
 */
@Slf4j
@RestController
@RequestMapping("/hkMacau/returnpassport")
@NotIntercept
public class HkMacauReturnPassportController {


    @Autowired
    private BHkMacauTaiwanExitentrypermitHandle bhkMacauTaiwanExitentrypermitHandle;


    @RequestMapping("/recognize")
    @CacheResult
    public R recognize(BHkMacauReturnPassportReq request) {
        BHkMacauTaiwanExitentrypermitRequest bHkMacauTaiwanExitentrypermitRequest = new BHkMacauTaiwanExitentrypermitRequest();
        bHkMacauTaiwanExitentrypermitRequest.setImageUrl(request.getImageUrl());
        bHkMacauTaiwanExitentrypermitRequest.setImageBase64(request.getImageBase64());
        bHkMacauTaiwanExitentrypermitRequest.setPdfFile(request.getPdfFile());
        bHkMacauTaiwanExitentrypermitRequest.setPdfFileNum(request.getPdfFileNum());
        bHkMacauTaiwanExitentrypermitRequest.setExitentrypermitType("hk_mc_return_passport_" + request.getSite());

        ApiR<Map> bR = bhkMacauTaiwanExitentrypermitHandle.handle(bHkMacauTaiwanExitentrypermitRequest);
        if (bR.isSuccess()) {
            Map<String,Object> data = bR.getData();
            if (ApiConstants.front.equals(request.getSite())) {
                BHkMacauReturnPassportFrontResp resp = new BHkMacauReturnPassportFrontResp();
                resp.setType(MapUtils.getByExpr(data,"words_result.type.[0].word"));//	是	array[]	种类
                resp.setValidDate(MapUtils.getByExpr(data,"words_result.valid_date.[0].word"));//	是	array[]	有效期
                resp.setRemarks(MapUtils.getByExpr(data,"words_result.remarks.[0].word"));//	是	array[]	备注
                resp.setRoundTripNumber(MapUtils.getByExpr(data,"words_result.round_trip_number.[0].word"));//	是	array[]	往返有效
                return R.ok().setData(resp);
            } else {
                BHkMacauReturnPassportBackResp resp = new BHkMacauReturnPassportBackResp();
                resp.setNameChn(MapUtils.getByExpr(data,"words_result.name_chn.[0].word"));//	是	array[]	姓名
                resp.setNameEng(MapUtils.getByExpr(data,"words_result.name_eng.[0].word"));//	是	array[]	姓名（英文）
                resp.setBirthday(MapUtils.getByExpr(data,"words_result.birthday.[0].word"));//	是	array[]	出生日期
                resp.setSex(MapUtils.getByExpr(data,"words_result.sex.[0].word"));//	是	array[]	性别
                resp.setValidDate(MapUtils.getByExpr(data,"words_result.valid_date.[0].word"));//是	array[]	有效期限
                resp.setIssueAuthority(MapUtils.getByExpr(data,"words_result.issue_authority.[0].word"));//	是	array[]	签发机关
                resp.setIssueTimes(MapUtils.getByExpr(data,"words_result.issue_times.[0].word"));//	是	array[]	签发次数
                resp.setCardNumber(MapUtils.getByExpr(data,"words_result.card_number.[0].word"));//	是	array[]	证件号码
                return R.ok().setData(resp);
            }
        }
        return R.error(bR.getErrorMsg());

    }
}
