package controllers

import "eta/eta_mini_crm_ht/models"

type ClassifyController struct {
	BaseAuthController
}

// List
// @Title 系统类别列表
// @Description 系统类别列表
// @Success 200 {object} models.LoginResp
// @router /list [get]
func (this *ClassifyController) List() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()

	classifyList, err := models.GetClassifyList()
	if err != nil {
		br.Msg = "分类列表获取失败"
		br.ErrMsg = "分类列表获取失败，系统错误，Err:" + err.Error()
		return
	}
	classifyList = getClassifyTree(classifyList, 0)

	br.Data = classifyList
	br.Ret = 200
	br.Msg = "列表获取成功"
	br.Success = true
}

// getClassifyTree 递归获取分类树
func getClassifyTree(classifyList []*models.ClassifyView, parentId int) []*models.ClassifyView {
	res := make([]*models.ClassifyView, 0)
	for _, v := range classifyList {
		if v.ParentId == parentId {
			t := new(models.ClassifyView)
			t.Id = v.Id
			t.ClassifyName = v.ClassifyName
			t.ParentId = v.ParentId
			t.CreateTime = v.CreateTime
			t.ModifyTime = v.ModifyTime
			t.Enabled = v.Enabled
			t.ClassifyLabel = v.ClassifyLabel
			t.Child = getClassifyTree(classifyList, v.Id)
			res = append(res, t)
		}
	}
	return res
}
