<?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.tourism.webadmin.back.dao.DeliveryOrderMapper">
    <resultMap id="BaseResultMap" type="com.tourism.webadmin.back.model.DeliveryOrder">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="delivery_time" jdbcType="TIMESTAMP" property="deliveryTime"/>
        <result column="delivery_address" jdbcType="VARCHAR" property="deliveryAddress"/>
        <result column="delivery_phone" jdbcType="VARCHAR" property="deliveryPhone"/>
        <result column="delivery_remark" jdbcType="VARCHAR" property="deliveryRemark"/>
        <result column="tableware" jdbcType="TINYINT" property="tableware"/>
        <result column="order_no" jdbcType="VARCHAR" property="orderNo"/>
        <result column="order_time" jdbcType="TIMESTAMP" property="orderTime"/>
        <result column="pay_type" jdbcType="TINYINT" property="payType"/>
        <result column="order_amount" jdbcType="DECIMAL" property="orderAmount"/>
        <result column="pay_amount" jdbcType="DECIMAL" property="payAmount"/>
        <result column="order_status" jdbcType="TINYINT" property="orderStatus"/>
        <result column="customer_id" jdbcType="BIGINT" property="customerId"/>
        <result column="refund_status" jdbcType="TINYINT" property="refundStatus"/>
        <result column="pack_charge" jdbcType="DECIMAL" property="packCharge"/>
        <result column="delivery_fee" jdbcType="DECIMAL" property="deliveryFee"/>
        <result column="create_user_id" jdbcType="BIGINT" property="createUserId"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="update_user_id" jdbcType="BIGINT" property="updateUserId"/>
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
        <result column="data_state" jdbcType="TINYINT" property="dataState"/>
        <result column="shop_name" jdbcType="VARCHAR" property="shopName"/>
    </resultMap>

    <insert id="insertList" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO tour_delivery_order
            (id,
            delivery_time,
            delivery_address,
            delivery_phone,
            delivery_remark,
            tableware,
            order_no,
            order_time,
            pay_type,
            order_amount,
            pay_amount,
            order_status,
            customer_id,
            refund_status,
            pack_charge,
            delivery_fee,
            create_user_id,
            create_time,
            update_user_id,
            update_time,
            data_state,
            shop_name)
        VALUES
        <foreach collection="list" index="index" item="item" separator="," >
            (#{item.id},
            #{item.deliveryTime},
            #{item.deliveryAddress},
            #{item.deliveryPhone},
            #{item.deliveryRemark},
            #{item.tableware},
            #{item.orderNo},
            #{item.orderTime},
            #{item.payType},
            #{item.orderAmount},
            #{item.payAmount},
            #{item.orderStatus},
            #{item.customerId},
            #{item.refundStatus},
            #{item.packCharge},
            #{item.deliveryFee},
            #{item.createUserId},
            #{item.createTime},
            #{item.updateUserId},
            #{item.updateTime},
            #{item.dataState},
            #{item.shopName})
        </foreach>
    </insert>

    <!-- 如果有逻辑删除字段过滤，请写到这里 -->
    <sql id="filterRef">
        <!-- 这里必须加上全包名，否则当filterRef被其他Mapper.xml包含引用的时候，就会调用Mapper.xml中的该SQL片段 -->
        <include refid="com.tourism.webadmin.back.dao.DeliveryOrderMapper.inputFilterRef"/>
        AND tour_delivery_order.data_state = ${@com.tourism.common.core.constant.GlobalDeletedFlag@NORMAL}
    </sql>

    <!-- 这里仅包含调用接口输入的主表过滤条件 -->
    <sql id="inputFilterRef">
        <if test="deliveryOrderFilter != null">
            <if test="deliveryOrderFilter.shopName != null and deliveryOrderFilter.shopName != ''">
                <bind name = "safeDeliveryOrderShopName" value = "'%' + deliveryOrderFilter.shopName + '%'" />
                AND tour_delivery_order.shop_name LIKE #{safeDeliveryOrderShopName}
            </if>
            <if test="deliveryOrderFilter.orderNo != null and deliveryOrderFilter.orderNo != ''">
                <bind name = "safeDeliveryOrderOrderNo" value = "'%' + deliveryOrderFilter.orderNo + '%'" />
                AND tour_delivery_order.order_no LIKE #{safeDeliveryOrderDeliveryorderNo}
            </if>
            <if test="deliveryOrderFilter.deliveryPhone != null and deliveryOrderFilter.deliveryPhone != ''">
                <bind name = "safeDeliveryOrderDeliveryPhone" value = "'%' + deliveryOrderFilter.deliveryPhone + '%'" />
                AND tour_delivery_order.delivery_phone LIKE #{safeDeliveryOrderDeliveryPhone}
            </if>
            <if test="deliveryOrderFilter.payType != null">
                AND tour_delivery_order.pay_type = #{deliveryOrderFilter.payType}
            </if>
            <if test="deliveryOrderFilter.orderStatus != null">
                AND tour_delivery_order.order_status = #{deliveryOrderFilter.orderStatus}
            </if>
            <if test="deliveryOrderFilter.refundStatus != null">
                AND tour_delivery_order.refund_status = #{deliveryOrderFilter.refundStatus}
            </if>
            <!-- 用户id是否为空 -->
            <if test="deliveryOrderFilter.createUserId != null">
                AND tour_delivery_order.create_user_id = #{deliveryOrderFilter.createUserId}
            </if>
        </if>
    </sql>

    <select id="getDeliveryOrderList" resultMap="BaseResultMap" parameterType="com.tourism.webadmin.back.model.DeliveryOrder">
        SELECT * FROM tour_delivery_order
        <where>
            <include refid="filterRef"/>
        </where>
        <if test="orderBy != null and orderBy != ''">
            ORDER BY ${orderBy}
        </if>
    </select>
</mapper>
