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

package pposRequest

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

//new...
func (that *PPOSClient) NewCfgCommonCateCfgList() *CfgCommonCateCfgListRequest {
	return &CfgCommonCateCfgListRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/cfg/cfgCommon/cateCfgList",
			publicParamsSlice: []string{
				"business_id",
			},
		},
	}
}

//Request...
type CfgCommonCateCfgListRequest struct {
	Request
	CateId int64 `json:"cate_id"` //类别分组
}

func (that *CfgCommonCateCfgListRequest) SetCateId(value int64) *CfgCommonCateCfgListRequest {
	that.CateId = value
	return that
}

//Params ...
func (that *CfgCommonCateCfgListRequest) Params() (url.Values, error) {
	that.initParams()
	_params := that.params
	_params.Set("cate_id", utils.ToString(that.CateId))
	//
	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 *CfgCommonCateCfgListRequest) Do() (*CfgCommonCateCfgListResponse, error) {
	//
	_data, _err := that.Client.GetData(that.apiAddress, that)
	if _err != nil {
		return nil, _err
	}
	//
	_slice := []string{
		"cate_id",
		"cate_name",
		"cfg_list",
	}
	//
	_sliceData, _ok := _data.(map[string]interface{})
	if !_ok {
		return nil, errors.New(that.apiAddress + "请求响应格式不正确")
	}
	//
	for _, _val := range _slice {
		if _, _ok := _sliceData[_val]; !_ok {
			return nil, errors.New(that.apiAddress + "请求响应参数不正确")
		}
	}
	_tmp := new(CfgCommonCateCfgListResponse)
	if _err := that.ParseResponeMp(_tmp, _sliceData); _err != nil {
		return nil, errors.New(that.apiAddress + "请求响应格式化异常:" + _err.Error())
	}
	//
	_itemSlice := []*BaseCfg{}
	_itemData, _ok := _sliceData["cfg_list"].([]interface{})
	if !_ok {
		return nil, errors.New(that.apiAddress + "请求响应格式不正确,错误代码01")
	}
	for _, _v := range _itemData {
		_item := new(BaseCfg)
		if _err := that.ParseResponeMp(_item, _v); _err != nil {
			return nil, errors.New(that.apiAddress + "请求响应格式化异常:" + _err.Error())
		}
		_itemSlice = append(_itemSlice, _item)
	}
	_tmp.CfgList = _itemSlice
	//
	return _tmp, nil
}
