package pposRequest

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

//参数：子系统ID、具体业务类型
func (that *PPOSClient) NewOrderInfoRequest(sysID int64, businessType int64) (*OrderDetailRequest, error) {
	switch sysID {
	case 11:
		return that.NewMemberOrderInfoRequest(), nil //会员
	case 12:
		return that.NewTicketOrderInfoRequest(), nil //门票
	case 13:
		return that.NewFieldOrderInfoRequest(), nil //场地
	case 14:
		return that.NewGoodsOrderInfoRequest(), nil //商品
	case 15:
		return that.NewCosOrderInfoRequest(), nil //通用收款
	case 16:
		return that.NewEventsOrderInfoRequest(), nil //赛事
	case 17:
		if businessType == 1701 || businessType == 1705 {
			return that.NewCourseOrderInfoRequest(), nil //课程
		}
		if businessType == 1703 {
			return that.NewClassOrderInfoRequest(), nil //课时
		}
	case 18:
		return that.NewLeaseOrderInfoRequest(), nil //租赁
	case 30:
		return that.NewActivityOrderInfoRequest(), nil //活动平台
	case 40:
		return that.NewMmpCashOrderInfoRequest(), nil //会员中台
	default:
		return nil, errors.New("该业务接口暂未实现")
	}
	return nil, errors.New("该业务接口暂未实现")
}

//订单详情（门票订单）http://183.60.143.50:9095
func (that *PPOSClient) NewTicketOrderInfoRequest() *OrderDetailRequest {
	return &OrderDetailRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/ticket/ticketOrderDetail/subOrderInfo",
		},
		bT: time.Now(),
	}
}

//订单详情（通用收款订单）
func (that *PPOSClient) NewCosOrderInfoRequest() *OrderDetailRequest {
	return &OrderDetailRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/cos/order/subOrderInfo",
		},
		bT: time.Now(),
	}
}

//订单详情 (商品订单)
func (that *PPOSClient) NewGoodsOrderInfoRequest() *OrderDetailRequest {
	return &OrderDetailRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/goods/goodsOrderDetail/subOrderInfo",
		},
		bT: time.Now(),
	}
}

//订单详情（场地订单）
func (that *PPOSClient) NewFieldOrderInfoRequest() *OrderDetailRequest {
	return &OrderDetailRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/field/fieldOrderDetail/subOrderInfo",
		},
		bT: time.Now(),
	}
}

//订单详情（会员订单）
func (that *PPOSClient) NewMemberOrderInfoRequest() *OrderDetailRequest {
	return &OrderDetailRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/cardWork/memberCard/subOrderInfo",
		},
		bT: time.Now(),
	}
}

//订单详情 (赛事订单)
func (that *PPOSClient) NewEventsOrderInfoRequest() *OrderDetailRequest {
	return &OrderDetailRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/events/order/subOrderInfo",
		},
		publicParamsSlice: []string{
			"order_no",
		},
		bT: time.Now(),
	}
}

//订单详情 (课程订单)
func (that *PPOSClient) NewClassOrderInfoRequest() *OrderDetailRequest {
	return &OrderDetailRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/class/classOrder/subOrderInfo",
		},
		publicParamsSlice: []string{
			"order_no",
		},
		bT: time.Now(),
	}
}

//订单详情 (课时订单)
func (that *PPOSClient) NewCourseOrderInfoRequest() *OrderDetailRequest {
	return &OrderDetailRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/class/courseOrder/subOrderInfo",
		},
		publicParamsSlice: []string{
			"order_no",
		},
		bT: time.Now(),
	}
}

//订单详情 (租赁订单)
func (that *PPOSClient) NewLeaseOrderInfoRequest() *OrderDetailRequest {
	return &OrderDetailRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/lease/leaseOrderDetail/subOrderInfo",
		},
		publicParamsSlice: []string{
			"order_no",
		},
		bT: time.Now(),
	}
}

//订单详情 (活动平台)
func (that *PPOSClient) NewActivityOrderInfoRequest() *OrderDetailRequest {
	return &OrderDetailRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/activityPlatform/activity/applyOrderInfo",
		},
		publicParamsSlice: []string{
			"order_no",
		},
		bT: time.Now(),
	}
}

//订单详情 (会员中台现金账户订单)
func (that *PPOSClient) NewMmpCashOrderInfoRequest() *OrderDetailRequest {
	return &OrderDetailRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/memberMiddlePlatform/cashOrder/subOrderInfo",
		},
		publicParamsSlice: []string{
			"order_no",
		},
		bT: time.Now(),
	}
}

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

func (that *OrderDetailRequest) SetParentOrderNo(value string) *OrderDetailRequest {
	that.initParams()
	that.params.Set("parent_order_no", value) //总单号
	return that
}
func (that *OrderDetailRequest) SetOrderNo(value string) *OrderDetailRequest {
	that.initParams()
	that.params.Set("order_no", value)
	return that
}

//Params ...
func (that *OrderDetailRequest) 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 *OrderDetailRequest) Do() (*OrderInfoResponse, error) {
	//返回data
	_data, _err := that.Client.GetData(that.apiAddress, that)
	log.Println(that.apiAddress+" 耗时: ", time.Since(that.bT))
	if _err != nil {
		return nil, _err
	}
	_mp, _ok := _data.(map[string]interface{})
	if !_ok {
		return nil, errors.New(that.apiAddress + "请求响应格式不正确,错误代码01")
	}
	_slice := []string{"order", "item", "member_info"}
	for _, _v := range _slice {
		if _, _ok := _mp[_v]; !_ok {
			return nil, errors.New(that.apiAddress + "请求响应格式不正确,错误代码02")
		}
	}
	//返回结果
	_rs := new(OrderInfoResponse)
	//
	_stadiumInfo := new(StadiumInfo)
	if _, _ok := _mp["stadium_info"].(map[string]interface{}); _ok {
		if _err := that.ParseResponeMp(_stadiumInfo, _mp["stadium_info"], false); _err != nil {
			return nil, errors.New(that.apiAddress + "请求响应格式化异常:" + _err.Error())
		}
	}
	_rs.StadiumInfo = _stadiumInfo
	//
	_memberInfo := new(MemberInfo)
	if _, _ok := _mp["member_info"].(map[string]interface{}); _ok {
		if _err := that.ParseResponeMp(_memberInfo, _mp["member_info"], false); _err != nil {
			return nil, errors.New(that.apiAddress + "请求响应格式化异常:" + _err.Error())
		}
	}
	_rs.MemberInfo = _memberInfo
	//
	_orderInfo := new(OrderInfo)
	if _err := that.ParseResponeMp(_orderInfo, _mp["order"], false); _err != nil {
		return nil, errors.New(that.apiAddress + "请求响应格式化异常:" + _err.Error())
	}
	_rs.Order = _orderInfo
	//
	_itemData, _ok := _mp["item"].([]interface{})
	if !_ok {
		return nil, errors.New(that.apiAddress + "请求响应格式不正确,错误代码01")
	}
	_itemSlice := []*OrderItem{}
	for _, _v := range _itemData {
		_item := new(OrderItem)
		if _err := that.ParseResponeMp(_item, _v, false); _err != nil {
			return nil, errors.New(that.apiAddress + "请求响应格式化异常:" + _err.Error())
		}
		_itemSlice = append(_itemSlice, _item)
	}
	_rs.Item = _itemSlice
	//
	return _rs, nil
}
