package pposRequest

import (
	"errors"
	"net/url"
)

func (that *PPOSClient) NewTicketCodeRequest() *TicketCodeGetRequest {
	return &TicketCodeGetRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/ticket/ticketSale/getTicketCodeByOrderNo",
			publicParamsSlice: []string{
				"order_no",
			},
		}}
}

type TicketCodeGetRequest struct {
	Request
}

func (that *TicketCodeGetRequest) SetOrderNo(value string) *TicketCodeGetRequest {
	that.initParams()
	that.params.Set("order_no", value) //信息获取通过order_no
	return that
}

func (that *TicketCodeGetRequest) SetStatus(value string) *TicketCodeGetRequest {
	that.initParams()
	that.params.Set("status", value) //状态
	return that
}

//Params ...
func (that *TicketCodeGetRequest) 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 *TicketCodeGetRequest) Do() ([]*TicketCodeResponse, error) {
	//返回data
	_data, _err := that.Client.GetData(that.apiAddress, that)
	if _err != nil {
		return nil, _err
	}

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

	_list := []*TicketCodeResponse{}
	for _, _val := range _result {
		_mp, _ok := _val.(map[string]interface{})
		if !_ok {
			return nil, errors.New(that.apiAddress + "请求响应格式不正确,错误代码02")
		}

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