package dao

import (
	"demo/configs"
	"demo/data/domain"
	"fmt"
)

func GetListAuthorityAll() []domain.BackAuthority {
	var authority domain.BackAuthority
	var arr []domain.BackAuthority
	configs.Engine.Desc("id").FindAndCount(&arr, &authority)
	return arr
}
func AddAllListAuthorityAll(arr []*domain.BackAuthority) {
	for i := range arr {
		insert, err := configs.Engine.Insert(arr[i])
		if err != nil {
			fmt.Println(err)
		}
		fmt.Println(insert)
	}
}

// CheckAuthorityByUserId 根据userid查看用户是否具有该权限
func CheckAuthorityByUserId(userId, authorityId int64) bool {
	var user domain.User
	get, err := configs.Engine.Table("user").Where("id=?", userId).Get(&user)
	if user.Id == 0 || !get || err != nil {
		return false
	}
	var bras domain.BackRoleAuthority
	count, err := configs.Engine.Table("back_role_authority").Where("role_id=? and authority_id=?", user.RoleId, authorityId).Count(&bras)
	if err != nil || count == 0 {
		return false
	}
	return true

}
