package future_good

import (
	"eta/eta_mobile/controllers"
	"eta/eta_mobile/models"
	"eta/eta_mobile/models/data_manage"
	"eta/eta_mobile/models/system"
	"eta/eta_mobile/utils"
	"fmt"
)

// FutureGoodChartClassifyController 期货指标分类
type FutureGoodChartClassifyController struct {
	controllers.BaseAuthController
}

// ChartClassifyList
// @Title 商品价格图表分类列表
// @Description 商品价格图表分类列表接口
// @Param   IsShowMe   query   bool  true       "是否只看我的，true、false"
// @Success 200 {object} data_manage.ChartClassifyListResp
// @router /chart_classify/list [get]
func (this *FutureGoodChartClassifyController) 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
		}
		// 移除没有权限的图表
		allNodes := handleNoPermissionChart(resp.AllNodes, noPermissionChartIdMap)
		resp.AllNodes = allNodes

		br.Ret = 200
		br.Success = true
		br.Msg = "获取成功"
		br.Data = resp
		fmt.Println("source my classify")
		return
	}

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

	allChartInfo, err := data_manage.GetChartInfoAll([]int{utils.CHART_SOURCE_FUTURE_GOOD, utils.CHART_SOURCE_FUTURE_GOOD_PROFIT})
	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 {
		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
}

// getChartClassifyListForMe 获取我创建的图表
func getChartClassifyListForMe(adminInfo system.Admin, resp *data_manage.ChartClassifyListResp) (errMsg string, err error) {
	rootList, err := data_manage.GetChartClassifyByParentId(0, utils.CHART_SOURCE_FUTURE_GOOD)
	if err != nil && err.Error() != utils.ErrNoRow() {
		errMsg = "获取失败"
		return
	}

	allChartInfo, err := data_manage.GetChartInfoByAdminId([]int{utils.CHART_SOURCE_FUTURE_GOOD, utils.CHART_SOURCE_FUTURE_GOOD_PROFIT}, adminInfo.AdminId)
	if err != nil && err.Error() != utils.ErrNoRow() {
		errMsg = "获取失败"
		return
	}

	chartInfoMap := make(map[int][]*data_manage.ChartClassifyItems)
	for _, v := range allChartInfo {
		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
		}
	}
	resp.AllNodes = rootList

	return
}

// 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
}
