<?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.scm.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.scm.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" />
        <collection property="productBasSpecsVoList" ofType="com.hzs.scm.product.vo.BdProductBasSpecsVo">
            <result column="PK_SPECS_TYPE" property="pkSpecsType" />
            <result column="SPECS_TYPE_NAME" property="specsTypeName" />
            <result column="PK_SPECS" property="pkSpecs" />
            <result column="NAME" property="name" />
        </collection>
    </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 bd
            on bp.pk_id=bd.pk_product_bas and bd.pk_country=#{pkCountry}
            inner join bd_product_specs bs
            on bd.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 bs.del_flag=0 and bc.del_flag=0
            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.PK_ID,
        pb.PRODUCT_CODE,
        pb.PRODUCT_NAME,
        pb.PK_CLASSIFY,

        pb.WEIGHT   ,
        pb.LENGTH  ,
        pb.WIDTH ,
        pb.HEIGHT ,
        pb.COVER      ,
        pb.SHELF_LIFE ,
        pb.CREATION_TIME ,

        bu.NAME unitStr,bp.unit

        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 1 = 1 and pb.DEL_FLAG = 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_COUNTRY = #{pkCountry}

<!--        <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 distinct ba.pk_id,ba.product_code,ba.product_name,
         bp.unit,bu.name unitStr,bcs.pk_specs_type,bt.type_name specs_type_name,bs.pk_specs,bcs.specs_name name
         from bd_product_bas ba
         inner join bd_product bp
         on ba.pk_id=bp.pk_product_bas
         inner join bd_wares_detail bd
         on bd.pk_product=bp.pk_id
         inner join bd_wares_specs bs
         on bd.pk_id = bs.pk_wares_detail
         left join bd_specs bcs
         on bcs.pk_id=bs.pk_specs and bcs.del_flag=0
         left join bd_specs_type bt
         on bt.pk_id= bs.pk_specs_type and bt.del_flag=0
         left join bd_unit bu
         on bu.pk_id=ba.unit and bu.del_flag=0
         where ba.del_flag=0 and bp.del_flag=0
         and bd.del_flag=0 and bs.del_flag=0
         and ba.product_code=#{productCode}
           and bp.pk_country=#{pkCountry}
         order by bcs.pk_specs_type
     </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
        where 1 = 1 and pb.DEL_FLAG = 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="selectbypkSupplierList" resultMap="BdProductBasExt">

        select pb.* from BD_PRODUCT_BAS pb
        inner join BD_PRODUCT bp on bp.PK_PRODUCT_BAS = pb.PK_ID
        where pb.del_flag = 0 and bp.DEL_FLAG = 0


    </select>

    <select id="selectbyNotSupplierList" resultMap="BdProductBasExt">
        select pb.* from BD_PRODUCT_BAS pb
        inner join BD_PRODUCT bp on bp.PK_PRODUCT_BAS = pb.PK_ID
        where pb.del_flag = 0 and bp.DEL_FLAG = 0
        <if test="query != null and query != ''">
            and (pb.PRODUCT_CODE like #{query} || '%'  OR  pb.PRODUCT_NAME like #{query} || '%')
        </if>
    </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 1 = 1 and pb.DEL_FLAG = 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="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>
