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

package pposRequest

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

//new...
func (that *PPOSClient) NewCfgCateList() *CfgCateListRequest {
	return &CfgCateListRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/cfg/cfgCate/list",
		},
	}
}

//Request...
type CfgCateListRequest struct {
	Request
	Flag int64 `json:"flag"` //1-商户端获取开启协议的业务,例如1
}

func (that *CfgCateListRequest) SetFlag(value int64) *CfgCateListRequest {
	that.Flag = value
	return that
}

//Params ...
func (that *CfgCateListRequest) Params() (url.Values, error) {
	that.initParams()
	_params := that.params
	_params.Set("flag", utils.ToString(that.Flag))
	return _params, nil
}

//Do ...
func (that *CfgCateListRequest) Do() (CfgCateListResponse, error) {
	_rs := CfgCateListResponse{}
	//
	_data, _err := that.Client.GetData(that.apiAddress, that)
	if _err != nil {
		return _rs, _err
	}
	//
	_slice := []string{
		"count",
		"list",
		"page",
		"size",
	}
	//
	_sliceData, _ok := _data.(map[string]interface{})
	if !_ok {
		return _rs, errors.New(that.apiAddress + "请求响应格式不正确")
	}
	//
	for _, _val := range _slice {
		if _, _ok := _sliceData[_val]; !_ok {
			return _rs, errors.New(that.apiAddress + "请求响应参数不正确")
		}
	}
	//
	_str, _err := json.Marshal(_sliceData)
	if _err != nil {
		return _rs, errors.New(that.apiAddress + "Umarshal failed!")
	}
	_err = json.Unmarshal(_str, &_rs)
	if _err != nil {
		return _rs, errors.New(that.apiAddress + "Umarshal failed2!")
	}
	return _rs, nil
}
