<?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="org.linlinjava.litemall.db.dao.OrderMapper">
    <update id="updateWithOptimisticLocker" parameterType="map">
        update litemall_order
        <set>
            <if test="order.id != null">
                id = #{order.id,jdbcType=INTEGER},
            </if>
            <if test="order.userId != null">
                user_id = #{order.userId,jdbcType=INTEGER},
            </if>
            <if test="order.orderSn != null">
                order_sn = #{order.orderSn,jdbcType=VARCHAR},
            </if>
            <if test="order.orderStatus != null">
                order_status = #{order.orderStatus,jdbcType=SMALLINT},
            </if>
            <if test="order.aftersaleStatus != null">
                aftersale_status = #{order.aftersaleStatus,jdbcType=SMALLINT},
            </if>
            <if test="order.consignee != null">
                consignee = #{order.consignee,jdbcType=VARCHAR},
            </if>
            <if test="order.mobile != null">
                mobile = #{order.mobile,jdbcType=VARCHAR},
            </if>
            <if test="order.address != null">
                address = #{order.address,jdbcType=VARCHAR},
            </if>
            <if test="order.message != null">
                message = #{order.message,jdbcType=VARCHAR},
            </if>
            <if test="order.goodsPrice != null">
                goods_price = #{order.goodsPrice,jdbcType=DECIMAL},
            </if>
            <if test="order.freightPrice != null">
                freight_price = #{order.freightPrice,jdbcType=DECIMAL},
            </if>
            <if test="order.couponPrice != null">
                coupon_price = #{order.couponPrice,jdbcType=DECIMAL},
            </if>
            <if test="order.integralPrice != null">
                integral_price = #{order.integralPrice,jdbcType=DECIMAL},
            </if>
            <if test="order.grouponPrice != null">
                groupon_price = #{order.grouponPrice,jdbcType=DECIMAL},
            </if>
            <if test="order.orderPrice != null">
                order_price = #{order.orderPrice,jdbcType=DECIMAL},
            </if>
            <if test="order.actualPrice != null">
                actual_price = #{order.actualPrice,jdbcType=DECIMAL},
            </if>
            <if test="order.payId != null">
                pay_id = #{order.payId,jdbcType=VARCHAR},
            </if>
            <if test="order.payTime != null">
                pay_time = #{order.payTime,jdbcType=TIMESTAMP},
            </if>
            <if test="order.shipSn != null">
                ship_sn = #{order.shipSn,jdbcType=VARCHAR},
            </if>
            <if test="order.shipChannel != null">
                ship_channel = #{order.shipChannel,jdbcType=VARCHAR},
            </if>
            <if test="order.shipTime != null">
                ship_time = #{order.shipTime,jdbcType=TIMESTAMP},
            </if>
            <if test="order.refundAmount != null">
                refund_amount = #{order.refundAmount,jdbcType=DECIMAL},
            </if>
            <if test="order.refundType != null">
                refund_type = #{order.refundType,jdbcType=VARCHAR},
            </if>
            <if test="order.refundContent != null">
                refund_content = #{order.refundContent,jdbcType=VARCHAR},
            </if>
            <if test="order.refundTime != null">
                refund_time = #{order.refundTime,jdbcType=TIMESTAMP},
            </if>
            <if test="order.confirmTime != null">
                confirm_time = #{order.confirmTime,jdbcType=TIMESTAMP},
            </if>
            <if test="order.comments != null">
                comments = #{order.comments,jdbcType=SMALLINT},
            </if>
            <if test="order.endTime != null">
                end_time = #{order.endTime,jdbcType=TIMESTAMP},
            </if>
            <if test="order.addTime != null">
                add_time = #{order.addTime,jdbcType=TIMESTAMP},
            </if>
            <if test="order.updateTime != null">
                update_time = #{order.updateTime,jdbcType=TIMESTAMP},
            </if>
            <if test="order.deleted != null">
                deleted = #{order.deleted,jdbcType=BIT},
            </if>
        </set>
        where id = #{order.id,jdbcType=INTEGER} and update_time = #{lastUpdateTime,jdbcType=INTEGER}
    </update>

    <select id="getOrderIds" resultType="hashmap">
        select o.id, o.add_time
        from litemall_order o
        left join litemall_user u
        on o.user_id = u.id
        left join litemall_order_goods og
        on o.id = og.order_id
        <where>
            <if test="query != null">
                ${query}
            </if>
        </where>
        group by o.id
        <if test="orderByClause != null">
            order by ${orderByClause}
        </if>
    </select>
    <resultMap type="org.linlinjava.litemall.db.domain.OrderVo" id="orderList">
        <id column="id" property="id"/>
        <result column="order_sn" property="orderSn"/>
        <result column="order_status" property="orderStatus"/>
        <result column="actual_price" property="actualPrice"/>
        <result column="freight_price" property="freightPrice"/>
        <result column="integral_price" property="integralPrice"/>
        <result column="order_price" property="orderPrice"/>
        <result column="pay_time" property="payTime"/>
        <result column="add_time" property="addTime"/>
        <result column="ship_channel" property="shipChannel"/>
        <result column="ship_sn" property="shipSn"/>
        <result column="consignee" property="consignee"/>
        <result column="address" property="address"/>
        <result column="mobile" property="mobile"/>
        <result column="message" property="message"/>
        <result column="user_id" property="userId"/>
        <result column="user_name" property="userName"/>
        <result column="user_avatar" property="userAvatar"/>

        <collection property="goodsVoList" ofType="org.linlinjava.litemall.db.domain.OrderGoodsVo">
            <id column="ogid" property="id"/>
            <result column="goods_id" property="goodsId"/>
            <result column="product_id" property="productId"/>
            <result column="goods_name" property="goodsName"/>
            <result column="goods_picture" property="picUrl"/>
            <result column="goods_specifications" property="specifications" typeHandler="org.linlinjava.litemall.db.mybatis.JsonStringArrayTypeHandler"/>
            <result column="goods_number" property="number"/>
            <result column="goods_price" property="price"/>
        </collection>
    </resultMap>
    <select id="getOrderList" resultMap="orderList">
        select o.id, o.order_sn, o.order_status, o.actual_price, o.freight_price, o.add_time, o.message,
        o.consignee, o.address, o.mobile, o.pay_time, o.order_price, o.ship_channel, o.ship_sn,
        u.id user_id, u.nickname user_name, u.avatar user_avatar, o.integral_price,
        og.id ogid, og.goods_id, og.product_id, og.goods_name, og.pic_url goods_picture,
        og.specifications goods_specifications, og.number goods_number, og.price goods_price
        from litemall_order o
        left join litemall_user u
        on o.user_id = u.id
        left join litemall_order_goods og
        on o.id = og.order_id
        left join litemall_goods g
        on og.goods_id = g.id
        <where>
            <if test="query != null">
                ${query}
            </if>
        </where>
        <if test="orderByClause != null">
            order by ${orderByClause}
        </if>
    </select>
</mapper>