package back

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

func GetAuthorityByUserId(userId int64) ([]domain.BackAuthority, error) {
	var BA = make([]domain.BackAuthority, 0)
	err := configs.Engine.Table("back_authority").
		Where("id in (SELECT authority_id from back_role_authority where back_role_authority.role_id = "+
			"( SELECT `user`.role_id FROM `user` WHERE `user`.id = ? LIMIT 1 ))", userId).Find(&BA)
	//err := configs.Engine.Table("back_authority").
	//	Join("Inner", "back_role_authority", "back_authority.id = back_role_authority.authority_id").
	//	Join("Inner", "back_role", "back_role_authority.role_id = back_role.id").
	//	Join("Inner", "user", "back_role.id = user.role_id").
	//	Where("user.id = ?", userId).Find(&BA)
	if err != nil {
		return BA, err
	}
	fmt.Println("66666:", BA)
	return BA, nil
}

func GetMenuByUserId(userId int64) ([]domain.BackMenu, error) {
	var BM = make([]domain.BackMenu, 0)
	err := configs.Engine.Table("back_menu").
		Where("id in (SELECT menu_id from back_role_menu where back_role_menu.role_id = "+
			"( SELECT `user`.role_id FROM `user` WHERE `user`.id = ? LIMIT 1 ))", userId).Find(&BM)
	if err != nil {
		return BM, err
	}
	fmt.Println("66666:", BM)
	return BM, nil
}
