package pposRequest

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

//NewMemberCardPayRequest  会员卡支付
func (that *PPOSClient) NewMemberCardPayRequest() *OrderPayRequest {
	return &OrderPayRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/member/memberCard/pay",
		},
		publicParamsSlice: []string{
			"member_card_id", //卡ID
			"trade_no",       //交易号
			"pay_worth",      //扣除价值
		},
		bT: time.Now(),
	}
}

//NewClassCoursePayRequest  课程课时卡支付
func (that *PPOSClient) NewClassCoursePayRequest() *OrderPayRequest {
	return &OrderPayRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/class/studentCourse/pay",
		},
		publicParamsSlice: []string{
			"student_course_id", //卡ID
			"trade_no",          //交易号
			"pay_worth",         //扣除价值
		},
		bT: time.Now(),
	}
}

//NewRechargeCardPayRequest  充值卡卡支付
func (that *PPOSClient) NewRechargeCardPayRequest() *OrderPayRequest {
	return &OrderPayRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/memberMiddlePlatform/rechargeCard/pay",
		},
		publicParamsSlice: []string{
			"recharge_card_id", //卡ID
			"trade_no",          //交易号
			"password",         //充值卡密码
			"use_member_id",    //充值卡使用人用户id
		},
		bT: time.Now(),
	}
}

type OrderPayRequest struct {
	Request
	publicParamsSlice []string  //必传参数名
	bT                time.Time //耗时时间
}

func (that *OrderPayRequest) SetMemberCardId(value int64) *OrderPayRequest {
	that.initParams()
	that.params.Set("member_card_id", utils.ToString(value))
	return that
}
func (that *OrderPayRequest) SetRechargeCardId(value int64) *OrderPayRequest {
	that.initParams()
	that.params.Set("recharge_card_id", utils.ToString(value))
	return that
}
func (that *OrderPayRequest) SetUseMemberId(value int64) *OrderPayRequest {
	that.initParams()
	that.params.Set("use_member_id", utils.ToString(value))
	return that
}
func (that *OrderPayRequest) SetStudentCourseId(value int64) *OrderPayRequest {
	that.initParams()
	that.params.Set("student_course_id", utils.ToString(value))
	return that
}
func (that *OrderPayRequest) SetOrderNo(value string) *OrderPayRequest {
	that.initParams()
	that.params.Set("order_no", value)
	return that
}
func (that *OrderPayRequest) SetTradeNo(value string) *OrderPayRequest {
	that.initParams()
	that.params.Set("trade_no", value)
	return that
}
func (that *OrderPayRequest) SetParentOrderNo(value string) *OrderPayRequest {
	that.initParams()
	that.params.Set("parent_order_no", value)
	return that
}
func (that *OrderPayRequest) SetPassword(value string) *OrderPayRequest {
	that.initParams()
	that.params.Set("password", value)
	return that
}
func (that *OrderPayRequest) SetOrderFrom(value interface{}) *OrderPayRequest {
	that.initParams()
	that.params.Set("order_from", utils.ToString(value))
	return that
}
func (that *OrderPayRequest) SetPayType(value int64) *OrderPayRequest {
	that.initParams()
	that.params.Set("pay_type", utils.ToString(value)) //1是退款 2是支付(仅仅针对于内部会员卡支付场景)
	return that
}
func (that *OrderPayRequest) SetPayWorth(value string) *OrderPayRequest {
	that.initParams()
	that.params.Set("pay_worth", value)
	return that
}
func (that *OrderPayRequest) SetOrderInfo(value string) *OrderPayRequest {
	that.initParams()
	that.params.Set("order_info", value)
	return that
}
func (that *OrderPayRequest) SetOrderSubject(value string) *OrderPayRequest {
	that.initParams()
	that.params.Set("order_subject", value)
	return that
}
func (that *OrderPayRequest) SetNotifyUrl(value string) *OrderPayRequest {
	that.initParams()
	that.params.Set("notify_url", value)
	return that
}
func (that *OrderPayRequest) SetExtendField(value string) *OrderPayRequest {
	that.initParams()
	that.params.Set("extend_field", value) //扩展字段，存放一些扩展的信息
	return that
}

//Params ...
func (that *OrderPayRequest) 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 MemberCardPayResponse struct {
	PayWorth        int64  `json:"pay_worth"`
	Worth           int64  `json:"worth"`
	RecordId        int64  `json:"record_id"`
	MemberId        int64  `json:"member_id"`          //会员ID(支付人)
	MemberCardId    int64  `json:"member_card_id"`     //会员卡ID(支付人)
	BuyMemberId     int64  `json:"buy_member_id"`      //会员ID(办理人)
	BuyMemberCardId int64  `json:"buy_member_card_id"` //会员卡ID(办理人)
	CardNum         string `json:"card_num"`           //会员卡号
	PCardNum        string `json:"p_card_num"`         //物理卡号
	Name            string `json:"name"`               //会员名称
	Phone           string `json:"phone"`              //手机号
	WxOpenId        string `json:"wx_open_id"`         //微信openID
}

//Do ...
func (that *OrderPayRequest) Do() (*MemberCardPayResponse, error) {
	//返回data
	_data, _err := that.Client.GetData(that.apiAddress, that)
	log.Println(that.apiAddress+" 耗时: ", time.Since(that.bT))
	if _err != nil {
		return nil, _err
	}
	_mp, _ok := _data.(map[string]interface{})
	if !_ok {
		return nil, errors.New(that.apiAddress + "请求响应格式不正确,错误代码01")
	}

	//返回结果
	_rs := new(MemberCardPayResponse)
	if _err := that.ParseResponeMp(_rs, _mp, false); _err != nil {
		return nil, errors.New(that.apiAddress + "请求响应格式化异常:" + _err.Error())
	}
	//
	return _rs, nil
}
