package pposRequest

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

//参数：子系统ID
func (that *PPOSClient) NewRefundAuditNoticeRequest(sysID int64, businessType int64) (*RefundAuditNoticeRequest, error) {
	switch sysID {
	case 11:
		return that.NewMemberRefundAuditNoticeRequest(), nil //会员
	case 12:
		return that.NewTicketRefundAuditNoticeRequest(), nil //门票
	case 13:
		return that.NewFieldRefundAuditNoticeRequest(), nil //场地
	case 14:
		return that.NewGoodsRefundAuditNoticeRequest(), nil //商品
	case 15:
		return that.NewCosRefundAuditNoticeRequest(), nil //通用收款
	case 16:
		return that.NewEventsRefundAuditNoticeRequest(), nil //赛事
	case 17:
		if businessType == 1702 || businessType == 1706 || businessType == 1708 {
			return that.NewCourseRefundAuditNoticeRequest(), nil //课程、续课
		}
		if businessType == 1704 {
			return that.NewClassRefundAuditNoticeRequest(), nil //课时
		}
		return nil, errors.New("not this businessType")
	case 18:
		return that.NewLeaseRefundAuditNoticeRequest(), nil //租赁
	case 20:
		return that.NewJointgoodsRefundAuditNoticeRequest(), nil //组合产品
	case 30:
		return that.NewActivityRefundAuditNoticeRequest(), nil //活动平台
	case 40:
		return that.NewMmpCashOrderRefundAuditNoticeRequest(), nil //会员中台
	default:
		return nil, errors.New("not this type request")
	}
}

//订单退物回调（门票订单）
func (that *PPOSClient) NewTicketRefundAuditNoticeRequest() *RefundAuditNoticeRequest {
	return &RefundAuditNoticeRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/ticket/orderRefund/refundNotice",
		},
		publicParamsSlice: []string{
			"parent_order_no",
			"order_no",
		},
		bT: time.Now(),
	}
}

//订单退物回调（组合产品订单）
func (that *PPOSClient) NewJointgoodsRefundAuditNoticeRequest() *RefundAuditNoticeRequest {
	return &RefundAuditNoticeRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/marketing/orderRefund/refundNotice",
		},
		publicParamsSlice: []string{
			"parent_order_no",
			"order_no",
		},
		bT: time.Now(),
	}
}

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

//订单退物回调 (商品订单)
func (that *PPOSClient) NewGoodsRefundAuditNoticeRequest() *RefundAuditNoticeRequest {
	return &RefundAuditNoticeRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/goods/goodsOrder/refundNotice",
		},
		publicParamsSlice: []string{
			"parent_order_no",
			"order_no",
		},
		bT: time.Now(),
	}
}

//订单退物回调（场地订单）
func (that *PPOSClient) NewFieldRefundAuditNoticeRequest() *RefundAuditNoticeRequest {
	return &RefundAuditNoticeRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/field/orderRefund/refundNotice",
		},
		publicParamsSlice: []string{
			"parent_order_no",
			"order_no",
		},
		bT: time.Now(),
	}
}

//订单退物回调（会员订单）
func (that *PPOSClient) NewMemberRefundAuditNoticeRequest() *RefundAuditNoticeRequest {
	return &RefundAuditNoticeRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/cardWork/memberCard/refundNotice",
		},
		publicParamsSlice: []string{
			"parent_order_no",
			"order_no",
		},
		bT: time.Now(),
	}
}

//订单退物回调 (赛事订单)
func (that *PPOSClient) NewEventsRefundAuditNoticeRequest() *RefundAuditNoticeRequest {
	return &RefundAuditNoticeRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/events/contestOrder/refundNotice",
		},
		publicParamsSlice: []string{
			"parent_order_no",
			"order_no",
		},
		bT: time.Now(),
	}
}

//订单退物回调 (培训课时订单)
func (that *PPOSClient) NewClassRefundAuditNoticeRequest() *RefundAuditNoticeRequest {
	return &RefundAuditNoticeRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/class/classOrder/refundNotice",
		},
		publicParamsSlice: []string{
			"parent_order_no",
			"order_no",
		},
		bT: time.Now(),
	}
}

//订单退物回调 (培训课程订单)
func (that *PPOSClient) NewCourseRefundAuditNoticeRequest() *RefundAuditNoticeRequest {
	return &RefundAuditNoticeRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/class/courseOrder/refundNotice",
		},
		publicParamsSlice: []string{
			"parent_order_no",
			"order_no",
		},
		bT: time.Now(),
	}
}

//订单退物回调 (租赁订单)
func (that *PPOSClient) NewLeaseRefundAuditNoticeRequest() *RefundAuditNoticeRequest {
	return &RefundAuditNoticeRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/lease/orderRefund/refundNotice",
		},
		publicParamsSlice: []string{
			"parent_order_no",
			"order_no",
		},
		bT: time.Now(),
	}
}

//订单退物回调 (活动平台皮卡订单)
func (that *PPOSClient) NewActivityRefundAuditNoticeRequest() *RefundAuditNoticeRequest {
	return &RefundAuditNoticeRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/activityPlatform/activity/applyOrderRefundNotice",
		},
		publicParamsSlice: []string{
			"parent_order_no",
			"order_no",
		},
		bT: time.Now(),
	}
}

//订单退物回调 (会员中台订单)
func (that *PPOSClient) NewMmpCashOrderRefundAuditNoticeRequest() *RefundAuditNoticeRequest {
	return &RefundAuditNoticeRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/memberMiddlePlatform/cashOrder/refundNotice",
		},
		publicParamsSlice: []string{
			"parent_order_no",
			"order_no",
		},
		bT: time.Now(),
	}
}

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

func (that *RefundAuditNoticeRequest) SetParentOrderNo(value string) *RefundAuditNoticeRequest {
	that.initParams()
	that.params.Set("parent_order_no", value) //订单中心的退单总订单号，唯一
	return that
}
func (that *RefundAuditNoticeRequest) SetOrderNo(value string) *RefundAuditNoticeRequest {
	that.initParams()
	that.params.Set("order_no", value) //退款订单订单号(业务子系统的订单号)，唯一
	return that
}

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