package data_manage_permission

import (
	"eta/eta_api/models/data_manage"
	"eta/eta_api/models/data_manage/data_manage_permission"
	"eta/eta_api/utils"
	"fmt"
)

type EdbClassifyPermission struct {
	ClassifyId       int `description:"分类id"`
	IsJoinPermission int `description:"是否加入权限管控,0:不加入;1:加入;默认:0"`
	EdbInfoId        int `orm:"column(edb_info_id);pk"`
}

// CheckEdbPermissionByPermissionIdList
// @Description: 检查EDB指标(含预测指标)权限
// @author: Roc
// @datetime 2024-03-28 16:12:08
// @param edbIsJoinPermission int
// @param edbClassifyIsJoinPermission int
// @param edbInfoId int
// @param edbClassifyId int
// @param permissionEdbInfoIdList []int
// @param permissionEdbClassifyIdList []int
// @return hasAuth bool
func CheckEdbPermissionByPermissionIdList(edbIsJoinPermission, edbClassifyIsJoinPermission, edbInfoId, edbClassifyId int, permissionEdbInfoIdList, permissionEdbClassifyIdList []int) (hasAuth bool) {
	//hasAuth = true

	// 判断 分类是否纳入权限管控
	if edbClassifyIsJoinPermission == 1 {
		// 不属于已授权的分类,那么就无权限
		if !utils.InArrayByInt(permissionEdbClassifyIdList, edbClassifyId) {
			return false
		}
	}

	// 判断 指标是否纳入权限管控
	if edbIsJoinPermission == 1 {
		// 不属于已授权的指标,那么就无权限
		if !utils.InArrayByInt(permissionEdbInfoIdList, edbInfoId) {
			return false
		}
	}

	hasAuth = true

	return
}

// CheckEdbClassifyPermissionByPermissionIdList
// @Description: 检查EDB分类权限
// @author: Roc
// @datetime 2024-03-29 14:35:19
// @param edbClassifyIsJoinPermission int
// @param edbClassifyId int
// @param permissionEdbClassifyIdList []int
// @return hasAuth bool
func CheckEdbClassifyPermissionByPermissionIdList(edbClassifyIsJoinPermission, edbClassifyId int, permissionEdbClassifyIdList []int) (hasAuth bool) {
	// 判断 分类是否纳入权限管控
	if edbClassifyIsJoinPermission == 1 {
		// 不属于已授权的分类,那么就无权限
		if !utils.InArrayByInt(permissionEdbClassifyIdList, edbClassifyId) {
			return
		}
	}

	hasAuth = true

	return
}

// GetUserEdbAndClassifyPermissionList
// @Description: 根据用户获取已经授权指标ID列表和指标分类ID列表
// @author: Roc
// @datetime 2024-03-28 17:20:36
// @param userId int
// @param edbInfoId int 如果传递了edbInfoId,则只返回该edbInfoId的权限列表(其实也就是为了判断是否存在该指标权限了,目的是为了少获取数据)
// @param classifyId int  classifyId,则只返回该classifyId的权限列表(其实也就是为了判断是否存在该指标分类权限了,目的是为了少获取数据)
// @return edbIdList []int
// @return classifyIdList []int
// @return err error
func GetUserEdbAndClassifyPermissionList(userId, edbInfoId, classifyId int) (edbIdList, classifyIdList []int, err error) {
	// 获取有权限的指标ID列表
	edbIdList, err = data_manage_permission.GetPermissionEdbIdList(userId, edbInfoId)
	if err != nil {
		fmt.Println("获取授权指标列表失败, err:", err)
		return
	}

	// 获取有权限的指标分类ID列表
	classifyIdList, err = data_manage_permission.GetPermissionEdbClassifyIdList(userId, classifyId)
	if err != nil {
		fmt.Println("获取授权指标分类列表失败, err:", err)
		return
	}

	return
}

// GetUserEdbClassifyPermissionList
// @Description: 根据用户获取已经授权指标分类ID列表
// @author: Roc
// @datetime 2024-03-28 17:20:36
// @param userId int
// @param classifyId int  classifyId,则只返回该classifyId的权限列表(其实也就是为了判断是否存在该指标分类权限了,目的是为了少获取数据)
// @return classifyIdList []int
// @return err error
func GetUserEdbClassifyPermissionList(userId, classifyId int) (classifyIdList []int, err error) {
	// 获取有权限的指标分类ID列表
	classifyIdList, err = data_manage_permission.GetPermissionEdbClassifyIdList(userId, classifyId)
	if err != nil {
		fmt.Println("获取授权指标分类列表失败, err:", err)
		return
	}

	return
}

// CheckEdbPermissionByEdbInfoId
// @Description: 根据指标id和指标分类id,检查EDB指标(含预测指标)权限(方法内部自己获取所有的指标和指标分类权限,不用额外传递)
// @author: Roc
// @datetime 2024-03-29 13:28:06
// @param edbInfoId int
// @param edbClassifyId int
// @param edbIsJoinPermission int
// @param userId int
// @return hasAuth bool
// @return err error
func CheckEdbPermissionByEdbInfoId(edbInfoId, edbClassifyId, edbIsJoinPermission, userId int) (hasAuth bool, err error) {
	currClassify, err := data_manage.GetEdbClassifyById(edbClassifyId)
	if err != nil {
		return
	}
	if currClassify != nil {
		return CheckEdbPermission(edbIsJoinPermission, currClassify.IsJoinPermission, userId, edbInfoId, edbClassifyId)
	}

	return
}

// CheckEdbPermission
// @Description: 检查EDB指标(含预测指标)权限(方法内部自己获取所有的指标和指标分类权限,不用额外传递)
// @author: Roc
// @datetime 2024-03-28 16:12:08
// @param edbIsJoinPermission int
// @param edbClassifyIsJoinPermission int
// @param edbInfoId int
// @param edbClassifyId int
// @return hasAuth bool
func CheckEdbPermission(edbIsJoinPermission, edbClassifyIsJoinPermission, userId, edbInfoId, edbClassifyId int) (hasAuth bool, err error) {
	edbIdList, classifyIdList, err := GetUserEdbAndClassifyPermissionList(userId, edbInfoId, edbClassifyId)
	if err != nil {
		fmt.Println("GetUserEdbAndClassifyPermissionList err:", err)
		return
	}

	hasAuth = CheckEdbPermissionByPermissionIdList(edbIsJoinPermission, edbClassifyIsJoinPermission, edbInfoId, edbClassifyId, edbIdList, classifyIdList)

	return
}