<?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.ruoyi.system.mapper.PromotionActivityRuleMapper">

    <resultMap type="PromotionActivityRule" id="PromotionActivityRuleResult">
        <result property="id"    column="id"    />
        <result property="activityId"    column="activity_id"    />
        <result property="productId"    column="product_id"    />
        <result property="threshold"    column="threshold"    />
        <result property="reduceAmount"    column="reduce_amount"    />
        <result property="discountRate"    column="discount_rate"    />
        <result property="minQuantity"    column="min_quantity"    />
        <result property="productName"    column="product_name"    />
        <result property="productImage"    column="product_image"    />
        <result property="productPrice"    column="product_price"    />
    </resultMap>

    <select id="selectRulesByActivityId" parameterType="Long" resultMap="PromotionActivityRuleResult">
        SELECT
            r.*,
            f.name AS product_name,
            f.image AS product_image,
            f.price AS product_price
        FROM promotion_activity_rule r
        LEFT JOIN pos_food f ON r.product_id = f.id
        WHERE r.activity_id = #{activityId}
        ORDER BY r.id ASC
    </select>

    <select id="selectActiveRulesByStoreId" parameterType="Long" resultMap="PromotionActivityRuleResult">
        SELECT r.*, f.name AS product_name, f.image AS product_image, f.price AS product_price
        FROM promotion_activity_rule r
        INNER JOIN promotion_activity a ON r.activity_id = a.id AND a.status = 1
        LEFT JOIN pos_food f ON r.product_id = f.id
        WHERE a.store_id = #{storeId}
        ORDER BY a.type ASC, r.id ASC
    </select>

</mapper>
