package pposRequest

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

//参数：子系统ID、具体业务类型
func (that *PPOSClient) NewPayAfterRequest(sysID int64, businessType int64) (*OrderNoticeRequest, error) {
	switch sysID {
	case 11:
		return that.NewMemberPayAfterRequest(), nil //会员
	case 12:
		return that.NewTicketPayAfterRequest(), nil //门票
	case 13:
		return that.NewFieldPayAfterRequest(), nil //场地
	case 14:
		return that.NewGoodsPayAfterRequest(), nil //商品
	case 15:
		return that.NewCosPayAfterRequest(), nil //通用收款
	case 16:
		return that.NewEventsPayAfterRequest(), nil //赛事
	case 17:
		if businessType == 1701 || businessType == 1705 || businessType == 1707 {
			return that.NewCoursePayAfterRequest(), nil //课程
		}
		if businessType == 1703 {
			return that.NewClassPayAfterRequest(), nil //课时
		}
		return nil, errors.New("not this businessType")
	case 18:
		return that.NewLeasePayAfterRequest(), nil //租赁
	case 19:
		return that.NewMCPayAfterRequest(), nil //消息
	case 20:
		return that.NewJointgoodsPayAfterRequest(), nil //营销-组合
	case 30:
		return that.NewActivityPayAfterRequest(), nil //活动平台
	case 40:
		return that.NewMmpCashOrderPayAfterRequest(), nil //会员中台
	default:
		return nil, errors.New("not this type request")
	}
}

//异步回调到子系统url(门票订单)
func (that *PPOSClient) NewTicketPayAfterRequest() *OrderNoticeRequest {
	return &OrderNoticeRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/ticket/pay/payNotice",
			publicParamsSlice: []string{
				"parent_order_no",
				"order_no",
				"sys_id",
				"payment_way",
			},
		},
		SysId: 12,
	}
}

//异步回调到子系统url(营销-组合订单)
func (that *PPOSClient) NewJointgoodsPayAfterRequest() *OrderNoticeRequest {
	return &OrderNoticeRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/marketing/pay/payNotice",
			publicParamsSlice: []string{
				"parent_order_no",
				"order_no",
				"sys_id",
				"payment_way",
			},
		},
		SysId: 20,
	}
}

//异步回调到子系统url(通用扣款)
func (that *PPOSClient) NewCosPayAfterRequest() *OrderNoticeRequest {
	return &OrderNoticeRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/cos/order/payNotice",
		},
		SysId: 15,
	}
}

//异步回调到子系统url(商品订单)
func (that *PPOSClient) NewGoodsPayAfterRequest() *OrderNoticeRequest {
	return &OrderNoticeRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/goods/goodsOrder/payNotice",
			publicParamsSlice: []string{
				"parent_order_no",
				"order_no",
				"sys_id",
				"payment_way",
			},
		},
		SysId: 14,
	}
}

//异步回调到子系统url(场地订单)
func (that *PPOSClient) NewFieldPayAfterRequest() *OrderNoticeRequest {
	return &OrderNoticeRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/field/pay/payNotice",
			publicParamsSlice: []string{
				"parent_order_no",
				"order_no",
				"sys_id",
				"payment_way",
			},
		},
		SysId: 13,
	}
}

//异步回调到子系统url(会员订单)
func (that *PPOSClient) NewMemberPayAfterRequest() *OrderNoticeRequest {
	return &OrderNoticeRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/cardWork/memberCard/payNotice",
			publicParamsSlice: []string{
				"parent_order_no",
				"order_no",
				"sys_id",
				"payment_way",
			},
		},
		SysId: 11,
	}
}

//异步回调到子系统url(赛事订单)
func (that *PPOSClient) NewEventsPayAfterRequest() *OrderNoticeRequest {
	return &OrderNoticeRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/events/order/payNotice",
		},
		SysId: 16,
	}
}

//异步回调到子系统url(课时订单)
func (that *PPOSClient) NewClassPayAfterRequest() *OrderNoticeRequest {
	return &OrderNoticeRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/class/classOrder/payNotice",
		},
		SysId: 17,
	}
}

