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

    <select id="findPage" resultType="com.sckw.fleet.model.vo.KwfFleetVo" parameterType="java.util.Map" >
        select
            id, name, contacts, phone, remark, status, create_by createBy, create_time createTime,
            update_by updateBy, update_time updateTime, fl.captain_id captainId
        from kwf_fleet fl
        where fl.del_flag = 0
        <if test="entId != null and entId != ''">
            and fl.ent_id = #{entId, jdbcType=BIGINT}
        </if>
        <if test="captainId != null and captainId != ''">
            and fl.captain_id = #{captainId, jdbcType=BIGINT}
        </if>
        <if test="name != null and name != ''">
            and fl.name = #{name, jdbcType=VARCHAR}
        </if>
        <if test="contacts != null and contacts != ''">
            and fl.contacts = #{contacts, jdbcType=VARCHAR}
        </if>
        <if test="phone != null and phone != ''">
            and fl.phone = #{phone, jdbcType=VARCHAR}
        </if>
        <if test="startTime != null and startTime != '' " >
            and DATE( fl.create_time) <![CDATA[ >= ]]> #{startTime,jdbcType=TIMESTAMP}
        </if>
        <if test="endTime != null and endTime != '' " >
            and DATE( fl.create_time ) <![CDATA[ <= ]]> #{endTime,jdbcType=TIMESTAMP}
        </if>
        <choose>
            <when test="ids != null and ids != '' and ids.size() > 0">
                and fl.id in
                <foreach collection="ids" item="id" open="(" close=")" separator=",">
                    #{id,jdbcType=BIGINT}
                </foreach>
            </when>
        </choose>
        <if test="keywords != null and keywords != ''">
            and (
                fl.name like concat('%',trim(#{keywords}),'%')
                or fl.contacts like concat('%',trim(#{keywords}),'%')
                or fl.phone like concat('%',trim(#{keywords}),'%')
            )
        </if>
        ORDER BY fl.create_time desc
    </select>

    <select id="findList" resultType="java.util.Map" parameterType="java.util.Map" >
        select
            id, name, contacts, phone, remark, captain_id captainId
        from kwf_fleet fl
        where fl.del_flag = 0
        <if test="entId != null and entId != ''">
            and fl.ent_id = #{entId, jdbcType=BIGINT}
        </if>
        <if test="captainId != null and captainId != ''">
            and fl.captain_id = #{captainId, jdbcType=BIGINT}
        </if>
        <if test="name != null and name != ''">
            and fl.name like concat('%',#{name},'%')
        </if>
        <if test="wholeName != null and wholeName != ''">
            and fl.name = #{wholeName, jdbcType=VARCHAR}
        </if>
        <if test="contacts != null and contacts != ''">
            and fl.contacts = #{contacts, jdbcType=VARCHAR}
        </if>
        <if test="phone != null and phone != ''">
            and fl.phone = #{phone, jdbcType=VARCHAR}
        </if>
        <if test="keywords != null and keywords != ''">
            and (
                fl.name like concat('%',#{keywords},'%')
                or fl.contacts like concat('%',#{keywords},'%')
                or fl.phone like concat('%',#{keywords},'%')
            )
        </if>
        ORDER BY fl.create_time desc
    </select>

    <select id="findTruckList" resultType="com.sckw.fleet.model.vo.KwfFleetTruckCountVo" >
        SELECT
        IFNULL(fl.name, '未归属车队车辆') fleetName, count(tr.id) truckTotal,
        count(case when tr.business_status = 0 then 1 end) idleTotal,
        count(case when tr.business_status = 1 then 1 end) busyTotal
        from kwf_truck tr
        left join kwf_truck_ent tre on tre.truck_id = tr.id
        left join kwf_fleet_truck flt on flt.truck_id = tr.id and flt.del_flag = 0
        left join kwf_fleet fl on fl.id = flt.fleet_id and fl.del_flag = 0
        where tr.del_flag = 0 and tre.del_flag = 0
        <if test="entId != null and entId != ''">
            and tre.ent_id = #{entId, jdbcType=BIGINT}
        </if>
        <if test="fleetName != null and fleetName != ''">
            and fl.name like concat('%',#{fleetName},'%')
        </if>
        GROUP BY fl.name ORDER BY truckTotal desc
    </select>

</mapper>
