package pposRequest

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

//NewMemberCardPayQueryRequest  会员卡支付查询
func (that *PPOSClient) NewMemberCardPayQueryRequest() *memberCardOrderPayQueryRequest {
	return &memberCardOrderPayQueryRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/member/memberCard/payQuery",
		},
		publicParamsSlice: []string{
			"trade_no",       //交易号
		},
		bT: time.Now(),
	}
}

type memberCardOrderPayQueryRequest struct {
	Request
	publicParamsSlice []string  //必传参数名
	bT                time.Time //耗时时间
}

func (that *memberCardOrderPayQueryRequest) SetMemberCardId(value int64) *memberCardOrderPayQueryRequest {
	that.initParams()
	that.params.Set("member_card_id", utils.ToString(value))
	return that
}
func (that *memberCardOrderPayQueryRequest) SetStudentCourseId(value int64) *memberCardOrderPayQueryRequest {
	that.initParams()
	that.params.Set("student_course_id", utils.ToString(value))
	return that
}
func (that *memberCardOrderPayQueryRequest) SetOrderNo(value string) *memberCardOrderPayQueryRequest {
	that.initParams()
	that.params.Set("order_no", value)
	return that
}
func (that *memberCardOrderPayQueryRequest) SetTradeNo(value string) *memberCardOrderPayQueryRequest {
	that.initParams()
	that.params.Set("trade_no", value)
	return that
}
func (that *memberCardOrderPayQueryRequest) SetParentOrderNo(value string) *memberCardOrderPayQueryRequest {
	that.initParams()
	that.params.Set("parent_order_no", value)
	return that
}

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

//Do ...
func (that *memberCardOrderPayQueryRequest) Do() (*MemberCardPayResponse, error) {
	//返回data
	_data, _err := that.Client.GetData(that.apiAddress, that)
	log.Println(that.apiAddress+" 耗时: ", time.Since(that.bT))
	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(MemberCardPayResponse)
	_err = json.Unmarshal(_str, &_rs)
	if _err != nil {
		return _rs, errors.New(that.apiAddress + _err.Error())
	}
	return _rs, nil



	/*_mp, _ok := _data.(map[string]interface{})
	if !_ok {
		return nil, errors.New(that.apiAddress + "请求响应格式不正确,错误代码01")
	}

	//返回结果
	_rs := new(MemberCardPayResponse)
	if _err := that.ParseResponeMp(_rs, _mp); _err != nil {
		return nil, errors.New(that.apiAddress + "请求响应格式化异常:" + _err.Error())
	}
	//
	return _rs, nil*/
}
