package pposRequest

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

//参数：子系统ID
func (that *PPOSClient) NewRefundItemRequest(sysID int64, businessType int64) (*RefundItemRequest, error) {
	switch sysID {
	case 11:
		return that.NewMemberRefundItemRequest(), nil //会员
	case 12:
		return that.NewTicketRefundItemRequest(), nil //门票
	case 13:
		return that.NewFieldRefundItemRequest(), nil //场地
	case 14:
		return that.NewGoodsRefundItemRequest(), nil //商品
	case 15:
		return that.NewCosRefundItemRequest(), nil //通用收款
	case 16:
		return that.NewEventsRefundItemRequest(), nil //赛事
	case 17:
		if businessType == 1701 || businessType == 1705 || businessType == 1707 {
			return that.NewCourseRefundItemRequest(), nil //课程、续课
		}
		if businessType == 1703 {
			return that.NewClassRefundItemRequest(), nil //课时
		}
		return nil, errors.New("not this businessType")
	case 18:
		return that.NewLeaseRefundItemRequest(), nil //租赁
	case 20:
		return that.NewJointgoodsRefundItemRequest(), nil //组合产品
	case 30:
		return that.NewActivityRefundItemRequest(), nil //活动平台
	case 40:
		return that.NewMmpCashOrderRefundItemRequest(), nil //会员中台
	default:
		return nil, errors.New("not this type request")
	}
}

//可退项目（门票订单）
func (that *PPOSClient) NewTicketRefundItemRequest() *RefundItemRequest {
	return &RefundItemRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/ticket/orderRefund/refundItem",
		},
		bT: time.Now(),
	}
}

//可退项目（组合产品订单）
func (that *PPOSClient) NewJointgoodsRefundItemRequest() *RefundItemRequest {
	return &RefundItemRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/marketing/orderRefund/refundItem",
		},
		bT: time.Now(),
	}
}

//可退项目（通用收款订单）
func (that *PPOSClient) NewCosRefundItemRequest() *RefundItemRequest {
	return &RefundItemRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/cos/orderRefund/refundItem",
		},
		bT: time.Now(),
	}
}

//可退项目 (商品订单)
func (that *PPOSClient) NewGoodsRefundItemRequest() *RefundItemRequest {
	return &RefundItemRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/goods/goodsOrder/refundItem",
		},
		bT: time.Now(),
	}
}

//可退项目（场地订单）
func (that *PPOSClient) NewFieldRefundItemRequest() *RefundItemRequest {
	return &RefundItemRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/field/orderRefund/refundItem",
		},
		bT: time.Now(),
	}
}

//可退项目（会员订单）
func (that *PPOSClient) NewMemberRefundItemRequest() *RefundItemRequest {
	return &RefundItemRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/cardWork/memberCard/refundItem",
		},
		bT: time.Now(),
	}
}

//可退项目 (赛事订单)
func (that *PPOSClient) NewEventsRefundItemRequest() *RefundItemRequest {
	return &RefundItemRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/events/contestOrder/refundItem",
		},
		publicParamsSlice: []string{
			"parent_order_no",
			"order_no",
		},
		bT: time.Now(),
	}
}

//可退项目 (课程订单)
func (that *PPOSClient) NewCourseRefundItemRequest() *RefundItemRequest {
	return &RefundItemRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/class/courseOrder/refundItem",
		},
		publicParamsSlice: []string{
			"parent_order_no",
			"order_no",
		},
		bT: time.Now(),
	}
}

//可退项目 (课时订单)
func (that *PPOSClient) NewClassRefundItemRequest() *RefundItemRequest {
	return &RefundItemRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/class/classOrder/refundItem",
		},
		publicParamsSlice: []string{
			"parent_order_no",
			"order_no",
		},
		bT: time.Now(),
	}
}

//可退项目 (租赁订单)
func (that *PPOSClient) NewLeaseRefundItemRequest() *RefundItemRequest {
	return &RefundItemRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/lease/orderRefund/refundItem",
		},
		publicParamsSlice: []string{
			"parent_order_no",
			"order_no",
		},
		bT: time.Now(),
	}
}

//可退项目 (活动平台)
func (that *PPOSClient) NewActivityRefundItemRequest() *RefundItemRequest {
	return &RefundItemRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/activityPlatform/activity/applyOrderRefundItem",
		},
		publicParamsSlice: []string{
			"parent_order_no",
			"order_no",
		},
		bT: time.Now(),
	}
}

//可退项目 (会员中台)
func (that *PPOSClient) NewMmpCashOrderRefundItemRequest() *RefundItemRequest {
	return &RefundItemRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/memberMiddlePlatform/cashOrder/refundItem",
		},
		publicParamsSlice: []string{
			"parent_order_no",
			"order_no",
		},
		bT: time.Now(),
	}
}

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

}

func (that *RefundItemRequest) SetTradeNo(value string) *RefundItemRequest {
	that.initParams()
	that.params.Set("trade_no", value)
	return that
}
func (that *RefundItemRequest) SetParentOrderNo(value string) *RefundItemRequest {
	that.initParams()
	that.params.Set("parent_order_no", value)
	return that
}
func (that *RefundItemRequest) SetOrderNo(value string) *RefundItemRequest {
	that.initParams()
	that.params.Set("order_no", value)
	return that
}

//Params ...
func (that *RefundItemRequest) 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 *RefundItemRequest) Do() (*OrderRefundItemResponse, 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"}
	for _, _v := range _slice {
		if _, _ok := _mp[_v]; !_ok {
			return nil, errors.New(that.apiAddress + "请求响应格式不正确,错误代码02")
		}
	}
	//返回结果
	_rs := new(OrderRefundItemResponse)
	//
	if _err := that.ParseResponeMp(_rs.Order, _mp["order"]); _err != nil {
		return nil, errors.New(that.apiAddress + "请求响应格式化异常:" + _err.Error())
	}
	//
	_itemData, _ok := _mp["item"].([]interface{})
	if !_ok {
		return nil, errors.New(that.apiAddress + "请求响应格式不正确,错误代码01")
	}
	//
	for _k, _v := range _itemData {
		_item := new(RefundItem)
		if _err := that.ParseResponeMp(_item, _v); _err != nil {
			return nil, errors.New(that.apiAddress + "请求响应格式化异常:" + _err.Error())
		}
		_rs.Item = append(_rs.Item, _item)
		_rs.Item[_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 + "请求响应格式化异常:" + _err.Error())
					}
					_rs.Item[_k].CodeList = append(_rs.Item[_k].CodeList, _codeValue)
				}
			}
		}
	}
	//
	return _rs, nil
}
