package com.example.demo.repository;

import com.example.demo.model.Order;
import com.example.demo.model.OrderStatus;
import com.example.demo.model.OrderType;
import com.example.demo.model.User;
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 java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

@Repository
public interface OrderRepository extends JpaRepository<Order, Long> {
    
    /**
     * 根据订单号查找订单
     */
    Optional<Order> findByOrderNumber(String orderNumber);
    
    /**
     * 根据用户ID查找订单
     */
    List<Order> findByUserId(Long userId);
    
    /**
     * 根据用户ID分页查找订单
     */
    Page<Order> findByUserId(Long userId, Pageable pageable);
    
    /**
     * 根据订单状态查找订单
     */
    List<Order> findByStatus(OrderStatus status);
    
    /**
     * 根据订单状态分页查找订单
     */
    Page<Order> findByStatus(OrderStatus status, Pageable pageable);
    
    /**
     * 根据订单类型查找订单
     */
    List<Order> findByOrderType(OrderType orderType);
    
    /**
     * 根据用户ID和订单状态查找订单
     */
    List<Order> findByUserIdAndStatus(Long userId, OrderStatus status);
    
    /**
     * 根据创建时间范围查找订单
     */
    List<Order> findByCreatedAtBetween(LocalDateTime startDate, LocalDateTime endDate);
    
    /**
     * 根据用户ID和创建时间范围查找订单
     */
    List<Order> findByUserIdAndCreatedAtBetween(Long userId, LocalDateTime startDate, LocalDateTime endDate);
    
    /**
     * 统计用户订单数量
     */
    long countByUserId(Long userId);
    
    /**
     * 统计指定状态的订单数量
     */
    long countByStatus(OrderStatus status);
    
    /**
     * 统计用户指定状态的订单数量
     */
    long countByUserIdAndStatus(Long userId, OrderStatus status);
    
    /**
     * 查找用户的最近订单
     */
    @Query("SELECT o FROM Order o WHERE o.user.id = :userId ORDER BY o.createdAt DESC")
    List<Order> findRecentOrdersByUserId(@Param("userId") Long userId, Pageable pageable);
    
    /**
     * 查找待支付的订单
     */
    @Query("SELECT o FROM Order o WHERE o.status IN ('PENDING', 'CONFIRMED') AND o.createdAt < :expireTime")
    List<Order> findExpiredPendingOrders(@Param("expireTime") LocalDateTime expireTime);
    
    /**
     * 根据订单号模糊查询
     */
    @Query("SELECT o FROM Order o WHERE o.orderNumber LIKE %:orderNumber%")
    List<Order> findByOrderNumberContaining(@Param("orderNumber") String orderNumber);
    
    /**
     * 根据用户邮箱查找订单
     */
    @Query("SELECT o FROM Order o WHERE o.contactEmail = :email")
    List<Order> findByContactEmail(@Param("email") String email);
    
    /**
     * 根据用户手机号查找订单
     */
    @Query("SELECT o FROM Order o WHERE o.contactPhone = :phone")
    List<Order> findByContactPhone(@Param("phone") String phone);
    
    /**
     * 统计指定时间范围内的订单数量
     */
    @Query("SELECT COUNT(o) FROM Order o WHERE o.createdAt BETWEEN :startDate AND :endDate")
    long countByCreatedAtBetween(@Param("startDate") LocalDateTime startDate, @Param("endDate") LocalDateTime endDate);
    
    /**
     * 统计指定时间范围内的订单总金额
     */
    @Query("SELECT SUM(o.totalAmount) FROM Order o WHERE o.createdAt BETWEEN :startDate AND :endDate AND o.status = 'COMPLETED'")
    BigDecimal sumTotalAmountByCreatedAtBetween(@Param("startDate") LocalDateTime startDate, @Param("endDate") LocalDateTime endDate);
    
    /**
     * 查找需要自动取消的订单
     */
    @Query("SELECT o FROM Order o WHERE o.status = 'PENDING' AND o.createdAt < :cancelTime")
    List<Order> findOrdersToAutoCancel(@Param("cancelTime") LocalDateTime cancelTime);
    
    // 新增的查询方法
    
    /**
     * 根据状态和订单号模糊查询（分页）
     */
    Page<Order> findByStatusAndOrderNumberContainingIgnoreCase(OrderStatus status, String orderNumber, Pageable pageable);
    
    /**
     * 根据订单号模糊查询（分页）
     */
    Page<Order> findByOrderNumberContainingIgnoreCase(String orderNumber, Pageable pageable);
    
    /**
     * 根据用户查找订单（分页）
     */
    Page<Order> findByUser(User user, Pageable pageable);
    
    /**
     * 根据用户和状态查找订单（分页）
     */
    Page<Order> findByUserAndStatus(User user, OrderStatus status, Pageable pageable);
    
    /**
     * 根据用户和订单号模糊查询（分页）
     */
    Page<Order> findByUserAndOrderNumberContainingIgnoreCase(User user, String orderNumber, Pageable pageable);
    
    /**
     * 根据用户、状态和订单号模糊查询（分页）
     */
    Page<Order> findByUserAndStatusAndOrderNumberContainingIgnoreCase(User user, OrderStatus status, String orderNumber, Pageable pageable);
    
    /**
     * 统计指定时间之后的订单数量
     */
    long countByCreatedAtAfter(LocalDateTime dateTime);
    
    /**
     * 统计用户指定时间之后的订单数量
     */
    long countByUserAndCreatedAtAfter(User user, LocalDateTime dateTime);
    
    /**
     * 统计用户的订单数量
     */
    long countByUser(User user);
    
    /**
     * 统计用户指定状态的订单数量
     */
    long countByUserAndStatus(User user, OrderStatus status);
    
    /**
     * 统计指定状态的订单总金额
     */
    @Query("SELECT SUM(o.totalAmount) FROM Order o WHERE o.status = :status")
    BigDecimal sumTotalAmountByStatus(@Param("status") OrderStatus status);
    
    /**
     * 统计用户指定状态的订单总金额
     */
    @Query("SELECT SUM(o.totalAmount) FROM Order o WHERE o.user = :user AND o.status = :status")
    BigDecimal sumTotalAmountByUserAndStatus(@Param("user") User user, @Param("status") OrderStatus status);
}
