package pposRequest

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

func (that *PPOSClient) NewMemberCardIdsByCardNumRequest() *MemberCardIdsByCardNumRequest {
	return &MemberCardIdsByCardNumRequest{
		Request: Request{
			Client:            that,
			apiAddress:        "/member/memberCard/getByCardNum",
			publicParamsSlice: []string{},
		}}
}

type MemberCardIdsByCardNumRequest struct {
	Request
}

func (that *MemberCardIdsByCardNumRequest) SetCardNum(value string) *MemberCardIdsByCardNumRequest {
	that.initParams()
	that.params.Set("card_num", value)
	return that
}

//Params ...
func (that *MemberCardIdsByCardNumRequest) 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 *MemberCardIdsByCardNumRequest) Do() ([]*MemberCardNumInfo, int64, error) {
	//返回data
	_data, _err := that.Client.GetData(that.apiAddress, that)
	if _err != nil {
		return nil, 0, _err
	}
	//
	_mp, _ok := _data.(map[string]interface{})
	if !_ok {
		return nil, 0, errors.New(that.apiAddress + "请求响应格式不正确,错误代码01")
	}
	_itemData, _ok := _mp["special_card"].([]interface{})
	if !_ok {
		return nil, 0, errors.New(that.apiAddress + "请求响应格式不正确,错误代码01")
	}
	_itemSlice := []*MemberCardNumInfo{}
	for _, _v := range _itemData {
		_item := new(MemberCardNumInfo)
		if _err := that.ParseResponeMp(_item, _v); _err != nil {
			return nil, 0, errors.New(that.apiAddress + "请求响应格式化异常:" + _err.Error())
		}
		_itemSlice = append(_itemSlice, _item)
	}
	_memberId := int64(0)
	if _mp != nil {
		if utils.ToString(_mp["member_card_id"]) != "0" {
			_itemSlice = append(_itemSlice, &MemberCardNumInfo{MemberCardId: utils.ToInt64(_mp["member_card_id"])})
		}
		if utils.ToString(_mp["member_id"]) != "0" {
			_itemSlice = append(_itemSlice, &MemberCardNumInfo{MemberCardId: utils.ToInt64(_mp["member_card_id"])})
		}
		_memberId = utils.ToInt64(_mp["member_id"])
	}

	//返回结果
	return _itemSlice, _memberId, nil
}
