package pposRequest

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

//参数：子系统ID、具体业务类型
func (that *PPOSClient) NewOrderPayPriceRequest(sysID int64, businessType int64) (*OrderPayPriceRequest, error) {
	switch sysID {
	case 11:
		return that.NewCardOrderPayPriceRequest(), nil //会员
	case 12:
		return that.NewTicketOrderPayPriceRequest(), nil //门票
	case 13:
		return that.NewFieldOrderPayPriceRequest(), nil //场地
	case 14:
		return that.NewGoodsOrderPayPriceRequest(), nil //商品
	case 15:
		return that.NewCosOrderPayPriceRequest(), nil //通用收款
	case 16:
		return that.NewEventOrderPayPriceRequest(), nil //赛事
	case 17:
		if businessType == 1701 || businessType == 1705 || businessType == 1707 {
			return that.NewCourseOrderPayPriceRequest(), nil //课程
		}
		if businessType == 1703 {
			return that.NewClassOrderPayPriceRequest(), nil //课时
		}
	case 18:
		return that.NewLeaseOrderPayPriceRequest(), nil //租赁
	case 19:
		return that.NewMCOrderPayPriceRequest(), nil //消息
	case 20:
		return that.NewJointgoodsOrderPayPriceRequest(), nil //营销-组合
	case 30:
		return that.NewActivityOrderPayPriceRequest(), nil //活动平台
	case 40:
		return that.NewMmpCashOrderPayPriceRequest(), nil //会员中台
	default:
		return nil, errors.New("not this type request")
	}
	return nil, errors.New("not this type request")
}

//NewCosOrderCreateRequest 订单询价/改价 （场地）
func (that *PPOSClient) NewFieldOrderPayPriceRequest() *OrderPayPriceRequest {
	return &OrderPayPriceRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/field/pay/payPrice",
		},
		publicParamsSlice: []string{
			"sys_id",
			"parent_order_no",
			"order_no",
			"payment_way",
			"company_id",
		},
		SysID: 13,
		bT:    time.Now(),
	}
}

//NewTicketOrderPayPriceRequest 订单询价/改价 （门票）
func (that *PPOSClient) NewTicketOrderPayPriceRequest() *OrderPayPriceRequest {
	return &OrderPayPriceRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/ticket/pay/payPrice",
		},
		publicParamsSlice: []string{
			"sys_id",
			"parent_order_no",
			"order_no",
			"payment_way",
			"company_id",
		},
		SysID: 12,
		bT:    time.Now(),
	}
}

//NewJointgoodsOrderPayPriceRequest 订单询价/改价 （营销-组合）
func (that *PPOSClient) NewJointgoodsOrderPayPriceRequest() *OrderPayPriceRequest {
	return &OrderPayPriceRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/marketing/pay/payPrice",
		},
		publicParamsSlice: []string{
			"sys_id",
			"parent_order_no",
			"order_no",
			"payment_way",
			"company_id",
		},
		SysID: 20,
		bT:    time.Now(),
	}
}

//NewCosOrderPayPriceRequest 订单询价/改价 （通用收款）
func (that *PPOSClient) NewCosOrderPayPriceRequest() *OrderPayPriceRequest {
	return &OrderPayPriceRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/cos/order/payPrice",
		},
		publicParamsSlice: []string{
			"sys_id",
			"parent_order_no",
			"order_no",
			"payment_way",
			"company_id",
		},
		SysID: 15,
		bT:    time.Now(),
	}
}

//NewGoodsOrderPayPriceRequest 订单询价/改价 （商品）
func (that *PPOSClient) NewGoodsOrderPayPriceRequest() *OrderPayPriceRequest {
	return &OrderPayPriceRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/goods/goodsOrder/payPrice",
		},
		publicParamsSlice: []string{
			"sys_id",
			"parent_order_no",
			"order_no",
			"payment_way",
			"company_id",
		},
		SysID: 14,
		bT:    time.Now(),
	}
}

//NewCardOrderPayPriceRequest 订单询价/改价 （会员卡）
func (that *PPOSClient) NewCardOrderPayPriceRequest() *OrderPayPriceRequest {
	return &OrderPayPriceRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/cardWork/memberCard/payPrice",
		},
		publicParamsSlice: []string{
			"sys_id",
			"parent_order_no",
			"order_no",
			"payment_way",
			"company_id",
		},
		SysID: 11,
		bT:    time.Now(),
	}
}

//NewEventOrderPayPriceRequest 订单询价/改价 （赛事活动）
func (that *PPOSClient) NewEventOrderPayPriceRequest() *OrderPayPriceRequest {
	return &OrderPayPriceRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/events/order/payPrice",
		},
		publicParamsSlice: []string{
			"sys_id",
			"parent_order_no",
			"order_no",
			"payment_way",
			"company_id",
		},
		SysID: 16,
		bT:    time.Now(),
	}
}

//NewCourseOrderPayPriceRequest 订单询价/改价 （课程）
func (that *PPOSClient) NewCourseOrderPayPriceRequest() *OrderPayPriceRequest {
	return &OrderPayPriceRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/class/courseOrder/payPrice",
		},
		publicParamsSlice: []string{
			"sys_id",
			"parent_order_no",
			"order_no",
			"payment_way",
			"company_id",
		},
		SysID: 17,
		bT:    time.Now(),
	}
}

//NewClassOrderPayPriceRequest 订单询价/改价 （课时）
func (that *PPOSClient) NewClassOrderPayPriceRequest() *OrderPayPriceRequest {
	return &OrderPayPriceRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/class/classOrder/payPrice",
		},
		publicParamsSlice: []string{
			"sys_id",
			"parent_order_no",
			"order_no",
			"payment_way",
			"company_id",
		},
		SysID: 17,
		bT:    time.Now(),
	}
}

