/**
 * 核心服务接口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) NewCfgWxPublicExportList() *CfgWxPublicExportListRequest {
	return &CfgWxPublicExportListRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/wxMp/public/list",
			publicParamsSlice: []string{
			},
		},
	}
}

//Request...
type CfgWxPublicExportListRequest struct {
	Request
	Page int64 `json:"page"` //页码
	Size int64 `json:"size"` //每页数量
}

func (that *CfgWxPublicExportListRequest) SetPage(value int64) *CfgWxPublicExportListRequest {
	that.initParams()
	that.params.Set("page",utils.ToString(value))
	return that
}

func (that *CfgWxPublicExportListRequest) SetSize(value int64) *CfgWxPublicExportListRequest {
	that.initParams()
	that.params.Set("size",utils.ToString(value))
	return that
}

//Params ...
func (that *CfgWxPublicExportListRequest) 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 *CfgWxPublicExportListRequest) Do() ([]*CfgWxPublicExportList, error) {
	//
	_data, _err := that.Client.GetData(that.apiAddress, that)
	if _err != nil {
		return nil, _err
	}
	//
	_slice := []string{
		"count",
		"list",
		"page",
		"size",
	}
	//
	_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 + "请求响应参数不正确")
		}
	}
	//data为 分页结构
	_listData, _err := that.ParseList(_data)
	if _err != nil {
		return nil, _err
	}
	//返回结果
	_rs := []*CfgWxPublicExportList{}
	_list, _ok := _listData.List.([]interface{})
	if !_ok {
		return nil, errors.New(that.apiAddress + "请求响应格式不正确")
	}
	for _, _v := range _list {
		_vData, _ok := _v.(map[string]interface{})
		if !_ok {
			return nil, errors.New(that.apiAddress + "请求响应格式不正确2")
		}
		_tmp := new(CfgWxPublicExportList)
		if _err := that.ParseResponeMp(_tmp, _vData); _err != nil {
			return nil, errors.New(that.apiAddress + "请求响应格式化异常:" + _err.Error())
		}
		_rs = append(_rs, _tmp)
	}
	//
	return _rs, nil
}
