package pposRequest

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

//NewBaseAccountBusiness 获取账户授权的中心场馆
func (that *PPOSClient) NewBaseAccountBusiness() *BaseAccountBusinessRequest {
	return &BaseAccountBusinessRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/base/account/business",
		},
	}
}

//BaseAccountBusinessRequest ...
type BaseAccountBusinessRequest struct {
	Request
}

func (that *BaseAccountBusinessRequest) SetStatus(status, page, size int) *BaseAccountBusinessRequest {
	that.initParams()
	that.params.Set("status", utils.ToString(status))
	that.params.Set("page", utils.ToString(page))
	that.params.Set("size", utils.ToString(size))
	that.params.Set("account_id", utils.ToString(2))
	return that
}

//Params ...
func (that *BaseAccountBusinessRequest) 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 BaseAccountBusinessResponse struct {
	BusinessId      int64  `json:"business_id"`
	BusinessLogoUrl string `json:"business_logo_url"`
	BusinessName    string `json:"business_name"`
}
type BaseAccountBusinessStadiumResponse struct {
	StadiumId   int64  `json:"stadium_id"`
	Logo        string `json:"logo"`
	StadiumName string `json:"stadium_name"`
}

//Do ...
func (that *BaseAccountBusinessRequest) Do() ([]*BaseAccountBusinessResponse, 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 := make([]*BaseAccountBusinessResponse, 0)
	//
	_str, _err := json.Marshal(_sliceData)
	if _err != nil {
		return nil, errors.New(that.apiAddress + "Umarshal failed!")
	}
	_err = json.Unmarshal(_str, &_rs)
	if _err != nil {
		return nil, errors.New(that.apiAddress + "Umarshal failed2!")
	}
	//
	return _rs, nil
}