//NewLeaseOrderPayPriceRequest 订单询价/改价 （租赁）
func (that *PPOSClient) NewLeaseOrderPayPriceRequest() *OrderPayPriceRequest {
	return &OrderPayPriceRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/lease/pay/payPrice",
		},
		publicParamsSlice: []string{
			"sys_id",
			"parent_order_no",
			"order_no",
			"payment_way",
			"company_id",
		},
		SysID: 18,
		bT:    time.Now(),
	}
}

//NewMCOrderPayPriceRequest 订单询价/改价 （消息）
func (that *PPOSClient) NewMCOrderPayPriceRequest() *OrderPayPriceRequest {
	return &OrderPayPriceRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/mc/order/payPrice",
		},
		publicParamsSlice: []string{
			"sys_id",
			"parent_order_no",
			"order_no",
			"payment_way",
			"company_id",
		},
		SysID: 19,
		bT:    time.Now(),
	}
}

//NewActivityOrderPayPriceRequest 订单询价/改价 （活动平台）
func (that *PPOSClient) NewActivityOrderPayPriceRequest() *OrderPayPriceRequest {
	return &OrderPayPriceRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/activityPlatform/activity/applyOrderPayPrice",
		},
		publicParamsSlice: []string{
			"sys_id",
			"parent_order_no",
			"order_no",
			"payment_way",
			"company_id",
		},
		SysID: 16,
		bT:    time.Now(),
	}
}

//NewMmpCashOrderPayPriceRequest 订单询价/改价 （会员中台）
func (that *PPOSClient) NewMmpCashOrderPayPriceRequest() *OrderPayPriceRequest {
	return &OrderPayPriceRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/memberMiddlePlatform/cashOrder/payPrice",
		},
		publicParamsSlice: []string{
			"sys_id",
			"parent_order_no",
			"order_no",
			"payment_way",
			"company_id",
		},
		SysID: 40,
		bT:    time.Now(),
	}
}

//NewShopOrderPayPriceRequest 订单询价/改价 （支付前置-确认价格-提交时调用）
func (that *PPOSClient) NewShopOrderPayPriceRequest() *OrderPayPriceRequest {
	return &OrderPayPriceRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/shop/order/price",
		},
		publicParamsSlice: []string{
			"sys_id",
			"parent_order_no",
			"payment_way",
			"company_id",
		},
		SysID: 0,
		bT:    time.Now(),
	}
}

type OrderPayPriceRequest struct {
	Request
	SysID             int64            //
	SkuItems          map[string]int64 //
	publicParamsSlice []string         //必传参数名
	bT                time.Time        //耗时时间
}

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

func (that *OrderPayPriceRequest) SetOrderNo(orderNo int64) *OrderPayPriceRequest {
	that.initParams()
	that.params.Set("order_no", utils.ToString(orderNo))
	return that
}

func (that *OrderPayPriceRequest) SetPaymentWay(paymentWay string) *OrderPayPriceRequest {
	that.initParams()
	that.params.Set("payment_way", utils.ToString(paymentWay))
	return that
}

func (that *OrderPayPriceRequest) SetMemberCardID(memberCardID string) *OrderPayPriceRequest {
	that.initParams()
	that.params.Set("member_card_id", utils.ToString(memberCardID))
	return that
}
func (that *OrderPayPriceRequest) SetSysID(value int64) *OrderPayPriceRequest {
	that.initParams()
	that.params.Set("sys_id", utils.ToString(value))
	return that
}
func (that *OrderPayPriceRequest) SetIsMemberDiscount(value int64) *OrderPayPriceRequest {
	that.initParams()
	that.params.Set("is_member_discount", utils.ToString(value))
	return that
}
func (that *OrderPayPriceRequest) SetDiscountInfo(value string) *OrderPayPriceRequest {
	that.initParams()
	that.params.Set("discount_info", value)
	return that
}
func (that *OrderPayPriceRequest) SetOrderFrom(value string) *OrderPayPriceRequest {
	that.initParams()
	that.params.Set("order_from", value)
	return that
}
func (that *OrderPayPriceRequest) SetIsUpdate(value string) *OrderPayPriceRequest {
	that.initParams()
	that.params.Set("is_update", value) //是否需要即时更新数据库信息，如果为1，表示更新
	return that
}

//Params ...
func (that *OrderPayPriceRequest) Params() (url.Values, error) {
	that.initParams()
	_params := that.params
	_params.Set("sys_id", utils.ToString(that.SysID))
	//
	for _, _v := range that.publicParamsSlice {
		if _params.Get(_v) == "" {
			return nil, errors.New(that.apiAddress + "缺少参数:" + _v)
		}
	}
	return _params, nil
}

//Do ...
func (that *OrderPayPriceRequest) 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")
	}
	//支付前置询价
	if that.apiAddress == "/shop/order/price" {
		return nil, nil
	}
	//
	_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)
	//
	if _, _ok := _mp["stadium_info"].(map[string]interface{}); _ok {
		_stadiumInfo := new(StadiumInfo)
		if _err := that.ParseResponeMp(_stadiumInfo, _mp["stadium_info"], false); _err != nil {
			return nil, errors.New(that.apiAddress + "请求响应格式化异常:" + _err.Error())
		}
		_rs.StadiumInfo = _stadiumInfo
	}
	//
	if _, _ok := _mp["member_info"].(map[string]interface{}); _ok {
		_memberInfo := new(MemberInfo)
		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
}
