package tao_vegetable

import (
	"git.rosy.net.cn/baseapi/platformapi/mtwmapi"
	"git.rosy.net.cn/baseapi/platformapi/tao_vegetable"
	"git.rosy.net.cn/jx-callback/globals"
	"net/http"
	"strings"
)

// OnCallbackMsg 淘宝回调接口
func OnCallbackMsg(orderStatus, orderId string, msg interface{}) (response *tao_vegetable.CallBackResult) {
	//jxutils.CallMsgHandlerAsync(func() {
	//	response = CurPurchaseHandler.onOrderMsg(orderStatus, orderId, msg)
	//}, jxutils.ComposeUniversalOrderID(orderId, model.VendorIDTaoVegetable))
	return CurPurchaseHandler.onOrderMsg(orderStatus, orderId, msg)
}

func forwardOrderToGy(msg *mtwmapi.CallbackMsg, orderStatus string) {
	cl := http.Client{}
	callbackUrl := GetMsgCallBackUrl(orderStatus, msg.AppID)
	request, err := http.NewRequest(http.MethodPost, "http://callback-jxgy.jxc4.com/taoBaoVegetable/"+callbackUrl, strings.NewReader(msg.FormData.Encode()))
	if err != nil {
		return
	}
	request.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
	cl.Do(request)
}

func GetMsgCallBackUrl(msgType, appId string) string {
	interfaceUrl := ""
	switch msgType {
	case tao_vegetable.OrderStatusApplyAfs:
		interfaceUrl = "/applyCancelOrder "
	case tao_vegetable.OrderStatusCancelAfs:
		interfaceUrl = "/userCancelRefund"
	case tao_vegetable.OrderStatusOnSaleCancel:
		interfaceUrl = "/cancelOnSaleRefundOrder"
	default:
		globals.SugarLogger.Errorf("淘宝[app_id :=%s ,callbackUrl := %s ]回调推送到果园错误,回调地址不正确", appId, msgType)
	}
	return interfaceUrl
}
