<?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="org.jeecg.modules.oroqen.mapper.OroqenProductMapper">

    <!-- 根据分类ID查询产品列表 -->
    <select id="selectByCategoryId" resultType="org.jeecg.modules.oroqen.entity.OroqenProduct">
        SELECT * FROM oroqen_product 
        WHERE category_id = #{categoryId} 
        AND status = 1 
        ORDER BY sort_order ASC, create_time DESC
    </select>

    <!-- 查询推荐产品 -->
    <select id="selectFeaturedProducts" resultType="org.jeecg.modules.oroqen.entity.OroqenProduct">
        SELECT * FROM oroqen_product 
        WHERE is_featured = 1 
        AND status = 1 
        ORDER BY sort_order ASC, create_time DESC
        <if test="limit != null">
            LIMIT #{limit}
        </if>
    </select>

    <!-- 查询热销产品 -->
    <select id="selectHotProducts" resultType="org.jeecg.modules.oroqen.entity.OroqenProduct">
        SELECT * FROM oroqen_product 
        WHERE is_hot = 1 
        AND status = 1 
        ORDER BY sales_count DESC, create_time DESC
        <if test="limit != null">
            LIMIT #{limit}
        </if>
    </select>

    <!-- 更新销量 -->
    <update id="updateSalesCount">
        UPDATE oroqen_product 
        SET sales_count = IFNULL(sales_count, 0) + #{quantity}
        WHERE id = #{productId}
    </update>

    <!-- 更新库存 -->
    <update id="updateStock">
        UPDATE oroqen_product 
        SET stock = IFNULL(stock, 0) + #{quantity}
        WHERE id = #{productId}
    </update>

</mapper>