package es.uv.saic.domain;

import java.util.List;

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

@Repository
public interface CategoriaRepository  extends JpaRepository<Categoria, Integer> {
	
	@Query(value="SELECT * FROM categories WHERE pare = ?1 AND tambit IN('U', 'C', ?2) ORDER BY ordre ASC;", nativeQuery=true)
	List<Categoria> findByPareTambitAndU(Integer pare, String tambit); 

	@Query(value="SELECT * FROM categories WHERE pare = ?1 AND (tambit = ?2) ORDER BY ordre ASC;", nativeQuery=true)
	List<Categoria> findByPareTambit(Integer pare, String tambit); 

	@Query(value="SELECT * FROM categories WHERE tipus = ?1 AND tambit IN('U', 'C', ?2) ORDER BY ordre ASC;", nativeQuery=true)
	List<Categoria> findByTipusTambitAndU(String tipus, String tambit); 

	@Query(value="SELECT * FROM categories WHERE tipus = ?1 AND (tambit = ?2) ORDER BY ordre ASC;", nativeQuery=true)
	List<Categoria> findByTipusTambit(String tipus, String tambit); 
	
	@Query(value="SELECT * FROM categories WHERE pare IS NULL AND (tambit = ?1) ORDER BY ordre ASC;", nativeQuery=true)
	List<Categoria> findFirstLevel(String ambit);

	@Query(value="SELECT * FROM categories WHERE pare IS NULL AND tambit IN('U', 'C', ?1) ORDER BY ordre ASC;", nativeQuery=true)
	List<Categoria> findFirstLevelAndU(String ambit);

}
