package ability19

import (
	"errors"
	"topsdk"
	"topsdk/ability19/request"
	"topsdk/ability19/response"
	"topsdk/util"
)

type Ability19 struct {
	Client *topsdk.TopClient
}

func NewAbility19(client *topsdk.TopClient) *Ability19 {
	return &Ability19{client}
}

/*
换货商家确认收货并发货
*/
func (ability *Ability19) TmallExchangeConfirmConsign(req *request.TmallExchangeConfirmConsignRequest, session string) (*response.TmallExchangeConfirmConsignResponse, error) {
	if ability.Client == nil {
		return nil, errors.New("Ability19 topClient is nil")
	}
	var jsonStr, err = ability.Client.ExecuteWithSession("tmall.exchange.confirm.consign", req.ToMap(), req.ToFileMap(), session)
	var respStruct = response.TmallExchangeConfirmConsignResponse{}
	if err != nil {
		// log.Println("tmallExchangeConfirmConsign 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 *Ability19) TmallExchangeGet(req *request.TmallExchangeGetRequest, session string) (*response.TmallExchangeGetResponse, error) {
	if ability.Client == nil {
		return nil, errors.New("Ability19 topClient is nil")
	}
	var jsonStr, err = ability.Client.ExecuteWithSession("tmall.exchange.get", req.ToMap(), req.ToFileMap(), session)
	var respStruct = response.TmallExchangeGetResponse{}
	if err != nil {
		// log.Println("tmallExchangeGet 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 *Ability19) TmallExchangeReceiveGet(req *request.TmallExchangeReceiveGetRequest, session string) (*response.TmallExchangeReceiveGetResponse, error) {
	if ability.Client == nil {
		return nil, errors.New("Ability19 topClient is nil")
	}
	var jsonStr, err = ability.Client.ExecuteWithSession("tmall.exchange.receive.get", req.ToMap(), req.ToFileMap(), session)
	var respStruct = response.TmallExchangeReceiveGetResponse{}
	if err != nil {
		// log.Println("tmallExchangeReceiveGet 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 *Ability19) TmallExchangeReturngoodsAgree(req *request.TmallExchangeReturngoodsAgreeRequest, session string) (*response.TmallExchangeReturngoodsAgreeResponse, error) {
	if ability.Client == nil {
		return nil, errors.New("Ability19 topClient is nil")
	}
	var jsonStr, err = ability.Client.ExecuteWithSession("tmall.exchange.returngoods.agree", req.ToMap(), req.ToFileMap(), session)
	var respStruct = response.TmallExchangeReturngoodsAgreeResponse{}
	if err != nil {
		// log.Println("tmallExchangeReturngoodsAgree 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 *Ability19) TmallExchangeMessageAdd(req *request.TmallExchangeMessageAddRequest, session string) (*response.TmallExchangeMessageAddResponse, error) {
	if ability.Client == nil {
		return nil, errors.New("Ability19 topClient is nil")
	}
	var jsonStr, err = ability.Client.ExecuteWithSession("tmall.exchange.message.add", req.ToMap(), req.ToFileMap(), session)
	var respStruct = response.TmallExchangeMessageAddResponse{}
	if err != nil {
		// log.Println("tmallExchangeMessageAdd 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 *Ability19) TmallExchangeMessagesGet(req *request.TmallExchangeMessagesGetRequest, session string) (*response.TmallExchangeMessagesGetResponse, error) {
	if ability.Client == nil {
		return nil, errors.New("Ability19 topClient is nil")
	}
	var jsonStr, err = ability.Client.ExecuteWithSession("tmall.exchange.messages.get", req.ToMap(), req.ToFileMap(), session)
	var respStruct = response.TmallExchangeMessagesGetResponse{}
	if err != nil {
		// log.Println("tmallExchangeMessagesGet 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 *Ability19) TmallExchangeAgree(req *request.TmallExchangeAgreeRequest, session string) (*response.TmallExchangeAgreeResponse, error) {
	if ability.Client == nil {
		return nil, errors.New("Ability19 topClient is nil")
	}
	var jsonStr, err = ability.Client.ExecuteWithSession("tmall.exchange.agree", req.ToMap(), req.ToFileMap(), session)
	var respStruct = response.TmallExchangeAgreeResponse{}
	if err != nil {
		// log.Println("tmallExchangeAgree 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 *Ability19) TmallExchangeRefuse(req *request.TmallExchangeRefuseRequest, session string) (*response.TmallExchangeRefuseResponse, error) {
	if ability.Client == nil {
		return nil, errors.New("Ability19 topClient is nil")
	}
	var jsonStr, err = ability.Client.ExecuteWithSession("tmall.exchange.refuse", req.ToMap(), req.ToFileMap(), session)
	var respStruct = response.TmallExchangeRefuseResponse{}
	if err != nil {
		// log.Println("tmallExchangeRefuse 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 *Ability19) TmallExchangeReturngoodsRefuse(req *request.TmallExchangeReturngoodsRefuseRequest, session string) (*response.TmallExchangeReturngoodsRefuseResponse, error) {
	if ability.Client == nil {
		return nil, errors.New("Ability19 topClient is nil")
	}
	var jsonStr, err = ability.Client.ExecuteWithSession("tmall.exchange.returngoods.refuse", req.ToMap(), req.ToFileMap(), session)
	var respStruct = response.TmallExchangeReturngoodsRefuseResponse{}
	if err != nil {
		// log.Println("tmallExchangeReturngoodsRefuse 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 *Ability19) TmallExchangeRefusereasonGet(req *request.TmallExchangeRefusereasonGetRequest, session string) (*response.TmallExchangeRefusereasonGetResponse, error) {
	if ability.Client == nil {
		return nil, errors.New("Ability19 topClient is nil")
	}
	var jsonStr, err = ability.Client.ExecuteWithSession("tmall.exchange.refusereason.get", req.ToMap(), req.ToFileMap(), session)
	var respStruct = response.TmallExchangeRefusereasonGetResponse{}
	if err != nil {
		// log.Println("tmallExchangeRefusereasonGet 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 *Ability19) TmallExchangeConsigngoods(req *request.TmallExchangeConsigngoodsRequest, session string) (*response.TmallExchangeConsigngoodsResponse, error) {
	if ability.Client == nil {
		return nil, errors.New("Ability19 topClient is nil")
	}
	var jsonStr, err = ability.Client.ExecuteWithSession("tmall.exchange.consigngoods", req.ToMap(), req.ToFileMap(), session)
	var respStruct = response.TmallExchangeConsigngoodsResponse{}
	if err != nil {
		// log.Println("tmallExchangeConsigngoods error", err)
		return &respStruct, err
	}
	err = util.HandleJsonResponse(jsonStr, &respStruct)
	if respStruct.Body == "" || len(respStruct.Body) == 0 {
		respStruct.Body = jsonStr
	}
	return &respStruct, err
}
