<?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.iocoder.mall.order.biz.dao.order.OrderMapper">

    <sql id="FIELDS">
        id, user_id, order_no, buy_price, discount_price, logistics_price, present_price, pay_amount,
        payment_time, delivery_time, receiver_time, closing_time,
        has_return_exchange,
        status, remark, create_time, update_time, `deleted`
    </sql>

    <sql id="updateFieldSql" >
        <set>
            <if test="orderNo != null">
                , order_no = #{orderNo}
            </if>
            <if test="buyPrice != null">
                , buy_price = #{buyPrice}
            </if>
            <if test="discountPrice != null">
                , discount_price = #{discountPrice}
            </if>
            <if test="logisticsPrice != null">
                , logistics_price = #{logisticsPrice}
            </if>
            <if test="logisticsPrice != null">
                , logistics_price = #{logisticsPrice}
            </if>
            <if test="presentPrice != null">
                , present_price = #{presentPrice}
            </if>
            <if test="payAmount != null">
                , pay_amount = #{payAmount}
            </if>
            <if test="deliveryTime != null">
                , delivery_time = #{deliveryTime}
            </if>
            <if test="paymentTime != null">
                , payment_time = #{paymentTime}
            </if>
            <if test="receiverTime != null">
                , receiver_time = #{receiverTime}
            </if>
            <if test="closingTime != null">
                , closing_time = #{closingTime}
            </if>
            <if test="hasReturnExchange != null">
                , has_return_exchange = #{hasReturnExchange}
            </if>

            <if test="status != null">
                , status = #{status}
            </if>
            <if test="remark != null">
                , remark = #{remark}
            </if>
            <if test="deleted != null">
                , `deleted` = #{deleted}
            </if>
            <if test="createTime != null">
                , create_time = #{createTime}
            </if>
            <if test="updateTime != null">
                , update_time = #{updateTime}
            </if>
        </set>
    </sql>

    <update id="updateById" parameterType="OrderDO">
        UPDATE `orders`
        <include refid="updateFieldSql" />
        WHERE id = #{id}
    </update>

    <update id="updateByIdAndStatus">
        UPDATE `orders`
        <set>
            <if test="updateObj.payAmount != null">
                , pay_amount = #{updateObj.payAmount}
            </if>
            <if test="updateObj.paymentTime != null">
                , payment_time = #{updateObj.paymentTime}
            </if>
            <if test="updateObj.status != null">
                , status = #{updateObj.status}
            </if>
        </set>
        WHERE id = #{id}
        AND status = #{status}
    </update>

    <select id="selectById" resultType="cn.iocoder.mall.order.biz.dataobject.OrderDO">
        SELECT
        <include refid="FIELDS" />
        FROM `orders`
        WHERE id = #{id}
    </select>

    <sql id="selectWhere">
        <if test="status != null">
            AND `status` = #{status}
        </if>
        <if test="userId != null">
            AND `user_id` = #{userId}
        </if>
        <if test="id != null">
            AND `id` = #{id}
        </if>
        <if test="orderNo != null">
            AND `order_no` = #{orderNo}
        </if>
        <if test="hasReturnExchange != null">
            AND `has_return_exchange` = #{hasReturnExchange}
        </if>
        <if test="startCreateTime != null and endCreateTime != null">
            AND `create_time` &gt;= #{startCreateTime}
            AND `create_time` &lt;= #{endCreateTime}
        </if>
    </sql>

    <select id="selectPageCount" resultType="java.lang.Integer">
        SELECT
        COUNT(*)
        FROM `orders`
        <where>
            <include refid="selectWhere" />
        </where>
    </select>

    <select id="selectPage" resultType="cn.iocoder.mall.order.biz.dataobject.OrderDO">
        SELECT
        <include refid="FIELDS" />
        FROM `orders`
        <where>
            <include refid="selectWhere" />
        </where>
        LIMIT ${pageNo * pageSize}, ${pageSize}
    </select>


</mapper>
