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

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