package pposRequest

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

//参数：子系统ID
func (that *PPOSClient) NewOrderOvertimeRequest(sysID int64, businessType int64) (*OverTimeCancelRequest, error) {
	switch sysID {
	case 11:
		return that.NewMemberCardOvertimeRequest(), nil //会员订单超时..
	case 12:
		return that.NewTicketOvertimeRequest(), nil //门票订单超时..
	case 13:
		return that.NewFieldOvertimeRequest(), nil //场地订单超时..
	case 14:
		return that.NewGoodsOvertimeRequest(), nil //商品订单超时
	case 16:
		return that.NewEventsOvertimeRequest(), nil //赛事订单超时
	case 17:
		if businessType == 1701 {
			return that.NewClassCourseOvertimeRequest(), nil //课程
		}
		return nil, errors.New("该业务接口暂未实现。")
	case 20:
		return that.NewJointgoodsOvertimeRequest(), nil //营销-组合订单超时..
	case 30:
		return that.NewActivityOvertimeRequest(), nil //活动平台
	case 40:
		return that.NewMmpCashOrderOvertimeRequest(), nil //会员中台
	default:
		return nil, errors.New("该业务接口暂未实现")
	}
}

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

//NewMemberCardOvertimeRequest 订单超时（会员订单,释放会员卡）
func (that *PPOSClient) NewMemberCardOvertimeRequest() *OverTimeCancelRequest {
	return &OverTimeCancelRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/cardWork/memberCard/overtime",
			publicParamsSlice: []string{
				"order_no",
			},
		},
	}
}

//NewFieldOvertimeRequest 订单超时（场地订单）
func (that *PPOSClient) NewFieldOvertimeRequest() *OverTimeCancelRequest {
	return &OverTimeCancelRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/field/wxPay/overtime",
			publicParamsSlice: []string{
				"parent_order_no",
				"order_no",
			},
		},
	}
}

//NewGoodsOvertimeRequest 订单超时（商品订单）
func (that *PPOSClient) NewGoodsOvertimeRequest() *OverTimeCancelRequest {
	return &OverTimeCancelRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/goods/goodsOrder/overtime",
			publicParamsSlice: []string{
				"parent_order_no",
				"order_no",
			},
		},
	}
}

//NewEventsOvertimeRequest 订单超时（赛事订单）
func (that *PPOSClient) NewEventsOvertimeRequest() *OverTimeCancelRequest {
	return &OverTimeCancelRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/events/order/overTime",
			publicParamsSlice: []string{
				"parent_order_no",
				"order_no",
			},
		},
	}
}

//NewTicketOvertimeRequest 订单超时（门票订单）
func (that *PPOSClient) NewTicketOvertimeRequest() *OverTimeCancelRequest {
	return &OverTimeCancelRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/ticket/apiNotice/overtime",
			publicParamsSlice: []string{
				"parent_order_no",
				"order_no",
			},
		},
	}
}

//NewJointgoodsOvertimeRequest 订单超时（营销-组合订单）
func (that *PPOSClient) NewJointgoodsOvertimeRequest() *OverTimeCancelRequest {
	return &OverTimeCancelRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/marketing/apiNotice/overtime",
			publicParamsSlice: []string{
				"parent_order_no",
				"order_no",
			},
		},
	}
}

//NewClassCourseOvertimeRequest 订单超时（课程订单）
func (that *PPOSClient) NewClassCourseOvertimeRequest() *OverTimeCancelRequest {
	return &OverTimeCancelRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/class/courseOrder/overtime",
			publicParamsSlice: []string{
				"parent_order_no",
				"order_no",
			},
		},
	}
}

//NewActivityOvertimeRequest 订单超时（活动平台）
func (that *PPOSClient) NewActivityOvertimeRequest() *OverTimeCancelRequest {
	return &OverTimeCancelRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/activityPlatform/activity/applyOrderOverTime",
			publicParamsSlice: []string{
				"parent_order_no",
				"order_no",
			},
		},
	}
}

//NewMmpCashOrderOvertimeRequest 订单超时（会员中台）
func (that *PPOSClient) NewMmpCashOrderOvertimeRequest() *OverTimeCancelRequest {
	return &OverTimeCancelRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/memberMiddlePlatform/cashOrder/overtime",
			publicParamsSlice: []string{
				"parent_order_no",
				"order_no",
			},
		},
	}
}

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

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

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