<?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.KwfDriverMapper">

    <select id="statisticsAll" resultType="com.sckw.fleet.model.vo.KwfTableTopCount" parameterType="java.util.Map" >
        SELECT
            dr.`auth_status` value, count(0) total
        from kwf_driver dr
        left join kwf_driver_card drc on drc.driver_id = dr.id and drc.del_flag = 0
        left join kwf_driver_license drl on drl.driver_id = dr.id and drl.del_flag = 0
        left join kwf_driver_qualification drq on drq.driver_id = dr.id and drq.del_flag = 0
        where dr.del_flag = 0
        <if test="authEntIdList != null and authEntIdList.size() != 0">
            and dr.ent_id in
            <foreach collection="authEntIdList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="params.entId != null and params.entId != ''">
            and dr.ent_id = #{params.entId, jdbcType=VARCHAR}
        </if>
        <if test="params.name != null and params.name != ''">
            and dr.name = #{params.name, jdbcType=VARCHAR}
        </if>
        <if test="params.idcard != null and params.idcard != ''">
            and dr.idcard = #{params.idcard, jdbcType=VARCHAR}
        </if>
        <if test="params.phone != null and params.phone != ''">
            and dr.phone = #{params.phone, jdbcType=VARCHAR}
        </if>
        <if test="params.licenseType != null and params.licenseType != ''">
            and drl.type = #{params.licenseType, jdbcType=VARCHAR}
        </if>
        <if test="params.startTime != null and params.startTime != '' " >
            and DATE( dr.create_time) <![CDATA[ >= ]]> #{params.startTime,jdbcType=TIMESTAMP}
        </if>
        <if test="params.endTime != null and params.endTime != '' " >
            and DATE( dr.create_time ) <![CDATA[ <= ]]> #{params.endTime,jdbcType=TIMESTAMP}
        </if>
        <if test="params.keywords != null and params.keywords != ''">
            and (
            dr.name like concat('%',#{params.keywords},'%')
            or dr.phone like concat('%',#{params.keywords},'%')
            )
        </if>
        GROUP BY dr.`auth_status`
    </select>

    <select id="statistics" resultType="com.sckw.fleet.model.vo.KwfTableTopCount" parameterType="java.util.Map" >
        SELECT
        dr.`auth_status` value, count(0) total
        from kwf_driver dr
        left join kwf_driver_ent dre on dre.driver_id = dr.id
        left join kwf_driver_card drc on drc.driver_id = dr.id and drc.del_flag = 0
        left join kwf_driver_license drl on drl.driver_id = dr.id and drl.del_flag = 0
        left join kwf_driver_qualification drq on drq.driver_id = dr.id and drq.del_flag = 0
        left join kwf_fleet_driver flr on flr.driver_id = dr.id and flr.del_flag = 0
        left join kwf_fleet fl on fl.id = flr.fleet_id and fl.del_flag = 0
        where dr.del_flag = 0 and dre.del_flag = 0
        <if test="entId != null and entId != ''">
            and dre.ent_id = #{entId, jdbcType=VARCHAR}
        </if>
        <if test="name != null and name != ''">
            and dr.name = #{name, jdbcType=VARCHAR}
        </if>
        <if test="idcard != null and idcard != ''">
            and dr.idcard = #{idcard, jdbcType=VARCHAR}
        </if>
        <if test="phone != null and phone != ''">
            and dr.phone = #{phone, jdbcType=VARCHAR}
        </if>
        <if test="licenseType != null and licenseType != ''">
            and drl.type = #{licenseType, 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="startTime != null and startTime != '' " >
            and DATE( dr.create_time) <![CDATA[ >= ]]> #{startTime,jdbcType=TIMESTAMP}
        </if>
        <if test="endTime != null and endTime != '' " >
            and DATE( dr.create_time ) <![CDATA[ <= ]]> #{endTime,jdbcType=TIMESTAMP}
        </if>
        <if test="noReport != null and noReport != ''">
            and dr.id not in (
            select driver_id from kwf_truck_report where del_flag = 0 and ent_id = #{entId, jdbcType=VARCHAR}
            )
        </if>
        <if test="keywords != null and keywords != ''">
            and (
            dr.name like concat('%',#{keywords},'%')
            or dr.phone like concat('%',#{keywords},'%')
            )
        </if>
        GROUP BY dr.`auth_status`
    </select>

    <select id="findPageAll" resultType="com.sckw.fleet.model.vo.KwfDriverVo" parameterType="java.util.Map" >
        SELECT
        dr.id, dr.name, dr.phone, dr.idcard, dr.status, dr.auth_status authStatus, drc.expire_time idcardExpireTime,
        drc.address, drl.driver_no driverNo, drl.type licenseType, drl.expire_time licenseExpireTime,
        drl.grant_unit licenseGrantUnit,drq.quali_no qualiNo, dr.ent_id entId, dr.create_by createBy,
        dr.create_time createTime,dr.update_time updateTime, dr.remark
        from kwf_driver dr
        left join kwf_driver_card drc on drc.driver_id = dr.id and drc.del_flag = 0
        left join kwf_driver_license drl on drl.driver_id = dr.id and drl.del_flag = 0
        left join kwf_driver_qualification drq on drq.driver_id = dr.id and drq.del_flag = 0
        where dr.del_flag = 0
        <if test="authEntIdList != null and authEntIdList.size() != 0">
            and dr.ent_id in
            <foreach collection="authEntIdList" separator="," close=")" open="(" item="item">
                #{item}
            </foreach>
        </if>
        <if test="params.entId != null and params.entId != ''">
            and dr.ent_id = #{params.entId, jdbcType=VARCHAR}
        </if>
        <if test="params.name != null and params.name != ''">
            and dr.name = #{params.name, jdbcType=VARCHAR}
        </if>
        <if test="params.idcard != null and params.idcard != ''">
            and dr.idcard = #{params.idcard, jdbcType=VARCHAR}
        </if>
        <if test="params.phone != null and params.phone != ''">
            and dr.phone = #{params.phone, jdbcType=VARCHAR}
        </if>
        <if test="params.licenseType != null and params.licenseType != ''">
            and drl.type = #{params.licenseType, jdbcType=VARCHAR}
        </if>
        <if test="params.status != null and params.status != ''">
            and dr.status = #{params.status, jdbcType=VARCHAR}
        </if>
        <if test="params.authStatus != null and params.authStatus != '' and params.authStatus != 0">
            and dr.auth_status = #{params.authStatus, jdbcType=VARCHAR}
        </if>
        <if test="params.startTime != null and params.startTime != '' " >
            and DATE( dr.create_time) <![CDATA[ >= ]]> #{params.startTime,jdbcType=TIMESTAMP}
        </if>
        <if test="params.endTime != null and params.endTime != '' " >
            and DATE( dr.create_time ) <![CDATA[ <= ]]> #{params.endTime,jdbcType=TIMESTAMP}
        </if>
        <choose>
            <when test="params.ids != null and params.ids != '' and params.ids.size() > 0">
                and dr.id in
                <foreach collection="params.ids" item="id" open="(" close=")" separator=",">
                    #{id,jdbcType=BIGINT}
                </foreach>
            </when>
        </choose>
        <if test="params.keywords != null and params.keywords != ''">
            and (
            dr.name like concat('%',#{params.keywords},'%')
            or dr.phone like concat('%',#{params.keywords},'%')
            )
        </if>
        ORDER BY dr.create_time desc
    </select>

    <select id="findPage" resultType="com.sckw.fleet.model.vo.KwfDriverVo" parameterType="java.util.Map" >
        SELECT
        dr.id, dr.name, dr.phone, dr.idcard, dr.status, dr.auth_status authStatus, drc.expire_time idcardExpireTime,
        drc.address, drl.driver_no driverNo, drl.type licenseType, drl.expire_time licenseExpireTime,
        drl.grant_unit licenseGrantUnit,drq.quali_no qualiNo, dr.ent_id entId, dr.create_by createBy,
        dr.create_time createTime,dr.update_time updateTime, dr.remark, fl.id fleetId, fl.name fleetName
        from kwf_driver dr
        left join kwf_driver_ent dre on dre.driver_id = dr.id
        left join kwf_driver_card drc on drc.driver_id = dr.id and drc.del_flag = 0
        left join kwf_driver_license drl on drl.driver_id = dr.id and drl.del_flag = 0
        left join kwf_driver_qualification drq on drq.driver_id = dr.id and drq.del_flag = 0
        left join kwf_fleet_driver flr on flr.driver_id = dr.id and flr.del_flag = 0
        left join kwf_fleet fl on fl.id = flr.fleet_id and fl.del_flag = 0
        where dr.del_flag = 0 and dre.del_flag = 0
        <if test="entId != null and entId != ''">
            and dre.ent_id = #{entId, jdbcType=VARCHAR}
        </if>
        <if test="name != null and name != ''">
            and dr.name = #{name, jdbcType=VARCHAR}
        </if>
        <if test="idcard != null and idcard != ''">
            and dr.idcard = #{idcard, jdbcType=VARCHAR}
        </if>
        <if test="phone != null and phone != ''">
            and dr.phone = #{phone, jdbcType=VARCHAR}
        </if>
        <if test="licenseType != null and licenseType != ''">
            and drl.type = #{licenseType, 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 dr.status = #{status, jdbcType=VARCHAR}
        </if>
        <if test="authStatus != null and authStatus != '' and authStatus != 0">
            and dr.auth_status = #{authStatus, jdbcType=VARCHAR}
        </if>
        <if test="startTime != null and startTime != '' " >
            and DATE( dr.create_time) <![CDATA[ >= ]]> #{startTime,jdbcType=TIMESTAMP}
        </if>
        <if test="endTime != null and endTime != '' " >
            and DATE( dr.create_time ) <![CDATA[ <= ]]> #{endTime,jdbcType=TIMESTAMP}
        </if>
        <if test="noReport != null and noReport != ''">
            and dr.id not in (
            select driver_id from kwf_truck_report where del_flag = 0 and ent_id = #{entId, jdbcType=VARCHAR}
            )
        </if>
        <choose>
            <when test="ids != null and ids != '' and ids.size() > 0">
                and dr.id in
                <foreach collection="ids" item="id" open="(" close=")" separator=",">
                    #{id,jdbcType=BIGINT}
                </foreach>
            </when>
        </choose>
        <if test="keywords != null and keywords != ''">
            and (
            dr.name like concat('%',trim(#{keywords}),'%')
            or dr.phone like concat('%',trim(#{keywords}),'%')
            )
        </if>
        ORDER BY dr.create_time desc
    </select>

    <select id="findList" resultType="java.util.Map" parameterType="java.util.Map" >
        SELECT
            dr.id, name, phone, idcard, total_complete totalComplete,
            total_take totalTake, total_weight totalWeight, dre.ent_id entId
        from kwf_driver dr
        left join kwf_driver_ent dre on dre.driver_id = dr.id
        where dr.del_flag = 0 and dre.del_flag = 0
        <if test="entId != null and entId != ''">
            and dre.ent_id = #{entId, jdbcType=VARCHAR}
        </if>
        <if test="name != null and name != ''">
            and dr.name like concat('%',#{name},'%')
        </if>
        <if test="wholeName != null and wholeName != ''">
            and dr.name =  #{wholeName, jdbcType=VARCHAR}
        </if>
        <if test="idcard != null and idcard != ''">
            and dr.idcard = #{idcard, jdbcType=VARCHAR}
        </if>
        <if test="phone != null and phone != ''">
            and dr.phone = #{phone, jdbcType=VARCHAR}
        </if>
        <if test="status != null and status != ''">
            and dr.status = #{status, jdbcType=VARCHAR}
        </if>
        <if test="authStatus != null and authStatus != '' and authStatus != 0">
            and dr.auth_status = #{authStatus, jdbcType=VARCHAR}
        </if>
        <if test="noReport != null and noReport != ''">
            and dr.id not in (
                select driver_id from kwf_truck_report where del_flag = 0 and ent_id = #{entId, jdbcType=VARCHAR}
            )
        </if>
        <choose>
            <when test="ids != null and ids != '' and ids.size() > 0">
                and dr.id in
                <foreach collection="ids" item="id" open="(" close=")" separator=",">
                    #{id,jdbcType=BIGINT}
                </foreach>
            </when>
        </choose>
        <if test="keywords != null and keywords != ''">
            and (
            dr.name like concat('%',#{keyWords},'%')
            or dr.idcard like concat('%',#{keywords},'%')
            or dr.phone like concat('%',#{keywords},'%')
            )
        </if>
        ORDER BY dr.create_time desc
    </select>

    <select id="findDriver" resultType="com.sckw.fleet.model.KwfDriver" parameterType="java.util.Map" >
        SELECT
            id, ent_id entId, name, phone, salt, password, idcard, total_complete totalComplete,
            total_take totalTake, total_weight totalWeight, business_status businessStatus, remark, status
        from kwf_driver dr
        where dr.del_flag = 0
        <if test="entId != null and entId != ''">
            and ent_id = #{entId, jdbcType=VARCHAR}
        </if>
        <if test="name != null and name != ''">
            and name =  #{name, jdbcType=VARCHAR}
        </if>
        <if test="idcard != null and idcard != ''">
            and idcard = #{idcard, jdbcType=VARCHAR}
        </if>
        <if test="phone != null and phone != ''">
            and phone = #{phone, jdbcType=VARCHAR}
        </if>
        <if test="status != null and status != ''">
            and status = #{status, jdbcType=VARCHAR}
        </if>
        <if test="authStatus != null and authStatus != '' and authStatus != 0">
            and dr.auth_status = #{authStatus, jdbcType=VARCHAR}
        </if>
        <choose>
            <when test="ids != null and ids != '' and ids.size() > 0">
                and dr.id in
                <foreach collection="ids" item="id" open="(" close=")" separator=",">
                    #{id,jdbcType=BIGINT}
                </foreach>
            </when>
        </choose>
        ORDER BY create_time desc
    </select>

    <select id="findEntDriver" resultType="com.sckw.fleet.model.KwfDriver" parameterType="com.sckw.fleet.model.KwfDriver" >
        SELECT
            dr.id, name, phone, salt, password, idcard, total_complete totalComplete,
            total_take totalTake, total_weight totalWeight, dre.ent_id entId
            from kwf_driver dr
            left join kwf_driver_ent dre on dre.driver_id = dr.id
        where dr.del_flag = 0 and dre.del_flag = 0
        and dre.ent_id = #{entId, jdbcType=VARCHAR}
        and dr.id = #{id, jdbcType=VARCHAR}
    </select>

    <select id="driverStatistics" resultType="java.util.Map" >
        select
            tabA.abnormalTotal, tabB.idleTotal, tabC.inTaskTotal, (tabB.idleTotal + tabC.inTaskTotal) allTotal
        from
        (SELECT
        count(1) abnormalTotal, '' indexNum
        from kwf_driver tr
        left join kwf_driver_ent tre on tre.driver_id = tr.id
        where tr.del_flag = 0 and tre.del_flag = 0 and tr.auth_status = 3 and tre.ent_id = #{entId, jdbcType=BIGINT}
        ) tabA
        left join
        (SELECT
        count(1) idleTotal, '' indexNum
        from kwf_driver tr
        left join kwf_driver_ent tre on tre.driver_id = tr.id
        where tr.del_flag = 0 and tre.del_flag = 0 and tr.business_status = 0 and tre.ent_id = #{entId, jdbcType=BIGINT}
        ) tabB	on tabB.indexNum = tabA.indexNum
        left join
        (SELECT
        count(1) inTaskTotal, '' indexNum
        from kwf_driver tr
        left join kwf_driver_ent tre on tre.driver_id = tr.id
        where tr.del_flag = 0 and tre.del_flag = 0 and tr.business_status = 1 and tre.ent_id = #{entId, jdbcType=BIGINT}
        ) tabC	on tabC.indexNum = tabA.indexNum
    </select>

</mapper>
