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

    <!-- 通用查询映射结果 -->
    <resultMap id="BdProductBasExt" type="com.hzs.common.domain.sale.product.ext.BdProductBasExt">
        <id column="PK_ID" property="pkId"/>
        <result column="CREATION_TIME" property="creationTime"/>
        <result column="MODIFIED_TIME" property="modifiedTime"/>
        <result column="DEL_FLAG" property="delFlag"/>
        <result column="PK_CREATOR" property="pkCreator"/>
        <result column="PK_MODIFIED" property="pkModified"/>
        <result column="PRODUCT_CODE" property="productCode"/>
        <result column="PRODUCT_NAME" property="productName"/>
        <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="COVER" property="cover"/>
        <result column="SHELF_LIFE" property="shelfLife"/>
        <result column="PK_PRODUCT" property="pkProduct"/>
        <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="BdProductBasVo" type="com.hzs.ground.product.vo.ProductBasVo">
        <id column="PK_ID" property="pkId"/>
        <result column="CREATION_TIME" property="creationTime"/>
        <result column="PRODUCT_CODE" property="productCode"/>
        <result column="PRODUCT_NAME" property="productName"/>
        <result column="PK_CLASSIFY" property="pkClassify"/>
        <result column="UNIT" property="unit"/>
        <result column="unitStr" property="unitStr"/>
        <result column="WEIGHT" property="weight"/>
        <result column="LENGTH" property="length"/>
        <result column="WIDTH" property="width"/>
        <result column="HEIGHT" property="height"/>
        <result column="COVER" property="cover"/>
        <result column="SHELF_LIFE" property="shelfLife"/>
        <result column="PK_PRODUCT" property="pkProduct"/>
        <result column="PK_CUBASDOC" property="pkCubasdoc"/>
    </resultMap>

    <select id="queryProductBasByIds" resultMap="BdProductBasExt">
        select bp.pk_id,bt.pk_id pk_product from BD_PRODUCT_BAS bp
        left join (select pk_id,pk_product_bas from bd_product where del_flag=0
        and pk_country = #{pkCountry} ) bt
        on bp.pk_id=bt.pk_product_bas
        where bp.del_flag=0
        and bp.pk_id in
        <foreach item="pkId" collection="idList" open="(" separator="," close=")">
            #{pkId}
        </foreach>
    </select>

    <select id="queryProductExtBySpecsId" resultMap="BdProductBasExt">
        <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_bas bp
            inner join bd_product_bas_specs bs
            on bp.pk_id=bs.pk_product_bas
            inner join bd_specs bc
            on bc.pk_specs_type=bs.pk_specs_type
            where bp.del_flag=0 and bs.del_flag=0 and bc.del_flag=0
            and bp.pk_country= #{pkCountry} and bp.pk_id = #{item.pkProductBas}
            and bs.pk_specs_type = #{item.pkSpecsType} and bc.pk_id= #{item.pkId}
        </foreach>
    </select>

    <select id="queryProductBasList" resultMap="BdProductBasVo">
        select
        pb.*,bu.NAME unitStr
        from BD_PRODUCT_BAS pb
        inner join BD_PRODUCT bp on bp.PK_PRODUCT_BAS = pb.PK_ID
        inner join BD_PRODUCT_EXTEND be on be.PK_PRODUCT = bp.pk_id
        left join bd_unit bu on bu.PK_ID = pb.UNIT
        where pb.DEL_FLAG = 0
        <if test="systemType != null">
            and pb.system_type = #{systemType}
        </if>
        and be.IS_PUT_ON = 0
        and (
        select count(pbs.PK_ID) from bd_product_bas_specs pbs
        where pbs.PK_PRODUCT_BAS = pb.pk_id and pbs.del_flag = 0
        ) > 0
        <if test="pkCubasdoc != null">
            and (select p.PK_CUBASDOC from BD_PRODUCT p where p.del_flag = 0 and p.PK_PRODUCT_BAS = pb.pk_id and p.PK_COUNTRY = #{pkCountry} and p.PK_CUBASDOC = #{pkCubasdoc}) > 0
        </if>
        <if test="query != null and query != ''">
            and ( pb.PRODUCT_CODE like #{query} || '%' or pb.PRODUCT_NAME like #{query} || '%' )
        </if>
        order by pb.CREATION_TIME desc
    </select>

    <select id="findByProductCode" resultMap="BdProductBasVo">
        select pb.*, bu.NAME unitStr
        from BD_PRODUCT_BAS pb
                 left join bd_unit bu on bu.PK_ID = pb.UNIT
        where 1 = 1
          and pb.PRODUCT_CODE = #{productCode}
          and pb.DEL_FLAG = 0
    </select>

    <select id="queryProductBasRelevanceList" resultMap="BdProductBasVo">
        select pb.*,bu.NAME unitStr,bp.PK_ID PK_PRODUCT
        from BD_PRODUCT_BAS pb
        left join bd_unit bu on bu.PK_ID = pb.UNIT
        inner join BD_PRODUCT bp on bp.PK_PRODUCT_BAS = pb.pk_id
        inner join BD_PRODUCT_EXTEND be on be.PK_PRODUCT = bp.pk_id
        where pb.DEL_FLAG = 0
        <if test="systemType != null">
            and pb.system_type = #{systemType}
        </if>
        and be.IS_PUT_ON = 0
        and (
        select count(pbs.PK_ID) from bd_product_bas_specs pbs
        where pbs.PK_PRODUCT_BAS = pb.pk_id and pbs.del_flag = 0
        ) > 0
        <if test="pkCountry != null">
            and bp.PK_COUNTRY = #{pkCountry}
        </if>
        <if test="query != null and query != ''">
            and ( pb.PRODUCT_CODE like '%' || #{query} || '%' or pb.PRODUCT_NAME like '%' || #{query} || '%' )
        </if>
    </select>

    <select id="selectByList" resultMap="BdProductBasExt">
        select pb.*,po.PK_ID PK_PRODUCT
        from BD_PRODUCT_BAS pb
        left join bd_product po
        on po.PK_PRODUCT_BAS = pb.pk_id
        where pb.del_flag = 0
        <if test="systemType != null">
            AND pb.SYSTEM_TYPE = #{systemType}
        </if>
        <if test="productCode != null and productCode !='' ">
            AND pb.PRODUCT_CODE like #{productCode}||'%'
        </if>
        <if test="productName != null and productName !='' ">
            AND pb.PRODUCT_NAME like #{productName}||'%'
        </if>
        <if test="startCreateDate != null">
            and pb.CREATION_TIME &gt;= #{startCreateDate}
        </if>
        <if test="endCreateDate != null">
            and pb.CREATION_TIME &lt;= #{endCreateDate}
        </if>
        order by pb.CREATION_TIME desc
    </select>

    <select id="selectbypkSupplierList" resultMap="BdProductBasVo">
        select pb.*
        from BD_PRODUCT_BAS pb
                 inner join BD_PRODUCT bp on bp.PK_PRODUCT_BAS = pb.PK_ID
                 inner join BD_PRODUCT_EXTEND be on be.PK_PRODUCT = bp.pk_id
        where pb.del_flag = 0
          and bp.DEL_FLAG = 0
          and bp.PK_SUPPLIER = #{pkSupplier}
          and be.IS_PUT_ON = 0
          and (
                  select count(pbs.PK_ID)
                  from bd_product_bas_specs pbs
                  where pbs.PK_PRODUCT_BAS = pb.pk_id
                    and pbs.del_flag = 0
              ) > 0
    </select>

    <select id="selectbyNotSupplierList" resultMap="BdProductBasVo">
        select pb.*
        from BD_PRODUCT_BAS pb
                 inner join BD_PRODUCT bp on bp.PK_PRODUCT_BAS = pb.PK_ID
                 inner join BD_PRODUCT_EXTEND be on be.PK_PRODUCT = bp.pk_id
        where pb.del_flag = 0
          and bp.DEL_FLAG = 0
          and be.IS_PUT_ON = 0
          and (
                  select count(pbs.PK_ID)
                  from bd_product_bas_specs pbs
                  where pbs.PK_PRODUCT_BAS = pb.pk_id
                    and pbs.del_flag = 0
              ) > 0
          and bp.PK_SUPPLIER is null
    </select>

    <select id="queryProductBasAllList" resultMap="BdProductBasVo">
        select
        pb.*,bu.NAME unitStr
        from BD_PRODUCT_BAS pb
        left join bd_unit bu on bu.PK_ID = pb.UNIT
        where pb.DEL_FLAG = 0
        <if test="systemType != null">
            and pb.system_type = #{systemType}
        </if>
        and (
        select count(pbs.PK_ID) from bd_product_bas_specs pbs
        where pbs.PK_PRODUCT_BAS = pb.pk_id and pbs.del_flag = 0
        ) > 0
        <if test="query != null and query != ''">
            and ( pb.PRODUCT_CODE like #{query} || '%' or pb.PRODUCT_NAME like #{query} || '%' )
        </if>
        order by pb.CREATION_TIME desc
    </select>

</mapper>
