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

    <!-- 通用查询映射结果 -->
    <resultMap id="SaOrder" type="com.hzs.common.domain.sale.order.SaOrder">
        <id column="PK_ID" property="pkId"/>
        <result column="ORDER_CODE" property="orderCode"/>
        <result column="ORDER_TYPE" property="orderType"/>
        <result column="PK_RATE" property="pkRate"/>
        <result column="ORDER_AMOUNT" property="orderAmount"/>
        <result column="ORDER_ACHIEVE" property="orderAchieve"/>
        <result column="POSTAGE" property="postage"/>
        <result column="PK_MEMBER" property="pkMember"/>
        <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="PK_REFERENCE" property="pkReference"/>
        <result column="PK_PLACEMENT" property="pkPlacement"/>
        <result column="TRAN_TYPE" property="tranType"/>
        <result column="DELIVERY_WAY" property="deliveryWay"/>
        <result column="ORDER_STATUS" property="orderStatus"/>
        <result column="PK_VERTEX" property="pkVertex"/>
        <result column="PAY_TYPE" property="payType"/>
        <result column="DEL_FLAG" property="delFlag"/>
        <result column="PK_CREATOR" property="pkCreator"/>
        <result column="CREATION_TIME" property="creationTime"/>
        <result column="PK_MODIFIED" property="pkModified"/>
        <result column="MODIFIED_TIME" property="modifiedTime"/>
        <result column="PK_COUNTRY" property="pkCountry"/>
        <result column="PK_MEMBER_STORE" property="pkMemberStore"/>
        <result column="PK_STOREHOUSE" property="pkStorehouse"/>
        <result column="PK_MEMBER_STORE" property="pkMemberStore"/>
        <result column="PK_ORIGINAL_ORDER" property="pkOriginalOrder"/>
        <result column="UPLOAD_ACHIEVE" property="uploadAchieve"/>
        <result column="ORDER_TYPE_EXTEND" property="orderTypeExtend"/>
        <result column="CONSUME_AMOUNT" property="consumeAmount"/>
        <result column="SYSTEM_TYPE" property="systemType"/>
        <result column="BOX_NUM" property="boxNum"/>
    </resultMap>

    <!-- 订单+详情返回映射 -->
    <resultMap id="saOrderExtResultMap" type="com.hzs.common.domain.sale.ext.SaOrderExt" extends="SaOrder">
        <result column="MEMBER_CODE" property="memberCode"/>
        <result column="MEMBER_NAME" property="memberName"/>
        <result column="CREATOR_CODE" property="creatorCode"/>
        <result column="PK_SETTLE_COUNTRY" property="pkSettleCountry"/>
        <result column="PK_CENTER_CODE" property="pkCenterCode"/>
        <result column="PK_STORE_LEVEL" property="pkStoreLevel"/>
        <result column="IN_EXCHANGE_RATE" property="inExchangeRate"/>
        <result column="PK_CORP" property="pkCorp"/>
        <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="PK_PRODUCT" property="pkProduct"/>
            <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="WARES_QUANTITY" property="waresQuantity"/>
            <result column="PRICE" property="price"/>
            <result column="ACHIEVEMENT" property="achievement"/>
            <result column="ASS_ACHIEVEMENT" property="assAchievement"/>
            <result column="ORDER_STATUS" property="orderStatus"/>
            <result column="IS_GIFT" property="isGift"/>
            <result column="PK_ORDER" property="pkOrder"/>
            <result column="PK_WARES" property="pkWares"/>
            <result column="PK_WARES_DETAIL" property="pkWaresDetail"/>
            <result column="PK_WARES_SPECS_SKU" property="pkWaresSpecsSku"/>
            <result column="PK_GRADE" property="pkGrade"/>
            <result column="PK_AWARDS" property="pkAwards"/>
            <result column="EXCHANGE_GOODS" property="exchangeGoods"/>
            <result column="IS_MAKER_GIFT" property="isMakerGift"/>
            <result column="EDIT_FLAG" property="editFlag"/>
            <result column="WARES_CODE" property="waresCode"/>
            <result column="MAKER_INCOME" property="makerIncome"/>
            <result column="ITEM_PK_STOREHOUSE" property="pkStorehouse"/>
            <result column="USE_PICK" property="usePick"/>
        </collection>
        <collection property="orderBonusList" ofType="com.hzs.common.domain.sale.order.SaOrderBonus">
            <result column="PK_BONUS_ORDER" property="pkOrder"/>
            <result column="PK_BONUS_ITEMS" property="pkBonusItems"/>
        </collection>
    </resultMap>

    <select id="querySaOrderByTime" resultMap="saOrderExtResultMap">
        select od.*, bc.in_exchange_rate
        from (
        select so.pk_id,
        so.pk_member,
        so.order_code,
        so.order_type,
        so.pk_rate,
        so.order_amount,
        so.order_achieve,
        so.order_ass_achieve,
        so.upload_achieve,
        so.del_flag,
        so.pk_reference,
        so.pk_vertex,
        so.pk_country,
        so.pay_time,
        nvl(so.order_type_extend,0) order_type_extend,
        so.pk_creator,
        so.consume_amount,
        so.box_num,
        so.rec_province,
        so.rec_city,
        so.rec_county,
        st.pk_id items_id,
        st.pk_order,
        st.pk_product,
        st.pk_wares,
        st.price,
        st.achievement,
        st.quantity,
        st.pk_grade,
        st.pk_awards,
        st.pk_wares_specs_sku,
        st.pk_wares_detail,
        st.wares_quantity,
        st.is_gift,
        st.specs_name,
        st.ass_achievement,
        st.is_maker_gift,
        st.USE_PICK,
        0 channel,
        sb.pk_order pk_bonus_order,
        bt.bonus_value pk_bonus_items,
        bw.wares_code
        from sa_order so
        inner join sa_order_items st
        on so.pk_id = st.pk_order
        left join sa_order_bonus sb
        on st.pk_id = sb.pk_order
        left join bd_bonus_items bt
        on bt.pk_id = sb.pk_bonus_items and bt.del_flag=0
        left join bd_wares bw
        on bw.pk_id=st.pk_wares and bw.del_flag=0
        where so.order_status = 1
        <if test="orderCode != null and orderCode != ''">
            and so.order_code=#{orderCode}
        </if>
        and so.pay_time >= #{startDate}
        and so.pay_time &lt; #{endDate}
        ) od
        left join bd_currency bc
        on od.pk_rate = bc.pk_id
        order by pay_time
    </select>

</mapper>
