package cn.palmte.work.model;


import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;


public interface SysPermissionRepository  extends JpaRepository<SysPermission, Integer> {


    @Query(" from SysPermission sp where sp.isDeleted=0 AND sp.level=?1 order by sort asc")
    public List<SysPermission> findSysPermissionByLevel(int level);

    @Query(" from SysPermission sp where sp.isDeleted=0 AND (sp.parentId=?1 OR sp.parentId in (select id from SysPermission where isDeleted = 0 and parentId in?1 )) order by sort asc")
    public List<SysPermission> findSysPermissionByParentId(int parentId);


    @Query(" from SysPermission where isDeleted=0 AND id in (select permissionId FROM SysRolePermission WHERE isDeleted=0 AND roleId =?1 ) ORDER BY level ASC,sort ASC  ")
    public List<SysPermission> findPermissionByRoleId(int roleId);


    @Query(" from SysPermission sp where sp.isDeleted=0 order by sort asc")
    public List<SysPermission> findAllPermission();

    @Query(" select sp.url from SysPermission sp where sp.isDeleted=0 order by sort asc")
    public List<String> findAllPermissionUrls();


    @Query(" from SysPermission sp where sp.isDeleted=0 AND sp.level<5 order by id asc")
    List<SysPermission> findPermissions();

    SysPermission findByUrlEquals(String url);

    SysPermission findByPermissionEquals(String permission);

}
