<?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.OrderDetailMqMapper">
    
    <resultMap type="OrderDetail" id="OrderDetailVoResult">
        <result property="orderDetailId"    column="orderDetailId"    />
        <result property="orderId"    column="orderId"    />
        <result property="suiteId"    column="suiteId"    />
        <result property="prodId"    column="prodId"    />
        <result property="prodName"    column="prodName"    />
        <result property="prodPropId"    column="prodPropId"    />
        <result property="prodPropName"    column="prodPropName"    />
        <result property="orgProdId"    column="orgProdId"    />
        <result property="prodType"    column="prodType"    />
        <result property="mateId"    column="mateId"    />
        <result property="mateName"    column="mateName"    />
        <result property="mateType"    column="mateType"    />
        <result property="vendorId"    column="vendorId"    />
        <result property="buyQty"    column="buyQty"    />
        <result property="afterQty"    column="afterQty"    />
        <result property="returnQty"    column="returnQty"    />
        <result property="buyPrice"    column="buyPrice"    />
        <result property="salePrice"    column="salePrice"    />
        <result property="expressName"    column="expressName"    />
        <result property="expressNo"    column="expressNo"    />
        <result property="expressTime"    column="expressTime"    />
        <result property="receiveTime"    column="receiveTime"    />
        <result property="confirmTime"    column="confirm_time"    />
        <result property="finishTime"    column="finishTime"    />
        <result property="orderDetailStatus"    column="orderDetailStatus"    />
        <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="selectOrderDetailVo">
        select
            o.orderDetailId,
            o.orderId,
            o.suiteId,
            o.prodId,
            o.prodName,
            o.prodPropId,
            o.prodPropName,
            o.orgProdId,
            o.prodType,
            o.mateId,
            o.mateName,
            o.mateType,
            o.vendorId,
            o.buyQty,
            o.afterQty,
            o.returnQty,
            o.buyPrice,
            o.salePrice,
            o.expressName,
            o.expressNo,
            o.expressTime,
            o.receiveTime,
            o.confirm_time,
            o.finishTime,
            o.orderDetailStatus,
            o.delFlag,
            o.createUser,
            o.createTime,
            o.updateUser,
            o.updateTime
        from t_order_detail o
    </sql>

    <select id="checkOrderFinished" resultType="int">
		select count(1) from t_order_detail
		where orderId = #{orderId}
            and orderDetailStatus not in ('CANCELLED', 'REJECTED', 'FINISHED')
	</select>

    <update id="updateReturnQty" parameterType="OrderDetail">
        update t_order_detail set afterQty = afterQty - afterQty,
                                  returnQty = returnQty + #{returnQty},
                                  updateUser = #{updateUser},
                                  updateTime = #{updateTime}
        where 1 = 1
            <if test="orderId != null "> and orderId = #{orderId}</if>
            <if test="orderDetailId != null "> and orderDetailId = #{orderDetailId}</if>
            <if test="suiteId != null and suiteId != ''"> and suiteId = #{suiteId}</if>
    </update>

</mapper>