<?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.TourCarDriverMapper">
    <resultMap id="BaseResultMap" type="com.tourism.webadmin.back.model.TourCarDriver">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="name" jdbcType="VARCHAR" property="name"/>
        <result column="sex" jdbcType="TINYINT" property="sex"/>
        <result column="age" jdbcType="TINYINT" property="age"/>
        <result column="driving_years" jdbcType="TINYINT" property="drivingYears"/>
        <result column="image" jdbcType="VARCHAR" property="image"/>
        <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="hot_value" jdbcType="INTEGER" property="hotValue"/>
        <result column="dept_id" jdbcType="BIGINT" property="deptId"/>
        <result column="mobile" jdbcType="VARCHAR" property="mobile"/>
        <result column="type" jdbcType="TINYINT" property="type"/>
        <result column="belong_tab" jdbcType="BIGINT" property="belongTab"/>
    </resultMap>

    <insert id="insertList">
        INSERT INTO tour_car_driver
            (id,
            name,
            sex,
            age,
            driving_years,
            image,
            create_user_id,
            create_time,
            update_user_id,
            update_time,
            data_state,
            mark,
            hot_value,
            dept_id,
            mobile,
            type,
            belong_tab)
        VALUES
        <foreach collection="list" index="index" item="item" separator="," >
            (#{item.id},
            #{item.name},
            #{item.sex},
            #{item.age},
            #{item.drivingYears},
            #{item.image},
            #{item.createUserId},
            #{item.createTime},
            #{item.updateUserId},
            #{item.updateTime},
            #{item.dataState},
            #{item.mark},
            #{item.hotValue},
            #{item.deptId},
            #{item.mobile},
            #{item.type},
            #{item.belongTab})
        </foreach>
    </insert>

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

    <!-- 这里仅包含调用接口输入的主表过滤条件 -->
    <sql id="inputFilterRef">
        <if test="tourCarDriverFilter != null">
            <if test="tourCarDriverFilter.id != null">
                AND tour_car_driver.id = #{tourCarDriverFilter.id}
            </if>
            <if test="tourCarDriverFilter.name != null and tourCarDriverFilter.name != ''">
                <bind name = "safeTourCarDriverName" value = "'%' + tourCarDriverFilter.name + '%'" />
                AND tour_car_driver.name LIKE #{safeTourCarDriverName}
            </if>
            <if test="tourCarDriverFilter.sex != null">
                AND tour_car_driver.sex = #{tourCarDriverFilter.sex}
            </if>
            <if test="tourCarDriverFilter.searchString != null and tourCarDriverFilter.searchString != ''">
                <bind name = "safeTourCarDriverSearchString" value = "'%' + tourCarDriverFilter.searchString + '%'" />
                AND IFNULL(tour_car_driver.name,'') LIKE #{safeTourCarDriverSearchString}
            </if>
            <if test="tourCarDriverFilter.type != null">
                AND tour_car_driver.type = #{tourCarDriverFilter.type}
            </if>
            <if test="tourCarDriverFilter.searchString != null and tourCarDriverFilter.searchString != ''">
                <bind name = "safeTourCarDriverSearchString" value = "'%' + tourCarDriverFilter.searchString + '%'" />
                AND IFNULL(tour_car_driver.name,'') LIKE #{safeTourCarDriverSearchString}
            </if>
            <if test="tourCarDriverFilter.belongTab != null">
                AND tour_car_driver.belong_tab = #{tourCarDriverFilter.belongTab}
            </if>
            <if test="tourCarDriverFilter.ageBegin != null">
                AND tour_car_driver.age >= #{tourCarDriverFilter.ageBegin}
            </if>
            <if test="tourCarDriverFilter.ageEnd != null">
                <![CDATA[AND tour_car_driver.age <= #{tourCarDriverFilter.ageEnd}]]>
            </if>
            <if test="tourCarDriverFilter.id != null">
                AND tour_car_driver.id = #{tourCarDriverFilter.id}
            </if>
            <if test="tourCarDriverFilter.startDate != null">
                AND tour_car_driver.id not in (
                    select driver_id from tour_car_order co where co.state in (1,3) and co.driver_id = tour_car_driver.id
                    and ((co.start_time  between #{tourCarDriverFilter.startDate} and #{tourCarDriverFilter.endDate}
                    or  co.end_time between #{tourCarDriverFilter.startDate} and #{tourCarDriverFilter.endDate})
                    or (<![CDATA[#{tourCarDriverFilter.startDate} < co.start_time and #{tourCarDriverFilter.endDate} > co.end_time]]>))
                )
            </if>
            <if test="tourCarDriverFilter.createUserId != null">
                AND tour_car_category.create_user_id = #{tourCarDriverFilter.createUserId}
            </if>
            <if test="tourCarDriverFilter.mobile != null and tourCarDriverFilter.mobile != ''">
                <bind name = "safeTourCarDriverMobile" value = "'%' + tourCarDriverFilter.mobile + '%'" />
                AND tour_car_driver.mobile LIKE #{safeTourCarDriverMobile}
            </if>
        </if>
    </sql>

    <select id="getTourCarDriverList" resultMap="BaseResultMap" parameterType="com.tourism.webadmin.back.model.TourCarDriver">
        SELECT *,co.score FROM tour_car_driver
        left join (select driver_id,ROUND(AVG(drive_mark), 0) as score from tour_car_order where drive_mark > 0 group by driver_id) co
        on tour_car_driver.id = co.driver_id
        <where>
            <include refid="filterRef"/>
        </where>
        <if test="orderBy != null and orderBy != ''">
            ORDER BY ${orderBy}
        </if>
    </select>
</mapper>
