package controllers

import (
	"encoding/json"
	"git.rosy.net.cn/baseapi/platformapi/fnpsapi"
	"git.rosy.net.cn/baseapi/utils"
	"git.rosy.net.cn/jx-callback/business/model/dao"
	"git.rosy.net.cn/jx-callback/business/model/legacymodel"
	"git.rosy.net.cn/jx-callback/business/partner/delivery/fn"
	"git.rosy.net.cn/jx-callback/globals"
	"git.rosy.net.cn/jx-callback/globals/api"
	"github.com/astaxie/beego/client/orm"
	"github.com/astaxie/beego/server/web"
	"io/ioutil"
	"net/http"
	"time"
)

type FnController struct {
	web.Controller
}

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

		callbackResponse = fn.OnStoreStatus(msg, reallyData)
		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, reallyData, callbackResponse := api.FnAPI.GetChainAbnormaltatusNotify(c.Ctx.Request)
		if callbackResponse.Code == -1 {
			c.Data["json"] = callbackResponse
			c.ServeJSON()
			return
		}

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

// FnToken 更新token
func (c *FnController) FnToken() {
	var tokenInfo struct {
		Token        string `json:"token"`
		RefreshToken string `json:"refresh_token"`
	}

	if c.Ctx.Input.Method() == http.MethodPost {
		data, err := ioutil.ReadAll(c.Ctx.Request.Body)
		if err != nil {
			c.Data["json"] = &fnpsapi.SignatureIsNotOk
			c.ServeJSON()
			return
		}

		var token = &tokenInfo
		if err = json.Unmarshal(data, token); err != nil {
			c.Data["json"] = &fnpsapi.SignatureIsNotOk
			c.ServeJSON()
			return
		}

		db := dao.GetDB()

		curConfig := &legacymodel.Config{
			Thirdparty: "fn",
		}
		if err = orm.NewOrm().Read(curConfig, "Thirdparty"); err != nil {
			globals.SugarLogger.Errorf("RefreshFnToken RefreshToken failed with error:%v", err)
		}
		if err != nil {
			c.Data["json"] = &fnpsapi.SignatureIsNotOk
			c.ServeJSON()
			return
		}

		curConfig.Token = token.Token
		curConfig.RefreshToken = token.RefreshToken
		curConfig.Date = utils.Time2Str(time.Now())
		curConfig.LastOperator = "system"
		api.FnAPI.SetToken(curConfig.Token)
		api.FnAPI.SetRefreshToken(curConfig.RefreshToken)

		dao.UpdateEntity(db, curConfig, "Token", "RefreshToken", "Date", "LastOperator")
		c.Data["json"] = &fnpsapi.SuccessResponse
		c.ServeJSON()
	} else {
		c.Abort("404")
	}
}
