package controllers

import (
	"git.rosy.net.cn/baseapi/platformapi/kuaishou_mini"
	"git.rosy.net.cn/jx-callback/business/partner/purchase/jx/localjx"
	"git.rosy.net.cn/jx-callback/globals/api"
	"github.com/astaxie/beego/server/web"
)

type KuaiShouController struct {
	web.Controller
}

// KuaiShouCallBack 快手回调
func (c *KuaiShouController) KuaiShouCallback() {
	payOrder, refundOrder, payType, msgId, err := api.KuaiShouApi.KauiShouCallback(c.Ctx.Request)
	if err != nil {
		c.Data["json"] = CallBackFail(msgId)
		c.ServeJSON()
		return
	}

	switch payType {
	case kuaishou_mini.CallbackTypePay:
		err = localjx.OnKSPayCallback(payOrder, nil, payType)
	case kuaishou_mini.CallbackTypeRefund:
		err = localjx.OnKSPayCallback(nil, refundOrder, payType)
	case kuaishou_mini.CallbackTypeSettle:
		c.Data["json"] = CallBackFail(msgId)
		c.ServeJSON()
		return
	default:
		c.Data["json"] = CallBackFail(msgId)
		c.ServeJSON()
		return
	}

	if err != nil {
		c.Data["json"] = CallBackFail(msgId)
		c.ServeJSON()
		return
	}

	c.Data["json"] = CallBackSuccess(msgId)
	c.ServeJSON()
	return
}

type CallBackResult struct {
	Result    string `json:"result"`
	MessageId string `json:"message_id"`
}

func CallBackSuccess(msgId string) *CallBackResult {
	return &CallBackResult{
		Result:    "1",
		MessageId: msgId,
	}
}

func CallBackFail(msgId string) *CallBackResult {
	return &CallBackResult{
		Result:    "-1",
		MessageId: msgId,
	}
}
