package controllers

import (
	"fmt"
	"net/http"

	"git.rosy.net.cn/baseapi/utils"

	"git.rosy.net.cn/jx-callback/business/partner/delivery/sfps"

	"git.rosy.net.cn/jx-callback/globals/api"
	"github.com/astaxie/beego/server/web"
)

type SFPSController struct {
	web.Controller
}

func (c *SFPSController) SfOrder() {
	if c.Ctx.Input.Method() == http.MethodPost {
		msg, callbackResponse := api.SfPsAPI.GetCallbackUrlIndex(c.Ctx.Request)
		if callbackResponse.ErrorCode == -1 {
			c.Data["data"] = -1
			c.ServeJSON()
			return
		}

		// 订单回调
		for k, v := range msg {
			callbackResponse = sfps.OnWaybillMsg(k, v)
			c.Data["data"] = callbackResponse
			fmt.Printf("SfOrder callbackResponse=%s", utils.Format4Output(callbackResponse, false))
			c.ServeJSON()
		}
	} else {
		c.Abort("404")
	}
}

// SfAbnormal 异常回调
func (c *FnController) SfAbnormal() {
	if c.Ctx.Input.Method() == http.MethodPost {
		msg, callbackResponse := api.SfPsAPI.GetRiderExceptionCallback(c.Ctx.Request)
		if callbackResponse.ErrorCode == -1 {
			c.Data["error_code"] = -1
			c.ServeJSON()
			return
		}

		callbackResponse = sfps.OnWaybillExceptSF(msg)
		c.Data["error_code"] = callbackResponse
		c.ServeJSON()
	} else {
		c.Abort("404")
	}
}
