package pposRequest

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

//参数：子系统ID、具体业务类型
func (that *PPOSClient) NewOrderCancelRequest(sysID int64, businessType int64) (*OrderCancelRequest, error) {
	switch sysID {
	case 11:
		return that.NewCardWorkMemberCardOrderCancelRequest(), nil //取消会员
	case 12:
		return that.NewTicketPayOrderCancelRequest(), nil //门票
	case 13:
		return that.NewFieldPayOrderCancelRequest(), nil //场地
	case 14:
		return that.NewGoodsOrderCancelRequest(), nil //商品
	case 15:
		return that.NewCosOrderCancelRequest(), nil //通用收款
	case 16:
		return that.NewWxEventsOrderCancelRequest(), nil //赛事
	case 17:
		if businessType == 1701 {
			return that.NewClassCourseOrderCancelRequest(), nil //课程
		}
		return nil, errors.New("该业务接口暂未实现。")
	case 18:
		return that.NewLeaseOrderCancelRequest(), nil //租赁
	case 19:
		return that.NewMCOrderCancelRequest(), nil //消息
	case 20:
		return that.NewJointgoodsOrderCancelRequest(), nil //营销-组合
	case 30:
		return that.NewActivityOrderCancelRequest(), nil //活动平台
	case 40:
		return that.NewMmpCashOrderCancelRequest(), nil //会员中台
	default:
		return nil, errors.New("该业务接口暂未实现")
	}
}

//
type OrderCancelRequest struct {
	Request
	TradeNo           string
	OrderNo           string
	publicParamsSlice []string //必传参数名
}

//NewTicketPayCancelRequest 订单取消（门票订单）
func (that *PPOSClient) NewTicketPayOrderCancelRequest() *OrderCancelRequest {
	return &OrderCancelRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/ticket/pay/cancelOrder",
			publicParamsSlice: []string{
				"trade_no",
				"order_no",
			},
		},
	}
}

//NewJointgoodsOrderCancelRequest 订单取消（营销-组合订单）
func (that *PPOSClient) NewJointgoodsOrderCancelRequest() *OrderCancelRequest {
	return &OrderCancelRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/marketing/pay/cancelOrder",
			publicParamsSlice: []string{
				"trade_no",
				"order_no",
			},
		},
	}
}

//NewFieldPayCancelRequest 订单取消（场地订单）
func (that *PPOSClient) NewFieldPayOrderCancelRequest() *OrderCancelRequest {
	return &OrderCancelRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/field/pay/cancelOrder",
			publicParamsSlice: []string{
				"trade_no",
				"order_no",
			},
		},
	}
}

//NewCardWorkMemberCardCancelRequest 订单取消（会员订单）
func (that *PPOSClient) NewCardWorkMemberCardOrderCancelRequest() *OrderCancelRequest {
	return &OrderCancelRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/cardWork/memberCard/cancelOrder",
			publicParamsSlice: []string{
				"trade_no",
				"order_no",
			},
		},
	}
}

//NewMemberMemberCardCancelRequest 订单取消（会员订单,释放会员卡）
func (that *PPOSClient) NewMemberCardOrderCancelRequest() *OrderCancelRequest {
	return &OrderCancelRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/member/memberCard/cancelMemberCard",
			publicParamsSlice: []string{
				"trade_no",
				"order_no",
			},
		},
	}
}

//NewGoodsOrderCancelRequest 订单取消（商品订单）
func (that *PPOSClient) NewGoodsOrderCancelRequest() *OrderCancelRequest {
	return &OrderCancelRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/goods/goodsOrder/cancelOrder",
			publicParamsSlice: []string{
				"trade_no",
				"order_no",
			},
		},
	}
}

//NewCosOrderCancelRequest 订单取消（通用订单）
func (that *PPOSClient) NewCosOrderCancelRequest() *OrderCancelRequest {
	return &OrderCancelRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/cos/order/cancelOrder",
			publicParamsSlice: []string{
				"order_no",
			},
		},
	}
}

//异步回调到订单中心(订单取消)
func (that *PPOSClient) NewShopOrderCancelRequest() *OrderNoticeRequest {
	return &OrderNoticeRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/shop/order/cancel",
			publicParamsSlice: []string{
				"parent_order_no",
			},
		},
	}
}

//NewWxEventsOrderCancelRequest 订单取消（赛事订单）
func (that *PPOSClient) NewWxEventsOrderCancelRequest() *OrderCancelRequest {
	return &OrderCancelRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/events/order/cancelOrder",
			publicParamsSlice: []string{
				"order_no",
			},
		},
	}
}

//NewClassCourseOrderCancelRequest 订单取消（课程订单）
func (that *PPOSClient) NewClassCourseOrderCancelRequest() *OrderCancelRequest {
	return &OrderCancelRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/class/courseOrder/cancelOrder",
			publicParamsSlice: []string{
				"order_no",
			},
		},
	}
}

//NewLeaseOrderCancelRequest 订单取消（租赁订单）
func (that *PPOSClient) NewLeaseOrderCancelRequest() *OrderCancelRequest {
	return &OrderCancelRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/lease/pay/cancelOrder",
			publicParamsSlice: []string{
				"order_no",
			},
		},
	}
}

//NewMCOrderCancelRequest 订单取消（消息订单）
func (that *PPOSClient) NewMCOrderCancelRequest() *OrderCancelRequest {
	return &OrderCancelRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/mc/order/cancelOrder",
			publicParamsSlice: []string{
				"order_no",
			},
		},
	}
}

//NewActivityOrderCancelRequest 订单取消（活动平台）
func (that *PPOSClient) NewActivityOrderCancelRequest() *OrderCancelRequest {
	return &OrderCancelRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/activityPlatform/activity/applyOrderCancel",
			publicParamsSlice: []string{
				"order_no",
			},
		},
	}
}

//NewMmpCashOrderCancelRequest 订单取消（会员中台现金充值订单）
func (that *PPOSClient) NewMmpCashOrderCancelRequest() *OrderCancelRequest {
	return &OrderCancelRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/memberMiddlePlatform/cashOrder/cancelOrder",
			publicParamsSlice: []string{
				"trade_no",
				"order_no",
			},
		},
	}
}

//params...
func (that *OrderCancelRequest) SetOrderNo(value int64) *OrderCancelRequest {
	that.initParams()
	that.params.Set("order_no", utils.ToString(value))
	return that
}

func (that *OrderCancelRequest) SetParentOrderNo(value int64) *OrderCancelRequest {
	that.initParams()
	that.params.Set("parent_order_no", utils.ToString(value))
	return that
}

//Params ...
func (that *OrderCancelRequest) 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 *OrderCancelRequest) Do() (string, error) {
	//返回data
	_data, _err := that.Client.GetData(that.apiAddress, that)
	if _err != nil {
		return "", _err
	}
	//
	_msg, _ok := _data.(string)
	if _ok {
		return _msg, nil
	}
	//
	return "", nil
}
