package com.example.demo.repository;

import com.example.demo.model.OrderItem;
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.util.List;

@Repository
public interface OrderItemRepository extends JpaRepository<OrderItem, Long> {
    
    /**
     * 根据订单ID查找订单项
     */
    List<OrderItem> findByOrderId(Long orderId);
    
    /**
     * 根据产品SKU查找订单项
     */
    List<OrderItem> findByProductSku(String productSku);
    
    /**
     * 根据产品名称模糊查询订单项
     */
    @Query("SELECT oi FROM OrderItem oi WHERE oi.productName LIKE %:productName%")
    List<OrderItem> findByProductNameContaining(@Param("productName") String productName);
    
    /**
     * 统计指定产品的销售数量
     */
    @Query("SELECT SUM(oi.quantity) FROM OrderItem oi WHERE oi.productSku = :productSku")
    Long sumQuantityByProductSku(@Param("productSku") String productSku);
    
    /**
     * 统计指定产品的销售金额
     */
    @Query("SELECT SUM(oi.subtotal) FROM OrderItem oi WHERE oi.productSku = :productSku")
    java.math.BigDecimal sumSubtotalByProductSku(@Param("productSku") String productSku);
}



