<?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.order.mapper.SupplierOrderMapper">

    <resultMap id="saOrderExtResultMap" type="com.hzs.common.domain.sale.ext.SaOrderExt">
        <result column="PK_ID" property="pkId" />
        <result column="ORDER_CODE" property="orderCode"/>
        <result column="REC_NAME" property="recName"/>
        <result column="REC_PHONE" property="recPhone"/>
        <result column="REC_PROVINCE" property="recProvince"/>
        <result column="REC_CITY" property="recCity"/>
        <result column="REC_COUNTY" property="recCounty"/>
        <result column="REC_ADDRESS" property="recAddress"/>
        <result column="PAY_TIME" property="payTime"/>
        <result column="REMARK" property="remark"/>
        <result column="ORDER_STATUS" property="orderStatus"/>
        <collection property="orderItemsExtList" ofType="com.hzs.common.domain.sale.ext.SaOrderItemsExt">
            <result column="ITEMS_ID" property="pkId"/>
            <result column="PK_UNIT" property="pkUnit" />
            <result column="PRODUCT_NAME" property="productName"/>
            <result column="PRODUCT_CODE" property="productCode"/>
            <result column="COVER" property="cover"/>
            <result column="WEIGHT" property="weight"/>
            <result column="SPECS_NAME" property="specsName"/>
            <result column="SPECS_NAME_ID" property="specsNameId"/>
            <result column="QUANTITY" property="quantity"/>
            <result column="ORDER_STATUS" property="orderStatus"/>
            <result column="PK_WARES" property="pkWares"/>
            <result column="PK_WARES_DETAIL" property="pkWaresDetail"/>
            <result column="PK_WARES_SPECS_SKU" property="pkWaresSpecsSku"/>
            <result column="SHIPPING_CHANNEL" property="shippingChannel" />
            <result column="PK_SUPPLIER" property="pkSupplier" />
            <result column="B_ORDER_STATUS" property="orderStatus" />
            <result column="LOGISTICS_CODE" property="logisticsCode"/>
            <result column="LOGISTICS_COMPANY" property="logisticsCompany"/>
            <result column="SUPPLY_PRICE" property="supplyPrice" />
        </collection>
    </resultMap>
    <select id="querySaOrderByCondition" resultMap="saOrderExtResultMap">
        select so.pk_id,so.order_code,so.rec_name,so.rec_phone,so.rec_province,so.rec_city,
        so.rec_county,so.rec_address,so.pay_time,so.order_status,
        st.pk_id items_id,st.pk_order,st.pk_product,st.pk_wares,
        st.quantity,st.pk_wares_specs_sku,st.pk_wares_detail,st.is_gift,
        st.specs_name,st.order_status b_order_status,st.logistics_code,st.logistics_company,
        st.is_maker_gift,b.supply_price,b.product_code,b.product_name,
        pr.name rec_province_val,ci.name rec_city_val,co.name rec_county_val
        from sa_order so
        inner join sa_order_items st
        on so.pk_id = st.pk_order
        left join(
            select tp.supply_price,bd.product_code,bd.product_name,bd.pk_id 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
        ) b
        on st.pk_product=b.pk_id
        left join bd_area pr on pr.pk_id=so.rec_province
        left join bd_area ci on ci.pk_id=so.rec_city
        left join bd_area co on co.pk_id=so.rec_county
        where so.order_status = 1 and so.del_flag=0 and st.del_flag=0 and st.pk_supplier is not null
        <if test="orderQueryParam.pkSupplier != null">
            and st.pk_supplier=#{orderQueryParam.pkSupplier}
        </if>
        <if test="orderQueryParam.orderCode != null and orderQueryParam.orderCode != ''">
            and so.order_code=#{orderQueryParam.orderCode}
        </if>
        <if test="orderQueryParam.recName != null and orderQueryParam.recName != ''">
          and so.rec_name=#{orderQueryParam.recName}
        </if>
        <if test="orderQueryParam.recPhone != null and orderQueryParam.recPhone != ''">
            and so.rec_phone=#{orderQueryParam.recPhone}
        </if>
        <if test="orderQueryParam.startTime != null">
            and so.pay_time >= #{orderQueryParam.startTime}
        </if>
        <if test="orderQueryParam.endTime != null">
            and so.pay_time &lt; #{orderQueryParam.endTime}
        </if>
        <if test="orderQueryParam.productCode != null and orderQueryParam.productCode != ''">
            and b.product_code=#{orderQueryParam.productCode}
        </if>
        <if test="orderQueryParam.productName != null and orderQueryParam.productName != ''">
            and b.product_name=#{orderQueryParam.productName}
        </if>
        <if test="orderQueryParam.orderStatus != null">
            and so.order_status=#{orderQueryParam.orderStatus}
        </if>
        <if test="orderQueryParam.orderItemsParamList != null and orderQueryParam.orderItemsParamList.size >0">
            and st.pk_id in
            <foreach item="item" collection="orderQueryParam.orderItemsParamList" open="(" separator="," close=")">
                #{item.pkId}
            </foreach>
        </if>
        order by so.pay_time desc
    </select>
</mapper>
