package pposRequest

import (
	"errors"
	"net/url"
)

//NewMemberMemberGetByBlurRequest 通过（手机号、会员名称，卡号） 获取信息
func (that *PPOSClient) NewMemberMemberGetByBlurRequest() *MemberGetByBlurRequest {
	return &MemberGetByBlurRequest{
		Request: Request{
			Client:            that,
			apiAddress:        "/member/member/getOrderByBlur",
			publicParamsSlice: []string{},
		},
	}
}

//
type MemberGetByBlurRequest struct {
	Request
	publicParamsSlice []string //必传参数名
}

func (that *MemberGetByBlurRequest) SetKeyword(value string) *MemberGetByBlurRequest {
	that.initParams()
	that.params.Set("keyword", value)
	return that
}

//Params ...
func (that *MemberGetByBlurRequest) Params() (url.Values, error) {
	that.initParams()
	_params := that.params
	//
	for _, _v := range that.publicParamsSlice {
		if _params.Get(_v) == "" {
			return nil, errors.New(that.apiAddress + "缺少参数:" + _v)
		}
	}
	return _params, nil
}

type MemberGetByBlurResponse struct {
	MemberId     int64  `json:"member_id"`
	MemberCardId int64  `json:"member_card_id"`
	Sex          int64  `json:"sex"`
	CardNum      string `json:"card_num"`
	Name         string `json:"name"`
	Phone        string `json:"phone"`
	AvatarUrl    string `json:"avatar_url"`
}

//Do ...
func (that *MemberGetByBlurRequest) Do() ([]*MemberGetByBlurResponse, error) {
	//返回data
	_data, _err := that.Client.GetData(that.apiAddress, that)
	if _err != nil {
		return nil, _err
	}
	//返回结果
	_rs := []*MemberGetByBlurResponse{}
	_sliceData, _ok := _data.([]interface{})
	if !_ok {
		return nil, errors.New(that.apiAddress + "请求响应格式不正确,错误代码01")
	}
	for _, _v := range _sliceData {
		_vData, _ok := _v.(map[string]interface{})
		if !_ok {
			return nil, errors.New(that.apiAddress + "请求响应格式不正确,错误代码02")
		}
		_tmp := new(MemberGetByBlurResponse)
		if _err := that.ParseResponeMp(_tmp, _vData); _err != nil {
			return nil, errors.New(that.apiAddress + "请求响应格式化异常:" + _err.Error())
		}
		_rs = append(_rs, _tmp)
	}
	return _rs, nil
}
