package pposRequest

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

//new... 标准配置项 - 大分类
func (that *PPOSClient) NewCfgCommonRootCfgList() *CfgCommonCateCfgRootRequest {
	return &CfgCommonCateCfgRootRequest{
		Request: Request{
			Client:            that,
			apiAddress:        "/cfg/cfgCommon/rootCfgList",
			publicParamsSlice: []string{},
		},
	}
}

//Request...
type CfgCommonCateCfgRootRequest struct {
	Request
}

func (that *CfgCommonCateCfgRootRequest) SetCateId(value int64) *CfgCommonCateCfgRootRequest {
	that.initParams()
	that.params.Set("cate_id", utils.ToString(value))
	return that
}

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