/**
 * 核心服务接口sdk（golang版）
 *
 * @desc 啪啪运动 核心服务接口sdk
 * ---------------------------------------------------------------------
 * @author		unphp <unphp@qq.com>
 * @date		2018-12-18
 * @copyright	core-utils
 * ---------------------------------------------------------------------
 */

package pposRequest

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

//NewCfgCateGetByID 获取配置
func (that *PPOSClient) NewCfgCateGetByID() *CfgCateGetByIDRequest {
	return &CfgCateGetByIDRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/cfg/cfgCate/getByIds",
		},
	}
}

//CfgCateGetByIDRequest 业务类别
type CfgCateGetByIDRequest struct {
	Request
	TradeCateIds string `json:"trade_cate_ids"` //类别ID
	Tree         int64  `json:"tree"`           //类别根id
}

func (that *CfgCateGetByIDRequest) SetCateID(tradeCateIds string) *CfgCateGetByIDRequest {
	that.TradeCateIds = tradeCateIds
	return that
}

func (that *CfgCateGetByIDRequest) SetTree(tree int64) *CfgCateGetByIDRequest {
	that.Tree = tree
	return that
}
func (that *CfgCateGetByIDRequest) SetIsRefundRt(kind int64) *CfgCateGetByIDRequest {
	that.initParams()
	that.params.Set("kind", utils.ToString(kind))
	return that
}

//Params ...
func (that *CfgCateGetByIDRequest) Params() (url.Values, error) {
	that.initParams()
	_params := that.params
	_params.Set("trade_cate_ids", utils.ToString(that.TradeCateIds))
	if that.Tree > 0 {
		_params.Set("tree", utils.ToString(that.Tree))
	}
	return _params, nil
}

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

//
func (that *CfgCateGetByIDRequest) DoTree() (interface{}, 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 + "请求响应格式不正确")
	}
	for _, _v := range _sliceData {
		_, _ok := _v.(map[string]interface{})
		if !_ok {
			return nil, errors.New(that.apiAddress + "请求响应格式不正确2")
		}
	}
	return _data, nil
}
