package message

import (
	"spaceofcheng/services/models/model"
)

// GetNews 获取咨询列表
func (c *MessageController) GetNews() {
	org := c.Context.Get("org").(model.SysOrg)
	page, _ := c.GetInt("page")
	pageSize, _ := c.GetInt("pagesize")
	locationid := c.GetString("locationid")
	news, err := c.dao.GetNewsList(locationid, org.OrgId, page, pageSize)
	if err != nil {
		c.ResponseError(err)
	}
	c.ResponseJSON(news)
}

// GetNewsByID 获取咨询明细
func (c *MessageController) GetNewsByID() {
	newsid := c.GetString(":newsid")
	news, err := c.dao.GetNewsByID(newsid)
	if err != nil {
		c.ResponseError(err)
	}
	c.ResponseJSON(news)
}

// GetNewsByLocation 根据位置获取咨询明细
func (c *MessageController) GetNewsByLocation() {
	locationid := c.GetString("locationid")
	orgid := c.GetString("orgid")
	news, err := c.dao.GetNewsByLocation(locationid, orgid)
	if err != nil {
		c.ResponseError(err)
	}
	c.ResponseJSON(news)
}

// SaveNews 保存咨询明细
func (c *MessageController) SaveNews() {
	news := model.TaCmsNews{}
	if err := c.ParseForm(&news); err != nil {
		c.ResponseError(err)
	}
	locationids := c.GetString("locationids")
	newInfo, err := c.dao.SaveNews(news, locationids)
	if err != nil {
		c.ResponseError(err)
	}
	c.ResponseJSON(newInfo)
}

// DelNews 删除咨询明细
func (c *MessageController) DelNews() {
	newsid := c.GetString(":newsid")
	err := c.dao.DelNews(newsid)
	if err != nil {
		c.ResponseError(err)
	}
	c.ResponseJSON("删除成功！")
}
