<?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.store.mapper.IcStoreOutHMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="IcStoreOutHExt" type="com.hzs.common.domain.scm.store.ext.IcStoreOutHExt">
        <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="PK_CORP" property="pkCorp" />
        <result column="TOTAL_NUM" property="totalNum" />
        <result column="ORDER_CODE" property="orderCode" />
        <result column="PK_SEND_STORE" property="pkSendStore" />
        <result column="PK_ACCEPT_STORE" property="pkAcceptStore" />
        <result column="PK_MEMBER" property="pkMember" />
        <result column="REMARK" property="remark" />
        <result column="REC_ADDRESS" property="recAddress" />
        <result column="PAY_TIME" property="payTime" />
        <result column="REC_NAME" property="recName" />
        <result column="REC_PHONE" property="recPhone" />
        <result column="REC_PROVINCE_NAME" property="recProvinceName" />
        <result column="REC_CITY_NAME" property="recCityName" />
        <result column="STORE_NAME" property="storeName" />
        <result column="BONUS_RATIO" property="bonusRatio" />
        <collection property="storeOutBiExtList" ofType="com.hzs.common.domain.scm.store.ext.IcStoreOutBExt">
            <result column="ITEMS_ID" property="pkId"/>
            <result column="PK_STOREOUT_H" property="pkStoreoutH" />
            <result column="PRODUCT_NAME" property="productName"/>
            <result column="PRODUCT_CODE" property="productCode"/>
            <result column="COVER" property="cover"/>
            <result column="DEL_FLAG" property="delFlag" />
            <result column="PK_PRODUCT" property="pkProduct" />
            <result column="PK_WARES" property="pkWares" />
            <result column="QUANTITY" property="quantity" />
            <result column="ORDER_STATUS" property="orderStatus" />
            <result column="PK_WARES_SPECS_SKU" property="pkWaresSpecsSku" />
            <result column="PK_WARES_DETAIL" property="pkWaresDetail" />
            <result column="SPECS_NAME" property="specsName" />
            <result column="SPECS_NAME_ID" property="specsNameId" />
            <result column="WARES_QUANTITY" property="waresQuantity" />
            <result column="pk_product_bas" property="pkProductBas" />
            <result column="pk_unit" property="pkUnit" />
            <result column="pk_order" property="pkOrder" />
            <result column="pk_order_items" property="pkOrderItems" />
            <result column="pk_source" property="pkSource" />
            <result column="pk_source_b" property="pkSourceB" />
        </collection>
    </resultMap>
    <select id="queryStoreOutByCondition" resultMap="IcStoreOutHExt">
        select a.*,cs.store_name from (
        select ih.pk_country,ib.pk_storeout_h,ib.pk_id
        items_id,so.order_code,so.pay_time,so.rec_address,so.rec_phone,so.rec_name,bn.name unitName,province.name
        rec_province_name,
        city.name rec_city_name,bp.cover,bp.product_code,bp.product_name,ib.quantity,
        ib.pk_order,ib.pk_order_items,ib.pk_source,ib.pk_source_b,
        ib.specs_name,ib.specs_name_id,ih.pk_accept_store pk_send_store,ib.pk_product,bp.pk_product_bas,bp.unit pk_unit,
        (case when so.pk_member_store!=ih.pk_accept_store then so.pk_member_store end) pk_accept_store,
        (case when so.pk_member_store=ih.pk_accept_store then so.pk_member end) pk_member,
        nvl(ssb.bonus_ratio,0) bonus_ratio from ic_storeout_h ih
        inner join ic_storeout_b ib
        on ih.pk_id=ib.pk_storeout_h
        inner join sa_order so
        on so.pk_id=ib.pk_order
        inner join bd_product bp
        on bp.pk_id=ib.pk_product and bp.product_attr = 1
        left join sa_store_bonus ssb
        on ssb.pk_order=so.pk_id and ssb.del_flag=0 and ssb.pk_member_store=#{orderQueryParam.pkCuStore}
        left join bd_unit bn
        on bn.pk_id=bp.unit and bn.del_flag=0
        left join bd_area province on province.PK_ID=so.rec_province
        left join bd_area city on city.PK_ID=so.rec_city
        where ih.del_flag=0 and ib.del_flag=0
        and ih.pk_country=#{orderQueryParam.pkCountry} and ih.pk_accept_store=#{orderQueryParam.pkCuStore}
        <if test="orderQueryParam.orderType != null">and so.ORDER_TYPE= #{orderQueryParam.orderType}
        </if>
        <if test="orderQueryParam.orderStatus != null">and ib.order_status= #{orderQueryParam.orderStatus}
        </if>
        <if test="orderQueryParam.itemIdList != null and orderQueryParam.itemIdList.size >0">
            and ib.pk_id in
            <foreach collection="orderQueryParam.itemIdList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="orderQueryParam.orderCode != null and orderQueryParam.orderCode != ''">
            and so.order_code like #{orderQueryParam.orderCode} || '%'
        </if>
        <if test="orderQueryParam.recName != null and orderQueryParam.recName !=''">
            and so.rec_name like #{orderQueryParam.recName} || '%'
        </if>
        <if test="orderQueryParam.recPhone != null and orderQueryParam.recPhone != ''">
            and so.rec_phone like #{orderQueryParam.recPhone} || '%'
        </if>
        <if test="orderQueryParam.startTime != null and orderQueryParam.endTime != null">
            and so.pay_time >= #{orderQueryParam.startTime}
            and #{orderQueryParam.endTime} >= so.pay_time
        </if>
            order by so.pay_time desc
        )  a
        left join cu_store cs
        on a.pk_accept_store = cs.pk_id and cs.del_flag=0
    </select>

</mapper>
