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

package pposRequest

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

//NewCfgPayGetCfg 获取配置
func (that *PPOSClient) NewCfgPayGetCfg() *CfgPayGetCfgRequest {
	return &CfgPayGetCfgRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/cfg/pay/getCfg",
		},
	}
}

type CfgPayGetCfgResponse struct {
	AliConfig   *CfgPayConfig `json:"ali_config"`   //支付宝
	WxConfig    *CfgPayConfig `json:"wx_config"`    //微信
	UmsConfig   *CfgPayConfig `json:"ums_config"`   //银联商务
	UnionConfig *CfgPayConfig `json:"union_config"` //银联支付
}
type CfgPayConfig struct {
	ConfigId int64 `json:"t_config_id"`
	TypeId   int64 `json:"t_type_id"`
}

//CfgPayGetCfgRequest 业务类别
type CfgPayGetCfgRequest struct {
	Request
}

//Params ...
func (that *CfgPayGetCfgRequest) Params() (url.Values, error) {
	that.initParams()
	_params := that.params
	return _params, nil
}

func (that *CfgPayGetCfgRequest) SetUnitType(value int64) *CfgPayGetCfgRequest {
	that.initParams()
	that.params.Set("unit_type", utils.ToString(value))
	return that
}

func (that *CfgPayGetCfgRequest) SetUnitId(value int64) *CfgPayGetCfgRequest {
	that.initParams()
	that.params.Set("unit_id", utils.ToString(value))
	return that
}

//Do ...
func (that *CfgPayGetCfgRequest) Do() (*CfgPayGetCfgResponse, error) {
	//
	_data, _err := that.Client.GetData(that.apiAddress, that)
	if _err != nil {
		return nil, _err
	}
	//
	_sliceData, _ok := _data.(map[string]interface{})
	if !_ok {
		return nil, errors.New(that.apiAddress + "请求响应格式不正确")
	}
	_rs := new(CfgPayGetCfgResponse)
	_str, _err := json.Marshal(_sliceData)
	if _err != nil {
		return _rs, errors.New(that.apiAddress + "Umarshal failed!" + _err.Error())
	}
	_err = json.Unmarshal(_str, &_rs)
	if _err != nil {
		return _rs, errors.New(that.apiAddress + "Umarshal failed2!" + _err.Error())
	}
	//
	return _rs, nil
}
