<?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.SaOrderSettleMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="SaOrderSettleExt" type="com.hzs.common.domain.ground.order.ext.SaOrderSettleExt">
        <id column="PK_ID" property="pkId" />
        <result column="DEL_FLAG" property="delFlag" />
        <result column="CREATION_TIME" property="creationTime" />
        <result column="MODIFIED_TIME" property="modifiedTime" />
        <result column="PK_COUNTRY" property="pkCountry" />
        <result column="PK_CREATOR" property="pkCreator" />
        <result column="PK_MODIFIED" property="pkModified" />
        <result column="PK_ORDER" property="pkOrder" />
        <result column="PK_ORDER_ITEMS" property="pkOrderItems" />
        <result column="PK_SUPPLIER" property="pkSupplier" />
        <result column="PRICE" property="price" />
        <result column="QUANTITY" property="quantity" />
        <result column="TOTAL_PRICE" property="totalPrice" />
        <result column="SETTLE_STATUS" property="settleStatus" />
        <result column="ORDER_CODE" property="orderCode" />
        <result column="PRODUCT_CODE" property="productCode" />
        <result column="PRODUCT_NAME" property="productName" />
        <result column="PAY_TIME" property="payTime" />
        <result column="SUPPLY_NAME" property="supplyName" />
        <result column="PK_MEMBER" property="pkMember" />
        <result column="SETTLE_TYPE" property="settleType" />
    </resultMap>
    <update id="updateSaOrderSettle">
        merge into sa_order_settle st
        using(
        <foreach item="item" index="index" collection="orderSettleExtList" separator=" union ">
            select #{item.pkId} pk_id from dual
        </foreach>
            ) b on (st.pk_id=b.pk_id)
        when matched then
            update set st.settle_status=0,modified_time=sysdate
    </update>

    <select id="querySaOrderSettle" resultMap="SaOrderSettleExt">
        select so.order_code,so.pay_time,bp.product_code,bp.product_name,ta.name supply_name,
               st.*,ta.pk_member,nvl(ta.settle_type,2) settle_type from sa_order_settle st
       inner join sa_order_items se
                  on st.pk_order_items = se.pk_id
       inner join sa_order so
                  on st.pk_order=so.pk_id
       inner join bd_product bp
                  on se.pk_product=bp.pk_id
        left join th_supplier_apply ta
        on se.pk_supplier=ta.pk_id and ta.del_flag=0
        where st.del_flag=0 and se.del_flag=0
          and so.del_flag=0 and bp.del_flag=0
          and se.shipping_channel=2 and so.order_status=1
          and st.pk_country=#{queryParam.pkCountry}
        <if test="queryParam.pkSupplier != null">
            and se.pk_supplier=#{queryParam.pkSupplier}
        </if>
        <if test="queryParam.orderCode != null">
            and so.order_code=#{queryParam.orderCode}
        </if>
        <if test="queryParam.productCode != null and queryParam.productCode != ''">
            and bp.product_code=#{queryParam.productCode}
        </if>
        <if test="queryParam.productName != null and queryParam.productName != ''">
            and bp.product_name=#{queryParam.productName}
        </if>
        <if test="queryParam.startTime != null">
            and so.pay_time >=#{queryParam.startTime}
        </if>
        <if test="queryParam.endTime != null">
            and so.pay_time &lt; #{queryParam.endTime}
        </if>
        <if test="queryParam.idList != null and queryParam.idList.size>0">
            and se.order_status=3 and st.pk_id in
            <foreach collection="queryParam.idList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
    </select>

</mapper>
