<?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.wares.mapper.AcWaresRuleConfigMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="AcWaresRuleConfigExt" type="com.hzs.common.domain.activity.wares.ext.AcWaresRuleConfigExt">
        <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="PK_WARES" property="pkWares"/>
        <result column="QUANTITY" property="quantity"/>
        <result column="IS_FREE_MAIL" property="isFreeMail"/>
        <result column="PK_LAND_POSTAGE" property="pkLandPostage"/>
        <result column="PK_AIR_POSTAGE" property="pkAirPostage"/>
        <result column="PK_OCEAN_POSTAGE" property="pkOceanPostage"/>
        <result column="PK_WARES" property="pkWares"/>
        <result column="QUANTITY" property="quantity"/>
        <result column="SHIPPING_CHANNEL" property="shippingChannel"/>
        <result column="PK_SUPPLIER" property="pkSupplier"/>
        <result column="PRE_SALE_STATUS" property="preSaleStatus"/>
        <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="PRODUCT_QUANTITY" property="quantity"/>
            <result column="PRODUCT_CODE" property="productCode"/>
            <result column="PRICE" property="price"/>
        </collection>
    </resultMap>

    <select id="queryWaresGiftConfigByCondition" resultMap="AcWaresRuleConfigExt">
        select is_free_mail,pk_ocean_postage,pk_air_postage,
        pk_land_postage,pk_wares,quantity,pk_product,specs_name,
        specs_name_id,product_quantity,pk_supplier,shipping_channel, product_code,pre_sale_status from(
        <foreach collection="giftConfigParam.configParamDetailList" item="item" index="index" separator=" union ">
            select bw.is_free_mail,be.pk_ocean_postage,be.pk_air_postage,be.pre_sale_status,
            Pk_Land_Postage,aw.pk_wares,aw.quantity,ac.pk_product,ac.specs_name,
            ac.specs_name_id,ac.quantity product_quantity,
            bp.pk_supplier,bp.shipping_channel,bp.product_code from ac_base_config ba
            inner join ac_wares_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_wares bw
            on bw.pk_id =aw.pk_wares
            left join bd_wares_extend be
            on be.pk_wares = bw.pk_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 and bw.del_flag=0
            <if test="giftConfigParam.pkCountry != null">
                and ac.pk_country=#{giftConfigParam.pkCountry}
            </if>
            <if test="giftConfigParam.actType != null">
                and ba.act_type= #{giftConfigParam.actType}
            </if>
            <if test="giftConfigParam.orderType != null">
                and aw.order_type = #{giftConfigParam.orderType}
            </if>
            <if test="giftConfigParam.startDate != null">
                and ba.act_start_date &lt;= #{giftConfigParam.startDate}
            </if>
            <if test="giftConfigParam.endDate != null">
                and ba.act_end_date >= #{giftConfigParam.endDate}
            </if>
            <if test="item.pkWares != null">
                and aw.pk_wares = #{item.pkWares}
            </if>
            <if test="item.quantity != null">
                and aw.quantity &lt;= #{item.quantity}
            </if>
        </foreach>
        )
        order by pk_wares,quantity desc
    </select>


</mapper>
