package data

import (
	"eta/eta_api/models/data_manage"
	"eta/eta_api/models/system"
	"eta/eta_api/utils"
	"fmt"
	"strconv"
	"time"
)

func chartClassifyHaveChild(allNode []*data_manage.ChartClassifyItems, node *data_manage.ChartClassifyItems) (childs []*data_manage.ChartClassifyItems, yes bool) {
	for _, v := range allNode {
		if v.ParentId == node.ChartClassifyId {
			childs = append(childs, v)
		}
	}
	if len(childs) > 0 {
		yes = true
	}
	return
}

func ChartClassifyItemsMakeTree(sysUser *system.Admin, allNode []*data_manage.ChartClassifyItems, node *data_manage.ChartClassifyItems) {
	button := GetChartClassifyOpButton(sysUser, node.SysUserId)
	node.Button = button

	childs, _ := chartClassifyHaveChild(allNode, node) //判断节点是否有子节点并返回
	if len(childs) > 0 {
		for _, child := range childs {
			childButton := GetChartClassifyOpButton(sysUser, child.SysUserId)
			if child.Level == 3 {
				childButton.AddButton = false //第三级的话，默认图表都是没有添加按钮的
			}
			child.Button = childButton
			//node.Children = append(node.Children, child)
		}
		node.Children = append(node.Children, childs[0:]...) //添加子节点
		for _, v := range childs {                           //查询子节点的子节点，并添加到子节点
			_, has := chartClassifyHaveChild(allNode, v)
			if has {
				ChartClassifyItemsMakeTree(sysUser, allNode, v) //递归添加节点
			} else {
				childrenArr := make([]*data_manage.ChartClassifyItems, 0)
				v.Children = childrenArr
			}
		}
	} else {
		childrenArr := make([]*data_manage.ChartClassifyItems, 0)
		node.Children = childrenArr
	}
}

func FixChartClassify() {
	//新增三级分类
	list, err := data_manage.GetChartClassifyByLevel(2, 1)
	if err != nil {
		return
	}

	for _, v := range list {
		classify := new(data_manage.ChartClassify)
		classify.ParentId = v.ChartClassifyId
		classify.ChartClassifyName = v.ChartClassifyName
		classify.HasData = 0
		classify.CreateTime = time.Now()
		classify.ModifyTime = time.Now()
		if utils.RunMode == "debug" {
			classify.SysUserId = 11
		} else {
			classify.SysUserId = 163
		}
		classify.SysUserRealName = "admin"
		classify.Level = 3
		timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
		classify.UniqueCode = utils.MD5(utils.DATA_PREFIX + "_" + timestamp)
		classify.Sort = v.Sort
		classify.Source = utils.CHART_SOURCE_DEFAULT

		id, err := data_manage.AddChartClassify(classify)
		if err != nil {
			return
		}

		err = data_manage.FIXChartClassifyId(int(id), v.ChartClassifyId)
		if err != nil {
			return
		}
	}
}

func FixChartClassifySysUserId() {
	//获取所有父级分类信息
	list, err := data_manage.GetChartClassifyByLevel(2, 1)
	if err != nil {
		return
	}

	for _, v := range list {
		err := data_manage.EditChartClassifySysUser(v.ChartClassifyId, v.SysUserId, v.SysUserRealName)
		if err != nil {
			return
		}
		fmt.Println("ChartClassifyId:", v.ChartClassifyId)
	}
	fmt.Println("FixChartClassifySysUserId end")
}

// GetChartClassifyOpButton 获取ETA图库分类的操作权限
func GetChartClassifyOpButton(sysUser *system.Admin, belongUserId int) (button data_manage.ChartClassifyItemsButton) {
	//ficc管理员和超管和ficc研究员有权限创建和管理分类，可以编辑分类名称（分类名称不允许重复），可以拖动分类，改变分类顺序，可以拖动分类下模型，改变顺序，可以删除分类，若分类下有预测指标，则不允许删除；
	//if utils.InArrayByStr([]string{utils.ROLE_TYPE_CODE_ADMIN, utils.ROLE_TYPE_CODE_FICC_ADMIN, utils.ROLE_TYPE_CODE_RESEARCHR, utils.ROLE_TYPE_CODE_FICC_RESEARCHR}, sysUser.RoleTypeCode) {
	button.AddButton = true
	button.OpButton = true
	button.DeleteButton = true
	button.MoveButton = true
	//}

	return
}

// GetCanOpChartClassify 获取是否允许添加图表分类
func GetCanOpChartClassify(roleTypeCode string) (canOpClassify bool) {
	// 如果不是 超管 或者 ficc管理员 或者 ficc研究员，那么就没有权限
	if utils.InArrayByStr([]string{utils.ROLE_TYPE_CODE_ADMIN, utils.ROLE_TYPE_CODE_FICC_ADMIN, utils.ROLE_TYPE_CODE_RESEARCHR, utils.ROLE_TYPE_CODE_FICC_RESEARCHR}, roleTypeCode) {
		canOpClassify = true
	}

	return
}

// GetChartOpButton 获取ETA图库的操作权限
func GetChartOpButton(sysUser *system.Admin, belongUserId int) (button data_manage.ChartClassifyItemsButton) {
	//预测指标的添加人对该预测指标有全部操作权限，ficc管理员、超管对所有预测指标有全部操作权限；
	if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN || sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN || sysUser.AdminId == belongUserId || sysUser.PredictEdbPermission == 1 {
		button.AddButton = true
		button.OpButton = true
		button.DeleteButton = true
		button.MoveButton = true
	}

	return
}
