<?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.sckw.fleet.dao.KwfTruckReportMapper">

    <select id="statistics" resultType="com.sckw.fleet.model.vo.KwfTableTopCount" parameterType="java.util.Map" >
        SELECT
            tr.`auth_status` value, count(0) total
        from kwf_truck_report trr
        left join kwf_truck tr on tr.id = trr.truck_id
        left join kwf_driver dr on dr.id = trr.driver_id
        left join kwf_fleet_truck flt on flt.truck_id = tr.id and flt.ent_id = trr.ent_id and flt.del_flag = 0
        left join kwf_fleet fl on fl.id = flt.fleet_id and fl.ent_id = trr.ent_id and fl.del_flag = 0
        where trr.del_flag = 0 and tr.del_flag = 0 and dr.del_flag = 0
        <if test="entId != null and entId != ''">
            and trr.ent_id = #{entId, jdbcType=VARCHAR}
        </if>
        <if test="truckNo != null and truckNo != ''">
            and tr.truck_no = #{truckNo, jdbcType=VARCHAR}
        </if>
        <if test="truckType != null and truckType != ''">
            and tr.type = #{truckType, jdbcType=VARCHAR}
        </if>
        <if test="fleetId != null and fleetId != ''">
            and fl.id = #{fleetId, jdbcType=VARCHAR}
        </if>
        <if test="fleetName != null and fleetName != ''">
            and fl.name like concat('%',#{fleetName},'%')
        </if>
        <if test="status != null and status != ''">
            and tr.status = #{status, jdbcType=VARCHAR}
        </if>
        <if test="startTime != null and startTime != '' " >
            and DATE( trr.create_time) <![CDATA[ >= ]]> #{startTime,jdbcType=TIMESTAMP}
        </if>
        <if test="endTime != null and endTime != '' " >
            and DATE( trr.create_time ) <![CDATA[ <= ]]> #{endTime,jdbcType=TIMESTAMP}
        </if>
        <if test="keywords != null and keywords != ''">
            and (
            tr.truck_no like concat('%',#{keywords},'%')
            or dr.name like concat('%',#{keywords},'%')
            or dr.phone like concat('%',#{keywords},'%')
            )
        </if>
        GROUP BY tr.`auth_status`
    </select>

    <select id="findPage" resultType="com.sckw.fleet.model.vo.KwfTruckReportVo" parameterType="java.util.Map" >
        SELECT
            trr.id, trr.ent_id entId, trr.truck_id truckId, driver_id driverId, trr.remark, tr.status,
            trr.create_by createBy, trr.create_time createTime, trr.update_time updateTime, trr.remark,
            tr.truck_no truckNo, tr.actual_weight actualWeight, tr.business_status businessStatus,
            tr.auth_status truckAuthStatus, dr.`name` driverName, dr.phone driverPhone, dr.idcard driverIdcard,
            dr.ent_id driverEntId, dr.auth_status driverAuthStatus, tr.ent_id truckEntId, fl.name truckFleetName,
            tr.type truckType, tr.color, tr.trailer_no trailerNo
        from kwf_truck_report trr
        left join kwf_truck tr on tr.id = trr.truck_id
        left join kwf_driver dr on dr.id = trr.driver_id
        left join kwf_fleet_truck flt on flt.truck_id = tr.id and flt.ent_id = trr.ent_id and flt.del_flag = 0
        left join kwf_fleet fl on fl.id = flt.fleet_id and fl.ent_id = trr.ent_id and fl.del_flag = 0
        where trr.del_flag = 0 and tr.del_flag = 0 and dr.del_flag = 0
        <if test="entId != null and entId != ''">
            and trr.ent_id = #{entId, jdbcType=VARCHAR}
        </if>
        <if test="truckNo != null and truckNo != ''">
            and tr.truck_no = #{truckNo, jdbcType=VARCHAR}
        </if>
        <if test="truckType != null and truckType != ''">
            and tr.type = #{truckType, jdbcType=VARCHAR}
        </if>
        <if test="fleetId != null and fleetId != ''">
            and fl.id = #{fleetId, jdbcType=VARCHAR}
        </if>
        <if test="fleetName != null and fleetName != ''">
            and fl.name like concat('%',#{fleetName},'%')
        </if>
        <if test="status != null and status != ''">
            and tr.status = #{status, jdbcType=VARCHAR}
        </if>
        <if test="truckAuthStatus != null and truckAuthStatus != '' and truckAuthStatus != 0">
            and tr.auth_status = #{truckAuthStatus, jdbcType=VARCHAR}
        </if>
        <if test="authStatus != null and authStatus != '' and authStatus != 0">
            and tr.auth_status = #{authStatus, jdbcType=VARCHAR}
        </if>
        <if test="startTime != null and startTime != '' " >
            and DATE( trr.create_time) <![CDATA[ >= ]]> #{startTime,jdbcType=TIMESTAMP}
        </if>
        <if test="endTime != null and endTime != '' " >
            and DATE( trr.create_time ) <![CDATA[ <= ]]> #{endTime,jdbcType=TIMESTAMP}
        </if>
        <choose>
            <when test="ids != null and ids != '' and ids.size() > 0">
                and trr.id in
                <foreach collection="ids" item="id" open="(" close=")" separator=",">
                    #{id,jdbcType=BIGINT}
                </foreach>
            </when>
        </choose>
        <if test="keywords != null and keywords != ''">
            and (
            tr.truck_no like concat('%',#{keywords},'%')
            or dr.name like concat('%',#{keywords},'%')
            or dr.phone like concat('%',#{keywords},'%')
            )
        </if>
        ORDER BY tr.create_time desc
    </select>

    <select id="findList" resultType="java.util.Map" parameterType="java.util.Map" >
        SELECT
            trr.id, trr.ent_id entId, trr.status, dr.id driverId, dr.name driverName, dr.phone driverPhone,
            dr.idcard driverIdcard, tr.id truckId, tr.truck_no truckNo, tr.type truckType, tr.energy_type truckEnergyType,
            tr.use_type truckUseType, tr.color truckColor, tr.trailer_no truckTrailerNo, tr.auth_status authStatus,
        tr.business_status businessStatus, tr.actual_weight actualWeight
        from kwf_truck_report trr
        left join kwf_truck tr on tr.id = trr.truck_id
        left join kwf_driver dr on dr.id = trr.driver_id
        where trr.del_flag = 0 and tr.del_flag = 0 and dr.del_flag = 0
        <if test="truckNos != null and truckNos != ''">
            and FIND_IN_SET(tr.truck_no, #{truckNos, jdbcType=VARCHAR})
        </if>
        <if test="entId != null and entId != ''">
            and trr.ent_id = #{entId, jdbcType=VARCHAR}
        </if>
        <if test="truckId != null and truckId != ''">
            and trr.truck_id = #{truckId, jdbcType=VARCHAR}
        </if>
        <if test="driverId != null and driverId != ''">
            and trr.driver_id = #{driverId, jdbcType=VARCHAR}
        </if>
        <if test="truckNo != null and truckNo != ''">
            and tr.truck_no = #{truckNo, jdbcType=VARCHAR}
        </if>
        <if test="businessStatus != null and businessStatus != ''">
            and tr.business_status = #{businessStatus, jdbcType=VARCHAR}
        </if>
        <if test="authStatus != null and authStatus != '' and authStatus != 0">
            and tr.auth_status = #{authStatus, jdbcType=VARCHAR}
        </if>
        <if test="status != null and status != ''">
            and trr.status = #{status, jdbcType=VARCHAR}
        </if>
        <if test="keywords != null and keywords != ''">
            and (
            tr.truck_no like concat('%',#{keywords},'%')
            or dr.name like concat('%',#{keywords},'%')
            or dr.phone like concat('%',#{keywords},'%')
            )
        </if>
        ORDER BY tr.create_time desc
    </select>

</mapper>
