/**
 * 核心服务接口sdk（golang版）
 *
 * @desc 啪啪运动 核心服务接口sdk
 * ---------------------------------------------------------------------
 * @author		tang <unphp@qq.com>
 * @date		2021-05-26
 * @copyright	core-utils
 * ---------------------------------------------------------------------
 */
package cooper

import (
	"encoding/json"
	"errors"
	"net/url"
)

//NewGdPayerCodeApplyRequest ... 光大银行 - 云缴费 - 缴款码申请
func (that *CooperClient) NewGdPayerCodeApplyRequest() *GdPayerCodeApplyRequest {
	return &GdPayerCodeApplyRequest{
		Request: Request{
			Client: that,
			publicParamsSlice: []string{
				"pay_cfg",
				//...
			},
			apiAddress: "/pay/gdPayerCodeApply",
		},
	}
}

//GdPayerCodeApplyRequest
type GdPayerCodeApplyRequest struct {
	Request
}

//Params ...
func (that *GdPayerCodeApplyRequest) Params() (url.Values, error) {
	that.initParams()
	_params := that.params
	if that.publicParamsSlice != nil {
		for _, _v := range that.publicParamsSlice {
			if _params.Get(_v) == "" {
				return nil, errors.New(that.apiAddress + "缺少参数:" + _v)
			}
		}
	}
	return _params, nil
}

//
type GdPayerCodeApplyResponse struct {
	TradeNo string `json:"trade_no"`
	PayCode string `json:"pay_code"`
	AnsCode string `json:"ans_code"`
	AnsMsg  string `json:"ans_msg"`
}

//Do ...
func (that *GdPayerCodeApplyRequest) Do() (*GdPayerCodeApplyResponse, error) {
	//返回data
	_data, _err := that.Client.Post(that.apiAddress, that)
	if _err != nil {
		return nil, _err
	}
	//
	_sliceData, _ok := _data.(map[string]interface{})
	if !_ok {
		return nil, errors.New(that.apiAddress + "请求响应格式不正确")
	}
	//
	_str, _err := json.Marshal(_sliceData)
	if _err != nil {
		return nil, errors.New(that.apiAddress + _err.Error())
	}
	//返回结果
	_rs := new(GdPayerCodeApplyResponse)
	_err = json.Unmarshal(_str, &_rs)
	if _err != nil {
		return _rs, errors.New(that.apiAddress + _err.Error())
	}
	return _rs, nil
}
