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

    <!-- 通用查询映射结果 -->
    <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_STOREHOUSE" property="pkStorehouse"/>
        <result column="PK_ORIGINAL_ORDER" property="pkOriginalOrder"/>
        <result column="UPLOAD_ACHIEVE" property="uploadAchieve"/>
    </resultMap>

    <!-- 订单VO查询映射结果 -->
    <resultMap id="SaOrderVO" type="com.hzs.sale.order.vo.SaOrderVO">
        <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="ORDER_ASS_ACHIEVE" property="orderAssAchieve"/>
        <result column="POSTAGE" property="postage"/>
        <result column="MEMBER_CODE" property="memberCode"/>
        <result column="MEMBER_NAME" property="memberName"/>
        <result column="REFERENCE" property="reference"/>
        <result column="REFERENCE_NAME" property="referenceName"/>
        <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="TRAN_TYPE" property="tranType"/>
        <result column="DELIVERY_WAY" property="deliveryWay"/>
        <result column="VERTEX" property="vertex"/>
        <result column="VERTEX_NAME" property="vertexName"/>
        <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"/>
    </resultMap>

    <!-- 打印订单映射map -->
    <resultMap id="orderPrintMap" type="com.hzs.sale.order.vo.MyOrderPrintVO">
        <result column="member_code" property="memberCode"/>
        <result column="member_name" property="memberName"/>
        <result column="phone" property="phone"/>
        <result column="order_code" property="orderCode"/>
        <result column="pay_time" property="payTime"/>
        <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"/>
        <collection property="detailList" ofType="com.hzs.sale.order.vo.MyOrderPrintDetailVO">
            <result column="product_name" property="productName"/>
            <result column="quantity" property="quantity"/>
            <result column="price" property="price"/>
            <result column="achievement" property="achievement"/>
            <result column="ass_achievement" property="assAchievement"/>
        </collection>
    </resultMap>

    <!-- 订单+详情返回映射 -->
    <resultMap id="saOrderExtResultMap" type="com.hzs.common.domain.sale.ext.SaOrderExt" extends="SaOrder">
        <result column="pk_id" property="pkId"/>
        <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="STORE_MEMBER" property="storeMember" />
        <result column="RATIO" property="ratio" />
        <result column="LEVEL_VALUE" property="levelValue" />
        <result column="store_level_name" property="storeLevelName" />
        <result column="store_province" property="storeProvince" />
        <result column="rec_province_name" property="recProvinceName" />
        <result column="rec_city_name" property="recCityName" />
        <result column="rec_county_name" property="recCountyName" />
        <result column="store_name" property="storeName" />
        <result column="GRANT_STATUS" property="grantStatus" />
        <result column="PAY_MEMBER_CODE" property="payMemberCode" />
        <result column="PAY_MEMBER_NAME" property="payMemberName" />
        <collection property="orderItemsExtList" ofType="com.hzs.common.domain.sale.ext.SaOrderItemsExt">
            <result column="ITEMS_ID" property="pkId"/>
            <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="CHANNEL" property="channel"/>
            <result column="IS_MAKER_GIFT" property="isMakerGift"/>
        </collection>
    </resultMap>

    <select id="queryTotalOrderAchieve" resultMap="SaOrderVO">
        select member_code, sum(order_amount) order_amount, sum(order_achieve) order_achieve
        from sa_order
        where del_flag = 0
          and ORDER_STATUS = 0
          and member_code = #{memberCode}
        group by member_code
    </select>

    <select id="querySaOrderByCondition" resultMap="SaOrderVO">
        select so.order_code,so.order_type,so.pk_rate,so.order_amount,so.order_achieve,so.postage,
        cm.member_code,cm.member_name,par.member_code REFERENCE,par.member_name referenceName,so.rec_name,so.rec_phone,
        so.rec_province,so.rec_city,so.rec_county,so.rec_address,so.pay_time,so.remark,
        so.tran_type,so.delivery_way,ve.member_code vertex,ve.member_name vertexName from sa_order so
        inner join cu_member cm
        on so.pk_member = cm.pk_id
        left join cu_member par
        on so.pk_reference = par.pk_id
        left join cu_member ve
        on so.pk_vertex = ve.pk_id
        where so.del_flag=0 and cm.del_flag=0
        and so.pk_country = #{pkCountry}
        <if test="orderStatus != null">
            and so.order_status=#{orderStatus}
        </if>
        <if test="orderCode != null and orderCode != ''">
            and so.order_code like #{orderCode} || '%'
        </if>
        <if test="memberCode != null and memberCode != ''">
            and cm.member_code like #{memberCode} || '%'
        </if>
        <if test="memberName != null and memberName != ''">
            and cm.member_name like #{memberName} || '%'
        </if>
        <if test="parentCode != null and parentCode != ''">
            and par.member_code like #{parentCode} || '%'
        </if>
        <if test="parentName != null and parentName != ''">
            and par.member_name like #{parentName} || '%'
        </if>
        <if test="orderType != null">
            and so.order_type = #{orderType}
        </if>
        <if test="recName != null and recName !=''">
            and so.rec_name like #{recName} || '%'
        </if>
        <if test="recPhone != null and recPhone != ''">
            and so.rec_phone like #{recPhone} || '%'
        </if>
        <if test="startTime != null and endTime != ''">
            and so.pay_time >= #{startTime}
            and so.pay_time &lt; #{endTime}
        </if>
        <if test="tranType != null">
            and so.tran_type = #{tranType}
        </if>
    </select>

    <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.upload_achieve,
        so.del_flag,
        so.pk_reference,
        so.pk_vertex,
        so.pk_country,
        so.pay_time,
        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
        from sa_order so
        inner join sa_order_items st
        on so.pk_id = st.pk_order
        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 and bc.del_flag=0
        order by pay_time
    </select>

    <select id="querySaOrderByEffectiveDay" 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.upload_achieve,
        so.del_flag,
        so.pk_reference,
        so.pk_vertex,
        so.pk_country,
        so.pay_time,
        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
        from sa_order so
        inner join sa_order_items st
        on so.pk_id = st.pk_order
        inner join (select * from bd_system_config where del_flag=0 and key ='EFFECTIVE_DAYS_1') bs
        on so.pk_country=bs.pk_country
        where so.order_status = 1 and so.del_flag=0 and so.order_type=31
        and so.pay_time >= #{startDate} -bs.value
        and so.pay_time &lt; #{endDate} -bs.value
        ) od
        left join bd_currency bc
        on od.pk_rate = bc.pk_id and bc.del_flag=0
        order by pay_time
    </select>

    <select id="queryDeliverySaOrderByDay" resultMap="saOrderExtResultMap">
        select od.*, bc.in_exchange_rate
        from (
        select so.*,
        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,ts.province store_province,ts.pk_member store_member,bd.ratio,bl.level_value,bl.store_level_name
        from sa_order so
         inner join sa_order_items st
                    on so.pk_id = st.pk_order
         inner join cu_store ts
                    on ts.pk_id=so.pk_member_store
         inner join bd_store_level bl
                    on ts.pk_store_level=bl.pk_id
         inner join bd_store_level_detail bd
                    on bl.pk_id=bd.pk_store_level
        where so.del_flag=0 and st.del_flag=0 and ts.del_flag=0 and bl.del_flag=0 and bd.del_flag=0
          and so.order_status = 1 and st.order_status>=1 and so.order_type in(1,2)
          and delivery_way in(3,4)
        and so.pay_time >= #{startDate}
        and so.pay_time &lt; #{endDate}) od
        left join bd_currency bc
        on od.pk_rate = bc.pk_id and bc.del_flag=0
        order by pay_time
    </select>

    <!-- 批量更新订单发货方式 -->
    <update id="updateDeliveryWayBatch">
        update SA_ORDER so
        set so.DELIVERY_WAY = #{deliveryWay},
        so.PK_MODIFIED = #{userId},
        so.MODIFIED_TIME = sysdate
        where so.PK_ID in
        <foreach collection="idList" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
    </update>

    <select id="selectSum" resultType="decimal">
        select ifnull(sum(ORDER_AMOUNT), 0)
        from SA_ORDER
        where DEL_FLAG = '0'
          and ORDER_STATUS = '1'
    </select>

    <!-- 查询订单相关字段 -->
    <sql id="orderSelect">
        so
        .
        *
        ,
        cm.member_code,
        cm.member_name,
        cm.pk_settle_country,
        soi.pk_id items_id,
        soi.pk_product,
        bp.product_name,
        bp.product_code,
        bp.cover,
        bp.weight,
        soi.specs_name,
        soi.specs_name_id,
        soi.quantity,
        soi.price,
        soi.achievement,
        soi.ass_achievement,
        soi.order_status,
        soi.is_gift,
        bc.in_exchange_rate
    </sql>

    <!-- 根据订单id查询订单 -->
    <select id="queryOrderByPkId" resultMap="saOrderExtResultMap">
        select
        <include refid="orderSelect"/>
        from sa_order so
        inner join cu_member cm
        on cm.pk_id = so.pk_member
        left join sa_order_items soi
        on soi.pk_order = so.pk_id
        and soi.del_flag = 0
        inner join bd_product bp
        on bp.pk_id = soi.pk_product
        inner join bd_currency bc
        on bc.pk_id = so.pk_rate
        where so.del_flag = 0
        and so.pk_id = #{pkId}
        <if test="null != pkCountry">
            and so.pk_country = #{pkCountry}
        </if>
    </select>

    <!-- 根据订单编号查询订单 -->
    <select id="queryOrderByCode" resultMap="saOrderExtResultMap">
        select
        <include refid="orderSelect"/>
        from sa_order so
        inner join cu_member cm
        on cm.pk_id = so.pk_member
        left join sa_order_items soi
        on soi.pk_order = so.pk_id
        and soi.del_flag = 0
        inner join bd_product bp
        on bp.pk_id = soi.pk_product
        inner join bd_currency bc
        on bc.pk_id = so.pk_rate
        where so.del_flag = 0
        and so.order_code = #{orderCode}
        <if test="null != pkCountry">
            and so.pk_country = #{pkCountry}
        </if>
    </select>

    <!-- 根据订单编号查询订单以及详情 -->
    <select id="queryOrderListByCodeList" resultMap="saOrderExtResultMap">
        select
        <include refid="orderSelect"/>
        from sa_order so
        inner join cu_member cm
        on cm.pk_id = so.pk_member
        left join sa_order_items soi
        on soi.pk_order = so.pk_id
        and soi.del_flag = 0
        inner join bd_product bp
        on bp.pk_id = soi.pk_product
        inner join bd_currency bc
        on bc.pk_id = so.pk_rate
        where so.del_flag = 0
        and so.order_code in
        <foreach collection="orderCodeList" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        <if test="null != pkCountry">
            and so.pk_country = #{pkCountry}
        </if>
        order by so.order_code desc
    </select>

    <!-- 根据订单编号查询订单以及详情 -->
    <select id="queryOrderListByIdList" resultMap="saOrderExtResultMap">
        select
        <include refid="orderSelect"/>
        from sa_order so
        inner join cu_member cm
        on cm.pk_id = so.pk_member
        left join sa_order_items soi
        on soi.pk_order = so.pk_id
        and soi.del_flag = 0
        inner join bd_product bp
        on bp.pk_id = soi.pk_product
        inner join bd_currency bc
        on bc.pk_id = so.pk_rate
        where so.del_flag = 0
        and so.pk_id in
        <foreach collection="orderIdList" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        <if test="null != pkCountry">
            and so.pk_country = #{pkCountry}
        </if>
        order by so.order_code desc, soi.pk_id
    </select>

    <!-- 我的订单列表（前台） -->
    <select id="myOrderList" resultType="com.hzs.common.domain.sale.ext.SaOrderExt">
        <!-- 直销订单 -->
        select
        so.pk_id,
        so.order_code,
        so.creation_time,
        so.pay_time,
        so.order_type,
        so.order_amount,
        so.order_achieve,
        so.order_ass_achieve,
        so.postage,
        cm2.member_code creator_code,
        cm2.member_name creator_name,
        cm1.member_code member_code,
        cm1.member_name member_name,
        cm1.phone member_phone,
        so.order_status,
        so.rec_name,
        so.rec_phone,
        cs.PROVINCE province,
        cs.CITY city,
        cs.ADDRESS address,
        so.remark,
        so.pk_country,
        0 as merchant_type,
        bc.in_exchange_rate,
        so.DELIVERY_WAY deliveryWay,
        so.REC_PROVINCE orderRecProvince,
        so.REC_CITY orderRecCity,
        so.REC_COUNTY orderRecCounty,
        so.REC_ADDRESS orderRecAddress
        from (
        select distinct so.*
        from sa_order so
        <if test="param.orderStatus != null">
            left join sa_order_items soi
            on soi.pk_order = so.pk_id
            and soi.del_flag = 0
        </if>
        where so.del_flag = 0 and so.ORDER_TYPE != 20
        <if test="pkCountry != null">
            and so.pk_country = #{pkCountry}
        </if>
        and (so.pk_member = #{memberId} or so.pk_creator = #{memberId})
        <if test="param.orderStatus != null">
            <choose>
                <when test="param.orderStatus == 1">
                    <!-- 前台如果查询待发货，需要包括已合单 -->
                    and soi.order_status in (1, 2)
                </when>
                <otherwise>
                    and soi.order_status = #{param.orderStatus}
                </otherwise>
            </choose>
        </if>
        <if test="param.creationTimeStart != null">
            and so.creation_time >= #{param.creationTimeStart}
        </if>
        <if test="param.creationTimeEnd != null">
            and #{param.creationTimeEnd} >= so.creation_time
        </if>
        <if test="param.orderCode != null and param.orderCode != ''">
            and so.order_code like #{param.orderCode} || '%'
        </if>
        <if test="param.orderType != null">
            and so.order_type = #{param.orderType}
        </if>
        <if test="param.deliveryWay != null">
            and so.delivery_way = #{param.deliveryWay}
        </if>
        ) so
        inner join cu_member cm1
        on cm1.pk_id = so.pk_member
        inner join cu_member cm2
        on cm2.pk_id = so.pk_creator
        inner join bd_currency bc
        on bc.pk_id = so.pk_rate
        left join CU_STORE cs on cs.pk_id = so.PK_MEMBER_STORE
        <if test="param.memberCode != null and param.memberCode != ''">
            where cm1.member_code like #{param.memberCode} || '%'
        </if>
        order by creation_time desc
    </select>


    <!-- 我的订单列表（前台） -->
    <select id="myStoreOrderList" resultType="com.hzs.common.domain.sale.ext.SaOrderExt">
        select
        so.pk_id,
        so.order_code,
        so.creation_time,
        so.pay_time,
        so.order_type,
        so.order_amount,
        so.order_achieve,
        so.order_ass_achieve,
        so.postage,
        cm2.member_code creator_code,
        cm2.member_name creator_name,
        cm1.member_code member_code,
        cm1.member_name member_name,
        so.order_status,
        so.rec_name,
        so.rec_phone,
        cs.PROVINCE province,
        cs.CITY city,
        cs.ADDRESS address,
        cs.STORE_NAME storeName ,
        cs.STORE_CODE storeCode ,
        so.REC_PROVINCE orderRecProvince,
        so.REC_CITY orderRecCity,
        so.REC_COUNTY orderRecCounty,
        so.REC_ADDRESS orderRecAddress,
        so.remark,
        so.pk_country,
        0 as merchant_type,
        bc.in_exchange_rate,
        cm2.PHONE memberPhone,
        so.DELIVERY_WAY  deliveryWay,
        nvl(ssb.bonus_ratio,0) bonus_ratio
        from (
        select distinct so.*
        from sa_order so
        <if test="param.orderStatus != null">
            left join sa_order_items soi
            on soi.pk_order = so.pk_id
            and soi.del_flag = 0
        </if>
        where so.del_flag = 0 and so.ORDER_TYPE != 20
        <if test="pkCountry != null">
            and so.pk_country = #{pkCountry}
        </if>
        <if test="pkStore != null">
            and so.PK_MEMBER_STORE = #{pkStore}
        </if>

        <if test="param.orderStatus != null">
            <choose>
                <when test="param.orderStatus == 1">
                    <!-- 前台如果查询待发货，需要包括已合单 -->
                    and soi.order_status in (1, 2)
                </when>
                <otherwise>
                    and soi.order_status = #{param.orderStatus}
                </otherwise>
            </choose>
        </if>
        <if test="param.creationTimeStart != null">
            and so.creation_time >= #{param.creationTimeStart}
        </if>
        <if test="param.creationTimeEnd != null">
            and #{param.creationTimeEnd} >= so.creation_time
        </if>
        <if test="param.orderCode != null and param.orderCode != ''">
            and so.order_code like #{param.orderCode} || '%'
        </if>
        <if test="param.orderType != null">
            and so.order_type = #{param.orderType}
        </if>
        <if test="param.deliveryWay != null">
            and so.DELIVERY_WAY = #{param.deliveryWay}
        </if>
        ) so
        inner join cu_member cm1
        on cm1.pk_id = so.pk_member
        inner join cu_member cm2
        on cm2.pk_id = so.pk_creator
        inner join bd_currency bc
        on bc.pk_id = so.pk_rate
        inner join CU_STORE cs on cs.pk_id = so.PK_MEMBER_STORE
        left join sa_store_bonus ssb
        on ssb.pk_order=so.pk_id and ssb.del_flag=0 and ssb.pk_member_store=so.PK_MEMBER_STORE
        where  1 = 1 and so.order_status != 0
        <if test="param.memberCode != null and param.memberCode != ''">
             and cm1.member_code like #{param.memberCode} || '%'
        </if>
        <if test="param.storeCode != null and param.storeCode != ''">
            and cs.STORE_CODE like #{param.storeCode} || '%'
        </if>
        <if test="param.startPayTime != null">
            and so.pay_time &gt;= #{param.startPayTime}
        </if>
        <if test="param.endPayTime != null">
            and so.pay_time &lt;= #{param.startPayTime}
        </if>
        order by creation_time desc
    </select>

    <!-- 我的订单数量（前台） -->
    <select id="myOrderNum" resultType="int">
        select sum(pk_id)
        from (select count(so.pk_id) pk_id
        from sa_order so
        where del_flag = 0
        and so.pk_country = #{pkCountry}
        and (so.pk_member = #{memberId} or so.pk_creator = #{memberId})
        and exists (select 1
        from sa_order_items soi
        where soi.pk_order = so.pk_id
        and soi.del_flag = 0
        and soi.order_status in
        <foreach collection="statusList" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        )
        )
    </select>

    <!-- 订单打印（前台） -->
    <select id="orderPrint" resultMap="orderPrintMap">
        select cm.member_code,
               cm.member_name,
               cm.phone,
               so.order_code,
               so.pay_time,
               so.rec_name,
               so.rec_phone,
               so.rec_province,
               so.rec_city,
               so.rec_county,
               so.rec_address,
               bp.product_name,
               soi.quantity,
               soi.price,
               soi.achievement,
               soi.ass_achievement
        from sa_order so
                 inner join cu_member cm
                            on cm.pk_id = so.pk_member
                 left join sa_order_items soi
                           on soi.pk_order = so.pk_id
                               and soi.del_flag = 0
                 inner join BD_PRODUCT bp
                            on bp.pk_id = soi.pk_product
        where so.del_flag = 0
          and so.pk_country = #{pkCountry}
          and (so.pk_member = #{memberId} or so.pk_creator = #{memberId})
          and so.order_code = #{orderCode}
        order by bp.product_name
    </select>

    <!-- 订单业务列表 -->
    <select id="queryOrderBusinessList" resultType="com.hzs.sale.order.vo.OrderBusinessVO">
        select so.*,
        cm.member_code,
        cm.member_name,
        bp.product_name,
        soi.specs_name,
        soi.price,
        soi.achievement,
        soi.ass_achievement,
        soi.quantity,
        bc.in_exchange_rate
        from sa_order so
        inner join cu_member cm
        on cm.pk_id = so.pk_member
        left join sa_order_items soi
        on soi.pk_order = so.pk_id
        and soi.del_flag = 0
        inner join bd_product bp
        on bp.pk_id = soi.pk_product
        inner join bd_currency bc
        on bc.pk_id = so.pk_rate
        where so.del_flag = 0
        and so.pk_country = #{pkCountry}
        <!-- 订单显示的时候，待支付只有注册订单显示，其它状态不做限制 -->
        and (
        soi.order_status in (1, 2, 3, 5)
        or (soi.order_status = 0 and so.order_type in (1))
        )
        <if test="param.memberCode != null and param.memberCode != ''">
            and cm.member_code like #{param.memberCode} || '%'
        </if>
        <if test="param.memberName != null and param.memberName != ''">
            and cm.member_name like #{param.memberName} || '%'
        </if>
        <if test="param.orderCode != null and param.orderCode != ''">
            and so.order_code like #{param.orderCode} || '%'
        </if>
        <if test="param.productName != null and param.productName != ''">
            and bp.product_name like #{param.productName} || '%'
        </if>
        <if test="param.orderType != null">
            and so.order_type = #{param.orderType}
        </if>
        <if test="param.recProvince != null">
            and so.rec_province = #{param.recProvince}
        </if>
        <if test="param.recCity != null">
            and so.rec_city = #{param.recCity}
        </if>
        <if test="param.recCounty != null">
            and so.rec_county = #{param.recCounty}
        </if>
        <if test="param.recAddress != null and param.recAddress != ''">
            and so.rec_address like #{param.recAddress} || '%'
        </if>
        <if test="param.creationTime != null">
            and soi.creation_time >= #{param.creationTime[0]}
            and #{param.creationTime[1]} >= soi.creation_time
        </if>
        <if test="param.payTime != null">
            and so.pay_time >= #{param.payTime[0]}
            and #{param.payTime[1]} >= so.pay_time
        </if>
        <if test="param.orderCodeList != null and param.orderCodeList.size > 0">
            and so.order_code in
            <foreach collection="param.orderCodeList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="param.memberCodeList != null and param.memberCodeList.size > 0">
            and cm.member_code in
            <foreach collection="param.memberCodeList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <!-- 地区权限处理 -->
        <if test="param.areaScopeList != null and param.areaScopeList.size > 0">
            and so.rec_province in
            <foreach collection="param.areaScopeList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <!-- 体系权限处理 -->
        <if test="param.vertexIdList != null and param.vertexIdList.size > 0">
            and so.pk_vertex in
            <foreach collection="param.vertexIdList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        order by so.creation_time desc
    </select>

    <select id="selectByOrderInfoList" resultType="com.hzs.sale.order.vo.WaresOrderVo">
        select
        so.PK_ID pkOrder,
        so.ORDER_CODE orderCode,
        so.ORDER_TYPE orderType,
        cm.MEMBER_CODE memberCode,
        cm.MEMBER_NAME memberName,
        cm.PHONE phone,
        so.REC_NAME recName,
        so.REC_PHONE recPhone,
        cs.PROVINCE recProvince,
        cs.CITY recCity,
        cs.COUNTY recCountry,
        cs.ADDRESS recAddress,
        so.POSTAGE postage,
        so.DELIVERY_WAY deliveryWay,
        so.TRAN_TYPE tranType,
        so.ORDER_STATUS orderStatus,
        so.PAY_TYPE payType,
        so.REMARK remark,
        so.CREATION_TIME creationTime,
        so.PAY_TIME payTime,
        so.ORDER_AMOUNT orderAmount,
        so.ORDER_ACHIEVE orderAchieve,
        sor.REMARKS remarks,
        m.MEMBER_CODE buyMemberCode,
        m.MEMBER_NAME buyMemberName,
        cs.STORE_NAME storeName,
        mr.MEMBER_NAME orderParentName,
        mr.MEMBER_CODE orderParentCode

        from sa_order so
        left join CU_MEMBER cm on cm.PK_ID = so.PK_MEMBER
        left join CU_MEMBER m on m.PK_ID = so.PK_CREATOR
        left join CU_MEMBER mr on mr.PK_ID = so.PK_REFERENCE
        left join SA_ORDER_REMARKS sor on sor.PK_ORDER = so.PK_ID
        left join CU_STORE cs on cs.PK_ID = so.PK_MEMBER_STORE

        where 1 = 1 and so.DEL_FLAG = 0
        <if test="pkCountry != null">
            and so.PK_COUNTRY = #{pkCountry}
        </if>
        <if test="orderParentName != null and orderParentName != ''">
            and mr.MEMBER_NAME like #{orderParentName}||'%'
        </if>
        <if test="orderParentCode != null and orderParentCode != ''">
            and mr.MEMBER_CODE like #{orderParentCode}||'%'
        </if>
        <if test="buyMemberCode != null and buyMemberCode != ''">
            and m.MEMBER_CODE like #{buyMemberCode}||'%'
        </if>
        <if test="buyMemberName != null and buyMemberName != ''">
            and m.MEMBER_NAME like #{buyMemberName}||'%'
        </if>
        <if test="orderCode != null and orderCode != ''">
            and so.ORDER_CODE like '%' || #{orderCode}||'%'
        </if>
        <if test="memberCode != null and memberCode != ''">
            and cm.MEMBER_CODE like '%' || #{memberCode}||'%'
        </if>
        <if test="memberName != null and memberName != ''">
            and cm.MEMBER_NAME like '%' || #{memberName}||'%'
        </if>
        <if test="phone != null and phone != ''">
            and cm.PHONE like #{phone}||'%'
        </if>
        <if test="orderType != null">
            and so.ORDER_TYPE = #{orderType}
        </if>
        <if test="orderStatus != null">
            and so.ORDER_STATUS = #{orderStatus}
        </if>

        <if test="deliveryWay != null and deliveryWay != ''">
            and so.DELIVERY_WAY = #{deliveryWay}
        </if>
        <if test="tranType != null">
            and so.TRAN_TYPE = #{tranType}
        </if>
        <if test="payType != null">
            and so.PAY_TYPE = #{payType}
        </if>

        <if test="recName != null and recName != ''">
            and so.REC_NAME like '%' || #{recName} || '%'
        </if>
        <if test="storeName != null and storeName != ''">
            and cs.STORE_NAME like '%' || #{storeName} || '%'
        </if>
        <if test="recPhone != null and recPhone != ''">
            and so.REC_PHONE like '%' || #{recPhone} || '%'
        </if>
        <if test="recProvince != null">
            and so.REC_PROVINCE = #{recProvince}
        </if>
        <if test="recCity != null">
            and so.REC_CITY = #{recCity}
        </if>
        <if test="recCountry != null">
            and so.REC_COUNTY = #{recCountry}
        </if>
        <if test="recAddress != null and recAddress != ''">
            and so.REC_ADDRESS = #{recAddress}
        </if>
        <if test="startCreateDate != null">
            and so.CREATION_TIME &gt;= #{startCreateDate}
        </if>
        <if test="endCreateDate != null">
            and so.CREATION_TIME &lt;= #{endCreateDate}
        </if>
        <if test="startPayDate != null">
            and so.PAY_TIME &gt;= #{startPayDate}
        </if>
        <if test="endPayDate != null">
            and so.PAY_TIME &lt;= #{endPayDate}
        </if>
        <!-- 地区权限处理 -->
        <if test="areaScopeList != null and areaScopeList.size > 0">
            and so.rec_province in
            <foreach collection="areaScopeList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <!-- 体系权限处理 -->
        <if test="vertexIdList != null and vertexIdList.size > 0">
            and cm.pk_vertex in
            <foreach collection="vertexIdList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        order by so.creation_time desc
    </select>

    <!-- 查询订单以及会员信息 -->
    <select id="queryOrderMemberInfo" resultMap="saOrderExtResultMap">
        select so.*, cm.member_code, cm.member_name, cm.pk_center_code
        from sa_order so
        inner join cu_member cm
        on cm.pk_id = so.pk_member
        where so.del_flag = 0
        <if test="null != pkCountry">
            and so.pk_country = #{pkCountry}
        </if>
        <if test="pkMemberList != null and pkMemberList.size > 0">
            and so.pk_member in
            <foreach collection="pkMemberList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="orderTypeList != null and orderTypeList.size > 0">
            and so.order_type in
            <foreach collection="orderTypeList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="orderCodeList != null and orderCodeList.size > 0">
            and so.order_code in
            <foreach collection="orderCodeList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
    </select>

    <!-- 根据明细查询订单信息 -->
    <select id="queryOrderInfoByDetail" resultMap="SaOrder">
        select so.*
        from sa_order so
        where so.pk_id in (select so.pk_id
        from sa_order so
        left join sa_order_items soi
        on so.pk_id = soi.pk_order
        and soi.del_flag = 0
        where so.del_flag = 0
        <if test="null != pkCountry">
            and so.pk_country = #{pkCountry}
        </if>
        <if test="pkMemberList != null and pkMemberList.size > 0">
            and so.pk_member in
            <foreach collection="pkMemberList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="orderStatusList != null and orderStatusList.size > 0">
            and soi.order_status in
            <foreach collection="orderStatusList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
    </select>

    <!-- 查询选择订单列表 -->
    <select id="queryOrderSelect" resultType="com.hzs.common.domain.sale.ext.SaOrderItemsExt">
        select soi.pk_id,
        soi.pk_order,
        so.order_code,
        so.pk_member,
        cm.member_code,
        bp.product_name || '(' || soi.specs_name || ')' product_name,
        soi.quantity,
        so.rec_name,
        so.rec_phone,
        so.rec_province,
        so.rec_city,
        so.rec_county,
        so.rec_address,
        sd.logistics_code,
        sd.logistics_company
        from sa_order so
        inner join cu_member cm
        on cm.pk_id = so.pk_member
        left join sa_order_items soi
        on soi.pk_order = so.pk_id
        inner join bd_product bp
        on bp.pk_id = soi.pk_product
        left join sa_deliver_items sdi
        on sdi.pk_order = soi.pk_order
        and sdi.pk_order_items = sdi.pk_id
        left join sa_deliver sd
        on sd.pk_id = sdi.pk_deliver
        where so.del_flag = 0
        and so.pk_original_order is not null
        <if test="pkCountry != null">
            and so.pk_country = #{pkCountry}
        </if>
        <if test="param.orderCode != null and param.orderCode != ''">
            and so.order_code = #{param.orderCode}
        </if>
        <if test="param.memberCode != null and param.memberCode != ''">
            and cm.member_code like #{param.memberCode} || '%'
        </if>
        <if test="param.productName != null and param.productName != ''">
            and bp.product_name like #{param.productName} || '%'
        </if>
        <if test="param.recName != null and param.recName != ''">
            and so.rec_name = like #{param.recName} || '%'
        </if>
        <if test="param.recPhone != null and param.recPhone != ''">
            and so.rec_phone = #{param.recPhone}
        </if>
        <if test="param.recProvince != null">
            and so.rec_province = #{param.recProvince}
        </if>
        <if test="param.recCity != null">
            and so.rec_city = #{param.recCity}
        </if>
        <if test="param.recCounty != null">
            and so.rec_county = #{param.recCounty}
        </if>
        <if test="param.recAddress != null and param.recAddress != ''">
            and so.rec_address like #{param.recAddress} || '%'
        </if>
        <if test="param.logisticsCode != null and param.logisticsCode != ''">
            and sd.logistics_code like #{param.logisticsCode} || '%'
        </if>
        <if test="param.logisticsCompany != null and param.logisticsCompany != ''">
            sd.logistics_company like #{param.logisticsCompany} || '%'
        </if>
        <if test="param.pkOrderList != null and param.pkOrderList.size > 0">
            and so.pk_id in
            <foreach collection="param.pkOrderList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="param.orderCodeList != null and param.orderCodeList.size > 0">
            and so.order_code in
            <foreach collection="param.orderCodeList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
    </select>

    <update id="closeOrder">
        update sa_order
        set DEL_FLAG      = 1,
            MODIFIED_TIME = SYSDATE
        where (sysdate - interval '${data}' hour) > CREATION_TIME
          and ORDER_STATUS = 0
          and DEL_FLAG = 0
          and PK_COUNTRY = #{pkCountry}
          and ORDER_TYPE = #{orderType}
    </update>
    <update id="updateSaOrderByMember">
        update SA_ORDER
        set DEL_FLAG = 1,
        PK_MODIFIED = #{pkApprove},
        MODIFIED_TIME = #{currentDateTime}
        where del_flag= 0 and order_type in (1,4,7,20,21,24) and pk_member in
        <foreach collection="memberList" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
    </update>

    <select id="selectCancelableOrder" resultMap="SaOrder">
        select *
        from SA_ORDER
        where (select (select sysdate - interval '${data}' hour from dual) from dual) > CREATION_TIME
          and ORDER_STATUS = 0
          and DEL_FLAG = 0
          and PK_COUNTRY = #{pkCountry}
          and ORDER_TYPE = #{orderType}
    </select>
    <select id="selectBySumPv" resultType="decimal">
        select nvl(sum(so.ORDER_ACHIEVE), 0)
        from SA_ORDER so
        where so.PK_MEMBER = #{memberId}
          and so.PAY_TIME &gt;= #{startPayDate}
          and so.PAY_TIME &lt;= #{endPayDate}
          and so.ORDER_STATUS = '1'
          and so.DEL_FLAG = '0'
    </select>

    <select id="selectBySumAmount" resultType="decimal">
        select nvl(sum(so.ORDER_AMOUNT), 0)
        from SA_ORDER so
        where so.PK_MEMBER = #{memberId}
          and so.PAY_TIME &gt;= #{startPayDate}
          and so.PAY_TIME &lt;= #{endPayDate}
          and so.ORDER_STATUS = '1'
          and so.DEL_FLAG = '0'
    </select>

    <!-- 查询订单类型 -->
    <select id="queryOrderType" resultType="Integer">
        select distinct so.order_type
        from sa_order so
        where so.del_flag = 0
          and so.pk_country = #{pkCountry}
          and (so.pk_member = #{memberId} or so.pk_creator = #{memberId})
    </select>

    <!-- 二次发货列表 -->
    <select id="secondList" resultType="com.hzs.sale.deliver.vo.DeliverSecondVO">
        select so.pk_id,
        so.order_code,
        so.order_type,
        cm.member_code,
        cm.member_name,
        cm.phone,
        so.rec_name,
        so.rec_phone,
        so.rec_province,
        so.rec_city,
        so.rec_county,
        so.rec_address,
        so.order_amount,
        so.postage,
        so.order_achieve,
        so.order_ass_achieve,
        so.delivery_way,
        so.pay_type,
        so.remark,
        so.creation_time,
        so.pay_time,
        bs.name storehouseName,
        bc.in_exchange_rate
        from sa_order so
        inner join cu_member cm
        on cm.pk_id = so.pk_member
        inner join bd_currency bc
        on bc.pk_id = so.pk_rate
        left join bd_storehouse bs
        on bs.pk_id = so.pk_storehouse
        where so.del_flag = 0
        and so.pk_country = #{pkCountry}
        and (select count(soi.pk_id)
        from sa_order_items soi
        where soi.pk_order = so.pk_id
        and soi.del_flag = 0
        and soi.order_status in (0, 1, 2, 4)) = 0
        <if test="param.orderCode != null and param.orderCode != ''">
            and so.order_code like '%' || #{param.orderCode} || '%'
        </if>
        <if test="param.pkStorehouse != null ">
            and bs.pk_id = #{param.pkStorehouse}
        </if>
        <if test="param.memberCode != null and param.memberCode != ''">
            and cm.member_code like #{param.memberCode} || '%'
        </if>
        <if test="param.memberName != null and param.memberName != ''">
            and cm.member_name like #{param.memberName} || '%'
        </if>
        <if test="param.phone != null and param.phone != ''">
            and cm.phone like #{param.phone} || '%'
        </if>
        <if test="param.orderType != null">
            and so.order_type = #{param.orderType}
        </if>
        <if test="param.deliveryWay != null">
            and so.delivery_way = #{param.deliveryWay}
        </if>
        <if test="param.recName != null and param.recName != ''">
            and so.rec_name like '%' || #{param.recName} || '%'
        </if>
        <if test="param.recPhone != null and param.recPhone != ''">
            and so.rec_phone like '%' || #{param.recPhone} || '%'
        </if>
        <if test="param.recProvince != null">
            and so.rec_province = #{param.recProvince}
        </if>
        <if test="param.recCity != null">
            and so.rec_city = #{param.recCity}
        </if>
        <if test="param.recCounty != null">
            and so.rec_county = #{param.recCounty}
        </if>
        <if test="param.recAddress != null and param.recAddress != ''">
            and so.rec_address like #{param.recAddress} || '%'
        </if>
        <if test="param.payType != null">
            and so.pay_type = #{param.payType}
        </if>
        <if test="param.creationTime != null">
            and so.creation_time >= #{param.creationTime[0]}
            and #{param.creationTime[1]} >= so.creation_time
        </if>
        <if test="param.payTime != null">
            and so.pay_time >= #{param.payTime[0]}
            and #{param.payTime[1]} >= so.pay_time
        </if>
        <!-- 地区权限处理 -->
        <if test="param.areaScopeList != null and param.areaScopeList.size > 0">
            and so.rec_province in
            <foreach collection="param.areaScopeList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <!-- 体系权限处理 -->
        <if test="param.vertexIdList != null and param.vertexIdList.size > 0">
            and cm.pk_vertex in
            <foreach collection="param.vertexIdList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        order by so.creation_time desc
    </select>

    <select id="selectMyMarket" resultType="com.hzs.sale.index.vo.CuMemberMyMarketVO">
        select count(distinct o.pk_country) countryNum,
        count(distinct o.rec_province) regionNum
        from sa_order o
        where o.del_flag = 0
        and o.order_status = 1
        <if test="pkMember != null">
            and o.pk_member in
            (select m.pk_id
            from cu_member m
            where m.del_flag = 0
            and m.pay_status = 1
            and m.category = 0
            and m.pk_settle_country = #{pkCountry}
            start with m.pk_id = #{pkMember}
            connect by prior m.pk_id = m.pk_parent)
        </if>
        <if test="pkCountry != null">
            and o.pk_country = #{pkCountry}
        </if>
    </select>

    <select id="selectMyMarketDetail" resultType="com.hzs.sale.index.vo.CuMemberMyMarketDetailVO">
        select distinct o.pk_country pkCountry,
        o.rec_province province,
        a.light_name lightName
        from sa_order o
        left join bd_area a
        on o.rec_province = a.pk_id
        and a.del_flag = 0
        and a.enable_state = 0
        where 1 = 1
        <if test="pkMember != null">
            and o.pk_member in
            (select m.pk_id
            from cu_member m
            where m.del_flag = 0
            and m.pay_status = 1
            and m.category = 0
            and m.pk_settle_country = #{pkCountry}
            start with m.pk_id = #{pkMember}
            connect by prior m.pk_id = m.pk_parent)
        </if>
        and o.del_flag = 0
        and o.order_status = 1
        and o.rec_province is not null
        <if test="pkCountry != null">
            and o.pk_country = #{pkCountry}
        </if>
    </select>

    <!--    查询当天已支付订单信息-->
    <select id="querySaOrderByOrderType" resultMap="SaOrder">
        select so.pk_member,
        so.ORDER_TYPE
        from sa_order so
        where so.order_status = 1 and (so.ORDER_TYPE=1 or so.ORDER_TYPE=2 or so.ORDER_TYPE=3)
        <if test="payTime == null">
            AND TO_CHAR(so.PAY_TIME,'YYYY-MM-dd') = TO_CHAR(SYSDATE, 'YYYY-MM-dd')
        </if>
        <if test="payTime != null and payTime!='' ">
            AND TO_CHAR(so.PAY_TIME,'YYYY-MM-dd') = #{payTime}
        </if>

    </select>

    <!-- 查询指定数量订单SEQ -->
    <select id="querySeqList" resultType="long">
        SELECT SA_ORDER_SEQ.nextval
        FROM (select level from dual connect by #{param} >= level)
    </select>

    <!-- 批量插入订单数据 -->
    <insert id="insertBach">
        INSERT ALL
        <foreach collection="orderList" item="item" separator=" ">
            into SA_ORDER (
            PK_ID, ORDER_CODE, ORDER_TYPE, PK_RATE,
            ORDER_AMOUNT, ORDER_ACHIEVE, UPLOAD_ACHIEVE, REC_NAME, REC_PHONE,
            REC_PROVINCE, REC_CITY, REC_COUNTY, REC_ADDRESS,
            PAY_TIME, PAY_TYPE, ORDER_STATUS,
            PK_MEMBER, PK_REFERENCE, PK_PLACEMENT, PK_STOREHOUSE,
            PK_CREATOR, CREATION_TIME, PK_COUNTRY
            )
            values (
            #{item.pkId}, #{item.orderCode}, #{item.orderType}, #{item.pkRate, jdbcType=NUMERIC},
            #{item.orderAmount}, #{item.orderAchieve}, #{item.orderAchieve}, #{item.recName}, #{item.recPhone},
            #{item.recProvince}, #{item.recCity}, #{item.recCounty, jdbcType=NUMERIC}, #{item.recAddress},
            #{item.payTime}, #{item.payType}, #{item.orderStatus},
            #{item.pkMember}, #{item.pkReference}, #{item.pkPlacement}, #{item.pkStorehouse, jdbcType=NUMERIC},
            #{item.pkCreator}, #{item.creationTime}, #{item.pkCountry}
            )
        </foreach>
        SELECT 1 FROM dual
    </insert>
    <!-- 批量插入虚拟订单数据 -->
    <insert id="insertUnrealOrderBach">
        INSERT ALL
        <foreach collection="orderList" item="item" separator=" ">
            into SA_ORDER (
            ORDER_CODE, ORDER_TYPE, PK_RATE,
            ORDER_AMOUNT, ORDER_ACHIEVE,
            PAY_TIME, PAY_TYPE, ORDER_STATUS,
            PK_MEMBER,TRAN_TYPE,PK_MEMBER_STORE,
            PK_CREATOR, CREATION_TIME, PK_COUNTRY,
            PK_REFERENCE,DELIVERY_WAY,REC_NAME,REC_PHONE
            )
            values (
            #{item.orderCode}, #{item.orderType}, #{item.pkRate, jdbcType=NUMERIC},
            #{item.orderAmount}, #{item.orderAchieve},
            #{item.payTime}, #{item.payType}, #{item.orderStatus},
            #{item.pkMember} ,#{item.tranType},#{item.pkMemberStore},
            #{item.pkCreator}, #{item.creationTime}, #{item.pkCountry},
            #{item.pkReference},#{item.deliveryWay},#{item.recName},#{item.recPhone}

            )
        </foreach>
        SELECT 1 FROM dual
    </insert>

    <!-- 查询升级金额以及业绩 -->
    <select id="queryUpgradeAmountAndAchieve" resultMap="saOrderExtResultMap">
        select a.order_amount, b.order_achieve
        from (select nvl(sum(so.order_amount / bc.in_exchange_rate), 0) order_amount
              from sa_order so
                       left join bd_currency bc
                                 on bc.pk_id = so.pk_rate and bc.del_flag=0
              where so.del_flag = 0
                and so.order_status = 1
                and so.order_type = 2
                and so.pk_member = #{pkMember}) a
                 left join (select nvl(sum(so.order_achieve), 0) order_achieve
                            from sa_order so
                            where so.del_flag = 0
                              and so.order_status = 1
                              and so.order_type = 1
                              and so.pk_reference = #{pkMember}) b
                           on 1 = 1
    </select>

    <select id="getMemberOrderAchieve" resultType="decimal">
         select nvl(sum(so.ORDER_ACHIEVE),0) from sa_order so where so.DEL_FLAG = 0 and so.ORDER_STATUS = 1 and so.PK_MEMBER = #{pkMember} and so.ORDER_TYPE in (1,2)
    </select>

    <select id="getStoreNum" resultType="integer" >
        select sum(pk_id)
        from (select count(so.pk_id) pk_id
        from sa_order so
        where del_flag = 0
        and so.pk_country = #{pkCountry}
        and (so.PK_MEMBER_STORE = #{pkStore} )
        and exists (select 1
        from sa_order_items soi
        where soi.pk_order = so.pk_id
        and soi.del_flag = 0
        and soi.order_status in
        <foreach collection="statusList" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        )
        )
    </select>

    <select id="querySaOrderByDeliverList" resultMap="saOrderExtResultMap">
        select so.pk_id,
        so.pk_member,
        so.order_code,
        so.order_type,
        so.pk_rate,
        so.order_amount,
        so.order_achieve,
        so.upload_achieve,
        so.del_flag,
        so.pk_reference,
        so.pk_vertex,
        so.pk_country,
        so.pay_time,bh.pk_corp,so.pk_storehouse,
        st.pk_id       items_id,
        bp.unit pk_unit,
        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
        from sa_order so
        inner join sa_order_items st
        on so.pk_id = st.pk_order
        inner join bd_product bp
        on bp.pk_id=st.pk_product
        inner join sa_deliver_items sd
        on st.pk_id = sd.pk_order_items
        left join bd_storehouse bh
        on bh.pk_id=so.pk_storehouse
        where so.del_flag=0 and st.del_flag=0 and so.order_status = 1
        and bp.del_flag=0 and st.order_status >=3
        and sd.pk_id in
        <foreach collection="deliverItemExtList" item="item" open="(" close=")" separator=",">
            #{item.pkId}
        </foreach>
    </select>

    <!-- 获取订单PV总值 -->
    <select id="querySumAmountAndAchieve" resultMap="SaOrder">
        select t.pk_member,
               sum(t.order_amount) order_amount,
               sum(t.order_achieve) order_achieve
        from (select so.pk_member,
                     sum(nvl(so.order_amount, 0)) order_amount,
                     sum(nvl(so.order_achieve, 0)) order_achieve
              from sa_order so
              where so.del_flag = 0
                and so.order_status = 1
                and so.pk_member = #{pkMember}
                and so.order_type in (1, 2, 20)
                <if test="notOrderCode != null and notOrderCode != ''">
                    and so.order_code != #{notOrderCode}
                </if>
              group by so.pk_member
              union all
              select soa.pk_member,
                     sum(nvl(soa.order_amount, 0)) order_amount,
                     sum(nvl(soa.order_achieve, 0)) order_achieve
              from sa_order_achieve soa
              where soa.order_status = 1
                and soa.pk_member = #{pkMember}
              group by soa.pk_member) t
        group by t.pk_member
    </select>

    <select id="querySaOrderExtByPkId" resultType="com.hzs.common.domain.sale.order.SaOrder">
        select * from sa_order where pk_id=#{pkId}
    </select>

    <select id="queryStoreSaOrderByParam" resultMap="saOrderExtResultMap">
        select so.pk_id,so.pk_member,so.order_code,so.order_type,so.pk_rate,
               so.order_amount,so.order_achieve,cm.member_code,cm.member_name,
        pay.member_code pay_member_code,pay.member_name pay_member_name,nvl(a.grant_status,0) grant_status,
               so.pk_country,so.pay_time,bh.pk_corp,so.pk_storehouse,
               so.rec_address,so.rec_phone,so.rec_name,ts.store_code,ts.store_name,ts.binding_province store_province,
               st.pk_id       items_id,bp.unit pk_unit,province.name rec_province_name,
               city.name rec_city_name,county.name rec_county_name,
               bp.cover,bp.product_code,bp.product_name,st.pk_product,st.order_status,
               st.pk_wares,st.price,st.achievement,st.quantity,
               st.pk_grade,so.rec_province,so.rec_city,so.pk_member_store,
               st.pk_awards,st.specs_name_id,
               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,bl.level_value,bl.store_level_name
        from sa_order so
         inner join sa_order_items st
        on so.pk_id = st.pk_order
         inner join bd_product bp
        on bp.pk_id=st.pk_product and bp.product_attr = 1
        inner join cu_member cm
        on so.pk_member = cm.pk_id
        inner join cu_member pay
        on so.pk_creator=pay.pk_id
        left join(select distinct cb.grant_status,cd.pk_order from cu_member_bonus cb
        inner join cu_member_bonus_detail cd
        on cb.pk_id=cd.pk_bonus
        where cb.del_flag=0 and cd.del_flag=0) a
        on a.pk_order=so.pk_id
        inner join cu_store ts
        on ts.pk_id=so.pk_member_store
        inner join bd_store_level bl
        on ts.pk_store_level=bl.pk_id
        left join bd_storehouse bh
        on bh.pk_id=so.pk_storehouse
        left join bd_area province on province.PK_ID=so.rec_province
        left join bd_area city on city.PK_ID=so.rec_city
        left join bd_area county on county.PK_ID=so.rec_county
        where so.del_flag=0 and st.del_flag=0
          and bp.del_flag=0 and so.delivery_way in(3,4)
        <if test="orderQueryParam.pkCountry != null">
            and so.pk_country = #{orderQueryParam.pkCountry}
        </if>
        <if test="orderQueryParam.orderStatus != null">
            and st.order_status=#{orderQueryParam.orderStatus}
        </if>
        <if test="orderQueryParam.orderCode != null and orderQueryParam.orderCode != ''">
            and so.order_code like #{orderQueryParam.orderCode} || '%'
        </if>
        <if test="orderQueryParam.memberCode != null and orderQueryParam.memberCode != ''">
            and cm.member_code like #{orderQueryParam.memberCode} || '%'
        </if>
        <if test="orderQueryParam.memberName != null and orderQueryParam.memberName != ''">
            and cm.member_name like #{orderQueryParam.memberName} || '%'
        </if>
        <if test="orderQueryParam.orderType != null">
            and so.order_type = #{orderQueryParam.orderType}
        </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.itemIdList != null and orderQueryParam.itemIdList.size >0">
            and st.pk_id in
            <foreach collection="orderQueryParam.itemIdList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="orderQueryParam.storeName != null and orderQueryParam.storeName != ''">
            and ts.store_name like '%' || #{orderQueryParam.storeName} || '%'
        </if>
        <if test="orderQueryParam.storeCode != null and orderQueryParam.storeCode != ''">
            and ts.store_code like '%' || #{orderQueryParam.storeCode} || '%'
        </if>
        <if test="orderQueryParam.pkStore != null and orderQueryParam.pkStore != ''">
            and ts.pk_id=#{orderQueryParam.pkStore}
        </if>
        <if test="orderQueryParam.creationTimeStart != null">
            and so.PAY_TIME &gt;= #{orderQueryParam.creationTimeStart, jdbcType=DATE}
        </if>
        <if test="orderQueryParam.creationTimeEnd != null">
            and so.PAY_TIME &lt;= #{orderQueryParam.creationTimeEnd, jdbcType=DATE}
        </if>
        <if test="orderQueryParam.startDate != null">
            and so.PAY_TIME &gt;= #{orderQueryParam.startDate, jdbcType=DATE}
        </if>
        <if test="orderQueryParam.endDate != null">
            and so.PAY_TIME &lt;= #{orderQueryParam.endDate, jdbcType=DATE}
        </if>
        <if test="orderQueryParam.recProvince != null">
            and so.rec_province = #{orderQueryParam.recProvince}
        </if>
        <if test="orderQueryParam.recCity != null">
            and so.rec_city = #{orderQueryParam.recCity}
        </if>
        <if test="orderQueryParam.payMemberCode != null and orderQueryParam.payMemberCode != ''">
            and pay.member_code like #{orderQueryParam.payMemberCode} || '%'
        </if>
        <if test="orderQueryParam.payMemberName != null and orderQueryParam.payMemberName != ''">
            and pay.member_name like #{orderQueryParam.payMemberName} || '%'
        </if>
        order by so.pay_time desc
    </select>

    <!-- 查询购买订单数量 -->
    <select id="countOrderCountByYear" resultType="int">
        select count(1)
        from sa_order so
        where so.del_flag = 0
          and so.pk_member = #{pkMember}
          and so.order_type = #{orderType}
          and so.order_status = 1
          and to_char(so.pay_time, 'yyyy') = #{year}
    </select>

</mapper>
