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

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

//NewPayRefundRequest ... 微信、支付宝退款
func (that *CooperClient) NewPayRefundRequest() *PayRefundRequest {
	return &PayRefundRequest{
		Request: Request{
			Client:            that,
			publicParamsSlice: []string{
				//...
			},
			apiAddress: "/pay/refund",
		},
	}
}

//PayRefundRequest
type PayRefundRequest struct {
	Request
}

func (that *PayRefundRequest) SetSrcTradeNo(value string) *PayRefundRequest {
	that.initParams()
	that.params.Set("src_trade_no", value) //支付原交易号
	return that
}
func (that *PayRefundRequest) SetOrderNo(value string) *PayRefundRequest {
	that.initParams()
	that.params.Set("order_no", value) //总订单号
	return that
}
func (that *PayRefundRequest) SetOrderFrom(value string) *PayRefundRequest {
	that.initParams()
	that.params.Set("order_from", value) //订单来源
	return that
}
func (that *PayRefundRequest) SetPaymentWay(value string) *PayRefundRequest {
	that.initParams()
	that.params.Set("payment_way", value) //支付方式
	return that
}
func (that *PayRefundRequest) SetPaymentType(value string) *PayRefundRequest {
	that.initParams()
	that.params.Set("payment_type", value) //支付类型
	return that
}
func (that *PayRefundRequest) SetTConfigId(value int64) *PayRefundRequest {
	that.initParams()
	that.params.Set("t_config_id", utils.ToString(value)) //支付账户配置ID
	return that
}
func (that *PayRefundRequest) SetWxOpenId(value string) *PayRefundRequest {
	that.initParams()
	that.params.Set("wx_open_id", value) //用户唯一标识
	return that
}
func (that *PayRefundRequest) SetRefundAmount(value string) *PayRefundRequest {
	that.initParams()
	that.params.Set("refund_amount", value) //需要退款的金额，该金额不能大于订单金额,单位为分
	return that
}
func (that *PayRefundRequest) SetRefundReason(value string) *PayRefundRequest {
	that.initParams()
	that.params.Set("refund_reason", value) //退款的原因说明
	return that
}

//Params ...
func (that *PayRefundRequest) 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
}

//Do ...
func (that *PayRefundRequest) Do() (*PayResponse, error) {
	//返回data
	_data, _err := that.Client.Post(that.apiAddress, that)
	if _err != nil {
		return nil, _err
	}
	//
	_slice := []string{
		"trade_no", //支付中心交易号
	}
	//
	_sliceData, _ok := _data.(map[string]interface{})
	if !_ok {
		return nil, errors.New(that.apiAddress + "请求响应格式不正确")
	}
	//
	for _, _val := range _slice {
		if _, _ok := _sliceData[_val]; !_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(PayResponse)
	_err = json.Unmarshal(_str, &_rs)
	if _err != nil {
		return _rs, errors.New(that.apiAddress + _err.Error())
	}
	return _rs, nil
}
