package cn.ysk.cashier.repository.product;

import cn.ysk.cashier.pojo.product.TbProductStockDetail;
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.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface TbProductStockDetailRepository extends JpaRepository<TbProductStockDetail, Long>, JpaSpecificationExecutor<TbProductStockDetail> {

    @Query("SELECT detail FROM TbProductStockDetail detail " +
            "INNER JOIN TbProduct pro ON detail.productId = pro.id " +
            "AND pro.shopId = :shopId " +
            "AND (:categoryId IS NULL OR pro.categoryId = :categoryId) " +
            "AND (:productName IS NULL OR pro.name LIKE %:productName%) " +
            "AND (:type IS NULL OR pro.typeEnum = :type) " +
            "where detail.shopId = :shopId " +
            "AND detail.createdAt > :startTime " +
            "AND detail.createdAt < :endTime " +
            "AND detail.type IN :types " +
            "ORDER BY detail.id DESC")
    Page<TbProductStockDetail> findByProductParam(String shopId, String productName, String categoryId, String type,
                                                  List<String> types,
                                                  long startTime, long endTime, Pageable pageable);
}