// 在线时长分布
package controllers

import (
	"github.com/astaxie/beego"
	"box-gm/events"
	"box-gm/models"
)

type OnlineTimeController struct {
	beego.Controller
}

// 主界面
// 通过c.Data[key]返回数据
// ace模板文件通过{{.key}}获取数据
func (c *OnlineTimeController) 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"
	}
	minutes, _ := c.GetInt("minutes")
	if minutes == 0 {
		minutes = 1
	}
	days, _ := c.GetInt("days")
	if days == 0 {
		days = 7
	}
	
	series, categories := events.OnlineTime_get_range_day_time_count(days, minutes, serverId)
	filename := events.Save_onlinetime_records(series, minutes, userinfo.(*LoginInfo).Username)
	c.Data["Series"], c.Data["Categories"] = series, categories
	c.Data["Server_id"] = serverId
	c.Data["Days"] = days
	c.Data["Minutes"] = minutes
	if c.GetString("querydata") == "onlinetime" {
		c.Ctx.Output.Download(filename)
	}else {
		//界面模板文件
		c.TplName = "onlinetime.tpl"
	}
}
