package pposRequest

import (
	"errors"
	"net/url"
)

//business_type 业务类型 1订场2门票
func (that *PPOSClient) NewGetMiniFieldTicketProjectRequest(businessType int64) (*GetMiniFieldTicketProjectRequest, error) {
	switch businessType {
	case 1:
		return that.NewGetMiniFieldProjectRequest(), nil //订场
	case 2:
		return that.NewGetMiniTicketProjectRequest(), nil //购票
	default:
		return nil, errors.New("not this type request")
	}
}

//NewGetMiniFieldProjectRequest 订场项目
func (that *PPOSClient) NewGetMiniFieldProjectRequest() *GetMiniFieldTicketProjectRequest {
	return &GetMiniFieldTicketProjectRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/field/wxFieldBuyPlan/getMiniFieldProject",
		},
		publicParamsSlice: []string{
			"stadium_ids",
		},
	}
}

//NewGetMiniTicketProjectRequest 购票项目
func (that *PPOSClient) NewGetMiniTicketProjectRequest() *GetMiniFieldTicketProjectRequest {
	return &GetMiniFieldTicketProjectRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/ticket/wxTicketSale/getMiniTicketProject",
		},
		publicParamsSlice: []string{
			"stadium_ids",
		},
	}
}

type GetMiniFieldTicketProjectRequest struct {
	Request
	publicParamsSlice []string         //必传参数名
}

func (that *GetMiniFieldTicketProjectRequest) SetStadiumIds(value string) *GetMiniFieldTicketProjectRequest {
	that.initParams()
	that.params.Set("stadium_ids", value) //场馆id
	return that
}

func (that *GetMiniFieldTicketProjectRequest) SetSportTagIds(value string) *GetMiniFieldTicketProjectRequest {
	that.initParams()
	that.params.Set("sport_tag_ids", value) //运动项目id
	return that
}

//Params ...
func (that *GetMiniFieldTicketProjectRequest) 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 *GetMiniFieldTicketProjectRequest) Do() ([]*GetMiniFieldTicketProjectResponse, error) {
	//
	_data, _err := that.Client.GetData(that.apiAddress, that)
	if _err != nil {
		return nil, _err
	}
	//
	_sliceData, _ok := _data.([]interface{})
	if !_ok {
		return nil, errors.New(that.apiAddress + "请求响应格式不正确")
	}
	//返回结果
	_rs := []*GetMiniFieldTicketProjectResponse{}
	for _, _v := range _sliceData {
		_vData, _ok := _v.(map[string]interface{})
		if !_ok {
			return nil, errors.New(that.apiAddress + "请求响应格式不正确2")
		}
		_tmp := new(GetMiniFieldTicketProjectResponse)
		if _err := that.ParseResponeMp(_tmp, _vData); _err != nil {
			return nil, errors.New(that.apiAddress + "请求响应格式化异常:" + _err.Error())
		}
		_rs = append(_rs, _tmp)
	}
	return _rs, nil
}
