<?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.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>

    <!--
        插入数据
    -->
    <insert id="insert" parameterType="OrderDO" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
        INSERT INTO `order` (
            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`
        ) VALUES (
             #{userId}, #{orderNo}, #{buyPrice}, #{discountPrice}, #{logisticsPrice}, #{presentPrice}, #{payAmount},
             #{paymentTime}, #{deliveryTime}, #{receiverTime}, #{closingTime},
             #{hasReturnExchange},
             #{status}, #{remark}, #{createTime}, #{updateTime}, #{deleted}
        )
    </insert>

    <!--
        更新 - 可更新的字段
    -->
    <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>

    <!--
        更新 - 根据 id 更新
    -->
    <update id="updateById" parameterType="OrderDO">
        UPDATE `order`
        <include refid="updateFieldSql" />
        WHERE id = #{id}
    </update>

    <update id="updateByIdAndStatus">
        UPDATE `order`
        <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>

    <!--
        查询 - 根据id 查询
    -->
    <select id="selectById" resultType="cn.iocoder.mall.order.biz.dataobject.OrderDO">
        SELECT
        <include refid="FIELDS" />
        FROM `order`
        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>

    <!--
        查询 - 后台分页page Count
    -->
    <select id="selectPageCount" resultType="java.lang.Integer">
        SELECT
        COUNT(*)
        FROM `order`
        WHERE 1 = 1 <!-- TODO 芋艿 不要 1=1 ，会有问题，使用 where 标签 -->
        <include refid="selectWhere" />
    </select>

    <!--
        查询 - 后台分页page
    -->
    <select id="selectPage" resultType="cn.iocoder.mall.order.biz.dataobject.OrderDO">
        SELECT
        <include refid="FIELDS" />
        FROM `order`
        WHERE 1 = 1 <!-- TODO 芋艿 不要 1=1 ，会有问题，使用 where 标签 -->
        <include refid="selectWhere" />
        LIMIT ${pageNo * pageSize}, ${pageSize}
    </select>


</mapper>
