package pposRequest

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

//NewLockerGetCountRequest 集控柜
func (that *PPOSClient) NewLockerGetCountRequest() *LockerGetCountRequest {
	return &LockerGetCountRequest{
		Request: Request{
			Client:     that,
			publicParamsSlice: []string{},
			apiAddress:"/locker/lockers/getCountByBoxcentralIds",
		},
	}
}

//LockerGetCountRequest ...
type LockerGetCountRequest struct {
	Request
	KeyWord       string `json:"key_word"`       //
	BoxcentralIDSlice []int64
}

func (that *LockerGetCountRequest) SetBoxcentralIds(boxcentralID []int64) *LockerGetCountRequest {
	if len(that.BoxcentralIDSlice) < 1 {
		that.BoxcentralIDSlice = boxcentralID
	}
	return that
}

func (that *LockerGetCountRequest) SetKeyWord(keyWord string) *LockerGetCountRequest {
	that.initParams()
	that.params.Set("key_word", utils.ToString(keyWord))
	return that
}

//Params ...
func (that *LockerGetCountRequest) Params() (url.Values, error) {
	that.initParams()
	_params := that.params
	if that.BoxcentralIDSlice != nil {
		_params.Set("boxcentral_ids", utils.NewSliceInt64(that.BoxcentralIDSlice...).Join(","))
	}

	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 *LockerGetCountRequest) Do() ([]*LockerGetCountResponse, error) {
	//返回data
	_data, _err := that.Client.GetData(that.apiAddress, that)
	if _err != nil {
		return nil, _err
	}
	//返回结果
	_rs := []*LockerGetCountResponse{}
	_sliceData, _ok := _data.([]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(LockerGetCountResponse)
		if _err := that.ParseResponeMp(_tmp, _vData); _err != nil {
			return nil, errors.New(that.apiAddress + "请求响应格式化异常:" + _err.Error())
		}
		_rs = append(_rs, _tmp)
	}
	return _rs, nil
}
