package pposRequest

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

//new... 标准配置项 - 详情，通过ID获取
func (that *PPOSClient) NewCfgCommonGetRequest() *CfgCommonGetRequest {
	return &CfgCommonGetRequest{
		Request: Request{
			Client:            that,
			apiAddress:        "/cfg/cfgCommon/get",
			publicParamsSlice: []string{},
		},
	}
}

//Request...
type CfgCommonGetRequest struct {
	Request
}

func (that *CfgCommonGetRequest) SetCfgId(value int64) *CfgCommonGetRequest {
	that.initParams()
	that.params.Set("cfg_id", utils.ToString(value))
	return that
}

//Params ...
func (that *CfgCommonGetRequest) 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
}

type CfgCommonGetResponse struct {
	CfgCode  string `json:"cfg_code"`
	CfgId    int64  `json:"cfg_id"`
	CfgTitle string `json:"cfg_title"`
	CfgValue string `json:"cfg_value"`
}

//Do ...
func (that *CfgCommonGetRequest) Do() ([]*CfgCommonGetResponse, 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 := []*CfgCommonGetResponse{}
	for _, _v := range _mp {
		_v1, _ok := _v.(map[string]interface{})
		if !_ok {
			return nil, errors.New(that.apiAddress + "请求响应格式不正确,错误代码01")
		}
		//
		_item := new(CfgCommonGetResponse)
		//
		if _err := that.ParseResponeMp(_item, _v1); _err != nil {
			return nil, errors.New(that.apiAddress + "请求响应格式化异常:" + _err.Error())
		}
		_dataSlice = append(_dataSlice, _item)
	}
	//
	return _dataSlice, nil
}
