<?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.xingxi.mq.server.order.mapper.ClearOrderVoMapper">
    
    <resultMap type="ClearOrder" id="ClearOrderResult">
        <result property="id"    column="id"    />
        <result property="orderId"    column="orderId"    />
        <result property="orderNo"    column="orderNo"    />
        <result property="buyerId"    column="buyerId"    />
        <result property="buyerRole"    column="buyerRole"    />
        <result property="tradeType"    column="tradeType"    />
        <result property="sellerId"    column="sellerId"    />
        <result property="sellerRole"    column="sellerRole"    />
        <result property="finishTime"    column="finishTime"    />
        <result property="orderStatus"    column="orderStatus"    />
        <result property="settleTime"    column="settleTime"    />
        <result property="settleStatus"    column="settleStatus"    />
        <result property="invoiceTime"    column="invoiceTime"    />
        <result property="invoiceStatus"    column="invoiceStatus"    />
        <result property="delFlag"    column="delFlag"    />
        <result property="createUser"    column="createUser"    />
        <result property="createTime"    column="createTime"    />
        <result property="updateUser"    column="updateUser"    />
        <result property="updateTime"    column="updateTime"    />
    </resultMap>

    <sql id="selectClearOrder">
        select
            c.id,
            c.orderId,
            c.orderNo,
            c.buyerId,
            c.buyerRole,
            c.tradeType,
            c.sellerId,
            c.sellerRole,
            c.finishTime,
            c.orderStatus,
            c.settleTime,
            c.settleStatus,
            c.invoiceTime,
            c.invoiceStatus,
            c.delFlag,
            c.createUser,
            c.createTime,
            c.updateUser,
            c.updateTime
        from t_clear_order c
    </sql>

    <update id="syncOrderStatusByOrderId" parameterType="ClearOrder">
        <![CDATA[
            update t_clear_order set
                t_clear_order.orderStatus = #{orderStatus},
                t_clear_order.updateTime  = #{updateTime},
                t_clear_order.updateUser  = #{updateUser}
            where t_clear_order.orderId = #{orderId}
                and not exists(select 1 from t_clear_order_detail
                where t_clear_order_detail.orderId = t_clear_order.orderId
                and t_clear_order_detail.buyerId = t_clear_order.buyerId
                and t_clear_order_detail.sellerId = t_clear_order.sellerId
                and t_clear_order_detail.orderStatus <> #{orderStatus})
        ]]>
    </update>

</mapper>