<?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.CommissionOrderVoMapper">
    
    <resultMap type="CommissionOrder" id="CommissionOrderVoResult">
        <result property="id"    column="id"    />
        <result property="orderId"    column="orderId"    />
        <result property="orderNo"    column="orderNo"    />
        <result property="orderTime"    column="orderTime"    />
        <result property="referrerId"    column="referrerId"    />
        <result property="referrerRole"    column="referrerRole"    />
        <result property="referrerStoreID"    column="referrerStoreID"    />
        <result property="buyerId"    column="buyerId"    />
        <result property="buyerRole"    column="buyerRole"    />
        <result property="sellerId"    column="sellerId"    />
        <result property="sellerRole"    column="sellerRole"    />
        <result property="orderAmount"    column="orderAmount"    />
        <result property="totalAmount"    column="totalAmount"    />
        <result property="businessCategory"    column="businessCategory"    />
        <result property="orderStatus"    column="orderStatus"    />
        <result property="finishTime"    column="finishTime"    />
        <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="selectCommissionOrderVo">
        select
            c.id,
            c.orderId,
            c.orderNo,
            c.orderTime,
            c.referrerId,
            c.referrerRole,
            c.referrerStoreID,
            c.buyerId,
            c.buyerRole,
            c.sellerId,
            c.sellerRole,
            c.orderAmount,
            c.totalAmount,
            c.businessCategory,
            c.orderStatus,
            c.finishTime,
            c.delFlag,
            c.createUser,
            c.createTime,
            c.updateUser,
            c.updateTime
        from t_commission_order c
    </sql>

    <update id="updateCommissionOrderVo" parameterType="CommissionOrder">
        update t_commission_order
        <trim prefix="SET" suffixOverrides=",">
            <if test="orderAmount != null">orderAmount = orderAmount- #{orderAmount},</if>
            <if test="totalAmount != null">totalAmount = totalAmount- #{totalAmount},</if>
            <if test="orderStatus != null and orderStatus != ''">orderStatus = #{orderStatus},</if>
            <if test="finishTime != null">finishTime = #{finishTime},</if>
            <if test="delFlag != null and delFlag != ''">delFlag = #{delFlag},</if>
            <if test="updateUser != null and updateUser != ''">updateUser = #{updateUser},</if>
            <if test="updateTime != null">updateTime = #{updateTime},</if>
        </trim>
        where id = #{id}
    </update>

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

</mapper>