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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.hzs.common.domain.sale.ext.BdWaresExt">
        <id column="PK_ID" property="pkId"/>
        <id column="PK_WARES_EXTEND" property="pkWaresExtend"/>
        <result column="PK_COUNTRY" property="pkCountry"/>
        <result column="WARES_CODE" property="waresCode"/>
        <result column="WARES_NAME" property="waresName"/>
        <result column="PK_TRANSACTION" property="pkTransaction"/>
        <result column="WARES_PRICE" property="waresPrice"/>
        <result column="WARES_ACHIEVE" property="waresAchieve"/>
        <result column="PK_AREA_CLASSIFY" property="pkAreaClassify"/>
        <result column="COVER" property="cover"/>
        <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="DETAILS" property="details"/>
        <result column="SORT" property="sort"/>
        <result column="SPECIAL_AREA" property="specialArea"/>
        <result column="IS_SINGLE" property="isSingle"/>
        <result column="IS_FREE_MAIL" property="isFreeMail"/>
        <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="IS_MAKER_GIFT" property="isMakerGift"/>
        <result column="ARRIVAL_TIME" property="arrivalTime"/>
        <result column="WARN_MESSAGE" property="warnMessage"/>
        <result column="IS_LAND_TRANS" property="isLandTrans"/>
        <result column="IS_AIR_TRANS" property="isAirTrans"/>
        <result column="IS_OCEAN_TRANS" property="isOceanTrans"/>
        <result column="PK_AGREEMENT" property="pkAgreement"/>
        <result column="VIDEO_URL" property="videoUrl"/>
        <result column="DETAILS_TYPE" property="detailsType"/>
        <result column="AFTER_GUARANTEE" property="afterGuarantee"/>
        <result column="giftNames" property="giftNames"/>
        <result column="quantity" property="quantity"/>
        <result column="PRICE" property="price"/>
        <result column="inventory_num" property="inventoryNum" />
    </resultMap>

    <resultMap id="resultMap" type="com.hzs.sale.wares.vo.WaresVo">
        <result column="SORT" property="sort"/>
        <result column="COVER" property="cover"/>
        <result column="WARES_NAME" property="waresName"/>
        <result column="WARES_CODE" property="waresCode"/>
        <result column="WARES_PRICE" property="waresPrice"/>
        <result column="WARES_ACHIEVE" property="waresAchieve"/>
        <result column="SPECIAL_AREA" property="specialArea"/>
        <result column="IS_SINGLE" property="isSingle"/>
        <result column="IS_FREE_MAIL" property="isFreeMail"/>
        <result column="CLASSIFY_NAME" property="classifyName"/>
        <result column="OPERATE_SCOPE" property="operateScope"/>
        <result column="IS_SALE" property="isSale"/>
        <result column="IS_PUT_ON" property="isPutOn"/>
        <result column="PRE_SALE_STATUS" property="preSaleStatus"/>
        <result column="PK_ID" property="pkWares"/>
        <result column="SALES" property="sales"/>
        <result column="ARRIVAL_TIME" property="arrivalTime"/>
        <result column="PUT_ON_TIME" property="putOnTime"/>
        <result column="PUT_OFF_TIME" property="putOffTime"/>
        <result column="actualSales" property="actualSales"/>
        <result column="CREATION_TIME" property="creationTime"/>
        <result column="IS_FORCED_READ" property="isForceoRead"/>
        <result column="FORCED_READ_TIME" property="forceoReadTime"/>
        <result column="IS_RECOMMEND" property="isRecommend"/>
        <result column="PK_TRANSACTION" property="pkTransaction"/>
        <result column="areaClassifyTransactionKey" property="areaClassifyTransactionKey"/>
    </resultMap>
    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        PK_CREATOR
        ,
        CREATION_TIME,
        PK_MODIFIED,
        MODIFIED_TIME,
        DEL_FLAG,
        PK_COUNTRY,
        PK_ID, WARES_CODE, WARES_NAME, PK_TRANSACTION, WARES_PRICE, WARES_ACHIEVE, PK_AREA_CLASSIFY, COVER1, COVER2, COVER3, COVER4, COVER5, COVER6, DETAILS, SORT, SPECIAL_AREA, IS_SINGLE,COVER,PK_AGREEMENT,VIDEO_URL,DETAILS_TYPE
    </sql>

    <select id="queryWaresByCondition" resultMap="BaseResultMap">
        select bw.PK_COUNTRY,bw.PK_ID,be.pk_id PK_WARES_EXTEND,be.IS_MAKER_GIFT, bw.WARES_CODE, bw.WARES_NAME, PK_TRANSACTION,
        PK_AREA_CLASSIFY, COVER1, COVER2, COVER3, COVER4, COVER5, COVER6,DETAILS_TYPE,
        bw.WARES_PRICE,
        bw.WARES_ACHIEVE,
        DETAILS, SORT, SPECIAL_AREA, IS_SINGLE,
        IS_PUT_ON, PRE_SALE_STATUS, PUT_ON_TIME, PUT_OFF_TIME, OPERATE_SCOPE, PREFIX_LABEL, COOL_LABEL,
        S_LABEL, SALES, IS_SALE, ARRIVAL_TIME, WARN_MESSAGE,
        IS_LAND_TRANS,IS_AIR_TRANS,IS_OCEAN_TRANS,VIDEO_URL
        from bd_wares bw
        inner join bd_wares_extend be
        on bw.pk_id = be.pk_wares
        where bw.del_flag=0 and be.del_flag=0
        and be.is_put_on=0
        <if test="specialAreaList != null and specialAreaList.size > 0">
            and bw.special_area in
            <foreach collection="specialAreaList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="pkCountry != null">
            and bw.PK_COUNTRY = #{pkCountry}
        </if>
        <if test="pkAreaClassify != null and pkAreaClassify != ''">
            and ( (
            select t.pk_id
            from BD_AREA_CLASSIFY t
            where t.pk_id = bw.PK_AREA_CLASSIFY
            start with t.PK_PARENT = #{pkAreaClassify}
            connect by t.PK_PARENT = prior t.PK_ID
            ) > 0
            or
            bw.PK_AREA_CLASSIFY = #{pkAreaClassify}
            )
        </if>
        <if test="waresCode != null and waresCode != ''">
            and bw.wares_code = #{waresCode}
        </if>
        <if test="waresName != null and waresName != ''">
            and bw.WARES_NAME like #{waresName} || '%'
        </if>
        order by bw.sort,bw.creation_time desc
    </select>

    <select id="queryOverWaresByCondition" resultMap="BaseResultMap">
        select bw.PK_COUNTRY,bw.PK_ID,be.pk_id PK_WARES_EXTEND,be.IS_MAKER_GIFT, bw.WARES_CODE, bw.WARES_NAME, PK_TRANSACTION,
        PK_AREA_CLASSIFY, COVER1, COVER2, COVER3, COVER4, COVER5, COVER6,DETAILS_TYPE,
        bw.WARES_PRICE,
        bw.WARES_ACHIEVE,
        DETAILS, SORT, SPECIAL_AREA, IS_SINGLE,
        IS_PUT_ON, PRE_SALE_STATUS, PUT_ON_TIME, PUT_OFF_TIME, OPERATE_SCOPE, PREFIX_LABEL, COOL_LABEL,
        S_LABEL, SALES, IS_SALE, ARRIVAL_TIME, WARN_MESSAGE,
        IS_LAND_TRANS,IS_AIR_TRANS,IS_OCEAN_TRANS,VIDEO_URL,nvl(b.available_num,0) inventory_num
        from bd_wares bw
        inner join bd_wares_extend be
        on bw.pk_id = be.pk_wares
        left join(
        select a.pk_id,min(available_num) available_num from ic_available_num iu
        inner join(
        select bd.pk_product,bw.pk_id from bd_wares bw
        inner join bd_wares_detail bd
        on bw.pk_id = bd.pk_wares
        where bw.del_flag=0 and bd.del_flag=0
        ) a
        on iu.pk_product=a.pk_product
        where iu.pk_country=#{pkCountry}
        group by a.pk_id
        ) b
        on b.pk_id=bw.pk_id
        where bw.del_flag=0 and be.del_flag=0
        and be.is_put_on=0
        <if test="specialAreaList != null and specialAreaList.size > 0">
            and bw.special_area in
            <foreach collection="specialAreaList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="pkCountry != null">
            and bw.PK_COUNTRY = #{pkCountry}
        </if>
        <if test="pkAreaClassify != null and pkAreaClassify != ''">
            and ( (
            select t.pk_id
            from BD_AREA_CLASSIFY t
            where t.pk_id = bw.PK_AREA_CLASSIFY
            start with t.PK_PARENT = #{pkAreaClassify}
            connect by t.PK_PARENT = prior t.PK_ID
            ) > 0
            or
            bw.PK_AREA_CLASSIFY = #{pkAreaClassify}
            )
        </if>
        <if test="waresCode != null and waresCode != ''">
            and bw.wares_code = #{waresCode}
        </if>
        <if test="waresName != null and waresName != ''">
            and bw.WARES_NAME like #{waresName} || '%'
        </if>
        order by bw.sort,bw.creation_time desc
    </select>

    <select id="getLastWaresCode" resultType="string">
        select WARES_CODE
        from (select t.WARES_CODE from BD_WARES t order by WARES_CODE desc)
        where rownum = 1
    </select>

    <select id="selectByWaresInfo" resultMap="resultMap">
        select
        bw.SORT,bw.COVER,bw.WARES_NAME,bw.WARES_CODE,bw.WARES_PRICE,bw.WARES_ACHIEVE,bw.SPECIAL_AREA ,bw.IS_SINGLE,bw.IS_FREE_MAIL,
        ac.CLASSIFY_NAME,we.OPERATE_SCOPE,we.IS_SALE,we.IS_PUT_ON,we.PRE_SALE_STATUS,bw.PK_ID,we.ARRIVAL_TIME,we.PUT_ON_TIME,we.PUT_OFF_TIME,
        we.SALES actualSales,we.SALES,bw.CREATION_TIME,bw.IS_RECOMMEND,bw.PK_TRANSACTION,ac.PK_TRANSACTION_KEY areaClassifyTransactionKey
        from bd_wares bw
        left join BD_WARES_EXTEND we on we.PK_WARES = bw.PK_ID
        left join BD_AREA_CLASSIFY ac on ac.PK_ID = bw.PK_AREA_CLASSIFY
        where 1 = 1
        and bw.del_flag = '0'
        <if test="isRecommend != null">
            and bw.IS_RECOMMEND = #{isRecommend}
        </if>
        <if test="pkCountry != null">
            and bw.PK_COUNTRY = #{pkCountry}
        </if>
        <if test="waresCode != null and waresCode != ''">
            and bw.wares_code = #{waresCode}
        </if>
        <if test="waresName != null and waresName != ''">
            and bw.WARES_NAME like '%' || #{waresName} || '%'
        </if>
        <if test="isPutOn != null">
            and we.IS_PUT_ON = #{isPutOn}
        </if>
        <if test="preSaleStatus != null">
            and we.PRE_SALE_STATUS = #{preSaleStatus}
        </if>
        <if test="operateScope != null  and operateScope != ''">
            and we.OPERATE_SCOPE = #{operateScope}
        </if>
        <if test="pkAreaClassify != null and pkAreaClassify != ''">
            and bw.PK_AREA_CLASSIFY = #{pkAreaClassify}
        </if>
        <if test="specialArea != null and specialArea != ''">
            and bw.SPECIAL_AREA = #{specialArea}
        </if>
        <if test="isSale != null">
            and we.IS_SALE = #{isSale}
        </if>
        <if test="isSingle != null">
            and bw.IS_SINGLE = #{isSingle}
        </if>
        <if test="isFreeMail != null">
            and bw.IS_FREE_MAIL = #{isFreeMail}
        </if>
        <if test="startCreateDate != null">
            and bw.CREATION_TIME &gt;= #{startCreateDate}
        </if>
        <if test="endCreateDate != null">
            and bw.CREATION_TIME &lt;= #{endCreateDate}
        </if>
        <if test="startPutInfoTime != null">
            and we.PUT_INTO_TIME &gt;= #{startPutInfoTime}
        </if>
        <if test="endPutInfoTime != null">
            and we.PUT_INTO_TIME &lt;= #{endPutInfoTime}
        </if>
        <if test="startPutOnTime != null">
            and we.PUT_ON_TIME &gt;= #{startPutOnTime}
        </if>
        <if test="endPutOnTime != null">
            and we.PUT_ON_TIME &lt;= #{endPutOnTime}
        </if>
        <if test="startPutOffTime != null">
            and we.PUT_OFF_TIME &gt;= #{startPutOffTime}
        </if>
        <if test="endPutOffTime != null">
            and we.PUT_OFF_TIME &lt;= #{endPutOffTime}
        </if>
        order by bw.sort desc
    </select>

    <select id="selectByMoveUpSort" resultType="integer">
        select min(ex.SORT)
        FROM bd_wares ex
        where ex.SORT &gt; (select SORT from bd_wares pe where pe.PK_ID = #{waresId})
          and DEL_FLAG = '0'
    </select>


    <select id="selectByMoveDownSort" resultType="integer">
        select max(ex.SORT)
        FROM bd_wares ex
        where ex.SORT &lt; (select sort from bd_wares pe where pe.PK_ID = #{waresId})
          and DEL_FLAG = '0'
    </select>

    <select id="selectMaxSort" resultType="integer">
        select max(sort)
        from bd_wares
        where DEL_FLAG = '0'
    </select>


    <update id="updateMoveUpBySort">
        update bd_wares
        set sort = sort + 1
        where sort &gt;= #{sort}
          and DEL_FLAG = '0'
    </update>

    <update id="updateMoveDownBySort">
        update bd_wares
        set sort = sort - 1
        where sort &lt;= #{sort}
          and DEL_FLAG = '0'
    </update>

    <select id="getMaxSort" resultType="integer">
        select max(bw.SORT)
        from bd_wares bw
        where bw.PK_COUNTRY = #{pkCountry}
    </select>

    <select id="selectByWaresCodeList" resultType="com.hzs.common.domain.system.config.BdAgreement">


        select ba.* from BD_AGREEMENT ba where 1 = 1 and
        (select count(bw.pk_id) from BD_WARES bw left join
        BD_WARES_EXTEND we on we.PK_WARES = bw.PK_ID
        where 1 = 1
        <if test="waresCodeList != null and waresCodeList.size > 0">
            and bw.WARES_CODE in
            <foreach collection="waresCodeList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        and ba.pk_id = we.PK_AGREEMENT ) > 0
    </select>

    <select id="queryRecommendWaresSpecial" resultType="integer">
        select DISTINCT t.SPECIAL_AREA
        from BD_WARES t
                 left join BD_WARES_EXTEND we on we.PK_WARES = t.PK_ID
        where IS_RECOMMEND = 0
          and we.IS_PUT_ON = 0
          and t.del_flag = 0
          and we.del_flag = 0
    </select>

    <select id="selectByRecommentList" resultMap="BaseResultMap">
        select t.* ,we.SALES,a.quantity ,a.PRICE from BD_WARES t
        left join BD_WARES_EXTEND we on we.PK_WARES = t.PK_ID
        left join (
        select wd.QUANTITY,wd.PK_WARES , min(ws.PRICE) PRICE from
        bd_wares_specs_sku ws
        left join BD_WARES_DETAIL wd
        on ws.PK_WARES_DETAIL = wd.pk_id
        where wd.del_flag = 0 group by wd.PK_WARES ,wd.QUANTITY
        ) a on a.PK_WARES = t.PK_ID
        where t.IS_RECOMMEND = 0 and we.IS_PUT_ON = 0 and t.PK_COUNTRY = #{pkCountry}
        and t.del_flag = 0
        <if test="specialAreaList != null and specialAreaList.size > 0">
            and t.SPECIAL_AREA in
            <foreach collection="specialAreaList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        order by t.SORT desc
    </select>

    <update id="updateByAutoStartAndAutoStart">
        update BD_WARES_EXTEND
        set IS_PUT_ON = 0
        where to_char(PUT_ON_TIME, 'yyyy-mm-dd hh24:mi:ss') &lt;= to_char(#{currentTime}, 'yyyy-mm-dd hh24:mi:ss')

          and IS_PUT_ON = '1'
    </update>
    <update id="updateByAutoStartAndAutoEnd">
        update BD_WARES_EXTEND
        set IS_PUT_ON = 1
        where to_char(PUT_OFF_TIME, 'yyyy-mm-dd hh24:mi:ss') &lt;= to_char(#{currentTime}, 'yyyy-mm-dd hh24:mi:ss')
          and IS_PUT_ON = '0'
    </update>

    <select id="queryRecommnedWares" resultMap="resultMap">
        select bw.*
        from BD_WARES bw
        where bw.SPECIAL_AREA = #{specialArea}
          and bw.del_flag = 0
          and bw.IS_RECOMMEND = 0
          and rownum = 2
    </select>

    <select id="queryRecommendWaresByCondition" resultMap="BaseResultMap">
        select bw.PK_COUNTRY,bw.PK_ID,be.pk_id PK_WARES_EXTEND, WARES_CODE, WARES_NAME, PK_TRANSACTION,
        PK_AREA_CLASSIFY, COVER1, COVER2, COVER3, COVER4, COVER5, COVER6,DETAILS_TYPE,
        bw.WARES_PRICE,
        bw.WARES_ACHIEVE,
        DETAILS, SORT, SPECIAL_AREA, IS_SINGLE,
        IS_PUT_ON, PRE_SALE_STATUS, PUT_ON_TIME, PUT_OFF_TIME, OPERATE_SCOPE, PREFIX_LABEL, COOL_LABEL,
        S_LABEL, SALES, IS_SALE, ARRIVAL_TIME, WARN_MESSAGE,
        IS_LAND_TRANS,IS_AIR_TRANS,IS_OCEAN_TRANS,VIDEO_URL

        from bd_wares bw
        inner join bd_wares_extend be
        on bw.pk_id = be.pk_wares
        where bw.del_flag=0 and be.del_flag=0
        and be.is_put_on=0
        and bw.IS_RECOMMEND = 0
        <if test="specialArea != null">
            and bw.special_area = #{specialArea}
        </if>
        <if test="pkCountry != null">
            and bw.PK_COUNTRY = #{pkCountry}
        </if>
        <if test="pkAreaClassify != null and pkAreaClassify != ''">
            and ( (
            select t.pk_id
            from BD_AREA_CLASSIFY t
            where t.pk_id = bw.PK_AREA_CLASSIFY
            start with t.PK_PARENT = #{pkAreaClassify}
            connect by t.PK_PARENT = prior t.PK_ID
            ) > 0
            or
            bw.PK_AREA_CLASSIFY = #{pkAreaClassify}
            )
        </if>
        <if test="waresCode != null and waresCode != ''">
            and bw.wares_code = #{waresCode}
        </if>
        <if test="waresName != null and waresName != ''">
            and bw.WARES_NAME like #{waresName} || '%'
        </if>

        order by bw.sort,bw.creation_time desc
    </select>


    <select id="queryByRecommendList" resultMap="BaseResultMap">
        select bw.* from bd_wares bw
        left join bd_wares_extend be on bw.pk_id = be.pk_wares
        where be.IS_PUT_ON = 0 and bw.del_flag = 0 and be.del_flag = 0
        <if test="specialArea != null">
            and bw.SPECIAL_AREA = #{specialArea}
        </if>
        <if test="pkCountry != null">
            and bw.PK_COUNTRY = #{pkCountry}
        </if>
        <if test="isRecommend != null">
            and bw.IS_RECOMMEND = #{isRecommend}
        </if>
        order by bw.SORT desc
    </select>


<select id="selectWaresBySpecial" resultType="com.hzs.common.domain.sale.ext.BdWaresInfoExt">
        select s.price,
               s.achieve,
               s.pk_id           pkId,
               s.pk_wares        pkWares,
               s.pk_wares_detail pkWaresDetail,
               d.pk_product      pkProduct
          from bd_wares           w,
               bd_wares_extend    e,
               bd_wares_detail    d,
               bd_wares_specs_sku s
         where w.pk_id = e.pk_wares
           and w.pk_id = d.pk_wares
           and w.pk_id = s.pk_wares
           and w.special_area = #{specialArea}
           and w.del_flag = 0
           and e.is_put_on = 0
           and d.del_flag = 0
           and e.del_flag = 0
           and s.del_flag = 0
           and w.pk_country = #{pkCountry}
</select>


</mapper>
