<?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="com.seer.teach.mall.mapper.OrderMapper">

    <resultMap id="orderResultMap" type="com.seer.teach.mall.entity.OrderEntity">
        <id column="id" property="id"/>
        <result column="order_sn" property="orderSn"/>
        <result column="user_id" property="userId"/>
        <result column="order_status" property="orderStatus"/>
        <result column="shipping_status" property="shippingStatus"/>
        <result column="pay_status" property="payStatus"/>
        <result column="consignee" property="consignee"/>
        <result column="address_id" property="addressId"/>
        <result column="province" property="province"/>
        <result column="city" property="city"/>
        <result column="district" property="district"/>
        <result column="address" property="address"/>
        <result column="mobile" property="mobile"/>
        <result column="remark" property="remark"/>
        <result column="postscript" property="postscript"/>
        <result column="pay_order_id" property="payOrderId"/>
        <result column="payment_mode" property="paymentMode"/>
        <result column="pay_channel_code" property="payChannelCode"/>
        <result column="comment_status" property="commentStatus"/>
        <result column="actual_price" property="actualPrice"/>
        <result column="order_price" property="orderPrice"/>
        <result column="goods_price" property="goodsPrice"/>
        <result column="order_total_coin" property="orderTotalCoin"/>
        <result column="add_time" property="addTime"/>
        <result column="confirm_time" property="confirmTime"/>
        <result column="pay_time" property="payTime"/>
        <result column="shipping_no" property="shippingNo"/>
        <result column="delivery_user_id" property="deliveryUserId"/>
        <result column="delivery_time" property="deliveryTime"/>
        <result column="logistics_code" property="logisticsCode"/>
        <result column="coin_config_id" property="coinConfigId"/>
        <result column="exchange_ratio" property="exchangeRatio"/>
        <result column="deleted" property="deleted"/>
        <result column="update_time" property="updateTime"/>
        <result column="create_by" property="createBy"/>
        <result column="create_time" property="createTime"/>
        <result column="update_by" property="updateBy"/>
        <result column="tenant_id" property="tenantId"/>
    </resultMap>

    <select id="getOrderPage" parameterType="com.seer.teach.mall.query.OrderQueryCondition" resultMap="orderResultMap">
        SELECT o.id, o.order_sn, o.user_id, o.order_status, o.shipping_status, o.pay_status, o.consignee,
        o.address_id, o.province, o.city, o.district, o.address, o.mobile, o.remark, o.postscript,
        o.pay_order_id, o.payment_mode, o.pay_channel_code, o.comment_status, o.actual_price,
        o.order_price, o.goods_price, o.order_total_coin, o.add_time, o.confirm_time, o.pay_time,
        o.shipping_no, o.delivery_user_id, o.delivery_time, o.logistics_code, o.coin_config_id,
        o.exchange_ratio, o.deleted, o.update_time, o.create_by, o.create_time, o.update_by, o.tenant_id
        FROM mall_order o
        <if test="condition.childOrderStatus != null">
            inner join mall_order_child oc on o.id = oc.parent_order_id
        </if>
        <where>
            1=1
            <if test="condition.orderStatus != null">
                AND o.order_status = #{condition.orderStatus}
            </if>
            <if test="condition.userId != null">
                AND o.user_id = #{condition.userId}
            </if>
            <if test="condition.orderSn != null and condition.orderSn != ''">
                AND o.order_sn = #{condition.orderSn}
            </if>
            <if test="condition.orderStatusList != null and condition.orderStatusList.size() > 0">
                AND o.order_status IN
                <foreach collection="condition.orderStatusList" item="status" open="(" separator="," close=")">
                    #{status}
                </foreach>
            </if>
            <if test="condition.consignee != null and condition.consignee != ''">
                AND o.consignee LIKE CONCAT(#{condition.consignee}, '%')
            </if>
            <if test="condition.mobile != null and condition.mobile != ''">
                AND o.mobile LIKE CONCAT(#{condition.mobile}, '%')
            </if>
            <if test="condition.payStatus != null">
                AND o.pay_status = #{condition.payStatus}
            </if>
            <if test="shippingStatus != null">
                AND o.shipping_status = #{shippingStatus}
            </if>
            <if test="condition.tenantId != null and condition.tenantId != ''">
                AND o.tenant_id = #{condition.tenantId}
            </if>
            <if test="condition.childOrderStatus != null">
                AND oc.order_status = #{condition.childOrderStatus}
            </if>
        </where>
        GROUP BY o.id
        ORDER BY o.id DESC
    </select>

</mapper>