<?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 pk_wares_detail,min(price) price,min(achieve) achieve,min(ASS_ACHIEVE) ass_achieve from bd_wares_range bs
            where
            1=1
            <if test="pkRange != null">
                and pk_range = #{pkRange}
            </if>
            and pk_wares in
            <foreach collection="pkWaresList" item="pkWares" open="(" separator="," close=")">
                #{pkWares}
            </foreach>
            group by 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
        FROM bd_range br
                 LEFT JOIN bd_wares_range wr ON wr.PK_RANGE = br.PK_ID
            AND wr.PK_WARES = #{pkWares}
            AND wr.del_flag = 0
        WHERE br.PK_COUNTRY = #{pkCountry}
          AND br.DEL_FLAG = 0
    </select>

</mapper>
