package com.heyu.api.controller.idcard;


import com.heyu.api.baidu.handle.certificate.BMultiIdcardHandle;
import com.heyu.api.baidu.request.certificate.BMultiIdcardRequest;
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.IdCardMultiBackResp;
import com.heyu.api.resp.certificate.IdCardMultiFrontResp;
import com.heyu.api.resp.certificate.IdCardMultiResp;
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=1742632520803#/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%2Fmulti_idcard&method=post
 *
 *
 * 身份证混贴识别
 *
 *
 *
 *接口描述
 * 身份证混贴识别支持自动检测与识别身份证正反面在同一张图片上的场景，一次识别图片中身份证正反面所有字段。
 *
 * 支持对二代居民身份证正反面所有8个字段进行结构化识别，包括姓名、性别、民族、出生日期、住址、身份证号、签发机关、有效期限，识别准确率超过99%；同时支持身份证正面头像检测，并返回头像切片的base64编码及位置信息。
 *
 * 同时，支持对用户上传的身份证图片进行图像风险和质量检测，可识别图片是否为复印件或临时身份证，是否被翻拍或编辑，是否存在正反颠倒、模糊、欠曝、过曝等质量问题。
 *
 * 增值能力	详情
 * 裁剪能力	头像检测与切片：返回头像切片的base64编码及位置信息
 * 身份证检测与切片：返回身份证的base64编码及位置信息（去掉证件外围多余背景、自动矫正拍摄角度）
 * 质量检测	身份证图片模糊检测
 * 身份证关键字段反光或过曝光
 * 身份证图片较暗或欠曝光
 * 身份证边框/四角不完整告警
 * 身份证头像或关键字段被遮挡/马赛克告警
 * 风险检测	身份证复印件告警
 * 临时身份证告警
 * 身份证翻拍告警
 * 身份证PS编辑告警
 * 身份证证号不合法告警
 * 身份证证号和姓名、出生日期、性别信息不一致告警
 *
 *
 */
@Slf4j
@RestController
@RequestMapping("/multi/id/card")
@NotIntercept
public class IdCardMultiController extends BaseController {


    @Autowired
    private BMultiIdcardHandle bMultiIdcardHandle;


    @RequestMapping("/recognize")
    @CacheResult
    public R recognize(BMultiIdcardRequest request) {
        IdCardMultiResp resp = new IdCardMultiResp();

        ApiR<Map> apiR = bMultiIdcardHandle.handle(request);
        if (apiR.isSuccess()) {
            Map<String,Object> data = apiR.getData();
            IdCardMultiFrontResp front = new IdCardMultiFrontResp();
            IdCardMultiBackResp back = new IdCardMultiBackResp();
            try {
                front.setRiskType(MapUtils.getByExpr(data,"words_result.[0].card_info.risk_type"));//": "normal",
                front.setIdcardNumberType(MapUtils.getByExpr(data,"words_result.[0].card_info.idcard_number_type"));//": 1,
                front.setEditTool(MapUtils.getByExpr(data,"words_result.[0].card_info.edit_tool"));//": "",
                front.setImageStatus(MapUtils.getByExpr(data,"words_result.[0].card_info.image_status"));//": "normal",
                front.setCardType(MapUtils.getByExpr(data,"words_result.[0].card_info.card_type"));//": "idcard_front",
                front.setDirection(MapUtils.getByExpr(data,"words_result.[0].card_info.direction"));//": 0

                front.setAddress(MapUtils.getByExpr(data,"words_result.[0].card_result.住址.words"));
                front.setGender(MapUtils.getByExpr(data,"words_result.[0].card_result.性别.words"));
                front.setBirthday(MapUtils.getByExpr(data,"words_result.[0].card_result.出生.words"));
                front.setIdCardNumber(MapUtils.getByExpr(data,"words_result.[0].card_result.公民身份号码.words"));
                front.setNationality(MapUtils.getByExpr(data,"words_result.[0].card_result.民族.words"));
                front.setRealName(MapUtils.getByExpr(data,"words_result.[0].card_result.姓名.words"));
            } catch (Exception e) {
                log.error("正向解决 ",e);
            }

            try {
                back.setRiskType(MapUtils.getByExpr(data,"words_result.[1].card_info.risk_type"));//": "normal",
                back.setEditTool(MapUtils.getByExpr(data,"words_result.[1].card_info.edit_tool"));//": "",
                back.setImageStatus(MapUtils.getByExpr(data,"words_result.[1].card_info.image_status"));//": "normal",
                back.setCardType(MapUtils.getByExpr(data,"words_result.[1].card_info.card_type"));//": "idcard_front",
                back.setDirection(MapUtils.getByExpr(data,"words_result.[1].card_info.direction"));//": 0

                back.setStartDate(MapUtils.getByExpr(data,"words_result.[1].card_result.签发日期.words"));
                back.setIssue(MapUtils.getByExpr(data,"words_result.[1].card_result.签发机关.words"));
                back.setEndDate(MapUtils.getByExpr(data,"words_result.[1].card_result.失效日期.words"));
            } catch (Exception e) {
               log.error("反向解析",e);
            }

            resp.setFront(front);
            resp.setBack(back);

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

        }

        return R.error(apiR.getErrorMsg());
    }

}
