package channel

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

// CaseController 信息
type ChannelController struct {
	dao *channel.ChannelServ
	controllers.BaseController
}

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

// GetChannelList获取渠道列表
func (c *ChannelController) GetChannelList() {
	caseIDs := c.GetString("caseid")
	if caseIDs == "" {
		cases := c.Context.Get("cases").([]model.SysUserCase)
		caseIDs = c.GetCaseIDs(cases)
	}

	page, _ := c.GetInt("page")
	pageSize, _ := c.GetInt("pagesize")
	list, err := c.dao.GetChannelList(caseIDs, page, pageSize)
	if err != nil {
		c.ResponseError(err)
	}
	c.ResponseJSON(list)
}

// GetChannelById根据id获取渠道
func (c *ChannelController) GetChannelById() {
	caseid := c.GetString(":channelId")
	channel, err := c.dao.GetChannelById(caseid)
	if err != nil {
		c.ResponseError(err)
	}
	c.ResponseJSON(channel)
}

// SaveChannel 保存渠道信息
func (c *ChannelController) SaveChannel() {
	channel := model.TaChannel{}
	if err := c.ParseForm(&channel); err != nil {
		c.ResponseError(err)
	}
	newChannel, err := c.dao.SaveChannel(channel)
	if err != nil {
		c.ResponseError(err)
	}
	c.ResponseJSON(newChannel)
}

// DeleteChannel 删除渠道
func (c *ChannelController) DeleteChannel() {
	channelId := c.GetString(":channelId")
	err := c.dao.DeleteChannel(channelId)
	if err != nil {
		c.ResponseError(err)
	}
	c.ResponseJSON("删除成功")
}
