<?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.BdWaresSpecsRelationMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.hzs.common.domain.sale.wares.BdWaresSpecsRelation">
        <id column="PK_ID" property="pkId"/>
        <result column="PK_WARES_SPECS" property="pkWaresSpecs"/>
        <result column="PK_WARES_SPECS_SKU" property="pkWaresSpecsSku"/>
        <result column="PK_WARES_DETAIL" property="pkWaresDetail"/>
    </resultMap>

    <select id="selectByWaresSpecsSku" resultType="com.hzs.common.domain.sale.ext.BdWaresSpecsRelationExt">
        select wsr.*, bs.SPECS_NAME specsName, ws.PK_SPECS pkSpecs
        from BD_WARES_SPECS_RELATION wsr
                 left join BD_WARES_SPECS ws on ws.PK_ID = wsr.PK_WARES_SPECS
                 left join BD_SPECS bs on bs.PK_ID = ws.PK_SPECS
        where wsr.PK_WARES_SPECS_SKU = #{pkWaresSpecsSku}
        order by bs.pk_id
    </select>

    <select id="getWaresProduct" resultType="com.hzs.common.domain.sale.ext.BdWaresSpecsRelationExt">
        select ws.PK_SPECS pkSpecs,
        wd.PK_PRODUCT pkProduct,wr.PRICE price,
        wr.ACHIEVE achieve,wr.PK_RANGE pkRange,
        r.RANGE_NAME rangeName
        from BD_WARES_SPECS_RELATION wsr
        left join BD_WARES_SPECS ws on ws.PK_WARES_DETAIL = wsr.PK_WARES_DETAIL
        left join bd_wares_detail wd on wd.PK_ID = ws.PK_WARES_DETAIL
        left join BD_WARES_SPECS_SKU wss on wss.PK_ID = wsr.PK_WARES_SPECS_SKU
        left join BD_WARES_RANGE wr on wr.PK_WARES_SPECS_SKU = wss.PK_ID
        left join bd_range r on r.PK_ID = wr.PK_RANGE
        where wd.PK_WARES = #{pkWares} and wd.PK_PRODUCT = #{pkProduct} and wsr.PK_WARES_SPECS_SKU = #{pkWaresSpecsSku} and wsr.del_flag = 0 and
        wd.del_flag = 0 and ws.del_flag = 0
        and wss.del_flag = 0
        and wr.del_flag = 0
        and ws.PK_SPECS in
        <foreach collection="waresSpecsIds" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        group by wr.PK_RANGE,ws.PK_SPECS ,wd.PK_PRODUCT ,wr.PRICE ,wr.ACHIEVE,r.RANGE_NAME,r.range_value
        order by r.range_value
    </select>

</mapper>
