package controllers

import (
	"fmt"
	beego "github.com/beego/beego/v2/server/web"
	"github.com/rs/xid"
	"merchant/models/merchant"
	"merchant/models/order"
	"merchant/sys/enum"
	"strings"
	"time"
)

type GenLink struct {
	KeepSession
}

func (c *GenLink) ShowGenLinkUI() {
	c.TplName = "gen_link.html"
}

func (c *GenLink) GenShopLink() {
	showMMValue, err := c.GetFloat("showMMValue")
	if err != nil {
		c.Data["errorMsg"] = "获取面额出错！"
		c.TplName = "error.html"
		return
	}
	productCode := strings.TrimSpace(c.GetString("productCode"))
	us := c.GetSession(enum.UserSession)
	u := us.(merchant.MerchantInfo)

	payLink, err := beego.AppConfig.String("payLink::url")

	if err != nil {
		payLink = "http://localhost:12305/"
	}

	orderParams := order.Params{
		PayKey:        u.MerchantKey,
		GeneratedTime: time.Now().Unix(),
		OrderNo:       xid.New().String(),
		Duration:      24,
		ProductCode:   productCode,
		ShowMMValue:   showMMValue,
		NotifyUrl:     fmt.Sprintf("%s%s", payLink, "shop/notify"),
	}

	c.Redirect(payLink+"?sign="+orderParams.Encrypt(), 302)
}
