package com.heyu.api.tencent.handle;


import com.heyu.api.data.utils.MobileUtils;
import com.heyu.api.data.utils.StringUtils;
import com.heyu.api.tencent.TencentBaseHandle;
import com.heyu.api.tencent.request.TCheckPhoneAndNameRequest;
import com.tencentcloudapi.faceid.v20180301.FaceidClient;
import com.tencentcloudapi.faceid.v20180301.models.CheckPhoneAndNameRequest;
import com.tencentcloudapi.faceid.v20180301.models.CheckPhoneAndNameResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/***
 * https://console.cloud.tencent.com/api/explorer?Product=faceid&Version=2018-03-01&Action=CheckPhoneAndName
 *
 * 手机号二要素核验
 */
@Component
public class TCheckPhoneAndNameHandle extends TencentBaseHandle<TCheckPhoneAndNameRequest, CheckPhoneAndNameResponse> {

    @Autowired
    private FaceidClient client;


    @Override
    public String check(TCheckPhoneAndNameRequest tCheckPhoneAndNameRequest) {
        if (StringUtils.isBlank(tCheckPhoneAndNameRequest.getName())
                && StringUtils.isBlank(tCheckPhoneAndNameRequest.getMobile())) {
            return "姓名+手机号不能为空";
        }
        if (!MobileUtils.validateMobile(tCheckPhoneAndNameRequest.getMobile())) {
            return "手机号格式不正确";
        }
        return null;
    }

    @Override
    public CheckPhoneAndNameResponse run(TCheckPhoneAndNameRequest ap) throws Exception {
        CheckPhoneAndNameRequest checkPhoneAndNameRequest = new CheckPhoneAndNameRequest();
        checkPhoneAndNameRequest.setName(ap.getName());
        checkPhoneAndNameRequest.setMobile(ap.getMobile());
        checkPhoneAndNameRequest.setEncryption(ap.getEncryption());
        return client.CheckPhoneAndName(checkPhoneAndNameRequest);
    }
}
