<?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.tofly.gpsboot.mapper.car.CarMapper">

    <resultMap id="carMap" type="com.tofly.pojo.plan.Car">
        <id property="id" column="id"/>
        <result property="carNo" column="car_no"/>
        <result property="type" column="type"/>
        <result property="model" column="model"/>
        <result property="department" column="department"/>
        <result property="chargePerson" column="charge_person"/>
        <result property="phone" column="phone"/>
        <result property="status" column="status"/>
        <result property="note" column="note"/>
        <result property="createUser" column="CREATE_USER"/>
        <result property="createTime" column="CREATE_TIME"/>
        <result property="delFlag" column="del_Flag"/>
        <association property="chargePersonName" column="charge_person"
                     select="selectUsername"/>
        <association property="createUserName" column="CREATE_USER"
                     select="selectUsername"/>
        <association property="departmentName" column="department"
                     select="selectDepartmentname"/>
    </resultMap>
        <resultMap id="carTrackMap" type="com.tofly.pojo.car.vo.CarTrackVo">
            <result property="id" column="id"/>
            <result property="type" column="type"/>
            <result property="model" column="model"/>
            <result property="carNo" column="car_no"/>
            <result property="carId" column="car_Id"/>
            <result property="note" column="note"/>
            <result property="beginDate" column="begin_Date"/>
            <result property="endDate" column="end_Date"/>
            <result property="userId" column="user_id"/>
            <association property="userName" column="USER_ID"
                         select="selectUsername"/>
            <association property="chargePersonName" column="charge_person"
                         select="selectUsername"/>
            <association property="departmentName" column="department"
                         select="selectDepartmentname"/>

        </resultMap>

    <select id="selectUsername"  resultType="java.lang.Object">
        select  real_name  from tf_smpn_user_x where id=#{id}
    </select>
    <select id="selectDepartmentname"  resultType="java.lang.Object">
        SELECT  name from tf_smpn_department_x where  id=#{id}
    </select>
    <select id="getCarTrackPage" resultMap="carTrackMap">
        SELECT
            a.id,
             a.car_id,
            a.USER_ID,
            b.type,
            b.car_no,
            b.charge_person,
            b.department,
            a.begin_Date,
            a.end_Date,
            b.model model

        FROM
            tf_ywpn_caruser_w a
                LEFT JOIN tf_ywpn_car_w b ON a.car_id = b.id
                left join tf_smpn_user_x u on a.user_id=u.id

                                                  where 1=1
            <if test="query!=null">
                <if test="query.queryLike !=null and query.queryLike !=''">
                    and (
                    b.car_no like '%' || #{query.queryLike} || '%' or
                    u.real_name like '%' || #{query.queryLike} || '%'
                    )
                </if>
                <if test="query.department !=null ">
                and    b.department=#{query.department}
                </if>
                <if test="query.beginTime !=null">
                       and  a.begin_Date &gt;=#{query.beginTime}
                </if>
                <if test="query.endTime !=null ">
                   and  a.begin_Date  &lt;= #{query.endTime}
                </if>
            </if>

    </select>
</mapper>
