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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.hzs.common.domain.sale.wares.BdWaresExtend">
        <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="IS_PUT_ON" property="isPutOn"/>
        <result column="PRE_SALE_STATUS" property="preSaleStatus"/>
        <result column="PUT_ON_TIME" property="putOnTime"/>
        <result column="PUT_OFF_TIME" property="putOffTime"/>
        <result column="OPERATE_SCOPE" property="operateScope"/>
        <result column="PREFIX_LABEL" property="prefixLabel"/>
        <result column="COOL_LABEL" property="coolLabel"/>
        <result column="S_LABEL" property="sLabel"/>
        <result column="SALES" property="sales"/>
        <result column="IS_SALE" property="isSale"/>
        <result column="ARRIVAL_TIME" property="arrivalTime"/>
        <result column="WARN_MESSAGE" property="warnMessage"/>
        <result column="IS_LAND_TRANS" property="isLandTrans"/>
        <result column="PK_LAND_POSTAGE" property="pkLandPostage"/>
        <result column="IS_AIR_TRANS" property="isAirTrans"/>
        <result column="PK_AIR_POSTAGE" property="pkAirPostage"/>
        <result column="IS_OCEAN_TRANS" property="isOceanTrans"/>
        <result column="PK_OCEAN_POSTAGE" property="pkOceanPostage"/>
        <result column="SELLING_POINTS" property="sellingPoints"/>
        <result column="IS_MAKER_GIFT" property="isMakerGift"/>
        <result column="PK_SPECIAL_CURRENCY" property="pkSpecialCurrency"/>
        <result column="MAKER_INCOME" property="makerIncome"/>
        <result column="SYSTEM_TYPE" property="systemType"/>
    </resultMap>

    <update id="updateWaresSalesAccrual">
        update BD_WARES_EXTEND
        <set>
            SALES = SALES + #{number}
        </set>
        where PK_WARES = #{pkWares}
    </update>

    <update id="updatePutOnTimeAndPutOffTimeByWaresId">
        update BD_WARES_EXTEND set
        <choose>
            <when test="putOffTime != null">
                PUT_OFF_TIME = #{putOffTime},
            </when>
            <otherwise>
                PUT_OFF_TIME = null,
            </otherwise>
        </choose>
        <choose>
            <when test="putOnTime != null">
                PUT_ON_TIME = #{putOnTime}
            </when>
            <otherwise>
                PUT_ON_TIME = null
            </otherwise>
        </choose>
        where PK_WARES = #{waresId}
    </update>

    <select id="selectByWaresCodeList" resultType="com.hzs.sale.order.vo.WaresPreSaleVo">
        select
        bw.WARES_CODE waresCode, bw.WARES_NAME waresName, bw.PRE_SALE_QUANTITY preSaleQuantity,
        we.PK_WARES pkWares, we.PRE_SALE_STATUS preSaleStatus
        from BD_WARES bw
        left join BD_WARES_EXTEND we on we.PK_WARES = bw.PK_ID
        where bw.del_flag = 0
        and we.del_flag = 0
        <if test="specialArea != null">
            and bw.special_area = #{specialArea}
        </if>
        <if test="waresCodeList != null and waresCodeList.size > 0">
            and bw.WARES_CODE in
            <foreach collection="waresCodeList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
    </select>

    <!-- 批量更新已用库存 -->
    <update id="batchUpdateInventoryUse">
        merge into bd_wares_extend bwe
        using (
        <foreach collection="limitList" item="items" separator=" union ">
            select
            #{items.pkWares} pk_wares,
            #{items.quantity} quantity
            from dual
        </foreach>
        ) tmp
        on (bwe.pk_wares = tmp.pk_wares)
        when matched then
        update set bwe.inventory_use = bwe.inventory_use + tmp.quantity
    </update>

</mapper>
