// 留存分布
package controllers

import (
	"box-gm/events"
	"box-gm/models"
	"box-gm/utils"
	"log"
	"time"

	"github.com/astaxie/beego"
)

type ResourceController struct {
	beego.Controller
}

// 主界面
// 通过c.Data[key]返回数据
// ace模板文件通过{{.key}}获取数据
func (c *ResourceController) 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
	}
	currTime := time.Now()

	// if c.GetString("querydata") == "resource" {
	// 	uid, _ := c.GetInt("uid")
	// 	itemId, _ := c.GetInt("item_id")
	// 	startTime := c.GetString("start_time")
	// 	endTime := c.GetString("end_time")

	// 	log.Printf("查询玩家资源变化 uid[%d] itemId[%d] startTime[%s] endTime[%s]", uid, itemId, startTime, endTime)

	// 	_startTime := utils.GetTime(startTime + " 00:00:00")
	// 	_endTime := utils.GetTime(endTime+ " 23:59:59")
	// 	filename := events.Resource_get_records(uid, _startTime, _endTime, itemId)

	// 	c.Ctx.Output.Download(filename)

	// }else {
	// 	c.Data["Player_id"] = 1001000001
	// 	c.Data["Item_id"] = 104
	// 	c.Data["Start_time"] = currTime.Format("2006-01-02")
	// 	c.Data["End_time"] = currTime.Format("2006-01-02")
	// 	//界面模板文件
	// 	c.TplName = "resource.tpl"
	// }

	filename := ""
	if c.GetString("action") == "query" || c.GetString("querydata") == "resource" {
		uid, _ := c.GetInt("uid")
		itemId, _ := c.GetInt("item_id")
		startTime := c.GetString("start_time")
		endTime := c.GetString("end_time")

		log.Printf("查询玩家资源变化 uid[%d] itemId[%d] startTime[%s] endTime[%s]", uid, itemId, startTime, endTime)

		_startTime := utils.GetTime(startTime + " 00:00:00")
		_endTime := utils.GetTime(endTime + " 23:59:59")
		eventList := events.Resource_get_records(uid, _startTime, _endTime, itemId)
		filename = events.Save_resource_records(eventList, userinfo.(*LoginInfo).Username)

		c.Data["Events"] = eventList
		c.Data["Player_id"] = uid
		c.Data["Item_id"] = itemId
		c.Data["Start_time"] = startTime
		c.Data["End_time"] = endTime
	} else {
		c.Data["Start_time"] = currTime.Format("2006-01-02")
		c.Data["End_time"] = currTime.Format("2006-01-02")
	}

	if c.GetString("querydata") == "resource" {
		c.Ctx.Output.Download(filename)
	} else {
		//界面模板文件
		c.TplName = "resource.tpl"
	}
}
