/**
 * 核心服务接口sdk（golang版）
 *
 * @desc 啪啪运动 核心服务接口sdk
 * ---------------------------------------------------------------------
 * @author		tang <zhifu.tang@papa.com.cn>
 * @date		2024-01-18
 * @copyright	core-utils
 * ---------------------------------------------------------------------
 */
package cooper

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

//NewPayGetWxBillListRequest ... 查询微信账单
func (that *CooperClient) NewPayGetWxBillListRequest() *PayGetWxBillListRequest {
	return &PayGetWxBillListRequest{
		Request: Request{
			Client: that,
			publicParamsSlice: []string{
				"t_config_id",
				//...
			},
			apiAddress: "/pay/billWxList",
		},
	}
}

//PayGetWxBillListRequest
type PayGetWxBillListRequest struct {
	Request
}

func (that *PayGetWxBillListRequest) SetTConfigId(value int64) *PayGetWxBillListRequest {
	that.initParams()
	that.params.Set("t_config_id", utils.ToString(value))
	return that
}

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

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

func (that *PayGetWxBillListRequest) SetPage(value int64) *PayGetWxBillListRequest {
	that.initParams()
	that.params.Set("page", utils.ToString(value))
	return that
}

func (that *PayGetWxBillListRequest) SetSize(value int64) *PayGetWxBillListRequest {
	that.initParams()
	that.params.Set("size", utils.ToString(value))
	return that
}

//Params ...
func (that *PayGetWxBillListRequest) 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 PayGetWxBillListResponse struct {
	Count int64                           `json:"count"` //count
	List  []*PayGetWxBillListItemResponse `json:"list"`  //list
	Page  int64                           `json:"page"`  //page
	Size  int64                           `json:"size"`  //size
}

//
type PayGetWxBillListItemResponse struct {
	Id            int64  `json:"id"`             //id
	TConfigId     int64  `json:"t_config_id"`    //id
	TradeNo       string `json:"trade_no"`       //交易号
	OutRefundNo   string `json:"out_refund_no"`  //退单号
	Price         string `json:"price"`          //金额
	TradeTypeId   int64  `json:"trade_type_id"`  //交易类型（进账、出账）
	TradeType     string `json:"trade_type"`     //交易类型
	TradeDate     int64  `json:"trade_date"`     //交易时间
	TransactionId string `json:"transaction_id"` //微信订单号
	RefundId      string `json:"refund_id"`      //微信退款单号
	Body          string `json:"body"`           //摘要信息
	TradeState    int64  `json:"trade_state"`    //交易状态：1支付 2退款
}

//Do ...
func (that *PayGetWxBillListRequest) Do() (*PayGetWxBillListResponse, error) {
	_rs := new(PayGetWxBillListResponse)
	_data, _err := that.Client.Post(that.apiAddress, that)
	if _err != nil {
		return _rs, _err
	}
	//
	_slice := []string{
		"count",
		"list",
		"page",
		"size",
	}
	//
	_sliceData, _ok := _data.(map[string]interface{})
	if !_ok {
		return _rs, errors.New(that.apiAddress + "请求响应格式不正确")
	}
	//
	for _, _val := range _slice {
		if _, _ok := _sliceData[_val]; !_ok {
			return _rs, errors.New(that.apiAddress + "请求响应参数不正确2")
		}
	}
	//
	_str, _err := json.Marshal(_sliceData)
	if _err != nil {
		return _rs, errors.New(that.apiAddress + _err.Error())
	}
	_err = json.Unmarshal(_str, &_rs)
	if _err != nil {
		return _rs, errors.New(that.apiAddress + _err.Error())
	}
	return _rs, nil
}
