<?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="updatePutOnTime">
        update BD_WARES_EXTEND
        set PUT_ON_TIME = null
        where PK_WARES = #{waresId}
    </update>

    <update id="updatePutOffTime">
        update BD_WARES_EXTEND
        set PUT_OFF_TIME = null
        where PK_WARES = #{waresId}
    </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>

    <update id="updateByPkWares">
        update BD_WARES_EXTEND
        set IS_PUT_ON           = #{isPutOn},
            PRE_SALE_STATUS     = #{preSaleStatus},
            PUT_ON_TIME         = #{putOnTime},
            PUT_OFF_TIME        = #{putOffTime},
            OPERATE_SCOPE       = #{operateScope},
            PREFIX_LABEL        = #{prefixLabel},
            COOL_LABEL          = #{coolLabel},
            S_LABEL             = #{sLabel},
            SALES               = #{sales},
            IS_SALE             = #{isSale},
            ARRIVAL_TIME        = #{arrivalTime},
            WARN_MESSAGE        = #{warnMessage},
            MODIFIED_TIME       = #{modifiedTime},
            IS_LAND_TRANS       = #{isLandTrans},
            IS_AIR_TRANS        = #{isAirTrans},
            IS_OCEAN_TRANS      = #{isOceanTrans},
            PK_LAND_POSTAGE     = #{pkLandPostage},
            PK_AIR_POSTAGE      = #{pkAirPostage},
            PK_OCEAN_POSTAGE    = #{pkOceanPostage},
            PK_MODIFIED         = #{pkModified},
            PK_AGREEMENT        = #{pkAgreement},
            SELLING_POINTS      = #{sellingPoints},
            IS_MAKER_GIFT       = #{isMakerGift},
            PK_SPECIAL_CURRENCY = #{pkSpecialCurrency}
        where PK_WARES = #{pkWares}
    </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>

</mapper>
