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

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

//NewPayGetOrderListRequest ... 查询订单交易
func (that *CooperClient) NewPayGetOrderListRequest() *PayGetOrderListRequest {
	return &PayGetOrderListRequest{
		Request: Request{
			Client: that,
			publicParamsSlice: []string{
				"business_id",
				//...
			},
			apiAddress: "/pay/getOrderList",
		},
	}
}

//PayGetOrderListRequest
type PayGetOrderListRequest struct {
	Request
}

//SetPaymentWay
func (that *PayGetOrderListRequest) SetPaymentWay(value int64) *PayGetOrderListRequest {
	that.initParams()
	that.params.Set("payment_way", utils.ToString(value))
	return that
}

func (that *PayGetOrderListRequest) SetOrderStatus(value int64) *PayGetOrderListRequest {
	that.initParams()
	that.params.Set("order_status", utils.ToString(value))
	return that
}

func (that *PayGetOrderListRequest) SetStartTime(value int64) *PayGetOrderListRequest {
	that.initParams()
	that.params.Set("start_time", utils.ToString(value))
	return that
}

func (that *PayGetOrderListRequest) SetEndTime(value int64) *PayGetOrderListRequest {
	that.initParams()
	that.params.Set("end_time", utils.ToString(value))
	return that
}

//Params ...
func (that *PayGetOrderListRequest) 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 PayGetOrderListResponse struct {
	TradeNo     string `json:"trade_no"`      //交易号
	OrderNo     string `json:"order_no"`      //订单号
	Price       int64  `json:"price"`         //金额
	PaymentWay  int64  `json:"payment_way"`   //支付方式
	TradeTypeId int64  `json:"trade_type_id"` //交易类型（进账、出账）
	TradeDate   int64  `json:"trade_date"`    //交易时间
}

//Do ...
func (that *PayGetOrderListRequest) Do() ([]*PayGetOrderListResponse, error) {
	//返回data
	_data, _err := that.Client.Post(that.apiAddress, that)
	if _err != nil {
		return nil, _err
	}
	//
	_str, _err := json.Marshal(_data)
	if _err != nil {
		return nil, errors.New(that.apiAddress + _err.Error())
	}
	//返回结果
	_rs := make([]*PayGetOrderListResponse, 0)
	_err = json.Unmarshal(_str, &_rs)
	if _err != nil {
		return _rs, errors.New(that.apiAddress + _err.Error())
	}
	return _rs, nil
}
