package controllers

import (
	"net/http"

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

// Operations about ELMOrder
type MtpsController struct {
	web.Controller
}

func (c *MtpsController) Status() {
	if c.Ctx.Input.Method() == http.MethodPost {
		if c.GetString("exception_id") != "" { // 美团配送的回调设置好象有问题，配送异常也会发到这里来，作一下兼容处理
			c.Except()
			return
		}

		obj, callbackResponse := api.MtpsAPI.GetOrderCallbackMsg(c.Ctx.Request)
		if callbackResponse == nil {
			callbackResponse = mtps.OnWaybillMsg(obj)
		}
		c.Data["json"] = callbackResponse
		c.ServeJSON()
	} else {
		c.Abort("404")
	}
}

func (c *MtpsController) Except() {
	if c.Ctx.Input.Method() == http.MethodPost {
		obj, callbackResponse := api.MtpsAPI.GetOrderExceptionCallbackMsg(c.Ctx.Request)
		if callbackResponse == nil {
			callbackResponse = mtps.OnWaybillExcept(obj)
		}
		c.Data["json"] = callbackResponse
		c.ServeJSON()
	} else {
		c.Abort("404")
	}
}

func (c *MtpsController) StoreStatus() {
	if c.Ctx.Input.Method() == http.MethodPost {
		obj, callbackResponse := api.MtpsAPI.GetShopStatusCallbackMsg(c.Ctx.Request)
		if callbackResponse == nil {
			callbackResponse = mtps.OnStoreStatus(obj)
		}
		c.Data["json"] = callbackResponse
		c.ServeJSON()
	} else {
		c.Abort("404")
	}
}
