<?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.ProductMapper">

    <!-- 分页查询商品列表(H5端)-支持排序 -->
    <select id="selectH5Page" resultType="com.ylx.massage.domain.vo.H5ProductVo">
        SELECT id, category_id, product_no,name,price_point, price_money, stock, sales,product_main_image,payment_type,sale_start_time
        FROM product
        WHERE deleted = 0 AND status = 1
        <if test="product.categoryId != null">
            AND category_id = #{product.categoryId}
        </if>
        <if test="name != null and name!='' ">
            and name like concat('%',#{name},'%')
        </if>
        <choose>
            <when test="sortField != null and sortField != '' and sortOrder != null and sortOrder != ''">
                ORDER BY
                <if test="sortField == 'price'">
                    price_money ${sortOrder == 'asc' ? 'ASC' : 'DESC'}
                </if>
                <if test="sortField == 'sales'">
                    sales ${sortOrder == 'asc' ? 'ASC' : 'DESC'}
                </if>
                <if test="sortField == 'comprehensive'">
                    /* 综合排序：按销量降序（实际综合得分在Java层计算） */
                    sales DESC
                </if>
            </when>
            <otherwise>
                ORDER BY sales DESC
            </otherwise>
        </choose>
    </select>

    <!-- 查询平台商品总销量（用于综合排序计算） -->
    <select id="selectPlatformStats" resultType="com.ylx.massage.domain.vo.PlatformProductStats">
        SELECT
            COALESCE(SUM(sales), 0) as totalSales
        FROM product
        WHERE deleted = 0 AND status = 1
    </select>

</mapper>