<?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.TourCarCategoryMapper">
    <resultMap id="BaseResultMap" type="com.tourism.webadmin.back.model.TourCarCategory">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="model" jdbcType="BIGINT" property="model"/>
        <result column="title" jdbcType="VARCHAR" property="title"/>
        <result column="name" jdbcType="VARCHAR" property="name"/>
        <result column="price" jdbcType="DECIMAL" property="price"/>
        <result column="unit" jdbcType="VARCHAR" property="unit"/>
        <result column="seat_number" jdbcType="TINYINT" property="seatNumber"/>
        <result column="image" jdbcType="VARCHAR" property="image"/>
        <result column="type" jdbcType="TINYINT" property="type"/>
        <result column="relation_driver_id" jdbcType="BIGINT" property="relationDriverId"/>
        <result column="label" jdbcType="VARCHAR" property="label"/>
        <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="mark" jdbcType="TINYINT" property="mark"/>
        <result column="dept_id" jdbcType="BIGINT" property="deptId"/>
        <result column="enable" jdbcType="TINYINT" property="enable"/>
        <result column="hot_value" jdbcType="INTEGER" property="hotValue"/>
        <result column="belong_tab" jdbcType="BIGINT" property="belongTab"/>
        <result column="luggage_number" jdbcType="TINYINT" property="luggageNumber"/>
    </resultMap>

    <insert id="insertList">
        INSERT INTO tour_car_category
            (id,
            model,
            title,
            name,
            price,
            unit,
            seat_number,
            image,
            type,
            relation_driver_id,
            label,
            create_user_id,
            create_time,
            update_user_id,
            update_time,
            data_state,
            mark,
            dept_id,
            enable,
            hot_value,
            belong_tab,
            luggage_number)
        VALUES
        <foreach collection="list" index="index" item="item" separator="," >
            (#{item.id},
            #{item.model},
            #{item.title},
            #{item.name},
            #{item.price},
            #{item.unit},
            #{item.seatNumber},
            #{item.image},
            #{item.type},
            #{item.relationDriverId},
            #{item.label},
            #{item.createUserId},
            #{item.createTime},
            #{item.updateUserId},
            #{item.updateTime},
            #{item.dataState},
            #{item.mark},
            #{item.deptId},
            #{item.enable},
            #{item.hotValue},
            #{item.belongTab},
            #{item.luggageNumber})
        </foreach>
    </insert>

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

    <!-- 这里仅包含调用接口输入的主表过滤条件 -->
    <sql id="inputFilterRef">
        <if test="tourCarCategoryFilter != null">
            <if test="tourCarCategoryFilter.model != null">
                AND tour_car_category.model = #{tourCarCategoryFilter.model}
            </if>
            <if test="tourCarCategoryFilter.title != null and tourCarCategoryFilter.title != ''">
                <bind name = "safeTourCarCategoryTitle" value = "'%' + tourCarCategoryFilter.title + '%'" />
                AND tour_car_category.title LIKE #{safeTourCarCategoryTitle}
            </if>
            <if test="tourCarCategoryFilter.name != null and tourCarCategoryFilter.name != ''">
                <bind name = "safeTourCarCategoryName" value = "'%' + tourCarCategoryFilter.name + '%'" />
                AND tour_car_category.name LIKE #{safeTourCarCategoryName}
            </if>
            <if test="tourCarCategoryFilter.priceStart != null">
                AND tour_car_category.price &gt;= #{tourCarCategoryFilter.priceStart}
            </if>
            <if test="tourCarCategoryFilter.priceEnd != null">
                AND tour_car_category.price &lt;= #{tourCarCategoryFilter.priceEnd}
            </if>
            <if test="tourCarCategoryFilter.seatNumber != null">
                AND tour_car_category.seat_number = #{tourCarCategoryFilter.seatNumber}
            </if>
            <if test="tourCarCategoryFilter.type != null">
                AND tour_car_category.type = #{tourCarCategoryFilter.type}
            </if>
            <if test="tourCarCategoryFilter.belongTab != null">
                AND tour_car_category.belong_tab = #{tourCarCategoryFilter.belongTab}
            </if>
            <if test="tourCarCategoryFilter.searchString != null and tourCarCategoryFilter.searchString != ''">
                <bind name = "safeTourCarCategorySearchString" value = "'%' + tourCarCategoryFilter.searchString + '%'" />
                AND CONCAT(IFNULL(tour_car_category.title,''), IFNULL(tour_car_category.name,'')) LIKE #{safeTourCarCategorySearchString}
            </if>
        </if>
    </sql>

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

    <!-- 支持基于一对一或者一对多从表字段过滤的SQL_ID -->
    <select id="getTourCarCategoryListEx" resultMap="BaseResultMap" >
        SELECT
            tour_car_category.*,co.score
        FROM
            tour_car_category
        LEFT JOIN
            (select category_id,ROUND(AVG(category_mark), 0) as score from tour_car_order where category_mark > 0 group by category_id) co on tour_car_category.id = co.category_id
        <if test="tourCarCategoryFilter.type != null and tourCarCategoryFilter.type == 2">
            INNER JOIN
                tour_car_driver ON tour_car_category.relation_driver_id = tour_car_driver.id AND tour_car_driver.data_state = ${@com.tourism.common.core.constant.GlobalDeletedFlag@NORMAL}
        </if>
        <where>
            <include refid="filterRef"/>
            <include refid="com.tourism.webadmin.back.dao.TourCarDriverMapper.inputFilterRef"/>
        </where>
        <if test="orderBy != null and orderBy != ''">
            ORDER BY ${orderBy}
        </if>
    </select>
</mapper>
