package correlation

import (
	"hongze/hongze_ETA_mobile_api/controllers"
	"hongze/hongze_ETA_mobile_api/models"
	"hongze/hongze_ETA_mobile_api/models/data_manage"
	"hongze/hongze_ETA_mobile_api/utils"
)

// CorrelationChartClassifyController 相关性图表分类
type CorrelationChartClassifyController struct {
	controllers.BaseAuthController
}

// ChartClassifyList
// @Title 相关性图表分类列表
// @Description 相关性图表分类列表接口
// @Param   IsShowMe   query   bool  true       "是否只看我的，true、false"
// @Param   Source   query   int  true       "图表类型，3：相关性，4：滚动相关性"
// @Success 200 {object} data_manage.ChartClassifyListResp
// @router /chart_classify/list [get]
func (this *CorrelationChartClassifyController) ChartClassifyList() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	resp := new(data_manage.ChartClassifyListResp)

	// 获取当前账号的不可见指标
	noPermissionChartIdMap := make(map[int]bool)
	{
		obj := data_manage.EdbInfoNoPermissionAdmin{}
		confList, err := obj.GetAllChartListByAdminId(this.SysUser.AdminId)
		if err != nil && err.Error() != utils.ErrNoRow() {
			br.Msg = "获取失败"
			br.ErrMsg = "获取不可见指标配置数据失败，Err:" + err.Error()
			return
		}
		for _, v := range confList {
			noPermissionChartIdMap[v.ChartInfoId] = true
		}
	}

	isShowMe, _ := this.GetBool("IsShowMe")
	//if isShowMe {
	//	errMsg, err := getChartClassifyListForMe(*this.SysUser, resp)
	//	if err != nil {
	//		br.Msg = errMsg
	//		br.ErrMsg = err.Error()
	//		return
	//	}
	//	br.Ret = 200
	//	br.Success = true
	//	br.Msg = "获取成功"
	//	br.Data = resp
	//	return
	//}

	source, _ := this.GetInt("Source")
	if source <= 0 {
		source = utils.CHART_SOURCE_CORRELATION
	}

	rootList, err := data_manage.GetChartClassifyByParentId(0, utils.CHART_SOURCE_CORRELATION)
	if err != nil && err.Error() != utils.ErrNoRow() {
		br.Msg = "获取失败"
		br.ErrMsg = "获取数据失败，Err:" + err.Error()
		return
	}

	allChartInfo, err := data_manage.GetChartInfoAll(source)
	if err != nil && err.Error() != utils.ErrNoRow() {
		br.Msg = "获取失败"
		br.ErrMsg = "获取图表信息失败，Err:" + err.Error()
		return
	}

	chartInfoMap := make(map[int][]*data_manage.ChartClassifyItems)
	for _, v := range allChartInfo {
		if !isShowMe {
			chartInfoMap[v.ChartClassifyId] = append(chartInfoMap[v.ChartClassifyId], v)
			continue
		}
		if v.SysUserId != this.SysUser.AdminId {
			continue
		}
		chartInfoMap[v.ChartClassifyId] = append(chartInfoMap[v.ChartClassifyId], v)
	}
	rootChildMap := make(map[int][]*data_manage.ChartClassifyItems)
	for _, v := range rootList {
		rootChildMap[v.ParentId] = append(rootChildMap[v.ParentId], v)
		if existItems, ok := chartInfoMap[v.ChartClassifyId]; ok {
			v.Children = existItems
		} else {
			items := make([]*data_manage.ChartClassifyItems, 0)
			v.Children = items
		}
	}

	// 移除没有权限的图表
	allNodes := handleNoPermissionChart(rootList, noPermissionChartIdMap)
	resp.AllNodes = allNodes

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

// handleNoPermissionChart 图表列表返回，将没有权限的图表移除
func handleNoPermissionChart(allNodes []*data_manage.ChartClassifyItems, noPermissionChartIdMap map[int]bool) (newAllNodes []*data_manage.ChartClassifyItems) {
	// 移除没有权限的图表
	newAllNodes = make([]*data_manage.ChartClassifyItems, 0)
	for _, node := range allNodes {
		// 二级分类
		tmpNodeInfo := *node
		tmpNodeList := make([]*data_manage.ChartClassifyItems, 0)

		if node.Children != nil {
			for _, chartInfo := range node.Children {
				// 如果指标不可见，那么就不返回该指标
				if _, ok := noPermissionChartIdMap[chartInfo.ChartInfoId]; ok {
					continue
				}
				tmpNodeList = append(tmpNodeList, chartInfo)
			}
		}

		tmpNodeInfo.Children = tmpNodeList
		newAllNodes = append(newAllNodes, &tmpNodeInfo)
	}

	return
}
