<?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.TourOrderMapper">
    <resultMap id="BaseResultMap" type="com.tourism.webadmin.back.model.TourOrder">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="order_no" jdbcType="VARCHAR" property="orderNo"/>
        <result column="departure_date" jdbcType="TIMESTAMP" property="departureDate"/>
        <result column="end_date" jdbcType="TIMESTAMP" property="endDate"/>
        <result column="adult_number" jdbcType="INTEGER" property="adultNumber"/>
        <result column="children_number" jdbcType="INTEGER" property="childrenNumber"/>
        <result column="currency" jdbcType="VARCHAR" property="currency"/>
        <result column="total_amount" jdbcType="DECIMAL" property="totalAmount"/>
        <result column="room_number" jdbcType="INTEGER" property="roomNumber"/>
        <result column="car_id" jdbcType="BIGINT" property="carId"/>
        <result column="car_amount" jdbcType="DECIMAL" property="carAmount"/>
        <result column="order_date" jdbcType="TIMESTAMP" property="orderDate"/>
        <result column="order_status" jdbcType="TINYINT" property="orderStatus"/>
        <result column="project_id" jdbcType="BIGINT" property="projectId"/>
        <result column="project_resume" jdbcType="VARCHAR" property="projectResume"/>
        <result column="customer_name" jdbcType="VARCHAR" property="customerName"/>
        <result column="customer_mobile" jdbcType="VARCHAR" property="customerMobile"/>
        <result column="customer_wechat" jdbcType="VARCHAR" property="customerWechat"/>
        <result column="customer_mobile_standby" jdbcType="VARCHAR" property="customerMobileStandby"/>
        <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="children_price" jdbcType="DECIMAL" property="childrenPrice"/>
        <result column="adult_price" jdbcType="DECIMAL" property="adultPrice"/>
        <result column="count_times" jdbcType="VARCHAR" property="countTimes"/>
        <result column="project_title" jdbcType="VARCHAR" property="projectTitle"/>
    </resultMap>

    <insert id="insertList">
        INSERT INTO tour_order
            (id,
            order_no,
            departure_date,
            end_date,
            adult_number,
            children_number,
            currency,
            total_amount,
            room_number,
            car_id,
            car_amount,
            order_date,
            order_status,
            project_id,
            project_resume,
            customer_name,
            customer_mobile,
            create_user_id,
            create_time,
            update_user_id,
            update_time,
            data_state,
            customer_wechat,
            customer_mobile_standby,
            children_price,
            adult_price,
            count_times,
            project_title)
        VALUES
        <foreach collection="list" index="index" item="item" separator="," >
            (#{item.id},
            #{item.orderNo},
            #{item.departureDate},
            #{item.endDate},
            #{item.adultNumber},
            #{item.childrenNumber},
            #{item.currency},
            #{item.totalAmount},
            #{item.roomNumber},
            #{item.carId},
            #{item.carAmount},
            #{item.orderDate},
            #{item.orderStatus},
            #{item.projectId},
            #{item.projectResume},
            #{item.customerName},
            #{item.customerMobile},
            #{item.createUserId},
            #{item.createTime},
            #{item.updateUserId},
            #{item.updateTime},
            #{item.dataState},
            #{item.customerWechat},
            #{item.customerMobileStandby},
             #{item.childrenPrice},
             #{item.adultPrice},
             #{item.countTimes},
             #{item.projectTitle})
        </foreach>
    </insert>

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

    <!-- 这里仅包含调用接口输入的主表过滤条件 -->
    <sql id="inputFilterRef">
        <if test="tourOrderFilter != null">
            <if test="tourOrderFilter.departureDateStart != null and tourOrderFilter.departureDateStart != ''">
                AND tour_order.departure_date &gt;= #{tourOrderFilter.departureDateStart}
            </if>
            <if test="tourOrderFilter.departureDateEnd != null and tourOrderFilter.departureDateEnd != ''">
                AND tour_order.departure_date &lt;= #{tourOrderFilter.departureDateEnd}
            </if>
            <if test="tourOrderFilter.endDateStart != null and tourOrderFilter.endDateStart != ''">
                AND tour_order.end_date &gt;= #{tourOrderFilter.endDateStart}
            </if>
            <if test="tourOrderFilter.endDateEnd != null and tourOrderFilter.endDateEnd != ''">
                AND tour_order.end_date &lt;= #{tourOrderFilter.endDateEnd}
            </if>
            <if test="tourOrderFilter.orderStatus != null">
                AND tour_order.order_status = #{tourOrderFilter.orderStatus}
            </if>
            <if test="tourOrderFilter.projectId != null">
                AND tour_order.project_id = #{tourOrderFilter.projectId}
            </if>
            <if test="tourOrderFilter.projectResume != null and tourOrderFilter.projectResume != ''">
                <bind name = "safeTourOrderProjectResume" value = "'%' + tourOrderFilter.projectResume + '%'" />
                AND tour_order.project_resume LIKE #{safeTourOrderProjectResume}
            </if>
            <if test="tourOrderFilter.customerName != null and tourOrderFilter.customerName != ''">
                <bind name = "safeTourOrderCustomerName" value = "'%' + tourOrderFilter.customerName + '%'" />
                AND tour_order.customer_name LIKE #{safeTourOrderCustomerName}
            </if>
            <if test="tourOrderFilter.customerWechat != null and tourOrderFilter.customerWechat != ''">
                <bind name = "safeTourOrderCustomerWechat" value = "'%' + tourOrderFilter.customerWechat + '%'" />
                AND tour_order.customer_wechat LIKE #{safeTourOrderCustomerWechat}
            </if>
            <if test="tourOrderFilter.customerMobile != null and tourOrderFilter.customerMobile != ''">
                <bind name = "safeTourOrderCustomerMobile" value = "'%' + tourOrderFilter.customerMobile + '%'" />
                AND tour_order.customer_mobile LIKE #{safeTourOrderCustomerMobile}
            </if>
            <if test="tourOrderFilter.searchString != null and tourOrderFilter.searchString != ''">
                <bind name = "safeTourOrderSearchString" value = "'%' + tourOrderFilter.searchString + '%'" />
                AND CONCAT(IFNULL(tour_order.project_resume,''), IFNULL(tour_order.customer_name,''), IFNULL(tour_order.customer_mobile,'')) LIKE #{safeTourOrderSearchString}
            </if>
            <if test="tourOrderFilter.createUserId != null">
                AND tour_order.create_user_id = #{tourOrderFilter.createUserId}
            </if>
            <if test="tourOrderFilter.groupPurchaseProgressId != null">
                AND tour_order.group_purchase_progress_id = #{tourOrderFilter.groupPurchaseProgressId}
            </if>
        </if>
    </sql>

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

    <select id="getTourUserList" resultType="com.tourism.webadmin.back.model.TourUser" parameterType="com.tourism.webadmin.back.model.TourUser">
        select tu.user_id,tu.show_name,tu.head_image_url,to1.create_time, to1.adult_number + to1.children_number as people_number from tour_user tu
        inner join tour_order to1 on tu.user_id = to1.create_user_id
        <where>
            to1.type = 1 and to1.order_status != 3
            <if test="groupUserFilter != null">
                <if test="groupUserFilter.projectId != null">
                    AND to1.project_id = #{groupUserFilter.projectId}
                </if>
                <if test="groupUserFilter.groupPurchaseProgressId != null">
                    AND to1.group_purchase_progress_id = #{groupUserFilter.groupPurchaseProgressId}
                </if>
                <if test="groupUserFilter.currentOrderId != null">
                    AND to1.id != #{groupUserFilter.currentOrderId}
                </if>
            </if>
        </where>
    </select>
</mapper>
