package pposRequest

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

//NewOrderStatusRequest 参数：子系统ID、具体业务类型
func (that *PPOSClient) NewOrderStatusRequest(sysID int64) (*OrderStatusRequest, error) {
	switch sysID {
	case 11:
		return that.NewCardWorkMemberCardOrderStatusRequest(), nil //会员
	case 12:
		return that.NewTicketOrderStatusRequest(), nil //门票
	case 13:
		return that.NewFieldOrderStatusRequest(), nil //场地
	//case 14:
	//	return that.NewFieldOrderStatusRequest(), nil //商品
	//case 15:
	//	return that.NewFieldOrderStatusRequest(), nil //通用收款
	//case 16:
	//	return that.NewFieldOrderStatusRequest(), nil //赛事
	case 17:
		return that.NewClassCourseOrderStatusRequest(), nil //培训：课程、课时
	//case 18:
	//	return that.NewFieldOrderStatusRequest(), nil //租赁
	default:
		return nil, errors.New("该业务接口暂未实现")
	}
}

//
type OrderStatusRequest struct {
	Request
	TradeTypeID       string   //业务类型
	publicParamsSlice []string //必传参数名
}

//OrderStatusResponse 响应：订单状态
type OrderStatusResponse struct {
	OrderStatus string `json:"order_status"`
}

//NewTicketOrderStatusRequest 门票订单
func (that *PPOSClient) NewTicketOrderStatusRequest() *OrderStatusRequest {
	return &OrderStatusRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/ticket/ticketOrderDetail/status",
			publicParamsSlice: []string{
				"order_no",
			},
		},
	}
}

//NewFieldOrderStatusRequest 场地订单
func (that *PPOSClient) NewFieldOrderStatusRequest() *OrderStatusRequest {
	return &OrderStatusRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/field/fieldOrderDetail/status",
			publicParamsSlice: []string{
				"order_no",
			},
		},
	}
}

//NewCardWorkMemberCardOrderStatusRequest 会员订单
func (that *PPOSClient) NewCardWorkMemberCardOrderStatusRequest() *OrderStatusRequest {
	return &OrderStatusRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/cardWork/memberCard/status",
			publicParamsSlice: []string{
				"order_no",
			},
		},
	}
}

//NewGoodsOrderStatusRequest 商品订单
func (that *PPOSClient) NewGoodsOrderStatusRequest() *OrderStatusRequest {
	return &OrderStatusRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/goods/goodsOrderDetail/status",
			publicParamsSlice: []string{
				"order_no",
			},
		},
	}
}

//NewClassCourseOrderStatusRequest 课程订单
func (that *PPOSClient) NewClassCourseOrderStatusRequest() *OrderStatusRequest {
	return &OrderStatusRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/class/courseOrder/status",
			publicParamsSlice: []string{
				"order_no",
			},
		},
	}
}

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

//Params ...
func (that *OrderStatusRequest) 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 *OrderStatusRequest) Do() (*OrderStatusResponse, error) {
	//返回data
	_data, _err := that.Client.GetData(that.apiAddress, that)
	if _err != nil {
		return nil, _err
	}
	_rs := new(OrderStatusResponse)
	if _err := utils.MapToStruct(_data, _rs); _err != nil {
		return nil, _err
	}
	return _rs, nil
}
