package cn.ysk.cashier.cons.repository;

import cn.ysk.cashier.cons.domain.TbConsInfoFlow;
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.Date;
import java.util.List;

public interface TbConsInfoFlowRepository extends JpaRepository<TbConsInfoFlow, Integer>, JpaSpecificationExecutor<TbConsInfoFlow> {


    @Query(value = "SELECT " +
            "p.`name` " +
            " FROM " +
            "tb_product_sku s" +
            " LEFT JOIN tb_product p ON s.product_id = p.id " +
            " WHERE " +
            "s.id = ?1",nativeQuery = true)
    String selectByPskId(Integer skuId);

    @Query("SELECT flow FROM TbConsInfoFlow flow " +
            "INNER JOIN TbConsInfo cons ON flow.consId = cons.id " +
            "AND cons.shopId = :shopId " +
            "AND (:consId IS NULL OR cons.id = :consId) " +
            "AND (:conTypeId IS NULL OR cons.conTypeId = :conTypeId) " +
            "AND (:conName IS NULL OR cons.conName LIKE %:conName%) " +
            "where flow.shopId = :shopId " +
            "AND flow.createTime > :startTime " +
            "AND flow.createTime < :endTime " +
            "AND flow.bizCode IN :types " +
            "ORDER BY flow.id DESC")
    Page<TbConsInfoFlow> findByConsParam(Integer shopId, Integer consId, String conName, Integer conTypeId, List<String> types,
                                         Date startTime, Date endTime, Pageable pageable);


}