<?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.custom.mapper.CustomCarMapper">

    <!-- 获取定位数据列表-->
    <select id="getGpsCarList" resultType="java.util.Map">
        select a.location_longitude,a.location_latitude,a.gpstime,a.carid,a.speed,b.car_number
        from tf_car_gps_upload a
        left join tf_car_baseinfo b on a.carid = b.id
        where a.gpstime >= #{gpsCarSearch.startTime}
        <![CDATA[    and a.gpstime <= #{gpsCarSearch.endTime}  ]]>
        <if test="gpsCarSearch.carId != null">
            and a.carid = #{gpsCarSearch.carId}
        </if>
        order by a.gpstime
    </select>

    <!-- 获取分段 -->
    <select id="getSection" resultType="java.util.Map">
        select a.location_longitude,a.location_latitude,a.gpstime,a.carid,a.speed,b.car_number
        from tf_car_gps_upload a
        left join tf_car_baseinfo b on a.carid = b.id
        where a.gpstime >= #{gpsCarSearch.startTime}
        <![CDATA[    and a.gpstime <= #{gpsCarSearch.endTime}  ]]>
        <if test="gpsCarSearch.carId != null">
            and a.carid = #{gpsCarSearch.carId}
        </if>
        order by a.gpstime
    </select>

    <!-- 车辆监控状态列表 -->
    <select id="listCarWatch" resultType="java.util.Map">
        select a.id as carId,a.department_id as deptId,(select name from tf_smpn_department_x d where
        d.id=a.department_id)as deptName,a.car_number as carNum,a.car_pic as avatar
        from tf_car_baseinfo a where a.del_flag = 1
        <if test="deptId != null">
            and a.department_id = #{deptId}
        </if>
        <if test="carId != null">
            and a.id = #{carId}
        </if>
        <if test="date != null">
            <![CDATA[and a.create_time <= #{date}]]>
        </if>
    </select>

    <select id="listCarWatch2" resultType="java.util.Map">
        select a.id as carId,a.department_id as deptId,(select name from tf_smpn_department_x d where
        d.id=a.department_id)as deptName,a.car_number as carNum,a.car_pic as avatar
        from tf_car_baseinfo a where a.del_flag = 0
        <if test="deptId != null">
            and a.department_id = #{deptId}
        </if>
        <if test="carId != null">
            and a.id = #{carId}
        </if>
        <if test="date != null">
            <![CDATA[and a.create_time <= #{date}]]>
            and a.del_time >= #{date}
        </if>
    </select>

    <resultMap id="listCarWatchMap" type="com.tofly.pojo.car.cusom.CarUploadResult">
        <result property="carId" column="CARID"/>
        <result property="carNumber" column="CAR_NUMBER"/>
        <result property="deptId" column="DEPARTMENT_ID"/>
        <result property="avatar" column="CAR_PIC"/>
        <association property="deptName" column="COMPANYID" select="getdeptName"></association>
    </resultMap>

    <select id="getdeptName" resultType="java.lang.Object">
      select name as deptName from tf_smpn_department_x where id=#{companyid}
    </select>


    <!-- 车辆油耗记录 -->
    <select id="getCarOilPage" resultType="java.util.Map">
        select o.*,b.car_number as car_num,(select u.real_name from TF_SMPN_USER_X u where u.id=o.create_user) as
        create_user_name from TF_CAR_OIL o,TF_CAR_BASEINFO b where b.id = o.car_id and b.del_flag = '1' and o.del_flag =
        '1'
        <if test="carOil.carId != null">
            and o.carId = #{carOil.carId}
        </if>
    </select>


</mapper>
