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

    <!-- 通用查询映射结果 -->
    <resultMap id="BdWaresRange" type="com.hzs.common.domain.sale.wares.BdWaresRange">
        <id column="PK_ID" property="pkId"/>
        <result column="PK_CREATOR" property="pkCreator"/>
        <result column="CREATION_TIME" property="creationTime"/>
        <result column="PK_MODIFIED" property="pkModified"/>
        <result column="MODIFIED_TIME" property="modifiedTime"/>
        <result column="DEL_FLAG" property="delFlag"/>
        <result column="PK_COUNTRY" property="pkCountry"/>
        <result column="PK_WARES" property="pkWares"/>
        <result column="PK_PRODUCT" property="pkProduct"/>
        <result column="PK_WARES_DETAIL" property="pkWaresDetail"/>
        <result column="PK_WARES_SPECS_SKU" property="pkWaresSpecsSku"/>
        <result column="PK_RANGE" property="pkRange"/>
        <result column="PRICE" property="price"/>
        <result column="ACHIEVE" property="achieve"/>
        <result column="ASS_ACHIEVE" property="assAchieve"/>
    </resultMap>

    <select id="queryWaresRangeByCondition" resultMap="BdWaresRange">
        select bd.pk_wares,(bs.price*bd.quantity) price,(bs.achieve*bd.quantity) achieve,(bs.ass_achieve*bd.quantity) ass_achieve from bd_wares_detail bd
        inner join (
        select bs.pk_wares_detail,min(bs.price) price,max(bs.achieve) achieve,max(bs.ASS_ACHIEVE) ass_achieve from bd_wares_range bs
        left join bd_wares_specs_sku bss on bss.pk_id = bs.PK_WARES_SPECS_SKU
        where
        1=1 and bss.IS_PUT_ON = 0
        <if test="pkRange != null">
            and bs.pk_range = #{pkRange}
        </if>
        and bs.pk_wares in
        <foreach collection="pkWaresList" item="pkWares" open="(" separator="," close=")">
            #{pkWares}
        </foreach>
        group by bs.pk_wares_detail
        ) bs
        on bd.pk_id = bs.pk_wares_detail
        where bd.del_flag=0
        and bd.pk_wares in
        <foreach collection="pkWaresList" item="pkWares" open="(" separator="," close=")">
            #{pkWares}
        </foreach>
    </select>

    <select id="queryWaresList" resultType="com.hzs.common.domain.sale.wares.BdWaresRange">

        SELECT wr.PK_ID              pkId,
               wr.PK_WARES           pkWares,
               wr.PK_PRODUCT         pkProduct,
               wr.PK_WARES_DETAIL    pkWaresDetail,
               br.pk_id              pkRange,
               wr.PK_WARES_SPECS_SKU pkWaresSpecsSku,
               wr.PRICE              price,
               wr.ACHIEVE            achieve,
               wr.PK_COUNTRY         pkCountry,
               wr.ASS_ACHIEVE        assAchieve,
               wd.IS_GIFT            isGift
        FROM bd_range br
                 LEFT JOIN bd_wares_range wr ON wr.PK_RANGE = br.PK_ID
                 left join BD_WARES_DETAIL wd on wd.PK_ID = wr.PK_WARES_DETAIL

        WHERE br.PK_COUNTRY = #{pkCountry}
          AND br.DEL_FLAG = 0
          AND wr.PK_WARES = #{pkWares}
          AND wr.del_flag = 0
    </select>

</mapper>
