package pposRequest

import (
	"errors"
	"net/url"
)

func (that *PPOSClient) NewMemberGetRequest() *MemberGetRequest {
	return &MemberGetRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/member/member/get",
			publicParamsSlice: []string{
				"member_id",
			},
		}}
}

type MemberGetRequest struct {
	Request
}

//Params ...
func (that *MemberGetRequest) 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 *MemberGetRequest) Do() (*MemberGet, error) {
	//返回data
	_data, _err := that.Client.GetData(that.apiAddress, that)
	if _err != nil {
		return nil, _err
	}
	//
	_info := new(MemberGet)
	if _b, _ok := _data.(bool); _ok && !_b {
		//会员不存在
		return _info, nil
	}
	_mp, _ok := _data.(map[string]interface{})
	if !_ok {
		return nil, errors.New(that.apiAddress + "请求响应格式不正确,错误代码01")
	}
	if _err := that.ParseResponeMp(_info, _mp); _err != nil {
		return nil, errors.New(that.apiAddress + "请求响应格式化异常:" + _err.Error())
	}
	//返回结果
	return _info, nil
}
