package pposRequest

import (
	"errors"
	"net/url"
)

//参数：子系统ID
func (that *PPOSClient) NewRefundDetailRequest(sysID int64, businessType int64) (*RefundDetailRequest, error) {
	switch sysID {
	case 11:
		return that.NewMemberRefundDetailRequest(), nil //会员
	case 12:
		return that.NewTicketRefundDetailRequest(), nil //门票
	case 13:
		return that.NewFieldRefundDetailRequest(), nil //场地
	case 14:
		return that.NewGoodsRefundDetailRequest(), nil //商品
	case 15:
		return that.NewCosRefundDetailRequest(), nil //通用收款
	case 16:
		return that.NewEventsRefundDetailRequest(), nil //赛事
	case 17:
		if businessType == 1701 || businessType == 1705 || businessType == 1707 {
			return that.NewCourseRefundDetailRequest(), nil //课程、续课
		}
		if businessType == 1703 {
			return that.NewClassRefundDetailRequest(), nil //课时
		}
		return nil, errors.New("not this businessType")
	case 18:
		return that.NewLeaseRefundDetailRequest(), nil //租赁
	case 20:
		return that.NewJointgoodsRefundDetailRequest(), nil //组合产品
	case 40:
		return that.NewMmpCashOrderRefundDetailRequest(), nil //会员中台
	default:
		return nil, errors.New("not this type request")
	}
}

//退单详情（门票订单）
func (that *PPOSClient) NewTicketRefundDetailRequest() *RefundDetailRequest {
	return &RefundDetailRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/ticket/orderRefund/refundInfos",
		},
	}
}

//退单详情（组合产品订单）
func (that *PPOSClient) NewJointgoodsRefundDetailRequest() *RefundDetailRequest {
	return &RefundDetailRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/marketing/orderRefund/refundInfos",
		},
	}
}

//退单详情（通用收款订单）
func (that *PPOSClient) NewCosRefundDetailRequest() *RefundDetailRequest {
	return &RefundDetailRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/cos/orderRefund/refundInfos",
		},
	}
}

//退单详情 (商品订单)
func (that *PPOSClient) NewGoodsRefundDetailRequest() *RefundDetailRequest {
	return &RefundDetailRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/goods/goodsOrder/refundInfos",
		},
	}
}

//退单详情（场地订单）
func (that *PPOSClient) NewFieldRefundDetailRequest() *RefundDetailRequest {
	return &RefundDetailRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/field/orderRefund/refundInfos",
		},
	}
}

//退单详情（会员订单）
func (that *PPOSClient) NewMemberRefundDetailRequest() *RefundDetailRequest {
	return &RefundDetailRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/cardWork/memberCard/refundInfos",
		},
	}
}

//退单详情 (赛事订单)
func (that *PPOSClient) NewEventsRefundDetailRequest() *RefundDetailRequest {
	return &RefundDetailRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/events/contestOrder/refundInfos",
		},
		publicParamsSlice: []string{
			"parent_order_no",
			"order_no",
		},
	}
}

//培训课程详情 (赛事订单)
func (that *PPOSClient) NewCourseRefundDetailRequest() *RefundDetailRequest {
	return &RefundDetailRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/class/courseOrder/refundInfos",
		},
		publicParamsSlice: []string{
			"parent_order_no",
			"order_no",
		},
	}
}

//培训课时详情 (赛事订单)
func (that *PPOSClient) NewClassRefundDetailRequest() *RefundDetailRequest {
	return &RefundDetailRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/class/classOrder/refundInfos",
		},
		publicParamsSlice: []string{
			"parent_order_no",
			"order_no",
		},
	}
}

//退单详情 (租赁订单)
func (that *PPOSClient) NewLeaseRefundDetailRequest() *RefundDetailRequest {
	return &RefundDetailRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/lease/orderRefund/refundInfos",
		},
		publicParamsSlice: []string{
			"parent_order_no",
			"order_no",
		},
	}
}

//退单详情（会员中台订单）
func (that *PPOSClient) NewMmpCashOrderRefundDetailRequest() *RefundDetailRequest {
	return &RefundDetailRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/memberMiddlePlatform/cashOrder/refundInfos",
		},
	}
}

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

func (that *RefundDetailRequest) SetTradeNo(value string) *RefundDetailRequest {
	that.initParams()
	that.params.Set("trade_no", value) //交易号
	return that
}
func (that *RefundDetailRequest) SetParentOrderNo(value string) *RefundDetailRequest {
	that.initParams()
	that.params.Set("parent_order_no", value) //（订单中心的总退单号），唯一
	return that
}
func (that *RefundDetailRequest) SetOrderNo(value string) *RefundDetailRequest {
	that.initParams()
	that.params.Set("order_no", value) //需要退单的原订单号(业务子系统的订单号)，唯一
	return that
}

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

type RefundDetailResponse struct {
	OrderRefund     *OrderRefund
	OrderItemRefund []*OrderItemRefund
}

//Do ...
func (that *RefundDetailRequest) Do() ([]*RefundDetailResponse, error) {
	//返回data
	_data, _err := that.Client.GetData(that.apiAddress, that)
	if _err != nil {
		return nil, _err
	}
	_result, _ok := _data.([]interface{})
	if !_ok {
		return nil, errors.New(that.apiAddress + "请求响应格式不正确,错误代码01")
	}
	_list := []*RefundDetailResponse{}
	for _, _val := range _result {
		_mp, _ok := _val.(map[string]interface{})
		if !_ok {
			return nil, errors.New(that.apiAddress + "请求响应格式不正确,错误代码02")
		}
		//
		_slice := []string{"order", "item"}
		for _, _v := range _slice {
			if _, _ok := _mp[_v]; !_ok {
				return nil, errors.New(that.apiAddress + "请求响应格式不正确,错误代码03")
			}
		}
		//返回结果
		_rs := new(RefundDetailResponse)
		//
		_orderInfo := new(OrderRefund)
		if _err := that.ParseResponeMp(_orderInfo, _mp["order"]); _err != nil {
			return nil, errors.New(that.apiAddress + "请求响应格式化异常1:" + _err.Error())
		}
		_rs.OrderRefund = _orderInfo
		//
		_itemData, _ok := _mp["item"].([]interface{})
		if !_ok {
			return nil, errors.New(that.apiAddress + "请求响应格式不正确,错误代码04")
		}
		_itemSlice := []*OrderItemRefund{}
		for _k, _v := range _itemData {
			_item := new(OrderItemRefund)
			if _err := that.ParseResponeMp(_item, _v); _err != nil {
				return nil, errors.New(that.apiAddress + "请求响应格式化异常2:" + _err.Error())
			}
			_itemSlice = append(_itemSlice, _item)
			_itemSlice[_k].CodeList = []*CodeList{}
			//
			if _value, _ok := _v.(map[string]interface{}); _ok {
				if _codeList, _ok := _value["code_list"].([]interface{}); _ok {
					for _, _v1 := range _codeList {
						_codeValue := new(CodeList)
						if _err := that.ParseResponeMp(_codeValue, _v1); _err != nil {
							return nil, errors.New(that.apiAddress + "请求响应格式化异常2:" + _err.Error())
						}
						_itemSlice[_k].CodeList = append(_itemSlice[_k].CodeList, _codeValue)
					}
				}
			}
		}
		_rs.OrderItemRefund = _itemSlice
		_list = append(_list, _rs)
	}
	//
	return _list, nil
}
