package controllers

import (
	"encoding/json"
	"hongze/hongze_open_api/models/request/chart"
	celuePushTable "hongze/hongze_open_api/models/tables/cygx/cygx_chart"
	"hongze/hongze_open_api/utils"
	"time"
)

// 图表模块
type ChartController struct {
	BaseAuth
}

// ChartChange
// @Title 图表变更通知的插入点接口
// @Description 图表变更通知的插入点接口
// @Param	request	body chart.CreatChartCeluePushReq true "type json string"
// @Success 200 创建成功
// @router /change [post]
func (c *ChartController) ChartChange() {
	var req chart.CreatChartCeluePushReq
	err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
	if err != nil {
		c.FailWithMessage("参数解析异常")
		return
	}
	//appid权限校验
	appid := req.Appid
	if utils.RunMode == "release" && appid != utils.CLPT_APPID {
		c.FailWithMessage("无权限")
		return
	}
	chartId := req.ChartId
	action := req.Action
	if chartId < 0 {
		c.FailWithMessage("缺少 chart_id 参数")
		return
	}
	if action != "add" && action != "edit" && action != "move" {
		c.FailWithMessage("action参数类型错误")
		return
	}
	item := new(celuePushTable.CygxChartCeluePush)
	item.ChartId = chartId
	item.Action = action
	item.CreateTime = time.Now()
	item.ModifyTime = time.Now()
	err = celuePushTable.AddCygxChartCeluePush(item)
	if err != nil {
		c.OkWithMessage("创建失败")
		return
	}
	c.OkWithMessage("创建成功")
}