<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ylx.massage.mapper.ProductOrderItemMapper">

    <select id="getOrderItemVoByOrderId" resultType="com.ylx.massage.domain.vo.OrderItemVo">
        SELECT
            poi.order_no,
            poi.sku_image,
            ps.spec_combo AS specName,
            poi.product_name,
            poi.product_id,
            ps.price_money AS payAmount,
            ps.price_point AS pointsUsed,
            ps.origin_price,
            p.payment_type AS paymentType
        FROM
            product_order_item poi
        LEFT JOIN product p ON poi.product_id = p.id
        LEFT JOIN product_sku ps ON poi.sku_id = ps.id
        WHERE
            poi.order_id = #{orderId}
    </select>

    <!-- 查询近30天各商品销量统计（仅统计已支付订单） -->
    <select id="selectLast30DaysSalesStats" resultType="com.ylx.massage.domain.vo.ProductSalesStats">
        SELECT
            poi.product_id AS productId,
            SUM(poi.quantity) AS last30DaysSales
        FROM product_order_item poi
        INNER JOIN product_order_info poi2 ON poi.order_id = poi2.id
        WHERE poi2.pay_status = 1
          AND poi2.is_deleted = 0
          AND poi2.create_time >= DATE_SUB(NOW(), INTERVAL 30 DAY)
        GROUP BY poi.product_id
    </select>
</mapper>