package gaf3.core.services.code.dao

import gaf3.core.services.code.entity.GafCodeItem
import org.hibernate.query.criteria.internal.predicate.ExistsPredicate
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.data.domain.Sort
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Modifying
import org.springframework.data.jpa.repository.Query
import org.springframework.data.repository.query.Param

interface GafCodeItemDAO : JpaRepository<GafCodeItem, String> {

    @Query(value = "select p from GafCodeItem p where p.type = ?1 order by p.code")
    fun findByType(type: String, page: Pageable): Page<GafCodeItem>

    fun findAllByType(type: String, sort: Sort): List<GafCodeItem>

    @Query(value = "select p from GafCodeItem p where p.type is null or p.type = '' or p.type = 'group' order by p.code")
    fun listTypes(): List<GafCodeItem>

    fun findOneByTypeAndAlias(type: String, alias: String): GafCodeItem?

    fun findOneByTypeAndCode(type: String, code: String): GafCodeItem?

    fun existsByTypeAndCode(type: String, code: String): Boolean

    @Modifying
    @Query(value = "update GafCodeItem p set p.status = :status where p.id = :id")
    fun status(@Param("id") id: String, @Param("status") status: String)

    @Modifying
    fun deleteAllByType(type: String);
}
