package dao

import (
	"exam_system/config"
	"exam_system/entity"
	"fmt"
)

func FindRoleByUserId(id int) []*entity.Role {
	sqlStr := `
	SELECT
	r.id,
	r.name
FROM
	user u
	LEFT JOIN user_role ur ON ur.user_id = u.id
	LEFT JOIN role r ON ur.role_id = r.id
	where u.id=?
	GROUP BY r.id;
`

	var r []*entity.Role

	err := config.DB.Select(&r, sqlStr, id)
	if err != nil {
		fmt.Printf("get failed, err:%v\n", err)
		return nil
	}
	return r
}

func FindRoleByName(roleName string) *entity.Role {
	sqlStr := "select id from role where name=? limit 1"

	var r entity.Role
	err := config.DB.Get(&r, sqlStr, roleName)
	if err != nil {
		fmt.Printf("get failed, err:%v\n", err)
		return nil
	}

	return &r
}
