package pposRequest

import (
	"errors"
	"net/url"
)

//获取位置
func (that *PPOSClient) NewPositionListRequest() *PositionListRequest {
	return &PositionListRequest{
		Request: Request{
			Client: that,
			publicParamsSlice: []string{
				"page",
				"size",
			},
			apiAddress: "/device/position/list",
		},
	}
}

//PositionListRequest ...
type PositionListRequest struct {
	Request
}

func (that *PositionListRequest) SetPage(value string) *PositionListRequest {
	that.initParams()
	that.params.Set("page", value) //页码
	return that
}

func (that *PositionListRequest) SetSize(value string) *PositionListRequest {
	that.initParams()
	that.params.Set("size", value)
	return that
}

func (that *PositionListRequest) SetCompanyId(value string) *PositionListRequest {
	that.initParams()
	that.params.Set("company_id", value)
	return that
}

func (that *PositionListRequest) SetBusinessId(value string) *PositionListRequest {
	that.initParams()
	that.params.Set("business_id", value)
	return that
}

func (that *PositionListRequest) SetAccountID(value string) *PositionListRequest {
	that.initParams()
	that.params.Set("account_id", value)
	return that
}

//Params ...
func (that *PositionListRequest) 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 PositionListDataResponse struct {
	PositionId int64 `json:"position_id"`
}

//Do ...
func (that *PositionListRequest) Do() ([]*PositionListDataResponse, error) {
	_data, _err := that.Client.GetData(that.apiAddress, that)
	if _err != nil {
		return nil, _err
	}
	//
	//_sliceData, _ok := _data.(map[string]interface{})
	//if !_ok {
	//	return nil, errors.New(that.apiAddress + "请求响应格式不正确")
	//}
	//
	_rs := []*PositionListDataResponse{}
	_mp, _ok := _data.(map[string]interface{})
	_sliceData, _ok := _mp["list"].([]interface{})
	if !_ok {
		return nil, errors.New(that.apiAddress + "请求响应格式不正确")
	}
	for _, _v := range _sliceData {
		_vData, _ok := _v.(map[string]interface{})
		if !_ok {
			return nil, errors.New(that.apiAddress + "请求响应格式不正确2")
		}
		//
		_tmp := new(PositionListDataResponse)
		if _err := that.ParseResponeMp(_tmp, _vData); _err != nil {
			return nil, errors.New(that.apiAddress + "请求响应格式化异常:" + _err.Error())
		}
		_rs = append(_rs, _tmp)
	}
	return _rs, nil
}
