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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.hzs.common.domain.scm.th.ThProduct">
        <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="PK_COUNTRY" property="pkCountry" />
        <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_SUPPLIER" property="pkSupplier" />
        <result column="SUPPLY_PRICE" property="supplyPrice" />
        <result column="RETAIL_PRICE" property="retailPrice" />
        <result column="SUPPLY_QUANTITY" property="supplyQuantity" />
        <result column="SALE_MODEL" property="saleModel" />
    </resultMap>

    <resultMap id="resultMap" type="com.hzs.ground.product.vo.ProductVo">
        <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="PK_COUNTRY" property="pkCountry" />
        <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_SUPPLIER" property="pkSupplier" />
        <result column="SUPPLY_PRICE" property="supplyPrice" />
        <result column="RETAIL_PRICE" property="retailPrice" />
        <result column="SUPPLY_QUANTITY" property="supplyQuantity" />
        <result column="SALE_MODEL" property="saleModel" />
        <result column="unitVal" property="unitVal" />
        <result column="productSpecType" property="productSpecType" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        CREATION_TIME,
        MODIFIED_TIME,
        DEL_FLAG,
        PK_CREATOR,
        PK_MODIFIED,
        PK_COUNTRY,
        PK_ID, PRODUCT_CODE, PRODUCT_NAME, PK_CLASSIFY, UNIT, WEIGHT, LENGTH, WIDTH, HEIGHT, COVER, SHELF_LIFE, PK_SUPPLIER, SUPPLY_PRICE, RETAIL_PRICE, SUPPLY_QUANTITY, SALE_MODEL
    </sql>

    <select id="queryByList" resultMap="resultMap">
        select
        bp.*,bu.NAME unitVal ,
        (select REPLACE(wm_concat(st.TYPE_NAME),',','/') from th_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

        from th_PRODUCT bp
        left join th_PRODUCT_EXTEND pe on pe.PK_PRODUCT = bp.PK_ID
        left join bd_unit bu on bu.PK_ID = bp.UNIT
        where 1 = 1 and bp.DEL_FLAG = '0'
        <if test="pkSupplier != null">
            and bp.PK_SUPPLIER = #{pkSupplier}
        </if>
        <if test="pkCountry != null">
            and bp.PK_COUNTRY = #{pkCountry}
        </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="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="queryProductApplyByProduct" resultType="com.hzs.common.domain.scm.th.ThProduct">
        select * from bd_product bd
          inner join bd_product_bas ba
                     on bd.pk_product_bas=ba.pk_id
          inner join th_product tp
                     on tp.product_code=ba.product_code
        where bd.del_flag=0 and ba.del_flag=0
          and tp.del_flag=0
    </select>

    <select id="getProductInfoById" resultType="com.hzs.ground.product.vo.ProductVo">
       select bp.*,su.NICK_NAME supplierName   from TH_PRODUCT  bp
       left join sys_user su on su.user_id = bp.PK_CREATOR
        where bp.pk_id = #{productId}

    </select>

</mapper>
