<?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.product.mapper.BdProductMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BdProduct" type="com.hzs.common.domain.sale.ext.BdProductExt">
        <id column="PK_ID" property="pkId"/>
        <result column="PRODUCT_CODE" property="productCode"/>
        <result column="PRODUCT_NAME" property="productName"/>
        <result column="PK_TRANSACTION" property="pkTransaction"/>
        <result column="PK_CLASSIFY" property="pkClassify"/>
        <result column="UNIT" property="unit"/>
        <result column="WEIGHT" property="weight"/>
        <result column="LENGTH" property="length"/>
        <result column="WIDTH" property="width"/>
        <result column="HEIGHT" property="height"/>
        <result column="POSTAGE_WEIGHT" property="postageWeight"/>
        <result column="RETAIL_PRICE" property="retailPrice"/>
        <result column="ENABLE_STATE" property="enableState"/>
        <result column="COVER" property="cover"/>
        <result column="SHELF_LIFE" property="shelfLife"/>
        <result column="IS_FRESH" property="isFresh"/>
        <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="PRODUCT_ATTR" property="productAttr"/>
        <result column="SHIPPING_CHANNEL" property="shippingChannel"/>
        <result column="REPURCHASE_TYPE" property="repurchaseType"/>
        <result column="SYSTEM_TYPE" property="systemType"/>
        <result column="WMS_CODE" property="wmsCode"/>
        <collection property="specsList" ofType="com.hzs.common.domain.sale.classify.BdSpecs">
            <result column="PK_SPECS_ID" property="pkId"/>
            <result column="PK_SPECS_TYPE" property="pkSpecsType"/>
            <result column="SPECS_NAME" property="specsName"/>
            <result column="PK_TRANSACTION_KEY" property="pkTransactionKey"/>
        </collection>
    </resultMap>

    <resultMap id="ProductResultMap" type="com.hzs.sale.product.vo.ProductVo">
        <id column="PK_ID" property="pkId"/>
        <result column="PRODUCT_CODE" property="productCode"/>
        <result column="PRODUCT_NAME" property="productName"/>
        <result column="PK_CLASSIFY" property="pkClassify"/>
        <result column="COVER" property="cover"/>
        <result column="SHIPPING_CHANNEL" property="shippingChannel"/>
        <result column="LENGTH" property="length"/>
        <result column="WIDTH" property="width"/>
        <result column="HEIGHT" property="height"/>
        <result column="WEIGHT" property="weight"/>
        <result column="POSTAGE_WEIGHT" property="postageWeight"/>
        <result column="UNIT" property="unit"/>
        <result column="PRODUCT_ATTR" property="productAttr"/>
        <result column="RETAIL_PRICE" property="retailPrice"/>
        <result column="SHELF_LIFE" property="shelfLife"/>
        <result column="productSpecType" property="productSpecType"/>
        <result column="IS_GIFT" property="isGift"/>
        <result column="IS_PICK_UP" property="isPickUp"/>
        <result column="IS_PRIZE" property="isPrize"/>
        <result column="IS_BAR_CODE" property="isBarCode"/>
        <result column="IS_MERGE" property="isMerge"/>
        <result column="IS_FRESH" property="isFresh"/>
        <result column="IS_OCEAN_TRANS" property="isOceanTrans"/>
        <result column="IS_AIR_TRANS" property="isAirTrans"/>
        <result column="IS_LAND_TRANS" property="isLandTrans"/>
        <result column="SORT" property="sort"/>
        <result column="CREATION_TIME" property="creationTime"/>
        <result column="unitVal" property="unitVal"/>
        <result column="custName" property="custName"/>
        <result column="productBasName" property="productBasName"/>
        <result column="PK_PRODUCT_BAS" property="pkProductBas"/>
        <result column="PK_SUPPLIER" property="pkSupplier"/>
        <result column="PK_CUBASDOC" property="pkCubasdoc"/>
        <result column="IS_PUT_ON" property="isPutOn"/>
        <result column="LISTING_TIME" property="listingTime"/>
        <result column="REMOVAL_TIME" property="removalTime"/>
        <result column="WMS_CODE" property="wmsCode"/>
    </resultMap>

    <select id="getLastProductCode" resultType="string">
        select product_code
        from (select t.product_code from BD_PRODUCT t where t.del_flag = 0 order by product_code desc)
        where rownum = 1
    </select>

    <select id="selectByQueryList" resultMap="ProductResultMap">
        select
        bp.PK_ID,bp.PRODUCT_CODE,bp.PRODUCT_NAME,bp.PK_CLASSIFY,bp.COVER,bp.SHIPPING_CHANNEL,bp.LENGTH,bp.WIDTH,bp.HEIGHT,bp.WEIGHT,bp.POSTAGE_WEIGHT,bp.UNIT,
        bp.PRODUCT_ATTR,bp.RETAIL_PRICE,bp.SHELF_LIFE,bp.IS_FRESH,
        (select REPLACE(wm_concat(st.TYPE_NAME),',','/') from BD_PRODUCT_SPECS ps
        left join BD_SPECS_TYPE st on st.PK_ID = ps.PK_SPECS_TYPE
        where ps.PK_PRODUCT = bp.PK_ID and ps.DEL_FLAG = '0') productSpecType,
        pe.IS_GIFT,pe.IS_PICK_UP,pe.IS_PRIZE,pe.IS_BAR_CODE,pe.IS_MERGE,pe.IS_LAND_TRANS ,pe.IS_AIR_TRANS,pe.IS_OCEAN_TRANS,pe.SORT,bp.CREATION_TIME,
        bu.NAME unitVal,bc.CUST_NAME custName,pe.IS_PUT_ON,bp.LISTING_TIME,bp.REMOVAL_TIME
        from BD_PRODUCT bp
        left join BD_PRODUCT_EXTEND pe on pe.PK_PRODUCT = bp.PK_ID
        left join BD_UNIT bu on bu.PK_ID = bp.UNIT
        left join BD_CUBASDOC bc on bc.pk_id = bp.PK_CUBASDOC
        where bp.DEL_FLAG = 0
        <if test="isExistSpecs != null">
            and (select count(ps.PK_ID) from BD_PRODUCT_SPECS ps where bp.PK_ID = ps.PK_PRODUCT and ps.del_flag = 0) > 0
        </if>
        <if test="pkCountry != null">
            and bp.PK_COUNTRY = #{pkCountry}
        </if>
        <if test="systemType != null">
            and bp.SYSTEM_TYPE = #{systemType}
        </if>
        <if test="queryInfo != null and queryInfo !='' ">
            AND ( bp.PRODUCT_CODE like #{queryInfo}||'%' or bp.PRODUCT_NAME like #{queryInfo}||'%')
        </if>
        <if test="productCode != null and productCode !='' ">
            AND bp.PRODUCT_CODE like #{productCode}||'%'
        </if>
        <if test="productName != null and productName !='' ">
            AND bp.PRODUCT_NAME like #{productName}||'%'
        </if>
        <if test="productAttr != null and productAttr !='' ">
            AND bp.PRODUCT_ATTR = #{productAttr}
        </if>
        <if test="pkClassify != null and pkClassify !='' ">
            AND bp.PK_CLASSIFY = #{pkClassify}
        </if>
        <if test="shippingChannel != null and shippingChannel !='' ">
            AND bp.SHIPPING_CHANNEL = #{shippingChannel}
        </if>
        <if test="unit != null and unit !='' ">
            AND bp.UNIT = #{unit}
        </if>
        <if test="isGift != null">
            AND pe.IS_GIFT = #{isGift}
        </if>
        <if test="isPickUp != null">
            AND pe.IS_PICK_UP = #{isPickUp}
        </if>
        <if test="isPrize != null">
            AND pe.IS_PRIZE = #{isPrize}
        </if>
        <if test="isMerge != null">
            AND pe.IS_MERGE = #{isMerge}
        </if>
        <if test="isBarCode != null">
            AND pe.IS_BAR_CODE = #{isBarCode}
        </if>
        <if test="isFresh != null">
            AND bp.IS_FRESH = #{isFresh}
        </if>
        <if test="isLandTrans != null">
            AND pe.IS_LAND_TRANS = #{isLandTrans}
        </if>
        <if test="isAirTrans != null">
            AND pe.IS_AIR_TRANS = #{isAirTrans}
        </if>
        <if test="isOceanTrans != null">
            AND pe.IS_OCEAN_TRANS = #{isOceanTrans}
        </if>
        <if test="isPutOn != null">
            AND pe.IS_PUT_ON = #{isPutOn}
        </if>
        <if test="startCreateDate != null">
            and bp.CREATION_TIME &gt;= #{startCreateDate}
        </if>
        <if test="endCreateDate != null">
            and bp.CREATION_TIME &lt;= #{endCreateDate}
        </if>
        order by pe.SORT desc
    </select>

    <!-- 查询带有规格的产品列表 -->
    <select id="queryProductExistSpecs" resultMap="BdProduct">
        select distinct bp.*
        from bd_product bp
        left join bd_product_specs bps
        on bps.pk_product = bp.pk_id
        left join BD_PRODUCT_EXTEND pe on pe.PK_PRODUCT = bp.PK_ID
        where bp.del_flag = 0
        and bp.pk_country = #{pkCountry}
        <if test="systemType != null">
            and bp.system_type = #{systemType}
        </if>
        and bp.enable_state = 0
        and bps.del_flag = 0
        and pe.IS_PUT_ON = 0
        order by bp.product_name
    </select>

    <select id="queryProductExtBySpecsId" resultMap="BdProduct">
        <foreach collection="productSpecsList" item="item" separator=" union ">
            select bp.pk_id,bp.product_code,bp.product_name,bp.pk_country,bp.cover,bc.pk_id pk_specs_id,bc.pk_specs_type,bc.specs_name from bd_product
            bp
            inner join bd_product_extend be
            on bp.pk_id = be.pk_product
            inner join bd_product_specs bs
            on bp.pk_id=bs.pk_product
            inner join bd_specs bc
            on bc.pk_specs_type=bs.pk_specs_type
            where bp.del_flag=0 and be.del_flag=0 and bs.del_flag=0 and bc.del_flag=0
            and bp.pk_country= #{pkCountry} and bp.pk_id = #{item.pkProduct}
            and bs.pk_specs_type = #{item.pkSpecsType} and bc.pk_id= #{item.pkId}
        </foreach>
    </select>

    <select id="getSupplierProductCensus" resultType="com.hzs.sale.product.vo.SupplierProductCensusVo">
        select
        bc.CUST_NAME custName ,bp.COVER cover,bp.PRODUCT_NAME productName,bp.PK_CLASSIFY pkClassify,bp.PRODUCT_CODE productCode,
        bp.SHIPPING_CHANNEL shippingChannel ,bp.LENGTH length,bp.WIDTH width,bp.HEIGHT height,bp.POSTAGE_WEIGHT postageWeight,
        bp.WEIGHT weight,bp.UNIT unit,bp.SHELF_LIFE shelfLife,
        (select REPLACE(wm_concat(st.TYPE_NAME),',','/') from BD_PRODUCT_SPECS ps
        left join BD_SPECS_TYPE st on st.PK_ID = ps.PK_SPECS_TYPE
        where ps.PK_PRODUCT = bp.PK_ID and ps.DEL_FLAG = '0') productSpecType,
        bp.PK_ID pkProduct,bp.RETAIL_PRICE retailPrice, bu.NAME unitVal,bp.CREATION_TIME creationTime
        from bd_product bp
        left join BD_UNIT bu on bu.PK_ID = bp.UNIT
        left join BD_PRODUCT_EXTEND pe on pe.PK_PRODUCT = bp.pk_id
        left join bd_cubasdoc bc on bc.PK_SUPPLIER = bp.PK_SUPPLIER
        where bp.del_flag = 0 and bp.PK_SUPPLIER is not null
        <if test="pkSupplier != null">
            and bp.PK_SUPPLIER = #{pkSupplier}
        </if>
        <if test="productCode != null and productCode !='' ">
            AND bp.PRODUCT_CODE like #{productCode}||'%'
        </if>
        <if test="productName != null and productName !='' ">
            AND bp.PRODUCT_NAME like #{productName}||'%'
        </if>
    </select>

    <select id="getByProductId" resultMap="ProductResultMap">
        select bp.*, pb.PRODUCT_NAME productBasName
        from bd_product bp
                 left join bd_product_bas pb on pb.pk_id = bp.PK_PRODUCT_BAS
        where bp.del_flag = 0
          and bp.pk_id = #{productId}
    </select>

</mapper>
