package controllers

import (
	"encoding/json"
	"hongze/hz_crm_api/models"
	"hongze/hz_crm_api/services"
	"hongze/hz_crm_api/utils"
)

// ClassifyController 分类
type ClassifyController struct {
	BaseAuthController
}

// ListClassify
// @Title 获取分类列表
// @Description 获取分类列表
// @Param   KeyWord   query   string  true       "检索关键词"
// @Param   CompanyType   query   string  false       "产品类型，枚举值：'ficc','权益';不传默认返回全部"
// @Param   HideDayWeek   query   int  false       "是否隐藏晨周报"
// @Success 200 {object} models.Classify
// @router /list [get]
func (this *ClassifyController) ListClassify() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	keyWord := this.GetString("KeyWord")
	companyType := this.GetString("CompanyType")
	hideDayWeek, _ := this.GetInt("HideDayWeek")

	req := new(services.GetClassifyListReq)
	req.Keyword = keyWord
	req.CompanyType = companyType
	req.HideDayWeek = hideDayWeek
	resp, err := services.GetClassifyList(req)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取失败，Err:" + err.Error()
		return
	}

	br.Data = resp
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
}

// @Title 含电话会分类列表
// @Description 含电话会分类列表
// @Success 200 {object} models.Classify
// @router /tel_list [get]
func (this *ClassifyController) TelListClassify() {
	// todo 获取电话会 是否需要改成从中间服务项目中获取
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()

	resp := make([]*models.SimpleClassifyList, 0)
	var condition, orderRule string
	var pars []interface{}
	condition += ` AND has_teleconference = 1`
	telList, e := models.GetClassifyByCondition(condition, orderRule, pars)
	if e != nil {
		return
	}
	firstList := make([]*models.SimpleClassifyList, 0)
	secondList := make([]*models.SimpleClassifyList, 0)
	secondMap := make(map[int][]*models.SimpleClassifyList, 0)
	for i := range telList {
		if telList[i].ParentId == 0 {
			firstList = append(firstList, telList[i])
		} else {
			secondList = append(secondList, telList[i])
			secondMap[telList[i].ParentId] = append(secondMap[telList[i].ParentId], telList[i])
		}
	}
	for i := range firstList {
		firstList[i].Child = secondMap[firstList[i].Id]
	}
	resp = firstList

	br.Data = resp
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
}

// Edit
// @Title 编辑报告分类
// @Description 编辑报告分类
// @Param	request  body  services.EditClassifyReq  true  "type json string"
// @Success 200 string "操作成功"
// @router /edit [post]
func (this *ClassifyController) Edit() {
	br := new(models.BaseResponse).Init()
	br.IsSendEmail = false
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	sysUser := this.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		br.Ret = 408
		return
	}
	var req services.EditClassifyReq
	if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	if req.ClassifyId <= 0 {
		br.Msg = "分类ID有误"
		return
	}

	err, errMsg := services.EditReportClassify(&req)
	if err != nil {
		br.Msg = errMsg
		br.ErrMsg = "编辑报告分类失败, Err:" + err.Error()
		return
	}

	// 清除小程序端的章节缓存
	{
		key := "hongze_yb:report_chapter_type:GetEffectTypeID"
		_ = utils.Rc.Delete(key)
	}

	br.Ret = 200
	br.Success = true
	br.Msg = "操作成功"
}
