package ability304

import (
	"errors"
	topsdk "git.rosy.net.cn/baseapi/platformapi/tao_vegetable/sdk"
	request2 "git.rosy.net.cn/baseapi/platformapi/tao_vegetable/sdk/ability304/request"
	response2 "git.rosy.net.cn/baseapi/platformapi/tao_vegetable/sdk/ability304/response"
	"git.rosy.net.cn/baseapi/platformapi/tao_vegetable/sdk/util"
	"log"
)

type Ability304 struct {
	Client *topsdk.TopClient
}

func NewAbility304(client *topsdk.TopClient) *Ability304 {
	return &Ability304{client}
}

/*
   获取ISV发起请求服务器IP
*/
func (ability *Ability304) TaobaoAppipGet(req *request2.TaobaoAppipGetRequest) (*response2.TaobaoAppipGetResponse, error) {
	if ability.Client == nil {
		return nil, errors.New("Ability304 topClient is nil")
	}
	var jsonStr, err = ability.Client.Execute("taobao.appip.get", req.ToMap(), req.ToFileMap())
	var respStruct = response2.TaobaoAppipGetResponse{}
	if err != nil {
		log.Println("taobaoAppipGet error", err)
		return &respStruct, err
	}
	err = util.HandleJsonResponse(jsonStr, &respStruct)
	if respStruct.Body == "" || len(respStruct.Body) == 0 {
		respStruct.Body = jsonStr
	}
	return &respStruct, err
}

/*
   获取授权账号对应的OpenUid
*/
func (ability *Ability304) TaobaoOpenuidGet(req *request2.TaobaoOpenuidGetRequest, session string) (*response2.TaobaoOpenuidGetResponse, error) {
	if ability.Client == nil {
		return nil, errors.New("Ability304 topClient is nil")
	}
	var jsonStr, err = ability.Client.ExecuteWithSession("taobao.openuid.get", req.ToMap(), req.ToFileMap(), session)
	var respStruct = response2.TaobaoOpenuidGetResponse{}
	if err != nil {
		log.Println("taobaoOpenuidGet error", err)
		return &respStruct, err
	}
	err = util.HandleJsonResponse(jsonStr, &respStruct)
	if respStruct.Body == "" || len(respStruct.Body) == 0 {
		respStruct.Body = jsonStr
	}
	return &respStruct, err
}

/*
   通过订单获取对应买家的openUID
*/
func (ability *Ability304) TaobaoOpenuidGetBytrade(req *request2.TaobaoOpenuidGetBytradeRequest, session string) (*response2.TaobaoOpenuidGetBytradeResponse, error) {
	if ability.Client == nil {
		return nil, errors.New("Ability304 topClient is nil")
	}
	var jsonStr, err = ability.Client.ExecuteWithSession("taobao.openuid.get.bytrade", req.ToMap(), req.ToFileMap(), session)
	var respStruct = response2.TaobaoOpenuidGetBytradeResponse{}
	if err != nil {
		log.Println("taobaoOpenuidGetBytrade error", err)
		return &respStruct, err
	}
	err = util.HandleJsonResponse(jsonStr, &respStruct)
	if respStruct.Body == "" || len(respStruct.Body) == 0 {
		respStruct.Body = jsonStr
	}
	return &respStruct, err
}

/*
   通过mixnick转换openuid
*/
func (ability *Ability304) TaobaoOpenuidGetBymixnick(req *request2.TaobaoOpenuidGetBymixnickRequest) (*response2.TaobaoOpenuidGetBymixnickResponse, error) {
	if ability.Client == nil {
		return nil, errors.New("Ability304 topClient is nil")
	}
	var jsonStr, err = ability.Client.Execute("taobao.openuid.get.bymixnick", req.ToMap(), req.ToFileMap())
	var respStruct = response2.TaobaoOpenuidGetBymixnickResponse{}
	if err != nil {
		log.Println("taobaoOpenuidGetBymixnick error", err)
		return &respStruct, err
	}
	err = util.HandleJsonResponse(jsonStr, &respStruct)
	if respStruct.Body == "" || len(respStruct.Body) == 0 {
		respStruct.Body = jsonStr
	}
	return &respStruct, err
}

/*
   业务文件获取
*/
func (ability *Ability304) TaobaoFilesGet(req *request2.TaobaoFilesGetRequest) (*response2.TaobaoFilesGetResponse, error) {
	if ability.Client == nil {
		return nil, errors.New("Ability304 topClient is nil")
	}
	var jsonStr, err = ability.Client.Execute("taobao.files.get", req.ToMap(), req.ToFileMap())
	var respStruct = response2.TaobaoFilesGetResponse{}
	if err != nil {
		log.Println("taobaoFilesGet error", err)
		return &respStruct, err
	}
	err = util.HandleJsonResponse(jsonStr, &respStruct)
	if respStruct.Body == "" || len(respStruct.Body) == 0 {
		respStruct.Body = jsonStr
	}
	return &respStruct, err
}

