<?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="PK_SPECIAL_CURRENCY" property="pkSpecialCurrency"/>
        <result column="SYSTEM_TYPE" property="systemType"/>
        <result column="AREA_INCOME" property="areaIncome"/>
        <result column="BOX_NUM" property="boxNum"/>
    </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="LISTING_TIME" property="listingTime"/>
        <result column="REMOVAL_TIME" property="removalTime"/>
        <result column="SORT_STATUS" property="sortStatus"/>
        <result column="IS_MAKER_GIFT" property="isMakerGift"/>
        <result column="TEMP_NAME" property="tempName"/>
        <result column="SYSTEM_TYPE" property="systemType"/>
        <result column="WARES_STATUS" property="waresStatus"/>
        <result column="WARES_APPROVER" property="waresApprover"/>
        <result column="WARES_APPROVAL_TIME" property="waresApprovalTime"/>
    </resultMap>

    <!-- 根据条件查询商品信息 -->
    <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, be.maker_income,be.PK_SPECIAL_CURRENCY
        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.wares_status = 5
        <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>

        <if test="null != waresCodeInList and waresCodeInList.size > 0">
            and bw.wares_code in
            <foreach collection="waresCodeInList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="null != waresCodeNotInList and waresCodeNotInList.size > 0">
            and bw.wares_code not in
            <foreach collection="waresCodeNotInList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        order by bw.SORT_STATUS desc , bw.WARES_PRICE ,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.LISTING_TIME,bw.REMOVAL_TIME,bw.SORT_STATUS,we.IS_MAKER_GIFT,
        sc.TEMP_NAME, we.maker_income, nvl(we.SYSTEM_TYPE, 2) SYSTEM_TYPE, bw.WARES_STATUS
        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
        left join BD_SPECIAL_CURRENCY sc on sc.pk_Id = we.PK_SPECIAL_CURRENCY
        where 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="systemType != null">
            and bw.SYSTEM_TYPE = #{systemType}
        </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 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 bw.LISTING_TIME &gt;= #{startPutOnTime}
        </if>
        <if test="endPutOnTime != null">
            and bw.LISTING_TIME &lt;= #{endPutOnTime}
        </if>
        <if test="startPutOffTime != null">
            and bw.REMOVAL_TIME &gt;= #{startPutOffTime}
        </if>
        <if test="endPutOffTime != null">
            and bw.REMOVAL_TIME &lt;= #{endPutOffTime}
        </if>
        <if test="waresStatus != null">
            and bw.WARES_STATUS &lt;= #{waresStatus}
        </if>
        order by bw.sort desc, bw.CREATION_TIME 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'
        <if test="specialArea != null">
            and ex.SPECIAL_AREA = #{specialArea}
        </if>
    </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'
        <if test="specialArea != null">
            and ex.SPECIAL_AREA = #{specialArea}
        </if>
    </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>

    <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>

    <!--    修改商品预售状态-->
    <update id="waresPreSale">
        update BD_WARES_EXTEND
        set PRE_SALE_STATUS = 0,
            MODIFIED_TIME   = SYSDATE
        where SYSDATE >= ARRIVAL_TIME
          and PRE_SALE_STATUS = 1
          and DEL_FLAG = 0

    </update>

    <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
        <!-- 商品必须通过审核后才能显示 -->
        and bw.wares_status = 5
        <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="getWaresAuthorityFirst" resultType="com.hzs.sale.wares.vo.WaresAuthorityVo">
        select wa.AUTHORITY_TYPE authorityType, cm.MEMBER_CODE memberCode, cm.MEMBER_NAME memberName
        from bd_wares_authority wa
                 left join cu_member cm on cm.pk_id = wa.PK_MEMBER
        where wa.del_flag = 0
          and wa.PK_WARES = #{pkWares}
          and rownum = 1
        order by wa.PK_id desc
    </select>

    <!-- 根据条件查询新零售商品信息 -->
    <select id="listRetailWaresByCondition" resultType="com.hzs.common.domain.sale.ext.BdWaresExt">
        select
        bw.PK_ID,
        bwe.IS_MAKER_GIFT,
        bw.WARES_CODE,
        bw.WARES_NAME,
        PK_AREA_CLASSIFY,
        COVER,
        COVER1,
        bw.WARES_PRICE,
        bw.WARES_ACHIEVE,
        SPECIAL_AREA,
        IS_SINGLE,
        IS_PUT_ON,
        PRE_SALE_STATUS,
        PREFIX_LABEL,
        COOL_LABEL,
        S_LABEL,
        SALES,
        IS_SALE,
        ARRIVAL_TIME,
        WARN_MESSAGE,
        bwe.PK_SPECIAL_CURRENCY
        from bd_wares bw
        inner join bd_wares_extend bwe
        on bw.pk_id = bwe.pk_wares
        <if test="null != param.pkGrade">
            left join BD_WARES_GRADE bwg
            on bwg.pk_wares = bw.pk_id
            and bwg.del_flag = 0
            <if test="param.pkCountry != null">
                and bwg.pk_country = #{param.pkCountry}
            </if>
        </if>
        <if test="null != param.pkAwards">
            left join BD_WARES_AWARDS bwa
            on bwa.pk_wares = bw.pk_id
            and bwa.del_flag = 0
            <if test="param.pkCountry != null">
                and bwa.pk_country = #{param.pkCountry}
            </if>
        </if>
        where bw.del_flag = 0
        <!-- 商品必须通过审核后才能显示 -->
        and bw.wares_status = 5
        and bwe.del_flag = 0
        and bwe.is_put_on = 0
        <if test="param.specialArea != null">
            and bw.special_area = #{param.specialArea}
        </if>
        <if test="param.pkAreaClassify != null">
            and (
            (
            select t.pk_id
            from BD_AREA_CLASSIFY t
            where t.pk_id = bw.PK_AREA_CLASSIFY
            start with t.PK_PARENT = #{param.pkAreaClassify}
            connect by t.PK_PARENT = prior t.PK_ID
            ) > 0
            or
            bw.PK_AREA_CLASSIFY = #{param.pkAreaClassify}
            )
        </if>
        <if test="param.waresName != null and param.waresName != ''">
            and bw.WARES_NAME like '%' || #{param.waresName} || '%'
        </if>
        <if test="null != param.pkGrade">
            and (bwg.pk_range = #{param.pkGrade} or bwg.pk_range is null)
        </if>
        <if test="null != param.pkAwards">
            and (bwa.pk_awards = #{param.pkAwards} or bwa.pk_awards is null)
        </if>
        <if test="param.pkCountry != null">
            and bw.PK_COUNTRY = #{param.pkCountry}
        </if>
        <if test="param.systemType">
            and bw.system_type = #{param.systemType}
        </if>
        order by bw.SORT desc, bw.CREATION_TIME desc
    </select>

    <!-- 查询盒数商品对应产品列表 -->
    <select id="listBoxProduct" resultType="com.hzs.common.domain.sale.wares.BdWaresDetail">
        select distinct bw.pk_id pk_wares, bwd.pk_product, bw.wares_code
        from bd_wares bw
        left join bd_wares_detail bwd
        on bw.pk_id = bwd.pk_wares
        and bwd.del_flag = 0
        and bwd.is_gift = 1
        where bw.del_flag = 0
        and bw.wares_code in
        <foreach collection="boxWaresList" item="items" open="(" close=")" separator=",">
            #{items}
        </foreach>
        <if test="pkProduct != null">
            and bwd.pk_product = #{pkProduct}
        </if>
        <if test="specialArea != null">
            and bw.special_area = #{specialArea}
        </if>
        order by bw.wares_code
    </select>

    <!-- 查询非在售商品 -->
    <select id="listWaresNoSale" resultType="com.hzs.common.domain.sale.ext.BdWaresExt">
        select bw.pk_id,
        bw.cover1,
        bw.wares_name,
        bw.wares_code,
        bw.special_area,
        bwe.pre_sale_status
        from bd_wares bw
        inner join bd_wares_extend bwe
        on bwe.pk_wares = bw.pk_id
        where bw.del_flag = 0
        and bwe.del_flag = 0
        and bwe.is_put_on = 0
        and bwe.pre_sale_status in (1, 3)
        <if test="param.waresCode != null and param.waresCode != ''">
            and bw.wares_code like '%' || #{param.waresCode} || '%'
        </if>
        <if test="param.waresName != null and param.waresName != ''">
            and bw.wares_name like '%' || #{param.waresName} || '%'
        </if>
        <if test="param.preSaleStatus != null">
            and bwe.pre_sale_status  = #{param.preSaleStatus}
        </if>
        order by bw.wares_name
    </select>

</mapper>
