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 SysRoleRepository  extends JpaRepository<SysRole, Integer> {

    /**
     * 分页查询所有角色
     * @return
     */
    @Query(" from SysRole sp where sp.deleted=0 and sp.isEnable=1 order by last_updated_time asc")
    public List<SysRole> findAllRole();


    /**
     * 根据角色id获取角色对象
     * @param id
     * @return
     */
    @Query(" from SysRole sp where sp.deleted=0 and sp.id=?1 ")
    public SysRole findSysRoleById(int id);

    /**
     * 根据角色类型查询所有角色
     * @param type
     * @return
     */
    @Query(" from SysRole role where role.deleted=0 and type=?1 order by last_updated_time asc")
    List<SysRole> findRoleByType(int type);

    @Query(" select COUNT(1) from SysRole where id!=?1 and name=?2 and deleted=0 ")
    public int cheakRoleName(int id, String name);

    /**
     * 查询 后台系统管理员的角色（后台角色表中只能有一个系统管理员角色）
     * @return
     */
    @Query("  from SysRole where deleted=0 and  isEnable=1 AND type=0 ")
    public SysRole findSuperAdminRole();

    /**
     * 查询所有启用状态的后台角色
     * @return
     */
    @Query("  from SysRole where deleted=0 and  isEnable=1 and type = 1")
    List<SysRole> getAllEnableSysRole();

    SysRole findByNameEqualsAndDeletedEquals(String name,int deleted);

}