/*
   刷新Access Token
*/
func (ability *Ability304) TaobaoTopAuthTokenRefresh(req *request2.TaobaoTopAuthTokenRefreshRequest) (*response2.TaobaoTopAuthTokenRefreshResponse, error) {
	if ability.Client == nil {
		return nil, errors.New("Ability304 topClient is nil")
	}
	var jsonStr, err = ability.Client.Execute("taobao.top.auth.token.refresh", req.ToMap(), req.ToFileMap())
	var respStruct = response2.TaobaoTopAuthTokenRefreshResponse{}
	if err != nil {
		log.Println("taobaoTopAuthTokenRefresh error", err)
		return &respStruct, err
	}
	err = util.HandleJsonResponse(jsonStr, &respStruct)
	if respStruct.Body == "" || len(respStruct.Body) == 0 {
		respStruct.Body = jsonStr
	}
	return &respStruct, err
}

/*
   获取Access Token
*/
func (ability *Ability304) TaobaoTopAuthTokenCreate(req *request2.TaobaoTopAuthTokenCreateRequest) (*response2.TaobaoTopAuthTokenCreateResponse, error) {
	if ability.Client == nil {
		return nil, errors.New("Ability304 topClient is nil")
	}
	var jsonStr, err = ability.Client.Execute("taobao.top.auth.token.create", req.ToMap(), req.ToFileMap())
	var respStruct = response2.TaobaoTopAuthTokenCreateResponse{}
	if err != nil {
		log.Println("taobaoTopAuthTokenCreate error", err)
		return &respStruct, err
	}
	err = util.HandleJsonResponse(jsonStr, &respStruct)
	if respStruct.Body == "" || len(respStruct.Body) == 0 {
		respStruct.Body = jsonStr
	}
	return &respStruct, err
}

/*
   TOPDNS配置
*/
func (ability *Ability304) TaobaoHttpdnsGet(req *request2.TaobaoHttpdnsGetRequest) (*response2.TaobaoHttpdnsGetResponse, error) {
	if ability.Client == nil {
		return nil, errors.New("Ability304 topClient is nil")
	}
	var jsonStr, err = ability.Client.Execute("taobao.httpdns.get", req.ToMap(), req.ToFileMap())
	var respStruct = response2.TaobaoHttpdnsGetResponse{}
	if err != nil {
		log.Println("taobaoHttpdnsGet error", err)
		return &respStruct, err
	}
	err = util.HandleJsonResponse(jsonStr, &respStruct)
	if respStruct.Body == "" || len(respStruct.Body) == 0 {
		respStruct.Body = jsonStr
	}
	return &respStruct, err
}

/*
   sdk信息回调
*/
func (ability *Ability304) TaobaoTopSdkFeedbackUpload(req *request2.TaobaoTopSdkFeedbackUploadRequest) (*response2.TaobaoTopSdkFeedbackUploadResponse, error) {
	if ability.Client == nil {
		return nil, errors.New("Ability304 topClient is nil")
	}
	var jsonStr, err = ability.Client.Execute("taobao.top.sdk.feedback.upload", req.ToMap(), req.ToFileMap())
	var respStruct = response2.TaobaoTopSdkFeedbackUploadResponse{}
	if err != nil {
		log.Println("taobaoTopSdkFeedbackUpload error", err)
		return &respStruct, err
	}
	err = util.HandleJsonResponse(jsonStr, &respStruct)
	if respStruct.Body == "" || len(respStruct.Body) == 0 {
		respStruct.Body = jsonStr
	}
	return &respStruct, err
}

/*
   获取TOP通道解密秘钥
*/
func (ability *Ability304) TaobaoTopSecretGet(req *request2.TaobaoTopSecretGetRequest) (*response2.TaobaoTopSecretGetResponse, error) {
	if ability.Client == nil {
		return nil, errors.New("Ability304 topClient is nil")
	}
	var jsonStr, err = ability.Client.Execute("taobao.top.secret.get", req.ToMap(), req.ToFileMap())
	var respStruct = response2.TaobaoTopSecretGetResponse{}
	if err != nil {
		log.Println("taobaoTopSecretGet error", err)
		return &respStruct, err
	}
	err = util.HandleJsonResponse(jsonStr, &respStruct)
	if respStruct.Body == "" || len(respStruct.Body) == 0 {
		respStruct.Body = jsonStr
	}
	return &respStruct, err
}
