package pposRequest

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

//参数：子系统ID
func (that *PPOSClient) NewRefundFreeRequest(sysID int64, businessType int64) (*RefundFreeRequest, error) {
	switch sysID {
	case 11:
		return that.NewMemberRefundFreeRequest(), nil //会员
	case 12:
		return that.NewTicketRefundFreeRequest(), nil //门票
	case 13:
		return that.NewFieldRefundFreeRequest(), nil //场地
	case 14:
		return that.NewGoodsRefundFreeRequest(), nil //商品
	case 15:
		return that.NewCosRefundFreeRequest(), nil //通用收款
	case 16:
		return that.NewEventsRefundFreeRequest(), nil //赛事
	case 17:
		if businessType == 1702 || businessType == 1706 || businessType == 1708 {
			return that.NewCourseRefundFreeRequest(), nil //课程、续课
		}
		if businessType == 1704 {
			return that.NewClassRefundFreeRequest(), nil //课时
		}
		return nil, errors.New("not this businessType")
	case 18:
		return that.NewLeaseRefundFreeRequest(), nil //租赁
	case 20:
		return that.NewJointgoodsRefundFreeRequest(), nil //组合产品
	case 30:
		return that.NewActivityRefundFreeRequest(), nil //活动平台
	case 40:
		return that.NewMmpCashOrderRefundFreeRequest(), nil //会员中台
	default:
		return nil, errors.New("not this type request")
	}
}

//退款订单退款（门票订单）
func (that *PPOSClient) NewTicketRefundFreeRequest() *RefundFreeRequest {
	return &RefundFreeRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/ticket/orderRefund/refundFree",
		},
		publicParamsSlice: []string{
			"parent_order_no",
			"order_no",
		},
		bT: time.Now(),
	}
}

//退款订单退款（组合产品订单）
func (that *PPOSClient) NewJointgoodsRefundFreeRequest() *RefundFreeRequest {
	return &RefundFreeRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/marketing/orderRefund/refundFree",
		},
		publicParamsSlice: []string{
			"parent_order_no",
			"order_no",
		},
		bT: time.Now(),
	}
}

//退款订单退款（通用收款订单）
func (that *PPOSClient) NewCosRefundFreeRequest() *RefundFreeRequest {
	return &RefundFreeRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/cos/orderRefund/refundFree",
		},
		publicParamsSlice: []string{
			"parent_order_no",
			"order_no",
		},
		bT: time.Now(),
	}
}

//退款订单退款 (商品订单)
func (that *PPOSClient) NewGoodsRefundFreeRequest() *RefundFreeRequest {
	return &RefundFreeRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/goods/goodsOrder/refundFree",
		},
		publicParamsSlice: []string{
			"parent_order_no",
			"order_no",
		},
		bT: time.Now(),
	}
}

//退款订单退款（场地订单）
func (that *PPOSClient) NewFieldRefundFreeRequest() *RefundFreeRequest {
	return &RefundFreeRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/field/orderRefund/refundFree",
		},
		publicParamsSlice: []string{
			"parent_order_no",
			"order_no",
		},
		bT: time.Now(),
	}
}

//退款订单退款（会员订单）
func (that *PPOSClient) NewMemberRefundFreeRequest() *RefundFreeRequest {
	return &RefundFreeRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/cardWork/memberCard/refundFree",
		},
		publicParamsSlice: []string{
			"parent_order_no",
			"order_no",
		},
		bT: time.Now(),
	}
}

//会员卡通过订单更新信息（会员订单）
func (that *PPOSClient) NewMemberRefundUpdateRequest() *RefundFreeRequest {
	return &RefundFreeRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/member/memberCard/updateForRefundOrder",
		},
		publicParamsSlice: []string{
			"parent_order_no",
			"order_no",
		},
		bT: time.Now(),
	}
}

//退款订单退款 (赛事订单)
func (that *PPOSClient) NewEventsRefundFreeRequest() *RefundFreeRequest {
	return &RefundFreeRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/events/contestOrder/refundFree",
		},
		publicParamsSlice: []string{
			"parent_order_no",
			"order_no",
		},
		bT: time.Now(),
	}
}

//退款订单退款 (培训课时订单)
func (that *PPOSClient) NewClassRefundFreeRequest() *RefundFreeRequest {
	return &RefundFreeRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/class/classOrder/refundFree",
		},
		publicParamsSlice: []string{
			"parent_order_no",
			"order_no",
		},
		bT: time.Now(),
	}
}

//退款订单退款 (培训课程订单)
func (that *PPOSClient) NewCourseRefundFreeRequest() *RefundFreeRequest {
	return &RefundFreeRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/class/courseOrder/refundFree",
		},
		publicParamsSlice: []string{
			"parent_order_no",
			"order_no",
		},
		bT: time.Now(),
	}
}

//退款订单退款 (租赁订单)
func (that *PPOSClient) NewLeaseRefundFreeRequest() *RefundFreeRequest {
	return &RefundFreeRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/lease/orderRefund/refundFree",
		},
		publicParamsSlice: []string{
			"parent_order_no",
			"order_no",
		},
		bT: time.Now(),
	}
}

//退款订单退款 (活动平台皮卡订单)
func (that *PPOSClient) NewActivityRefundFreeRequest() *RefundFreeRequest {
	return &RefundFreeRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/activityPlatform/activity/applyOrderRefundFree",
		},
		publicParamsSlice: []string{
			"parent_order_no",
			"order_no",
		},
		bT: time.Now(),
	}
}

//退款订单退款 (会员中台订单)
func (that *PPOSClient) NewMmpCashOrderRefundFreeRequest() *RefundFreeRequest {
	return &RefundFreeRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/memberMiddlePlatform/cashOrder/refundFree",
		},
		publicParamsSlice: []string{
			"parent_order_no",
			"order_no",
		},
		bT: time.Now(),
	}
}

//优惠撤销使用通知
func (that *PPOSClient) NewCouponBackNoticeRequest() *RefundFreeRequest {
	return &RefundFreeRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/coupon/orderDiscount/discountBackNotice",
		},
	}
}

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

func (that *RefundFreeRequest) SetParentOrderNo(value string) *RefundFreeRequest {
	that.initParams()
	that.params.Set("parent_order_no", value) //订单中心的退单总订单号，唯一
	return that
}
func (that *RefundFreeRequest) SetOperator(value int64) *RefundFreeRequest {
	that.initParams()
	that.params.Set("operator", utils.ToString(value)) //操作人
	return that
}
func (that *RefundFreeRequest) SetOrderNo(value string) *RefundFreeRequest {
	that.initParams()
	that.params.Set("order_no", value) //退款订单订单号(业务子系统的订单号)，唯一
	return that
}

//Params ...
func (that *RefundFreeRequest) 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 *RefundFreeRequest) Do() (interface{}, error) {
	//返回data
	_data, _err := that.Client.GetData(that.apiAddress, that)
	log.Println(that.apiAddress+" 耗时: ", time.Since(that.bT))
	if _err != nil {
		return nil, _err
	}
	_str, _ok := _data.(string)
	if _ok {
		return _str, nil
	}
	//
	return _data, nil
}
