package com.heyu.api.controller.certificate;


import com.heyu.api.baidu.handle.certificate.BRealEstateCertificateHandle;
import com.heyu.api.baidu.request.certificate.BRealEstateCertificateRequest;
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.RealEstateCertificateResp;
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=1740315498623#/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%2Freal_estate_certificate&method=post
 * 房产证识别
 *
 * 接口描述
 * 支持对房产证进行结构化识别，包括权利人、坐落、权利类型、面积、字第号 、不动产单元号、共有情况、用途、使用期限 、登记日期、共有人，全部 11 个字段。
 *
 *
 */
@Slf4j
@RestController
@RequestMapping("/house/certificate")
@NotIntercept
public class RealEstateCertificateController extends BaseController {


    @Autowired
    private BRealEstateCertificateHandle bRealEstateCertificateHandle;


    @RequestMapping("/recognize")
    @CacheResult
    public R recognize(BRealEstateCertificateRequest request) {
        RealEstateCertificateResp resp = new RealEstateCertificateResp();
        ApiR<Map> bR = bRealEstateCertificateHandle.handle(request);
        if (bR.isSuccess()) {
            Map<String,Object> data = bR.getData();
            resp.setPowerfulPerson(MapUtils.getByExpr(data,"words_result.权利人.[0].word")); //权利人
            resp.setAddress(MapUtils.getByExpr(data,"words_result.坐落.[0].word")); //坐落
            resp.setPowerfulType(MapUtils.getByExpr(data,"words_result.权利类型.[0].word")); // 权利类型
            resp.setArea(MapUtils.getByExpr(data,"words_result.面积.[0].word"));        // 面积
            resp.setNumber(MapUtils.getByExpr(data,"words_result.字第号.[0].word")); //  字第号
            resp.setUnitNumber(MapUtils.getByExpr(data,"words_result.不动产单元号.[0].word")); // 不动产单元号
            resp.setShare(MapUtils.getByExpr(data,"words_result.共有情况.[0].word"));// 共有情况
            resp.setUse(MapUtils.getByExpr(data,"words_result.用途.[0].word"));//用途
            resp.setTermOfUse(MapUtils.getByExpr(data,"words_result.使用期限.[0].word"));   // 使用期限
            resp.setRegisterDate(MapUtils.getByExpr(data,"words_result.登记日期.[0].word"));    // 登记日期
            resp.setSharePerson(MapUtils.getByExpr(data,"words_result.共有人.[0].word"));  //共有人

            return R.ok().setData(resp);
        }
        return R.error(bR.getErrorMsg());
    }


}
