package gaf3.core.services.dept.dao

import gaf3.core.services.dept.entity.GafDeptItem
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Query
import org.springframework.data.repository.query.Param

interface GafDeptItemDAO : JpaRepository<GafDeptItem, String> {
    @Query(value = "select case when count(p) > 0 then true else false end from GafDeptItem p where p.code = :code")
    fun exists(@Param("code") code: String): Boolean

    fun existsByPidAndName(pid: String, name: String): Boolean

    fun existsByPid(pid: String): Boolean

    @Query(value = "select p from GafDeptItem p where p.pid = :pid or (:pid = '' and p.pid is null) order by p.code, p.name")
    fun findByPid(@Param("pid") pid: String): List<GafDeptItem>

    @Query(value = "select p from GafDeptItem p where p.pid = '' or p.pid is null order by p.code, p.name")
    fun findRoot(): List<GafDeptItem>
}
