package pposRequest

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

//new...
func (that *PPOSClient) NewSyncStadiumOrderInfo() *SyncStadiumOrderInfoRequest {
	return &SyncStadiumOrderInfoRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/platform/openData/orderGetList",
		},
	}
}

//Request...
type SyncStadiumOrderInfoRequest struct {
	Request
}

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

func (that *SyncStadiumOrderInfoRequest) SetSize(value int64) *SyncStadiumOrderInfoRequest {
	that.initParams()
	that.params.Set("size", utils.ToString(value)) //每页条数
	return that
}

func (that *SyncStadiumOrderInfoRequest) SetStartTime(value int64) *SyncStadiumOrderInfoRequest {
	that.initParams()
	that.params.Set("start_time", utils.ToString(value)) //开始时间
	return that
}

func (that *SyncStadiumOrderInfoRequest) SetEndTime(value int64) *SyncStadiumOrderInfoRequest {
	that.initParams()
	that.params.Set("end_time", utils.ToString(value)) //结束时间
	return that
}

//Params ...
func (that *SyncStadiumOrderInfoRequest) 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 StadiumOrderResponse struct {
	Count int64                           `json:"count"` //count
	List  []*SyncStadiumOrderInfoResponse `json:"list"`  //list
	Page  string                          `json:"page"`  //page
	Size  string                          `json:"size"`  //size
}
type SyncStadiumOrderInfoResponse struct {
	Id                 string `json:"-"`
	TradeCateId        int64  `json:"trade_cate_id"`
	ParentOrderNo      string `json:"parent_order_no"`
	TradeNo            string `json:"trade_no"`
	StadiumId          int64  `json:"stadium_id"`
	VenueId            string `json:"venueId"`
	MemberId           int64  `json:"member_id"`
	UserName           string `json:"user_name"`
	UserMobile         string `json:"user_mobile"`
	PayPrice           int64  `json:"pay_price"`
	OrderStatus        int64  `json:"order_status"`
	OrderFrom          int64  `json:"order_from"`
	CTime              int64  `json:"c_time"`
	PaymentTime        int64  `json:"payment_time"`
	SportTagId         int64  `json:"sport_tag_id"`
	User3RdPlatformId  int64  `json:"user3rd_platform_id"`
	Satisfaction       int64  `json:"satisfaction"`
	UserReserveSiteNum string `json:"user_reserve_site_num"`
	OrderNum           string `json:"order_num"`
	SportDate          string `json:"sport_date"`
	ExerciseTime       string `json:"exercise_time"`
	OrderValidTime     int64  `json:"order_valid_time"`
}

//Do ...
func (that *SyncStadiumOrderInfoRequest) Do() (*StadiumOrderResponse, error) {
	_rs := new(StadiumOrderResponse)
	_data, _err := that.Client.GetData(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
}
