<?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="cn.com.xingxi.store.data.mapper.WxaStoreOrderMapper">

    <resultMap id="OrderDTOResult" type="OrderDTO">
        <result property="orderId" column="orderId"/>
        <result property="orderNo" column="orderNo"/>
        <result property="orderTime" column="orderTime"/>
        <result property="orderStatus" column="orderStatus"/>
        <result property="orderType" column="orderType"/>
        <result property="groupId" column="groupId"/>
        <result property="buildingNo" column="buildingNo"/>
        <result property="roomNo" column="roomNo"/>
        <result property="groupNo" column="groupNo"/>
        <result property="orderAmount" column="orderAmount"/>
        <result property="payAmount" column="payAmount"/>
        <result property="prodQuantity" column="prodQuantity"/>
        <result property="buyerId" column="buyerId"/>
        <result property="buyerRole" column="buyerRole"/>
        <result property="sellerId" column="sellerId"/>
        <result property="sellerRole" column="sellerRole"/>
        <result property="payeeId" column="payeeId"/>
        <result property="payeeRole" column="payeeRole"/>
        <result property="referrerId" column="referrerId"/>
        <result property="referrerRole" column="referrerRole"/>
        <result property="referrerRate" column="referrerRate"/>
        <result property="serviceRate" column="serviceRate"/>
        <result property="cancelTime" column="cancelTime"/>
        <result property="finishTime" column="finishTime"/>
        <result property="recProvId" column="recProvId"/>
        <result property="recProvName" column="recProvName"/>
        <result property="recCityId" column="recCityId"/>
        <result property="recCityName" column="recCityName"/>
        <result property="recAreaId" column="recAreaId"/>
        <result property="recAreaName" column="recAreaName"/>
        <result property="recAddress" column="recAddress"/>
        <result property="recName" column="recName"/>
        <result property="recPhone" column="recPhone"/>
        <result property="remark" column="remark"/>
        <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"/>
        <collection property="orderDetailDTOS" select="selectOrderDetailListByOrderId" column="orderId=orderId"/>
    </resultMap>

    <resultMap id="OrderDetailDTOsResult" type="OrderDetailDTO">
        <result property="orderDetailId" column="orderDetailId"/>
        <result property="orderId" column="orderId"/>
        <result property="prodId" column="prodId"/>
        <result property="prodName" column="prodName"/>
        <result property="pprId" column="pprId"/>
        <result property="pprName" column="pprName"/>
        <result property="upId" column="upId"/>
        <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="confirmTime" column="confirmTime"/>
        <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"/>
        <collection property="imageDTOS" select="selectProductImageByProdId" column="prodcutId=prodId"/>
        <collection property="orderDetailExprs" select="selectOrderDetailExprsListByOrderDetailId" column="orderDetailId"/>
    </resultMap>

    <select id="selectOrderDetailExtByOrderDetailId" parameterType="String" resultMap="OrderDetailDTOsResult">
        SELECT t_order_detail.* FROM t_order_detail
        WHERE t_order_detail.orderDetailId = #{orderDetailId}
    </select>

    <select id="selectOrderDetailListBySuiteId" parameterType="String" resultType="OrderDetail">
        SELECT t_order_detail.* FROM t_order_detail
        WHERE t_order_detail.suiteId = #{suiteId}
    </select>

    <select id="selectOrderByOrderId" resultMap="OrderDTOResult">
        SELECT t_order.*
        FROM t_order
        WHERE t_order.delFlag = '0'
        AND t_order.orderId = #{orderId}
    </select>

    <select id="selectOrderListByOrderStatus" resultMap="OrderDTOResult">
        SELECT t_order.*
        FROM t_order
        <where>
            t_order.delFlag = '0'
            AND t_order.sellerId = #{mercId}
            AND t_order.buyerId = #{userId}
            <if test="orderStatus != null and orderStatus != ''">
                AND t_order.orderStatus = #{orderStatus}
            </if>
        </where>
        ORDER BY t_order.orderTime desc
    </select>

    <select id="selectLockOrderByOrderNo" parameterType="String" resultMap="OrderDTOResult">
        SELECT t_order.*
        FROM t_order
        WHERE t_order.delFlag = '0'
          AND t_order.orderStatus = 'ORDERED'
          AND t_order.orderNo = #{orderNo}
        FOR UPDATE
    </select>

    <select id="selectLockPreCloseOrderByOrderId" parameterType="Long" resultMap="OrderDTOResult">
        SELECT t_order.*
        FROM t_order
        WHERE t_order.delFlag = '0'
          AND t_order.orderStatus IN ('ORDERED', 'PAYED')
          AND t_order.orderId = #{orderId}
        FOR UPDATE
    </select>


    <update id="updateOrderFinishByOrderId" parameterType="OrderDTO">
        update t_order
        <trim prefix="SET" suffixOverrides=",">
            <if test="finishTime != null">finishTime = #{finishTime},</if>
            <if test="orderStatus != null and orderStatus != ''">orderStatus = #{orderStatus},</if>
            <if test="updateUser != null and updateUser != ''">updateUser = #{updateUser},</if>
            <if test="updateTime != null">updateTime = #{updateTime},</if>
        </trim>
        <![CDATA[
         where NOT EXISTS (
            select t_order_detail.* from t_order_detail
            where t_order_detail.orderDetailStatus <> 'RECEIVED'
            and t_order_detail.orderId = t_order.orderId)
            and t_order.orderId = #{orderId}
        ]]>
    </update>


    <update id="updateOrderDetailFinishBySuiteId" parameterType="OrderDetail">
        update t_order_detail
        <trim prefix="SET" suffixOverrides=",">
            <if test="afterQty != null">afterQty = #{afterQty},</if>
            <if test="returnQty != null">returnQty = #{returnQty},</if>
            <if test="confirmTime != null">confirmTime = #{confirmTime},</if>
            <if test="finishTime != null">finishTime = #{finishTime},</if>
            <if test="orderDetailStatus != null and orderDetailStatus != ''">orderDetailStatus = #{orderDetailStatus},</if>
            <if test="updateUser != null and updateUser != ''">updateUser = #{updateUser},</if>
            <if test="updateTime != null">updateTime = #{updateTime},</if>
        </trim>
        where suiteId = #{suiteId}
    </update>


    <update id="updateOrderStatus" parameterType="OrderDTO">
        update t_order
        <trim prefix="SET" suffixOverrides=",">
            <if test="orderStatus != null and orderStatus != ''">orderStatus = #{orderStatus},</if>
            <if test="updateUser != null and updateUser != ''">updateUser = #{updateUser},</if>
            <if test="updateTime != null">updateTime = #{updateTime},</if>
        </trim>
        where orderId = #{orderId}
    </update>


    <update id="updateOrderDetailStatus" parameterType="OrderDetailDTO">
        update t_order_detail
        <trim prefix="SET" suffixOverrides=",">
            <if test="orderDetailStatus != null and orderDetailStatus != ''">orderDetailStatus = #{orderDetailStatus},</if>
            <if test="updateUser != null and updateUser != ''">updateUser = #{updateUser},</if>
            <if test="updateTime != null">updateTime = #{updateTime},</if>
        </trim>
        where orderDetailId = #{orderDetailId}
    </update>


    <update id="updateOrderDetailByOrderId" parameterType="OrderDetail">
        update t_order_detail
        <trim prefix="SET" suffixOverrides=",">
            <if test="delFlag != null and delFlag != ''">delFlag = #{delFlag},</if>
            <if test="orderDetailStatus != null and orderDetailStatus != ''">orderDetailStatus = #{orderDetailStatus},</if>
            <if test="updateUser != null and updateUser != ''">updateUser = #{updateUser},</if>
            <if test="updateTime != null">updateTime = #{updateTime},</if>
        </trim>
        where orderId = #{orderId}
    </update>

    <select id="selectOrderDetailExprsListByOrderDetailId" parameterType="Long" resultType="OrderDetailExprDTO">
        select id, orderDetailId, expressName, expressNo, sendTime, confirmTime
        from t_order_detail_expr
        where orderDetailId = #{orderDetailId}
    </select>

    <select id="selectProductImageByProdId"  resultType="ImageDTO">
        SELECT t_attch_file.fileId as imageId, t_attch_file.fileUrl as imageUrl
        FROM m_prod_picture
        INNER JOIN t_attch_file ON m_prod_picture.picImgId = t_attch_file.fileId
        WHERE m_prod_picture.delFlag = '0'
          AND m_prod_picture.prodId = #{prodcutId}
    </select>

    <select id="selectOrderDetailListByOrderId" resultMap="OrderDetailDTOsResult">
        SELECT t_order_detail.* FROM t_order_detail WHERE t_order_detail.orderId = #{orderId}
    </select>

</mapper>
