package message

import (
	"spaceofcheng/services/controllers"
	"spaceofcheng/services/models/model"
	"spaceofcheng/services/service/message"
)

// MessageController 信息
type MessageController struct {
	dao *message.MessageServ
	controllers.BaseController
}

// Constructor 初始化 Controller
// @Title Constructor
// @Description 初始化 Controller, 系统自动调用
func (c *MessageController) Constructor() {
	c.dao = message.NewMessageServ(c.Context)
}

// GetCmsInfoList 获取消息列表
func (c *MessageController) GetCmsInfoList() {
	org := c.Context.Get("org").(model.SysOrg)
	title := c.GetString("title")
	page, _ := c.GetInt("page")
	pageSize, _ := c.GetInt("pagesize")
	cmsInfos, err := c.dao.GetCmsInfoList(title, org.OrgId, page, pageSize)
	if err != nil {
		c.ResponseError(err)
	}
	c.ResponseJSON(cmsInfos)
}

// GetCmsInfoByLocation 根据位置获取消息列表
func (c *MessageController) GetCmsInfoByLocation() {
	locationid := c.GetString("locationid")
	orgid := c.GetString("orgid")
	cmsinfos, err := c.dao.GetCmsInfoByLocation(locationid, orgid)
	if err != nil {
		c.ResponseError(err)
	}
	c.ResponseJSON(cmsinfos)
}

// GetCmsInfoByID 根据id获取消息信息
func (c *MessageController) GetCmsInfoByID() {
	infoid := c.GetString(":infoid")
	info, err := c.dao.GetCmsInfoByID(infoid)
	if err != nil {
		c.ResponseError(err)
	}
	c.ResponseJSON(info)
}

// SaveCmsInfo 保存消息信息
func (c *MessageController) SaveCmsInfo() {
	cmsinfo := model.TaCmsInfo{}
	if err := c.ParseForm(&cmsinfo); err != nil {
		c.ResponseError(err)
	}
	newInfo, err := c.dao.SaveCmsInfo(cmsinfo)
	if err != nil {
		c.ResponseError(err)
	}
	c.ResponseJSON(newInfo)
}

// DelCmsInfo 删除消息信息
func (c *MessageController) DelCmsInfo() {
	infoid := c.GetString(":infoid")
	err := c.dao.DelCmsInfo(infoid)
	if err != nil {
		c.ResponseError(err)
	}
	c.ResponseJSON("删除成功！")
}
