package com.realnet.dlf.Repository;

import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import com.realnet.dlf.Entity.Design_lbrary;

@Repository
public interface Design_lbraryRepository extends JpaRepository<Design_lbrary, Integer> {

	@Query(value = "select * from design_lbrary where  created_by=?1", nativeQuery = true)
	List<Design_lbrary> findAll(Long creayedBy);

	@Query(value = "select * from design_lbrary where  created_by=?1", nativeQuery = true)
	Page<Design_lbrary> findAll(Pageable page, Long creayedBy);

	@Query(value = "select * from design_lbrary where  created_by=?1 && hedaer_id=?2 ", nativeQuery = true)
	List<Design_lbrary> getAllByhedId(Long creayedBy, Integer hedaer_id);

	@Query(value = "SELECT * from design_lbrary WHERE active=:active && templatetype=:templatetype && uitype=:fieldtype limit 1", nativeQuery = true)
	Design_lbrary getallByFieldType(@Param("active") boolean active, @Param("templatetype") String templatetype,
			@Param("fieldtype") String fieldtype);

	@Query(value = "SELECT * from design_lbrary WHERE active=:active && hedaer_id=:hedaer_id  && templatetype=:templatetype AND LOWER(uitype) = LOWER(:fieldtype) limit 1", nativeQuery = true)
	Design_lbrary getallByHeaderId(@Param("hedaer_id") Integer hedaer_id, @Param("active") boolean active,
			@Param("templatetype") String templatetype, @Param("fieldtype") String fieldtype);

	@Query(value = "SELECT * FROM design_lbrary " + "WHERE active=:active &&   templatetype = :operation_type "
			+ "AND LOWER(uitype) LIKE CONCAT(LOWER(:fieldtype), '%')", nativeQuery = true)
	List<Design_lbrary> getallDlfLine(@Param("active") boolean active, @Param("operation_type") String operationType,
			@Param("fieldtype") String fieldtype);

	@Query(value = "select * from design_lbrary WHERE id BETWEEN :startId AND :endId", nativeQuery = true)
	List<Design_lbrary> deleteByIdRange(@Param("startId") Long startId, @Param("endId") Long endId);

	@Query(value = "SELECT * FROM design_lbrary d WHERE active=:active &&   templatetype = :operation_type AND CONCAT(',', REPLACE(REPLACE(d.tag, '{', ''), '}', ''), ',') LIKE CONCAT('%,', :tag, ',%')", nativeQuery = true)
	List<Design_lbrary> searchByTag(@Param("active") boolean active, @Param("operation_type") String operationType,
			@Param("tag") String tag);
}