package com.example.demo.product.repo;

import com.example.demo.product.entity.Product;
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;

public interface ProductRepository extends JpaRepository<Product, Long> {

    @Query("SELECT p FROM Product p WHERE p.shopId = :shopId AND (p.deletedAt IS NULL) AND " +
            "(:kw IS NULL OR :kw = '' OR p.name LIKE CONCAT('%', :kw, '%') OR p.brand LIKE CONCAT('%', :kw, '%') OR p.model LIKE CONCAT('%', :kw, '%') OR p.spec LIKE CONCAT('%', :kw, '%') OR p.barcode LIKE CONCAT('%', :kw, '%')) AND " +
            "(:categoryId IS NULL OR p.categoryId = :categoryId) ORDER BY p.id DESC")
    Page<Product> search(@Param("shopId") Long shopId,
                         @Param("kw") String kw,
                         @Param("categoryId") Long categoryId,
                         Pageable pageable);

    boolean existsByShopIdAndBarcode(Long shopId, String barcode);
}







