package controllers

import (
	"encoding/json"
	"eta/eta_index_lib/models"
	"eta/eta_index_lib/utils"
	"strconv"
)

// @Title 查询/新增分类
// @Description 查询/新增分类接口
// @Param	request	body models.AddSmmClassifyReq true "type json string"
// @Success 200 {object} models.EdbClassify
// @router /smm_classify/get_or_add [post]
func (this *SmmController) SmmGetOrAdd() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	var req models.AddSmmClassifyReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	if req.ClassifyName == "" {
		br.Msg = "请输入分类名称"
		br.IsSendEmail = false
		return
	}
	if req.ParentId < 0 {
		br.Msg = "参数错误"
		br.IsSendEmail = false
		return
	}

	sysUserId := utils.InitAdminId
	sysUserName := utils.InitAdminName
	if req.SysUserId > 0 {
		sysUserId = strconv.Itoa(req.SysUserId)
	}
	if req.SysUserRealName != `` {
		sysUserName = req.SysUserRealName
	}

	//添加指标
	edbClassify, err, errMsg := models.SaveSmmClassify(req.ClassifyName, req.ParentId, req.Level, sysUserId, sysUserName)
	if errMsg != `` {
		br.Msg = errMsg
		br.ErrMsg = errMsg
		if err != nil {
			br.ErrMsg = errMsg + ";Err:" + err.Error()
		} else {
			br.IsSendEmail = false
		}
		return
	}
	br.Ret = 200
	br.Msg = "保存成功"
	br.Success = true
	br.Data = edbClassify
}
