<?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.hzs.activity.consume.mapper.AcConsumeRuleConfigMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="AcConsumeRuleConfigExt" type="com.hzs.common.domain.activity.consume.ext.AcConsumeRuleConfigExt">
        <id column="PK_ID" property="pkId"/>
        <result column="DEL_FLAG" property="delFlag"/>
        <result column="CREATION_TIME" property="creationTime"/>
        <result column="PK_CREATOR" property="pkCreator"/>
        <result column="MODIFIED_TIME" property="modifiedTime"/>
        <result column="PK_MODIFIED" property="pkModified"/>
        <result column="PK_COUNTRY" property="pkCountry"/>
        <result column="PK_BASE_ID" property="pkBaseId"/>
        <result column="ORDER_TYPE" property="orderType"/>
        <result column="ORDER_AMOUNT" property="orderAmount"/>
        <result column="ORDER_ACHIEVE" property="orderAchieve"/>
        <collection property="giftConfigExtList" ofType="com.hzs.common.domain.activity.base.ext.AcGiftConfigExt">
            <result column="PK_COUNTRY" property="pkCountry"/>
            <result column="PK_RULE_ID" property="pkRuleId"/>
            <result column="ACT_TYPE" property="actType"/>
            <result column="PK_PRODUCT" property="pkProduct"/>
            <result column="SPECS_NAME" property="specsName"/>
            <result column="SPECS_NAME_ID" property="specsNameId"/>
            <result column="QUANTITY" property="quantity"/>
            <result column="PRICE" property="price"/>
            <result column="PK_CONFIG_ID" property="pkId"/>
            <result column="SHIPPING_CHANNEL" property="shippingChannel"/>
            <result column="PK_SUPPLIER" property="pkSupplier"/>
            <result column="PRODUCT_CODE" property="productCode"/>
        </collection>
    </resultMap>

    <select id="queryConsumeGiftConfigByCondition" resultMap="AcConsumeRuleConfigExt">
        select
        ba.pk_id, aw.pk_id pk_config_id,
        aw.order_amount, aw.order_achieve,
        ac.pk_product, ac.specs_name, ac.specs_name_id, ac.quantity,
        bp.pk_supplier,bp.shipping_channel,bp.product_code
        from ac_base_config ba
        inner join ac_consume_rule_config aw
        on ba.pk_id = aw.pk_base_id
        inner join ac_gift_config ac
        on aw.pk_id = ac.pk_rule_id
        left join bd_product bp
        on bp.pk_id =ac.pk_product
        where ba.del_flag = 0 and aw.del_flag = 0
        and ac.del_flag = 0
        <if test="pkCountry != null">
            and ba.pk_country = #{pkCountry}
        </if>
        <if test="actType != null">
            and ba.act_type= #{actType}
        </if>
        <if test="orderType != null">
            and aw.order_type = #{orderType}
        </if>
        <if test="startDate != null">
            and ba.act_start_date &lt;= #{startDate}
        </if>
        <if test="endDate != null">
            and ba.act_end_date >= #{endDate}
        </if>
        <if test="orderAmount != null">
            and aw.order_amount &lt;= #{orderAmount}
        </if>
        <if test="orderAchieve != null">
            and aw.order_achieve &lt;= #{orderAchieve}
        </if>
        <choose>
            <when test="orderAmount != null and orderAmount >0 and orderAchieve != null and orderAchieve >0">
                order by aw.order_amount desc,aw.order_achieve desc
            </when>
            <when test="orderAchieve != null and orderAchieve >0">
                order by aw.order_achieve desc
            </when>
            <otherwise>
                order by aw.order_amount desc
            </otherwise>
        </choose>

    </select>

</mapper>