//异步回调到子系统url(课程订单)
func (that *PPOSClient) NewCoursePayAfterRequest() *OrderNoticeRequest {
	return &OrderNoticeRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/class/courseOrder/payNotice",
		},
		SysId: 17,
	}
}

//优惠核销通知（折扣、优惠券）
func (that *PPOSClient) NewCouponPayAfterRequest() *OrderNoticeRequest {
	return &OrderNoticeRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/coupon/orderDiscount/discountNotice",
		},
	}
}

//异步回调到第三方省平台（子系统）
func (that *PPOSClient) NewPlatformPayAfterRequest() *OrderNoticeRequest {
	return &OrderNoticeRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/platform/order/orderNotice",
			publicParamsSlice: []string{
				"parent_order_no",
			},
		},
	}
}

//异步回调到子系统url(租赁订单)
func (that *PPOSClient) NewLeasePayAfterRequest() *OrderNoticeRequest {
	return &OrderNoticeRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/lease/pay/payNotice",
		},
		SysId: 18,
	}
}

//异步回调到子系统url(消息订单)
func (that *PPOSClient) NewMCPayAfterRequest() *OrderNoticeRequest {
	return &OrderNoticeRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/mc/order/payNotice",
		},
		SysId: 19,
	}
}

//异步回调到子系统url(活动平台)
func (that *PPOSClient) NewActivityPayAfterRequest() *OrderNoticeRequest {
	return &OrderNoticeRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/activityPlatform/activity/applyOrderPayNotice",
		},
		SysId: 18,
	}
}

//异步回调到子系统url(会员中台)
func (that *PPOSClient) NewMmpCashOrderPayAfterRequest() *OrderNoticeRequest {
	return &OrderNoticeRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/memberMiddlePlatform/cashOrder/payNotice",
		},
		SysId: 40,
	}
}

//异步回调到订单中心(支付)
func (that *PPOSClient) NewShopPayAfterRequest() *OrderNoticeRequest {
	return &OrderNoticeRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/shop/order/notice",
			publicParamsSlice: []string{
				"parent_order_no",
			},
		},
	}
}

//异步回调到订单中心(退款)
func (that *PPOSClient) NewShopRefundAfterRequest() *OrderNoticeRequest {
	return &OrderNoticeRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/shop/order/refundNotice",
			publicParamsSlice: []string{
				"parent_order_no",
			},
		},
	}
}

//
type OrderNoticeRequest struct {
	Request
	SysId int64 //
}

func (that *OrderNoticeRequest) SetTradeNo(value string) *OrderNoticeRequest {
	that.initParams()
	that.params.Set("trade_no", value) //交易号（支付中心的交易号）
	return that
}
func (that *OrderNoticeRequest) SetParentOrderNo(value string) *OrderNoticeRequest {
	that.initParams()
	that.params.Set("parent_order_no", value) //交易号（订单中心的总订单号）
	return that
}
func (that *OrderNoticeRequest) SetShopOrderInfo(value string) *OrderNoticeRequest {
	that.initParams()
	that.params.Set("shop_order_info", value) //优惠券核销订单信息参数
	return that
}
func (that *OrderNoticeRequest) SetOperator(value int64) *OrderNoticeRequest {
	that.initParams()
	that.params.Set("operator", utils.ToString(value)) //操作人
	return that
}
func (that *OrderNoticeRequest) SetOrderNo(value string) *OrderNoticeRequest {
	that.initParams()
	that.params.Set("order_no", value) //业务子系统的订单号
	return that
}
func (that *OrderNoticeRequest) SetPaymentWay(value int64) *OrderNoticeRequest {
	//支付方式 (10:现金; 20:一卡通; 30:pos机; 40:团购券; 60:微信; 70:支付宝; 80：线下转账; 26:一卡通/微信组合支付; 27:一卡通/支付宝组合支付)
	that.initParams()
	that.params.Set("payment_way", utils.ToString(value))
	return that
}
func (that *OrderNoticeRequest) SetMemberCardId(value int64) *OrderNoticeRequest {
	that.initParams()
	that.params.Set("member_card_id", utils.ToString(value)) //[会员卡id]
	return that
}
func (that *OrderNoticeRequest) SetPayPrice(value int64) *OrderNoticeRequest {
	that.initParams()
	that.params.Set("pay_price", utils.ToString(value)) //[支付金额]
	return that
}
func (that *OrderNoticeRequest) SetSysId(value int64) *OrderNoticeRequest {
	that.initParams()
	that.params.Set("sys_id", utils.ToString(value)) //业务子系统id
	return that
}
func (that *OrderNoticeRequest) SetTransactionId(value string) *OrderNoticeRequest {
	that.initParams()
	that.params.Set("transaction_id", value) //[第三方交易号]
	return that
}
func (that *OrderNoticeRequest) SetBuyerUserId(value string) *OrderNoticeRequest {
	that.initParams()
	that.params.Set("buyer_user_id", value) //[买家支付账号]
	return that
}
func (that *OrderNoticeRequest) SetDiscountInfo(value string) *OrderNoticeRequest {
	//[折扣信息] {"is_modify":1,"modify_price_map":[{"sku":"131002800200218220200430","modify_price":10000},{"sku":"131002800200221220200430","modify_price":10000}]}
	that.initParams()
	that.params.Set("discount_info", value)
	return that
}
func (that *OrderNoticeRequest) SetOpenId(value string) *OrderNoticeRequest {
	that.initParams()
	that.params.Set("open_id", value)
	return that
}

