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

    <!-- 通用查询映射结果 -->
    <resultMap id="SkuResultMap" type="com.hzs.common.domain.sale.ext.BdWaresSpecsSkuExt">
        <id column="PK_ID" property="pkId"/>
        <result column="PK_WARES" property="pkWares"/>
        <result column="PRICE" property="price"/>
        <result column="ACHIEVE" property="achieve"/>
        <result column="ASS_ACHIEVE" property="assAchieve"/>
        <result column="SPEC_COVER" property="specCover"/>
        <result column="PK_WARES_DETAIL" property="pkWaresDetail"/>
        <result column="WARES_CODE" property="waresCode"/>
        <result column="QUANTITY" property="quantity"/>
        <result column="IS_GIFT" property="isGift"/>
        <result column="PK_PRODUCT" property="pkProduct"/>
        <result column="PK_WARES_SPECS" property="pkWaresSpecs"/>
        <result column="PK_SPECS_TYPE" property="pkSpecsType"/>
        <result column="SPECS_TYPE_NAME" property="specsTypeName"/>
        <result column="PK_SPECS" property="pkSpecs"/>
        <result column="SPECS_NAME" property="specsName"/>
        <result column="WARES_NAME" property="waresName"/>
        <result column="COVER1" property="cover1"/>
        <result column="COVER2" property="cover2"/>
        <result column="COVER3" property="cover3"/>
        <result column="COVER4" property="cover4"/>
        <result column="COVER5" property="cover5"/>
        <result column="COVER6" property="cover6"/>
        <result column="COVER" property="cover"/>
        <result column="VIDEO_URL" property="videoUrl"/>
        <result column="DETAILS" property="details"/>
        <result column="SELLING_POINTS" property="sellingPoints"/>
        <!--        <result column="giftNames" property="giftNames" />-->
        <result column="AFTER_GUARANTEE" property="afterGuarantee"/>
        <result column="SALES" property="sales"/>
        <result column="PRE_SALE_STATUS" property="preSaleStatus"/>
        <result column="ARRIVAL_TIME" property="arrivalTime"/>
        <result column="CREATION_TIME" property="creationTime"/>
        <result column="PRODUCT_NAME" property="productName"/>
        <result column="WARES_PRICE" property="waresPrice"/>
        <result column="SPECIAL_AREA" property="specialArea"/>
        <result column="IS_SINGLE" property="isSingle"/>
        <result column="IS_PUT_ON" property="isPutOn"/>
        <result column="IS_MAKER_GIFT" property="isMakerGift"/>
        <result column="ASS_ACHIEVE" property="assAchieve"/>
        <result column="VIP_PRICE" property="vipPrice"/>
        <result column="RETAIL_PRICE" property="retailPrice"/>
        <result column="PK_SPECIAL_CURRENCY" property="pkSpecialCurrency"/>
    </resultMap>

    <!-- 通用查询映射结果 -->
    <resultMap id="WaresItemVo" type="com.hzs.sale.shopping.vo.WaresItemVo">
        <result column="WARES_CODE" property="waresCode"/>
        <result column="COVER1" property="cover1"/>
        <result column="WARES_NAME" property="waresName"/>
        <result column="IS_MAKER_GIFT" property="isMakerGift"/>
        <collection property="waresItemsParamList" ofType="com.hzs.sale.shopping.vo.WaresItemWaresInfo">
            <result column="PK_WARES_DETAIL" property="pkWaresDetail"/>
            <result column="PK_WARES_SPECS_SKU" property="pkWaresSpecsSku"/>
            <result column="PRICE" property="price"/>
            <result column="ACHIEVE" property="achieve"/>
            <result column="ASS_ACHIEVE" property="assAchieve"/>
            <result column="PRODUCT_NAME" property="productName"/>
            <result column="SPEC_COVER" property="specCover"/>
            <result column="QUANTITY" property="quantity"/>
        </collection>
    </resultMap>

    <select id="queryWaresSpecsSku" resultMap="SkuResultMap">
        select bk.pk_wares,
        bk.pk_wares_detail,
        bk.pk_id,
        bw.wares_code,
        bd.quantity,
        bw.WARES_NAME,
        bw.DETAILS,
        be.SELLING_POINTS,
        bw.VIDEO_URL,
        bw.COVER,
        be.SALES,
        be.PRE_SALE_STATUS,
        bd.is_gift,
        bd.pk_product,
        bk.price,
        bk.achieve,
        bk.spec_cover,
        bw.COVER1,
        bw.COVER2,
        bw.COVER3,
        bw.COVER4,
        bw.COVER5,
        bw.COVER6,
        bw.COVER,
        bw.VIDEO_URL,
        bw.AFTER_GUARANTEE,
        bk.ASS_ACHIEVE
        , be.ARRIVAL_TIME
        , be.CREATION_TIME
        , be.PRE_SALE_STATUS
        , be.is_maker_gift
        , bk.vip_price,bk.retail_price,be.PK_SPECIAL_CURRENCY
        from bd_wares bw
        inner join bd_wares_extend be
        on bw.pk_id = be.pk_wares
        inner join bd_wares_detail bd
        on bw.pk_id = bd.pk_wares
        inner join bd_wares_specs_sku bk
        on bd.pk_id = bk.pk_wares_detail
        where bw.del_flag = 0
        and be.del_flag = 0
        and bd.del_flag = 0
        and be.is_put_on = 0
        and bk.IS_PUT_ON = 0
        and bk.del_flag = 0
        and bw.wares_code = #{waresCode}
        and bw.special_area = #{specialArea}
        <!-- 商品必须通过审核后才能显示 -->
        and bw.wares_status = 5
        order by bk.price, bk.PK_ID
    </select>

    <select id="queryWaresSpecsNameSku" resultMap="SkuResultMap">
        select bk.pk_id,sp.specs_name,wd.IS_GIFT from bd_wares_specs_sku bk
        inner join BD_WARES_DETAIL wd on wd.PK_ID = bk.PK_WARES_DETAIL
        inner join bd_wares_specs_relation br
        on bk.pk_id=br.pk_wares_specs_sku
        inner join bd_wares_specs bs
        on bs.pk_id=br.pk_wares_specs
        inner join bd_specs_type st
        on st.pk_id = bs.pk_specs_type
        inner join bd_specs sp
        on sp.pk_id = bs.pk_specs
        where bk.pk_id in
        <foreach collection="pkSkuIdList" item="pkSku" open="(" separator="," close=")">
            #{pkSku}
        </foreach>
        and br.del_flag = 0
        and bk.del_flag = 0
        and wd.DEL_FLAG=0
        and bs.DEL_FLAG=0
        and st.DEL_FLAG=0
        and sp.DEL_FLAG=0
        order by bs.PK_SPECS_TYPE
    </select>

    <select id="queryWaresDetailSkuBySpecs" resultType="com.hzs.common.domain.sale.ext.BdWaresSpecsSkuExt">
        select bw.wares_code,bs.pk_id,(bd.quantity * bs.price) waresPrice,
        (bd.quantity * bs.achieve) waresAchieve
        from bd_wares bw
        inner join bd_wares_extend be
        on bw.pk_id = be.pk_wares
        inner join bd_wares_detail bd
        on bw.pk_id = bd.pk_wares
        inner join (select bs.pk_wares_detail, bk.pk_id, bk.price, bk.achieve
        from bd_wares_specs bs
        inner join bd_wares_specs_relation br
        on bs.pk_id = br.pk_wares_specs
        inner join bd_wares_specs_sku bk
        on br.pk_wares_specs_sku = bk.pk_id
        where
        <foreach collection="cuWaresItemsParams.typeParamsSet" item="typeParams" open="(" separator=" or " close=")">
            pk_specs = #{typeParams.pkSpecs} and pk_specs_type = #{typeParams.pkSpecsType}
        </foreach>
        having count(pk_wares_specs_sku) = ${cuWaresItemsParams.typeParamsSet.size}
        group by bs.pk_wares_detail,bk.pk_id,price,achieve) bs
        on bd.pk_id = bs.pk_wares_detail
        where bw.del_flag = 0
        and be.del_flag = 0
        and bd.del_flag = 0
        and be.is_put_on = 0
        and bd.is_gift = 1
        and bw.wares_code = #{cuWaresItemsParams.waresCode}
        and bd.pk_id = #{cuWaresItemsParams.pkWaresDetail}
    </select>
    <update id="updateBySkuId">
        update bd_wares_specs_sku
        set PRICE           = #{price},
            SPEC_COVER      = #{specCover},
            IS_PUT_ON       = #{isPutOn},
            DEL_FLAG        = #{delFlag},
            ACHIEVE         = #{achieve},
            ASS_ACHIEVE     = #{assAchieve},
            PK_WARES_DETAIL = #{pkWaresDetail},
            RETAIL_PRICE    = #{retailPrice},
            VIP_PRICE       = #{vipPrice}
        where PK_ID = #{pkId}
    </update>


    <select id="selectByWaresSpecsSkuIdAndDel" resultMap="SkuResultMap">
        select *
        from bd_wares_specs_sku
        where PK_ID = #{pkWaresSpecsSku}
    </select>


    <select id="selectByWaresSpecsSku" resultMap="SkuResultMap">
        select bss.*,wd.PK_PRODUCT,wd.QUANTITY from bd_wares_specs_sku bss
        left join BD_WARES_DETAIL wd on wd.PK_ID = bss.PK_WARES_DETAIL
        and wd.del_flag = 0
        where bss.PK_WARES in
        <foreach collection="waresIds" item="pkWares" open="(" separator="," close=")">
            #{pkWares}
        </foreach>
        and bss.DEL_FLAG = 0 and bss.IS_PUT_ON = 0 order by bss.pk_id asc
    </select>

    <select id="selectByPkIds" resultType="com.hzs.sale.shopping.vo.WaresItemWaresInfo">
        select wss.SPEC_COVER specCover,bw.WARES_NAME waresName,wd.QUANTITY quantity,bp.PRODUCT_NAME productName ,wd.PK_ID pkWaresDetail,wss.PK_ID pkWaresSpecsSku,
        ( select listagg(bs.SPECS_NAME,',') WITHIN GROUP(order by bs.SPECS_NAME) AS skuInfo 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 wss.pk_id = wsr.PK_WARES_SPECS_SKU and wsr.del_flag = 0) skuInfo,
        wss.PRICE price,wss.ACHIEVE achieve,bw.SPECIAL_AREA specialArea,bw.PK_ID pkWares,wss.ASS_ACHIEVE assAchieve
        from
        bd_wares_specs_sku wss
        left join BD_WARES bw on bw.PK_ID = wss.PK_WARES
        left join BD_WARES_DETAIL wd on wd.PK_ID = wss.PK_WARES_DETAIL
        left join BD_PRODUCT bp on bp.PK_ID = wd.PK_PRODUCT
        where wss.pk_id in
        <foreach item="item" collection="pkWaresSpecsSku" separator="," open="(" close=")" index="">
            #{item}
        </foreach>
    </select>

    <select id="selectByMinWaresSpecsSku" resultMap="SkuResultMap">
        select bss.*, wd.PK_PRODUCT, wd.QUANTITY, bp.PRODUCT_NAME, wd.IS_GIFT
        from bd_wares_specs_sku bss
                 left join BD_WARES_DETAIL wd on wd.PK_ID = bss.PK_WARES_DETAIL
                 left join BD_PRODUCT bp on bp.PK_ID = wd.PK_PRODUCT
        where bss.PK_WARES = #{waresId}
          and bss.DEL_FLAG = 0
        order by bss.PRICE
    </select>

    <select id="selectByMinWaresSpecsSkuByPkWaresList" resultMap="SkuResultMap">
        select bss.*,wd.PK_PRODUCT,wd.QUANTITY,bp.PRODUCT_NAME,wd.IS_GIFT from bd_wares_specs_sku bss
        left join BD_WARES_DETAIL wd on wd.PK_ID = bss.PK_WARES_DETAIL
        left join BD_PRODUCT bp on bp.PK_ID = wd.PK_PRODUCT
        where bss.PK_WARES in
        <foreach collection="pkWaresIds" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        and bss.DEL_FLAG = 0
        and bss.is_put_on = 0
        order by bss.PRICE
    </select>

    <select id="selectByPkWaresSpecsSku" resultMap="SkuResultMap">
        select bss.*,
               wd.PK_PRODUCT,
               bp.PRODUCT_NAME,
               wd.QUANTITY bw.COVER1,bw.WARES_NAME,
               bw.WARES_CODE,
               bw.WARES_PRICE,
               bw.SPECIAL_AREA,
               bw.IS_SINGLE
        from bd_wares_specs_sku bss
                 left join BD_WARES_DETAIL wd on wd.PK_ID = bss.PK_WARES_DETAIL
                 left join BD_PRODUCT bp on bp.PK_ID = wd.PK_PRODUCT
                 left join BD_WARES bw on bw.PK_ID = bss.PK_WARES
        where bss.PK_ID = #{pkWaresSpecsSku}
          and bss.DEL_FLAG = 0
    </select>


    <select id="queryWaresSpecsRangeSku" resultMap="SkuResultMap">
        select bk.pk_wares,
        bk.pk_wares_detail,
        bk.pk_id,
        bw.wares_code,
        bd.quantity,
        bw.WARES_NAME,
        bw.DETAILS,
        be.SELLING_POINTS,
        bw.VIDEO_URL,
        bw.COVER,
        be.SALES,
        be.PRE_SALE_STATUS,
        bd.is_gift,
        bd.pk_product,
        bs.price,
        bs.achieve,
        bk.spec_cover,
        bw.COVER1,
        bw.COVER2,
        bw.COVER3,
        bw.COVER4,
        bw.COVER5,
        bw.COVER6,
        bw.COVER,
        bw.VIDEO_URL,
        bw.AFTER_GUARANTEE,
        bk.ASS_ACHIEVE
        , be.ARRIVAL_TIME
        , be.CREATION_TIME
        , be.PRE_SALE_STATUS
        , be.is_maker_gift
        , be.PK_SPECIAL_CURRENCY
        from bd_wares bw
        inner join bd_wares_extend be
        on bw.pk_id = be.pk_wares
        inner join bd_wares_detail bd
        on bw.pk_id = bd.pk_wares
        inner join bd_wares_specs_sku bk
        on bd.pk_id = bk.pk_wares_detail
        inner join (
        select t.pk_wares_specs_sku, t.price, t.achieve from BD_WARES_RANGE t
        left join BD_WARES w on w.pk_id = t.pk_wares
        where t.del_flag = 0 and w.del_flag = 0 and t.pk_range = #{pkRange} and w.wares_code = #{waresCode}
        ) bs on bs.pk_wares_specs_sku = bk.pk_id
        where bw.del_flag = 0
        and be.del_flag = 0
        and bd.del_flag = 0
        and be.is_put_on = 0
        and bk.IS_PUT_ON = 0
        and bk.DEL_FLAG=0
        and bw.wares_code = #{waresCode}
        and bw.special_area = #{specialArea}
        <!-- 商品必须通过审核后才能显示 -->
        and bw.wares_status = 5
        order by bs.price, bk.PK_ID
    </select>

    <select id="selectWaresItemsVO" resultMap="WaresItemVo">
        <foreach collection="productGroup" item="item" index="index" separator=" union ">
            select bw.wares_code,bw.cover1,bw.WARES_NAME,be.is_maker_gift,
            bs.price price,bs.achieve achieve,bs.ass_achieve ass_achieve,
            bd.pk_id pk_wares_detail,bs.pk_id pk_wares_specs_sku,bp.product_name,
            bs.spec_cover,#{item.quantity} quantity from BD_WARES bw
            inner join bd_wares_extend be
            on bw.pk_id = be.pk_wares
            inner join bd_wares_detail bd
            on bw.pk_id = bd.pk_wares
            inner join bd_wares_specs_sku bs
            on bd.pk_id = bs.pk_wares_detail
            left join bd_product bp
            on bd.pk_product = bp.pk_id
            where bw.del_flag=0 and bd.del_flag=0 and bs.del_flag=0
            and bw.WARES_CODE = #{waresCode}
            and bw.special_area= #{specialArea}
            and bs.pk_id = #{item.pkSkuId}
        </foreach>
    </select>

</mapper>
