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

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

//NewPayBillListRequest ... 账户交易账单列表
func (that *CooperClient) NewPayBillListRequest() *PayBillListRequest {
	return &PayBillListRequest{
		Request: Request{
			Client:            that,
			publicParamsSlice: []string{
				//...
			},
			apiAddress: "/pay/billList",
		},
	}
}

//PayBillListRequest
type PayBillListRequest struct {
	Request
}

//账户id，多个逗号隔开
func (that *PayBillListRequest) SetTConfigIds(value string) *PayBillListRequest {
	that.initParams()
	that.params.Set("t_config_ids", value)
	return that
}

//开始时间
func (that *PayBillListRequest) SetStartDate(value string) *PayBillListRequest {
	that.initParams()
	that.params.Set("start_date", value)
	return that
}

//结束时间
func (that *PayBillListRequest) SetEndDate(value string) *PayBillListRequest {
	that.initParams()
	that.params.Set("end_date", value)
	return that
}

//Params ...
func (that *PayBillListRequest) 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 PayBillListResponse struct {
	TConfigId   int64  `json:"t_config_id"`  //账户ID
	BillDate    string `json:"bill_date"`    //账单日期
	BillType    string `json:"bill_type"`    //账单类型
	DownloadUrl string `json:"download_url"` //账单下载地址
}

//Do ...
func (that *PayBillListRequest) Do() ([]*PayBillListResponse, 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([]*PayBillListResponse, 0)
	_err = json.Unmarshal(_str, &_rs)
	if _err != nil {
		return _rs, errors.New(that.apiAddress + _err.Error())
	}
	return _rs, nil
}