//Params ...
func (that *OrderNoticeRequest) 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 *OrderNoticeRequest) Do() (*OrderInfoResponse, error) {
	//返回data
	_data, _err := that.Client.GetData(that.apiAddress, that)
	if _err != nil {
		return nil, _err
	}
	_rs := new(OrderInfoResponse)
	_mp, _ok := _data.(map[string]interface{})
	if _ok {
		//返回结果
		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 + "请求响应格式化异常1:" + _err.Error())
			}
			_rs.MemberInfo = _memberInfo
		}
		//订单信息
		if _, _ok := _mp["order"].(map[string]interface{}); _ok {
			_orderInfo := new(OrderInfo)
			if _err := that.ParseResponeMp(_orderInfo, _mp["order"], false); _err != nil {
				return nil, errors.New(that.apiAddress + "请求响应格式化异常2:" + _err.Error())
			}
			_rs.Order = _orderInfo
		}
		//sku信息
		if _itemData, _ok := _mp["item"].([]interface{}); _ok {
			_itemSlice := []*OrderItem{}
			for _, _v := range _itemData {
				_item := new(OrderItem)
				if _err := that.ParseResponeMp(_item, _v, false); _err != nil {
					return nil, errors.New(that.apiAddress + "请求响应格式化异常3:" + _err.Error())
				}
				_itemSlice = append(_itemSlice, _item)
			}
			_rs.Item = _itemSlice
		}
	}
	//
	return _rs, nil
}

type ShopNoticeResponse struct {
	TransactionCode   string      `json:"transaction_code"`   //错误码
	TransactionStatus string      `json:"transaction_status"` //已支付
	TransactionMsg    string      `json:"transaction_msg"`    //对当前查询订单状态的描述和下一步操作的指引
	ReturnMsg         string      `json:"return_msg"`         //返回信息，如非空，为错误原因,签名失败,参数格式校验错误等
	ParentOrderNo     string      `json:"parent_order_no"`    //商户系统内部总订单号
	TimeEnd           string      `json:"time_end"`           //订单支付时间，格式为yyyyMMddHHmmss，
	TotalFee          interface{} `json:"total_fee"`          //订单总金额，单位为分
}

func (that *OrderNoticeRequest) ShopDo() (*ShopNoticeResponse, error) {
	//返回data
	_data, _err := that.Client.GetData(that.apiAddress, that)
	if _err != nil {
		return nil, _err
	}
	_rs := new(ShopNoticeResponse)
	if _err := that.ParseResponeMp(_rs, _data, false); _err != nil {
		return nil, _err
	}
	return _rs, nil
}
