// 付费人数统计
package controllers

import (
	"box-gm/events"
	"box-gm/models"

	"github.com/astaxie/beego"
)

type GoodsController struct {
	beego.Controller
}

// 主界面
// 通过c.Data[key]返回数据
// ace模板文件通过{{.key}}获取数据
func (c *GoodsController) Get() {
	userinfo := c.GetSession("user")
	if userinfo == nil {
		c.Ctx.Redirect(302, "/accountlogin")
		return
	}
	c.Data["username"] = userinfo.(*LoginInfo).Username
	c.Data["token"] = models.GetToken()
	userPermission := models.GetPermission(userinfo.(*LoginInfo).Username)
	if (userPermission & (1 << uint(models.ModelStatment))) == 0 {
		c.Ctx.Redirect(302, "/accountlogin")
		return
	}
	serverId := c.GetString("server_id")
	if serverId == "" {
		serverId = "1001"
	}
	days, _ := c.GetInt("days")
	if days == 0 {
		days = 7
	}
	series, categories := events.Goods_get_range_day_time_count(days, serverId)
	filename := events.Save_goods_records(series, userinfo.(*LoginInfo).Username)
	c.Data["Series"], c.Data["Categories"] = series, categories
	c.Data["Server_id"] = serverId
	c.Data["file"] = filename
	c.Data["Days"] = days
	if c.GetString("querydata") == "goods" {
		c.Ctx.Output.Download(filename)
	} else {
		//界面模板文件
		c.TplName = "goods.tpl"
	}
}
