package pposRequest

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

//NewMemberCardBusinessGetRequest 根据获取会员卡id获取信息
func (that *PPOSClient) NewMemberCardBusinessGetRequest() *MemberCardBusinessGetRequest {
	return &MemberCardBusinessGetRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/member/memberCard/businessGet",
			publicParamsSlice: []string{
				"member_card_id",
			},
		},
	}
}

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

func (that *MemberCardBusinessGetRequest) SetMemberCardId(value int64) *MemberCardBusinessGetRequest {
	that.initParams()
	that.params.Set("member_card_id", utils.ToString(value))
	return that
}

func (that *MemberCardBusinessGetRequest) SetWith(value int64) *MemberCardBusinessGetRequest {
	that.initParams()
	that.params.Set("with", utils.ToString(value))
	return that
}

func (that *MemberCardBusinessGetRequest) SetTradeNo(value string) *MemberCardBusinessGetRequest {
	that.initParams()
	that.params.Set("trade_no", value)
	return that
}

//
type MemberCardResponse struct {
	CompanyId       int64  `json:"company_id"`
	MemberCardId    int64  `json:"member_card_id"`
	Name            string `json:"name"`
	PCardId         int64  `json:"p_card_id"`
	CardLevel       int64  `json:"card_level"`
	CardNo          string `json:"card_no"`
	BusinessId      int64  `json:"business_id"`
	StadiumId       int64  `json:"stadium_id"`
	MemberId        int64  `json:"member_id"`
	AgreementId     int64  `json:"agreement_id"`
	Price           int64  `json:"price"`
	WorthPrice      int64  `json:"worth_price"`
	WorthDate       int64  `json:"worth_date"`
	ConsumePrice    int64  `json:"consume_price"`
	PayTrueWorth    int64  `json:"pay_true_worth"`
	PayGiveWorth    int64  `json:"pay_give_worth"`
	TrueWorth       int64  `json:"true_worth"`
	GiveWorth       int64  `json:"give_worth"`
	Worth           string `json:"worth"`
	MemberCardWorth string `json:"member_card_worth"`
	Status          int64  `json:"status"`
	PayTime         int64  `json:"pay_time"`
	OrderFrom       int64  `json:"order_from"`
	ProductId       int64  `json:"product_id"`
	//ValidityStartDate string             `json:"validity_start_date"`
	ValidityEndDate string             `json:"validity_end_date"`
	ActiveType      int64              `json:"active_type"`
	Des             string             `json:"des"`
	Introducer      int64              `json:"introducer"`
	IsDelete        int64              `json:"is_delete"`
	Operator        int64              `json:"operator"`
	MemberStatus    int64              `json:"member_status"`
	CardNum         string             `json:"card_num"`
	PCardNum        string             `json:"p_card_num"`
	OpenBusiness    string             `json:"open_business"`
	StatusStr       string             `json:"status_str"`
	MemberInfo      MemberInfoResponse `json:"member_info"`
	Unit            string             `json:"unit"`
	Type            int64              `json:"type"`
	CardLevelName   string             `json:"card_level_name"`
}
type MemberInfoResponse struct {
	MemberId      int64  `json:"member_id"`
	Name          string `json:"name"`
	Phone         string `json:"phone"`
	Avatar        string `json:"avatar"`
	Birthday      string `json:"birthday"`
	Sex           string `json:"sex"`
	Address       string `json:"address"`
	Remark        string `json:"remark"`
	WxOpenId      string `json:"wx_open_id"`
	MemberType    int64  `json:"member_type"`
	AvatarUrl     string `json:"avatar_url"`
	MemberTypeStr string `json:"member_type_str"`
}

//Params ...
func (that *MemberCardBusinessGetRequest) 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
}

//Do ...
func (that *MemberCardBusinessGetRequest) Do() (*MemberCardResponse, error) {
	//返回data
	_data, _err := that.Client.GetData(that.apiAddress, that)
	if _err != nil {
		return nil, _err
	}
	//返回结果
	_rs := new(MemberCardResponse)
	_byte, _err := json.Marshal(_data)
	if _err != nil {
		return nil, _err
	}
	_err = json.Unmarshal(_byte, _rs)
	if _err != nil {
		return nil, _err
	}
	return _rs, nil
}
