<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.cxyxiaomo.epp.shop.dao.OrderDao">

    <resultMap id="OrderResultMap" type="com.cxyxiaomo.epp.common.pojo.Order">
        <id column="id" property="id"/>
        <result column="user_id" property="userId"/>
        <result column="order_date" property="orderDate"/>
        <result column="order_status" property="orderStatus"/>
        <result column="order_price" property="orderPrice"/>
        <result column="pay_date" property="payDate"/>
        <result column="cancel_date" property="cancelDate"/>
        <result column="ship_date" property="shipDate"/>
        <result column="deliver_date" property="deliverDate"/>
        <result column="express_id" property="expressId"/>
        <result column="comment" property="comment"/>
    </resultMap>
    <resultMap id="OrderDetailResultMap" type="com.cxyxiaomo.epp.common.pojo.OrderDetail">
        <id property="id" column="id"/>
        <result property="orderId" column="order_id"/>
        <result property="goodId" column="good_id"/>
        <result property="goodCount" column="good_count"/>
        <result property="unitPrice" column="unit_price"/>
    </resultMap>

    <!-- 根据订单 ID 查询订单信息 -->
    <select id="getOrderById" resultMap="OrderResultMap">
        SELECT *
        FROM `order`
        WHERE id = #{orderId}
    </select>

    <!-- 根据订单 ID 查询订单详情 -->
    <select id="getOrderDetailById" resultMap="OrderDetailResultMap">
        SELECT *
        FROM `order_detail`
        WHERE order_id = #{orderId}
    </select>

    <!-- 根据用户 ID 查询订单列表 -->
    <select id="listOrderByUserId" resultType="com.cxyxiaomo.epp.common.pojo.Order">
        SELECT *
        FROM `order`
        WHERE user_id = #{userId}
        <if test="orderStatus != null and orderStatus != ''">
            AND order_status = #{orderStatus}
        </if>
        order by order_date desc
    </select>

    <!-- 创建订单 -->
    <insert id="insertOrder" parameterType="com.cxyxiaomo.epp.common.pojo.Order" useGeneratedKeys="true"
            keyProperty="orderId">
        INSERT INTO `order` (id, user_id, order_date, order_status, order_price)
        VALUES (#{id}, #{userId}, #{orderDate,javaType=java.time.LocalDateTime,jdbcType=TIMESTAMP}, #{orderStatus},
                #{orderPrice})
    </insert>

    <!-- 批量插入订单详情 -->
    <insert id="batchInsertOrderDetail" parameterType="java.util.List">
        INSERT INTO `order_detail` (order_id, good_id, good_count, unit_price)
        VALUES
        <if test="list != null and list.size() > 0">
            <foreach collection="list" item="detail" separator=",">
                (#{detail.orderId}, #{detail.goodId}, #{detail.goodCount}, #{detail.unitPrice})
            </foreach>
        </if>
        <if test="list == null or list.size() == 0">
            ()
        </if>
    </insert>

    <update id="updateOrderStatus" parameterType="com.cxyxiaomo.epp.common.pojo.Order">
        UPDATE `order`
        SET order_status = #{orderStatus}
        # 如果取消订单，那么更新 cancel_date
        <if test="orderStatus == 'Cancelled'">
            , cancel_date = CURRENT_TIMESTAMP
        </if>
        # 如果支付完成，那么更新 pay_date
        <if test="orderStatus == 'Processing'">
            , pay_date = CURRENT_TIMESTAMP
        </if>
        # 如果已经发货，那么更新 ship_date
        <if test="orderStatus == 'Shipped'">
            , ship_date = CURRENT_TIMESTAMP
        </if>
        # 如果已经确认收货，那么更新 deliver_date
        <if test="orderStatus == 'Delivered'">
            , deliver_date = CURRENT_TIMESTAMP
        </if>
        WHERE id = #{orderId} LIMIT 1
    </update>

    <!-- 根据订单 ID 删除订单信息及订单详情信息 -->
    <delete id="deleteOrderById" parameterType="java.lang.Long">
        DELETE
        FROM `order`
        WHERE id = #{orderId};
        -- 级联删除，无需手动删除订单详情
--         DELETE
--         FROM goods_order_details
--         WHERE order_id = #{orderId};
    </delete>


    <!-- manage 相关的 SQL -->
    <select id="getOrderList" resultType="com.cxyxiaomo.epp.common.pojo.Order"
            parameterType="com.cxyxiaomo.epp.common.query.OrderQuery">
        SELECT * FROM `order`
        <where>
            <if test="id != null">
                AND id = #{id}
            </if>
            <if test="userId != null">
                AND user_id = #{userId}
            </if>
            <if test="orderStatusCode != null and orderStatusCode != ''">
                AND order_status = #{orderStatusCode}
            </if>
            <if test="expressId != null and expressId != ''">
                AND express_id = #{expressId}
            </if>
        </where>
    </select>

    <!-- 更新发货信息 -->
    <update id="updateOrderShipInfo" parameterType="com.cxyxiaomo.epp.common.pojo.Order">
        UPDATE `order`
        <set>
            <if test="expressId != null">
                express_id = #{expressId},
            </if>
            <if test="comment != null">
                `comment` = #{comment},
            </if>
        </set>
        WHERE id = #{orderId}
    </update>
</mapper>
