package pposRequest

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

//NewTicketHandCardCountRequest 手环机
func (that *PPOSClient) NewTicketHandCardCountRequest() *TicketHandCardCountRequest {
	return &TicketHandCardCountRequest{
		Request: Request{
			Client:     that,
			apiAddress:"/ticket/handCard/getCountByBraceletIds",
			publicParamsSlice: []string{},
		},
	}
}

//TicketHandCardCountRequest ...
type TicketHandCardCountRequest struct {
	Request
	BraceletIDSlice []int64 //手环机ID
}

//根据手环机id获取手环统计
//func (that *TicketHandCardCountRequest) TicketHandCardCount() *TicketHandCardCountRequest {
//	that.apiAddress = "/ticket/handCard/getCountByBraceletIds"
//	return that
//}

func (that *TicketHandCardCountRequest) SetBraceletIds(braceletID []int64) *TicketHandCardCountRequest {
	if len(that.BraceletIDSlice) < 1 {
		that.BraceletIDSlice = braceletID
	}
	return that
}

//Params ...
func (that *TicketHandCardCountRequest) Params() (url.Values, error) {
	that.initParams()
	_params := that.params
	if that.BraceletIDSlice != nil {
		_params.Set("bracelet_ids", utils.NewSliceInt64(that.BraceletIDSlice...).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 *TicketHandCardCountRequest) Do() ([]*TicketHandCardCountResponse, error) {
	//
	//
	_data, _err := that.Client.GetData(that.apiAddress, that)
	if _err != nil {
		return nil, _err
	}
	//
	_rs := []*TicketHandCardCountResponse{}
	_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(TicketHandCardCountResponse)
		if _err := that.ParseResponeMp(_tmp, _vData); _err != nil {
			return nil, errors.New(that.apiAddress + "请求响应格式化异常:" + _err.Error())
		}
		_rs = append(_rs, _tmp)
	}
	return _rs, nil
}
