package cn.van.business.repository;

/**
 * @author Leo
 * @version 1.0
 * @create 2024/11/6 14:44
 * @description：
 */

import cn.van.business.model.jd.OrderRow;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

@Repository
public interface OrderRowRepository extends JpaRepository<OrderRow, String> {
    // 这里可以根据需要添加自定义的查询方法，例如：

    // 根据订单号查询订单行
    List<OrderRow> findByOrderId(long orderId);

    // 根据SKU ID 和订单号查询订单行
    List<OrderRow> findBySkuIdAndOrderId(long skuId, long orderId);

    // 根据有效码查询订单行
    List<OrderRow> findByValidCode(int validCode);

    // 查找  validCode != 15 或者 !=-1 的订单行 ,并且按orderTime 降序
    @Query("SELECT o FROM OrderRow o "
            + "WHERE o.validCode NOT IN :validCodes "
            + "AND o.unionId IN :unionIds "
            + "ORDER BY o.orderTime DESC")
    List<OrderRow> findByValidCodeNotInAndUnionIdIn(
            @Param("validCodes") int[] validCodes,
            @Param("unionIds") List<Long> unionIds
    );

    @Query("SELECT o FROM OrderRow o "
            + "WHERE o.validCode NOT IN :validCodes "
            + "AND o.orderTime >= :startDate "
            + "ORDER BY o.orderTime DESC")
    List<OrderRow> findByValidCodeNotInAndOrderTimeAfter(
            @Param("validCodes") int[] validCodes,
            @Param("startDate") @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate
    );
    // 修改后支持传入unionId列表
    @Query("SELECT o FROM OrderRow o "
            + "WHERE o.validCode NOT IN :validCodes "
            + "AND o.skuId = :skuId "
            + "AND o.unionId IN :unionIds "
            + "ORDER BY o.orderTime DESC")
    List<OrderRow> findBySkuIdAndUnionIdIn(
            @Param("validCodes") int[] validCodes,
            @Param("skuId") long skuId,
            @Param("unionIds") List<Long> unionIds
    );

    //// 在OrderRowRepository中添加模糊查询方法
    //// 模糊查询收件人姓名或地址（包含分页）
    //@Query("SELECT o FROM OrderRow o WHERE " + "o.recipientName LIKE %:keyword% OR " + "o.address LIKE %:keyword% " + "ORDER BY o.orderTime DESC")
    //Page<OrderRow> searchByRecipientOrAddress(@Param("keyword") String keyword, Pageable pageable);
}
