package dao

import (
	"exam_system/config"
	"exam_system/entity"
	"exam_system/result"
	"github.com/jmoiron/sqlx"
)

func FindPermByRoles(roleIds []int) *result.Result {
	sqlStr := `
	SELECT
	p.id,
	p.path,
	p.method,
	p.name
FROM
	role r, perm_role pr , perm p
	where r.id in (?) and  pr.role_id = r.id and  pr.perm_id = p.id
	GROUP BY p.id;`

	query, args, _ := sqlx.In(sqlStr, roleIds)
	var r []*entity.Perm
	query = config.DB.Rebind(query)
	err := config.DB.Select(&r, query, args...)
	if err != nil {
		return result.UNKNOW_ERROR.SetMsg(err.Error())
	}
	return result.SuccessResult(r)
}
