package pposRequest

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

//参数：子系统ID
func (that *PPOSClient) NewRefundPriceRequest(sysID int64, businessType int64) (*RefundPriceRequest, error) {
	switch sysID {
	case 11:
		return that.NewMemberRefundPriceRequest(), nil //会员
	case 12:
		return that.NewTicketRefundPriceRequest(), nil //门票
	case 13:
		return that.NewFieldRefundPriceRequest(), nil //场地
	case 14:
		return that.NewGoodsRefundPriceRequest(), nil //商品
	case 15:
		return that.NewCosRefundPriceRequest(), nil //通用收款
	case 16:
		return that.NewEventsRefundPriceRequest(), nil //赛事
	case 17:
		if businessType == 1701 || businessType == 1705 || businessType == 1707 {
			return that.NewCourseRefundPriceRequest(), nil //课程、续课
		}
		if businessType == 1703 {
			return that.NewClassRefundPriceRequest(), nil //课时
		}
		return nil, errors.New("not this businessType")
	case 18:
		return that.NewLeaseRefundPriceRequest(), nil //租赁
	case 20:
		return that.NewJointgoodsRefundPriceRequest(), nil //组合产品
	case 30:
		return that.NewActivityRefundPriceRequest(), nil //活动平台
	case 40:
		return that.NewMmpCashOrderRefundPriceRequest(), nil //会员中台
	default:
		return nil, errors.New("not this type request")
	}
}

//单点可退金额（门票订单）
func (that *PPOSClient) NewTicketRefundPriceRequest() *RefundPriceRequest {
	return &RefundPriceRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/ticket/orderRefund/refundPrice",
		},
		bT: time.Now(),
	}
}

//单点可退金额（组合产品订单）
func (that *PPOSClient) NewJointgoodsRefundPriceRequest() *RefundPriceRequest {
	return &RefundPriceRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/marketing/orderRefund/refundPrice",
		},
		bT: time.Now(),
	}
}

//单点可退金额（通用收款订单）
func (that *PPOSClient) NewCosRefundPriceRequest() *RefundPriceRequest {
	return &RefundPriceRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/cos/orderRefund/refundPrice",
		},
		bT: time.Now(),
	}
}

//单点可退金额 (商品订单)
func (that *PPOSClient) NewGoodsRefundPriceRequest() *RefundPriceRequest {
	return &RefundPriceRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/goods/goodsOrder/refundPrice",
		},
		bT: time.Now(),
	}
}

//单点可退金额（场地订单）
func (that *PPOSClient) NewFieldRefundPriceRequest() *RefundPriceRequest {
	return &RefundPriceRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/field/orderRefund/refundPrice",
		},
		bT: time.Now(),
	}
}

//单点可退金额（会员订单）
func (that *PPOSClient) NewMemberRefundPriceRequest() *RefundPriceRequest {
	return &RefundPriceRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/cardWork/memberCard/refundPrice",
		},
		bT: time.Now(),
	}
}

//单点可退金额 (赛事订单)
func (that *PPOSClient) NewEventsRefundPriceRequest() *RefundPriceRequest {
	return &RefundPriceRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/events/contestOrder/refundPrice",
		},
		publicParamsSlice: []string{
			"parent_order_no",
			"order_no",
		},
		bT: time.Now(),
	}
}

//单点可退金额 (培训课时订单)
func (that *PPOSClient) NewClassRefundPriceRequest() *RefundPriceRequest {
	return &RefundPriceRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/class/classOrder/refundPrice",
		},
		publicParamsSlice: []string{
			"parent_order_no",
			"order_no",
		},
		bT: time.Now(),
	}
}

//单点可退金额 (培训课程订单)
func (that *PPOSClient) NewCourseRefundPriceRequest() *RefundPriceRequest {
	return &RefundPriceRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/class/courseOrder/refundPrice",
		},
		publicParamsSlice: []string{
			"parent_order_no",
			"order_no",
		},
		bT: time.Now(),
	}
}

//单点可退金额 (租赁订单)
func (that *PPOSClient) NewLeaseRefundPriceRequest() *RefundPriceRequest {
	return &RefundPriceRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/lease/orderRefund/refundPrice",
		},
		publicParamsSlice: []string{
			"parent_order_no",
			"order_no",
		},
		bT: time.Now(),
	}
}

//单点可退金额 (活动平台)
func (that *PPOSClient) NewActivityRefundPriceRequest() *RefundPriceRequest {
	return &RefundPriceRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/activityPlatform/activity/applyOrderRefundPrice",
		},
		publicParamsSlice: []string{
			"parent_order_no",
			"order_no",
		},
		bT: time.Now(),
	}
}

//单点可退金额 (会员中台)
func (that *PPOSClient) NewMmpCashOrderRefundPriceRequest() *RefundPriceRequest {
	return &RefundPriceRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/memberMiddlePlatform/cashOrder/refundPrice",
		},
		publicParamsSlice: []string{
			"parent_order_no",
			"order_no",
		},
		bT: time.Now(),
	}
}

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

func (that *RefundPriceRequest) SetParentOrderNo(value string) *RefundPriceRequest {
	that.initParams()
	that.params.Set("parent_order_no", value)
	return that
}
func (that *RefundPriceRequest) SetOrderNo(value string) *RefundPriceRequest {
	that.initParams()
	that.params.Set("order_no", value)
	return that
}
func (that *RefundPriceRequest) SetSkuItems(value string) *RefundPriceRequest {
	that.initParams()
	that.params.Set("sku_items", value)
	return that
}
func (that *RefundPriceRequest) SetSysID(value int64) *RefundPriceRequest {
	that.initParams()
	that.params.Set("sys_id", utils.ToString(value))
	return that
}
func (that *RefundPriceRequest) SetPayScene(value string) *RefundPriceRequest {
	that.initParams()
	that.params.Set("pay_scene", value)
	return that
}
func (that *RefundPriceRequest) SetIsNoServiceFee(value int64) *RefundPriceRequest {
	that.initParams()
	that.params.Set("is_no_service_fee", utils.ToString(value))
	return that
}

//Params ...
func (that *RefundPriceRequest) 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 RefundPriceItem struct {
	OrderNo          string      `json:"order_no"`
	OrderItemNo      string      `json:"order_item_no"`
	Sku              string      `json:"sku"`
	SkuNum           int64       `json:"sku_num"`
	RefundNum        int64       `json:"refund_num"`
	SkuPrice         int64       `json:"sku_price"`
	SalePrice        int64       `json:"sale_price"`
	ItemPrice        int64       `json:"item_price"`
	PayPrice         int64       `json:"pay_price"`
	RefundPrice      int64       `json:"refund_price"`
	HandPrice        int64       `json:"hand_price"`
	RefundTotalPrice int64       `json:"refund_total_price"`
	CodeList         []*CodeList `json:"code_list"`
}
type OrderRefundPriceResponse struct {
	HandTotalPrice   int64 `json:"hand_total_price"`
	RefundTotalPrice int64 `json:"refund_total_price"`
	Item             []*RefundPriceItem
}

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