package pposRequest

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

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

//Request...
type CfgCommonGetByCodeRequest struct {
	Request
}

func (that *CfgCommonGetByCodeRequest) SetCfgCode(value string) *CfgCommonGetByCodeRequest {
	that.initParams()
	that.params.Set("cfg_code", utils.ToString(value))
	return that
}

func (that *CfgCommonGetByCodeRequest) SetCompanyId(value int64) *CfgCommonGetByCodeRequest {
	that.initParams()
	that.params.Set("company_id", utils.ToString(value))
	return that
}

func (that *CfgCommonGetByCodeRequest) SetBusinessId(value int64) *CfgCommonGetByCodeRequest {
	that.initParams()
	that.params.Set("business_id", utils.ToString(value))
	return that
}

//Params ...
func (that *CfgCommonGetByCodeRequest) 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 CfgCommonGetByCodeResponse struct {
	CfgCode  string `json:"cfg_code"`
	CfgId    int64  `json:"cfg_id"`
	CfgValue string `json:"cfg_value"`
	CfgFutureValue string `json:"cfg_future_value"`
	CfgWorkType int64 `json:"cfg_work_type"`
}

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