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

    <!-- 通用查询映射结果 -->
    <resultMap id="BdWaresDetailExt" type="com.hzs.common.domain.sale.ext.BdWaresDetailExt">
        <result column="PK_ID" property="pkId"/>
        <result column="PK_WARES_SPECS_SKU" property="pkWaresSpecsSku"/>
        <result column="WARES_CODE" property="waresCode"/>
        <result column="WARES_NAME" property="waresName"/>
        <result column="IS_FREE_MAIL" property="isFreeMail"/>
        <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="QUANTITY" property="quantity"/>
        <result column="IS_GIFT" property="isGift"/>
        <result column="SPECS_NAME" property="specsName"/>
        <result column="SPECS_NAME_ID" property="specsNameId"/>
        <result column="PRICE" property="price"/>
        <result column="ACHIEVE" property="achieve"/>
        <result column="ASS_ACHIEVE" property="assAchieve"/>
        <result column="PK_AIR_POSTAGE" property="pkAirPostage"/>
        <result column="PK_OCEAN_POSTAGE" property="pkOceanPostage"/>
        <result column="PK_OCEAN_POSTAGE" property="pkOceanPostage"/>
        <result column="WARES_QUANTITY" property="waresQuantity"/>
        <result column="IS_MAKER_GIFT" property="isMakerGift"/>
        <result column="PK_SPECIAL_CURRENCY" property="pkSpecialCurrency"/>
        <result column="SHIPPING_CHANNEL" property="shippingChannel"/>
        <result column="PK_SUPPLIER" property="pkSupplier"/>
        <result column="PRODUCT_CODE" property="productCode"/>
        <result column="PRE_SALE_STATUS" property="preSaleStatus"/>
        <result column="PK_PRODUCT_BAS" property="pkProductBas"/>
        <result column="PK_UNIT" property="pkUnit"/>
        <result column="AREA_INCOME" property="areaIncome"/>
        <result column="BOX_NUM" property="boxNum"/>
        <collection property="waresSpecsList" ofType="com.hzs.common.domain.sale.ext.WaresSpecsExt">
            <result column="PK_SPECS_TYPE" property="pkSpecsType"/>
            <result column="PK_SPECS" property="pkSpecs"/>
            <result column="B_SPECS_NAME" property="specsName"/>
        </collection>
    </resultMap>

    <select id="queryWaresDetailByCondition" resultMap="BdWaresDetailExt">
        <foreach collection="orderItemsParams" item="item" index="index" separator="union">
            select bd.pk_id,bs.pk_id pk_wares_specs_sku,bw.is_free_mail,be.pk_land_postage,
            be.pk_air_postage,be.pk_ocean_postage,bw.pk_id pk_wares,be.is_maker_gift,be.pre_sale_status,
            be.pk_special_currency,bw.wares_code, bw.wares_name,
            bd.is_gift is_gift,
            bd.pk_product,bs.price,bs.achieve,bp.pk_supplier,bp.shipping_channel,bp.product_code,
            bs.ass_achieve,b.specs_name,b.specs_name_id,
            bw.area_income, bw.box_num
            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
            left join bd_wares_specs_sku bs
            on bd.pk_id = bs.pk_wares_detail and bs.del_flag=0
            left join bd_product bp
            on bd.pk_product=bp.pk_id and bp.del_flag=0
            left join (
            select pk_wares_specs_sku,
            to_char(wm_concat(distinct(sc.specs_name))) specs_name,
            to_char(wm_concat(distinct(sc.pk_id))) specs_name_id
            from bd_wares_specs_relation br
            left join bd_wares_specs bp
            on br.pk_wares_specs= bp.pk_id
            left join bd_specs sc
            on bp.pk_specs = sc.pk_id
            <where>
                <if test="item.waresItemsParamList != null and item.waresItemsParamList.size >0">
                    pk_wares_specs_sku in
                    <foreach collection="item.waresItemsParamList" item="param" open="(" separator="," close=")">
                        #{param.pkWaresSpecsSku}
                    </foreach>
                </if>
            </where>
            group by pk_wares_specs_sku) b
            on bs.pk_id = b.pk_wares_specs_sku
            where bw.del_flag = 0 and be.del_flag = 0
            <if test="systemType != null">
                and bw.system_type = #{systemType}
            </if>
            and bd.del_flag = 0 and be.is_put_on = 0
            and be.is_sale = 0 and bs.is_put_on = 0
            <if test="item.waresCode != null and item.waresCode != ''">
                and bw.wares_code = #{item.waresCode}
            </if>
            <if test="specialArea != null and specialArea != ''">
                and bw.special_area = #{specialArea}
            </if>
            <choose>
                <when test="transType ==1">
                    and be.is_land_trans=0
                </when>
                <when test="transType ==2">
                    and be.is_air_trans=0
                </when>
                <otherwise>
                    and be.is_ocean_trans=0
                </otherwise>
            </choose>
            <if test="item.waresItemsParamList != null and item.waresItemsParamList.size >0">
                and bs.pk_id in
                <foreach collection="item.waresItemsParamList" item="param" open="(" separator="," close=")">
                    #{param.pkWaresSpecsSku}
                </foreach>
            </if>
        </foreach>
    </select>

    <select id="queryWaresDetailSpecsByCondition" resultMap="BdWaresDetailExt">
        select * from (
        <foreach collection="orderItemsParams" item="item" index="index" separator="union">
            select be.pk_wares,bw.wares_code,be.pre_sale_status,bs.pk_id pk_wares_specs_sku,bd.pk_product,
            bp.pk_specs,bt.pk_product_bas,bp.pk_specs_type,bc.specs_name b_specs_name,bt.unit pk_unit,
            bt.shipping_channel,nvl(bd.price,0) price,nvl(bd.achieve,0) achieve,bd.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
            left join bd_wares_specs_sku bs
            on bd.pk_id = bs.pk_wares_detail and bs.del_flag=0
            left join bd_wares_specs_relation br
            on br.pk_wares_specs_sku=bs.pk_id and br.del_flag=0
            left join bd_wares_specs bp
            on br.pk_wares_specs= bp.pk_id and bp.del_flag=0
            left join bd_specs bc
            on bc.pk_id=bp.pk_specs and bc.del_flag=0
            left join bd_product bt
            on bt.pk_id =bd.pk_product and bt.del_flag=0
            where bw.del_flag=0 and bd.del_flag=0 and be.del_flag=0
            and bw.special_area = #{specialArea}
            <if test="item.waresItemsParamList != null and item.waresItemsParamList.size >0">
                and bs.pk_id in
                <foreach collection="item.waresItemsParamList" item="param" open="(" separator="," close=")">
                    #{param.pkWaresSpecsSku}
                </foreach>
            </if>
        </foreach>
        )
        order by pk_wares_specs_sku,pk_specs
    </select>

    <select id="selectByWaresCode" resultType="com.hzs.sale.wares.param.CuProductParams">
        select
        p.cover,
        p.PK_ID productId ,
        p.PRODUCT_CODE productCode ,
        p.PRODUCT_NAME productName,
        wd.QUANTITY quantity,
        be.is_maker_gift isMakerGift,
        wd.IS_GIFT isGift
        from BD_WARES_DETAIL wd
        left join BD_PRODUCT p on p.PK_ID = wd.PK_PRODUCT
        inner join BD_WARES w on w.PK_ID = wd.PK_WARES
        left join BD_WARES_extend be on be.pk_wares=w.pk_id
        where wd.del_flag = 0
        and p.del_flag = 0
        and w.del_flag = 0
        and be.del_flag = 0
        and w.WARES_CODE = #{waresCode}
        and w.SPECIAL_AREA =#{specialArea}
        <if test="isGift != null">
            and wd.IS_GIFT = #{isGift}
        </if>
    </select>

    <select id="selectByPkWares" resultType="com.hzs.common.domain.sale.ext.BdWaresDetailExt">
        select wd.*, bp.PRODUCT_NAME productName
        from BD_WARES_DETAIL wd
                 left join BD_PRODUCT bp on bp.PK_ID = wd.PK_PRODUCT
        where wd.DEL_FLAG = 0
          and wd.PK_WARES = #{pkWares}
    </select>

    <select id="listByPkProduct" resultType="com.hzs.sale.product.vo.WaresDetailVo">

        select wd.PK_WARES pkWares,bw.IS_PRE_SALE isPreSale from BD_WARES_DETAIL wd
        left join bd_wares bw on bw.pk_id = wd.PK_WARES
        where wd.del_flag = 0 and bw.del_flag = 0
        <if test="pkProducts != null and pkProducts.size > 0">
            and wd.PK_PRODUCT in
            <foreach collection="pkProducts" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
    </select>

    <select id="selectByList" resultType="com.hzs.sale.wares.vo.WaresProductDetailVo">
        select bp.PRODUCT_CODE productCode, bp.PRODUCT_NAME productName, wd.QUANTITY productQuantity
        from BD_WARES_DETAIL wd
                 left join bd_product bp on bp.pk_id = wd.PK_PRODUCT
        where wd.del_flag = 0
          and wd.PK_WARES = #{pkWares}
    </select>

</mapper>
