// 实时数据
package controllers

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


	"github.com/astaxie/beego"
)

type HotfixController struct {
	beego.Controller
}

func (c *HotfixController) 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.ModelSystem))) == 0 {
		c.Ctx.Redirect(302, "/accountlogin")
		return
	}

	action := c.GetString("action")
	log.Printf("HotfixController Get action[%s]", action)
	switch action {
	case "select":
		id, _ := c.GetInt64("id")
		cp := models.GetHotfixInfoById(id)
		if cp == nil {
			c.Data["json"] = &map[string]interface{}{"status": false, "info": "参数不存在"}
			c.ServeJSON()
			return
		}

		c.Data["json"] = &map[string]interface{}{"status": true}
		c.ServeJSON()
	default:
		id, _ := c.GetInt64("id")
		cp := models.GetHotfixInfoById(id)
		if cp != nil {
			c.Data["Id"] = id
			c.Data["Channel"] = cp.Channel
			c.Data["Version"] = cp.Version
			c.Data["DowloadAddr"] = cp.DowloadAddr
			c.Data["FileSize"] = cp.FileSize
		}

		c.Data["infoList"] = models.GetHotfixInfo()
		c.TplName = "hotfix.tpl"
	}
}

func (c *HotfixController) Post() {
	userinfo := c.GetSession("user")
	log.Printf("HotfixController Post userinfo[%v]", userinfo)
	if userinfo == nil {
		c.Ctx.Redirect(302, "/accountlogin")
		return
	}
	channel, _ := c.GetInt64("channel")
	log.Printf("HotfixController Post channel[%d]", channel)
	hf := models.GetHotfixInfoByChannel(channel)
	if hf != nil {
		c.Data["json"] = &map[string]interface{}{"status": false, "info": "新增失败:渠道已配置"}
		c.ServeJSON()
		return
	}

	u := new(models.HotfixInfo)
	u.Channel = channel
	u.Version = c.GetString("version")
	u.DowloadAddr = c.GetString("addr")
	u.FileSize = c.GetString("filesize")

	if u.Channel == 0 || u.Version == "" {
		c.Data["json"] = &map[string]interface{}{"status": false, "info": "渠道和版本不能为空"}
		c.ServeJSON()
		return
	}

	_, err := models.AddHotfixInfo(u)
	if err != nil {
		c.Data["json"] = &map[string]interface{}{"status": false, "info": err.Error()}
		c.ServeJSON()
		return
	}

	c.Data["json"] = &map[string]interface{}{"status": true, "info": "新增热更新参数成功"}
	c.ServeJSON()
}

func (c *HotfixController) Put() {
	userinfo := c.GetSession("user")
	if userinfo == nil {
		c.Ctx.Redirect(302, "/accountlogin")
		return
	}

	u := new(models.HotfixInfo)
	u.Id, _ = c.GetInt64("id")
	u.Channel, _ = c.GetInt64("channel")
	u.Version = c.GetString("version")
	u.DowloadAddr = c.GetString("addr")
	u.FileSize = c.GetString("filesize")

	_, err := models.UpdateHotfixInfo(u)
	if err != nil {
		c.Data["json"] = &map[string]interface{}{"status": false, "info": err}
		c.ServeJSON()
		return
	}
	c.Data["json"] = &map[string]interface{}{"status": true, "info": "更新热更新参数成功"}
	c.ServeJSON()
}

func (c *HotfixController) Delete() {
	userinfo := c.GetSession("user")
	if userinfo == nil {
		c.Ctx.Redirect(302, "/accountlogin")
		return
	}

	id, _ := c.GetInt64("id")
	cp := models.GetHotfixInfoById(id)
	if cp == nil {
		c.Data["json"] = &map[string]interface{}{"status": false, "info": "参数不存在"}
		c.ServeJSON()
		return
	}

	models.DelHotfixInfoById(id)

	c.Data["json"] = &map[string]interface{}{"status": true, "info": "删除热更新参成功"}
	c.ServeJSON()
}
