package com.ghy.web.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.json.JSONUtil;
import com.aliyun.dytnsapi20200217.Client;
import com.aliyun.dytnsapi20200217.models.CertNoTwoElementVerificationRequest;
import com.aliyun.dytnsapi20200217.models.CertNoTwoElementVerificationResponse;
import com.ghy.web.core.config.AliApiConfig;
import com.ghy.web.pojo.vo.CertNoTwoElementReq;
import com.ghy.web.service.AliCloudService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class AliCloudServiceImpl implements AliCloudService {

    private final AliApiConfig aliApiConfig;


    @Override
    public void certNoTwoElementVerification(CertNoTwoElementReq certNoTwoElementReq) throws Exception{
        // 校验是否是历史记录
        Client client = aliApiConfig.createClient();
        CertNoTwoElementVerificationRequest request = new CertNoTwoElementVerificationRequest();
        request.setAuthCode(aliApiConfig.getAuthCode());
        request.setCertNo(certNoTwoElementReq.getCertNo());
        request.setCertName(certNoTwoElementReq.getCertName());
        log.info("调用身份证二要素校验接口,入参:{}", certNoTwoElementReq);
        CertNoTwoElementVerificationResponse response =client.certNoTwoElementVerification(request);
        log.info("调用身份证二要素校验接口,响应:{}", JSONUtil.toJsonStr(response.getBody()));
        Assert.isTrue("OK".equals(response.getBody().getCode()), Exception::new);
        Assert.isTrue("1".equals(response.getBody().getData().getIsConsistent()), "身份二要素验证失败!");
    }
}
