package controllers

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

type FnController struct {
	web.Controller
}

// 门店回掉
func (c *FnController) FnStore() {
	if c.Ctx.Input.Method() == http.MethodPost {
		msg, callbackResponse := api.FnAPI.GetChainstoreStatusNotify(c.Ctx.Request)
		if callbackResponse.Code == -1 {
			c.Data["code"] = callbackResponse
			c.ServeJSON()
			return
		}

		callbackResponse = fn.OnStoreStatus(msg)
		c.Data["code"] = callbackResponse
		c.ServeJSON()
	} else {
		c.Abort("404")
	}
}

// 订单状态
func (c *FnController) FnOrder() {
	if c.Ctx.Input.Method() == http.MethodPost {
		msg, result, callbackResponse := api.FnAPI.GetChainOrderStatusNotify(c.Ctx.Request)
		if callbackResponse.Code == -1 {
			c.Data["code"] = callbackResponse
			c.ServeJSON()
			return
		}

		// 订单回调
		callbackResponse = fn.OnWaybillMsg(msg, result)
		c.Data["code"] = callbackResponse
		c.ServeJSON()
	} else {
		c.Abort("404")
	}
}

// 异常回调
func (c *FnController) FnAbnormal() {
	if c.Ctx.Input.Method() == http.MethodPost {
		msg, callbackResponse := api.FnAPI.GetChainAbnormaltatusNotify(c.Ctx.Request)
		if callbackResponse.Code == -1 {
			c.Data["json"] = callbackResponse
			c.ServeJSON()
			return
		}

		callbackResponse = fn.OnWaybillExceptFn(msg.Param)
		c.Data["code"] = callbackResponse
		c.ServeJSON()
	} else {
		c.Abort("404")
	}
}
