// This file is auto-generated, don't edit it. Thanks.
package GoUtil

import (
	"fmt"

	sdk "github.com/alibabacloud-go/cloudauth-20190307/v4/client"
	openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
	console "github.com/alibabacloud-go/tea-console/client"
	util "github.com/alibabacloud-go/tea-utils/v2/service"
	"github.com/alibabacloud-go/tea/tea"
	credential "github.com/aliyun/credentials-go/credentials"
)

const (
	ACCESS_KEY_ID     = "LTAI5t8DKMexEjcBH1cCmrp9"
	ACCESS_KEY_SECRET = "p0KqKvTarTRXBXcBbUvfqSO9bynrCD"
)

type ClientInterface interface {
}

func IdVerify(UserName, Num string) (_err error) {
	// 构建request。
	request := &sdk.Id2MetaVerifyRequest{}
	request.ParamType = tea.String("normal")
	request.UserName = tea.String(UserName)
	request.IdentifyNum = tea.String(Num)
	// 自动路由服务。
	response, _err := Id2MetaVerifyAutoRoute(request)
	ret := util.ToJSONString(util.ToMap(response))
	if _err != nil {
		console.Log(tea.String("最终结果（若此处为空，则所有服务点均异常，请逐步调试）：" + tea.StringValue(ret)))
	}
	return _err
}

// Description:
//
// 主备服务点循环调用，获取到成功结果返回。
func Id2MetaVerifyAutoRoute(request *sdk.Id2MetaVerifyRequest) (_result *sdk.Id2MetaVerifyResponse, err error) {
	endpoints := []*string{tea.String("cloudauth.cn-shanghai.aliyuncs.com"), tea.String("cloudauth.cn-beijing.aliyuncs.com")}
	var lastResponse *sdk.Id2MetaVerifyResponse
	for _, endpoint := range endpoints {
		func() (_r *sdk.Id2MetaVerifyResponse, _e error) {
			defer func() {
				if r := tea.Recover(recover()); r != nil {
					_e = r
				}
			}()
			// 调用服务。
			response, _e := Id2MetaVerify(endpoint, request)
			if _e != nil {
				console.Error(tea.String("节点 " + tea.StringValue(endpoint) + " 调用失败，错误信息：" + _e.Error()))
				return // 如果调用失败，继续下一个服务点。
			}
			// 节点调用结果
			//ret := util.ToJSONString(util.ToMap(response))
			//console.Log(tea.String("节点 " + tea.StringValue(endpoint) + " 结果：" + tea.StringValue(ret) + " "))
			// 有一个服务调用成功即返回。
			lastResponse = response
			if !tea.BoolValue(util.IsUnset(response)) && tea.BoolValue(util.EqualNumber(tea.ToInt(response.StatusCode), tea.Int(200))) {
				if !tea.BoolValue(util.IsUnset(response.Body)) && tea.BoolValue(util.EqualString(response.Body.Code, tea.String("200"))) {
					return response, nil
				}
			}

			return nil, fmt.Errorf("节点 %s 调用失败，错误码：%s，错误信息：%s", tea.StringValue(endpoint), tea.StringValue(response.Body.Code), tea.StringValue(response.Body.Message))
		}()
	}
	_result = lastResponse
	if lastResponse == nil {
		return nil, fmt.Errorf("所有服务点调用失败，请检查网络或服务状态,或联系服务提供商")
	}
	if !tea.BoolValue(util.EqualString(lastResponse.Body.Code, tea.String("200"))) {
		return nil, fmt.Errorf("所有服务点调用失败，错误码：%s，错误信息：%s", tea.StringValue(lastResponse.Body.Code), tea.StringValue(lastResponse.Body.Message))
	}
	if !tea.BoolValue(util.EqualString(lastResponse.Body.ResultObject.BizCode, tea.String("1"))) {
		return nil, fmt.Errorf("校验失败，返回码：%s;", tea.StringValue(lastResponse.Body.ResultObject.BizCode))
	}
	return _result, nil
}

// Description:
//
// 获取服务Client实例，调用验证方法。
func Id2MetaVerify(endpoint *string, request *sdk.Id2MetaVerifyRequest) (_result *sdk.Id2MetaVerifyResponse, _err error) {
	// 获取SDK Client实例。
	client := CreateClient(endpoint)
	// 构建RuntimeObject
	runtime := &util.RuntimeOptions{}
	runtime.ReadTimeout = tea.Int(5000)
	runtime.ConnectTimeout = tea.Int(5000)
	// 连接
	_result = &sdk.Id2MetaVerifyResponse{}
	_body, _err := client.Id2MetaVerifyWithOptions(request, runtime)
	if _err != nil {
		return
	}
	_result = _body
	return
}

// Description:
//
// 安全创建服务Client实例。
func CreateClient(endpoint *string) (_result *sdk.Client) {
	// 获取Credential工具，此工具会从环境变量中读取AccessKey。
	credentialConfig := &credential.Config{
		Type:            tea.String("access_key"),
		AccessKeyId:     tea.String(ACCESS_KEY_ID),
		AccessKeySecret: tea.String(ACCESS_KEY_SECRET),
	}
	credential, _err := credential.NewCredential(credentialConfig)
	if _err != nil {
		return _result
	}
	// 创建SDK Client实例。
	apiConfig := &openapi.Config{}
	apiConfig.Credential = credential
	apiConfig.Endpoint = endpoint
	_result = &sdk.Client{}
	_result, _ = sdk.NewClient(apiConfig)
	return _result
}
