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

    <select id="statisticsAll" resultType="com.sckw.fleet.model.vo.KwfTableTopCount" parameterType="java.util.Map" >
        SELECT
            tr.`auth_status` value, count(0) total
        from kwf_truck tr
        left join kwf_truck_license trl on trl.truck_id = tr.id and trl.del_flag = 0
        left join kwf_transport_license tranl on tranl.truck_id = tr.id and tranl.del_flag = 0
        where tr.del_flag = 0
        <if test="authEntIdList != null and authEntIdList.size() != 0">
            and tr.ent_id in
            <foreach collection="authEntIdList" separator="," close=")" open="(" item="item">
                #{item}
            </foreach>
        </if>
        <if test="params.entId != null and params.entId != ''">
            and tr.ent_id = #{params.entId, jdbcType=VARCHAR}
        </if>
        <if test="params.truckNo != null and params.truckNo != ''">
            and tr.truck_no = #{params.truckNo, jdbcType=VARCHAR}
        </if>
        <if test="params.truckType != null and params.truckType != ''">
            and tr.type = #{params.truckType, jdbcType=VARCHAR}
        </if>
        <if test="params.energyType != null and params.energyType != ''">
            and tr.energy_type = #{params.energyType, jdbcType=VARCHAR}
        </if>
        <if test="params.useType != null and params.useType != ''">
            and tr.use_type = #{params.useType, jdbcType=VARCHAR}
        </if>
        <if test="params.status != null and params.status != ''">
            and tr.status = #{params.status, jdbcType=VARCHAR}
        </if>
        <if test="params.tartTime != null and params.startTime != '' " >
            and DATE( tr.create_time) <![CDATA[ >= ]]> #{params.startTime,jdbcType=TIMESTAMP}
        </if>
        <if test="params.endTime != null and params.endTime != '' " >
            and DATE( tr.create_time ) <![CDATA[ <= ]]> #{params.endTime,jdbcType=TIMESTAMP}
        </if>
        <if test="params.keywords != null and params.keywords != ''">
            and (
            tr.truck_no like concat('%',#{params.keywords},'%')
            )
        </if>
        GROUP BY tr.`auth_status`
    </select>

    <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 tr
        left join kwf_truck_ent tre on tre.truck_id = tr.id
        left join kwf_truck_license trl on trl.truck_id = tr.id and trl.del_flag = 0
        left join kwf_transport_license tranl on tranl.truck_id = tr.id and tranl.del_flag = 0
        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=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="energyType != null and energyType != ''">
            and tr.energy_type = #{energyType, jdbcType=VARCHAR}
        </if>
        <if test="useType != null and useType != ''">
            and tr.use_type = #{useType, 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( tr.create_time) <![CDATA[ >= ]]> #{startTime,jdbcType=TIMESTAMP}
        </if>
        <if test="endTime != null and endTime != '' " >
            and DATE( tr.create_time ) <![CDATA[ <= ]]> #{endTime,jdbcType=TIMESTAMP}
        </if>
        <if test="keywords != null and keywords != ''">
            and (
                tr.truck_no like concat('%',#{keywords},'%')
                <choose>
                    <when test="entIds != null and entIds != '' and entIds.size() > 0">
                        or tre.ent_id in
                        <foreach collection="entIds" item="id" open="(" close=")" separator=",">
                            #{id,jdbcType=BIGINT}
                        </foreach>
                    </when>
                    <when test="createBys != null and createBys != '' and createBys.size() > 0">
                        or tr.create_by in
                        <foreach collection="createBys" item="id" open="(" close=")" separator=",">
                            #{id,jdbcType=BIGINT}
                        </foreach>
                    </when>
                </choose>
            )
        </if>
        GROUP BY tr.`auth_status`
    </select>

    <select id="findPageAll" resultType="com.sckw.fleet.model.vo.KwfTruckVo" parameterType="java.util.Map" >
        SELECT
            tr.id, tr.ent_id entId, tr.truck_no truckNo, tr.type, tr.energy_type energyType, tr.use_type useType,
            tr.spec, tr.actual_weight actualWeight, tr.gross_weight grossWeight, tr.tare_weight tareWeight,
            tr.color, tr.eev, tr.trailer_no trailerNo, tr.total_complete totalComplete, tr.total_take totalTake,
            tr.total_weight totalWeight, tr.business_status businessStatus, tr.remark, tr.status, tr.auth_status authStatus,
            tr.create_by createBy, tr.create_time createTime, tr.update_by updateBy, tr.update_time updateTime,tr.car_axis,tr.vin,tr.brand,
            trl.owner, trl.vin, trl.reg_time regTime, trl.grant_time grantTime, trl.grant_unit grantUnit,
            trl.driving_no drivingNo, tranl.road_tran_no roadTranNo, tranl.firm_name tranFirmName,
            tranl.business_license_no businessLicenseNo
        from kwf_truck tr
        left join kwf_truck_license trl on trl.truck_id = tr.id and trl.del_flag = 0
        left join kwf_transport_license tranl on tranl.truck_id = tr.id and tranl.del_flag = 0
        where tr.del_flag = 0
        <if test="authEntIdList != null and authEntIdList.size() != 0">
            and tr.ent_id in
            <foreach collection="authEntIdList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="params.entId != null and params.entId != ''">
            and tr.ent_id = #{params.entId, jdbcType=VARCHAR}
        </if>
        <if test="params.truckNo != null and params.truckNo != ''">
            and tr.truck_no = #{params.truckNo, jdbcType=VARCHAR}
        </if>
        <if test="params.ruckType != null and params.truckType != ''">
            and tr.type = #{params.truckType, jdbcType=VARCHAR}
        </if>
        <if test="params.energyType != null and params.energyType != ''">
            and tr.energy_type = #{params.energyType, jdbcType=VARCHAR}
        </if>
        <if test="params.useType != null and params.useType != ''">
            and tr.use_type = #{params.useType, jdbcType=VARCHAR}
        </if>
        <if test="params.status != null and params.status != ''">
            and tr.status = #{params.status, jdbcType=VARCHAR}
        </if>
        <if test="params.authStatus != null and params.authStatus != '' and params.authStatus != 0">
            and tr.auth_status = #{params.authStatus, jdbcType=VARCHAR}
        </if>
        <if test="params.startTime != null and params.startTime != '' " >
            and DATE( tr.create_time) <![CDATA[ >= ]]> #{params.startTime,jdbcType=TIMESTAMP}
        </if>
        <if test="params.endTime != null and params.endTime != '' " >
            and DATE( tr.create_time ) <![CDATA[ <= ]]> #{params.endTime,jdbcType=TIMESTAMP}
        </if>
        <choose>
            <when test="params.ids != null and params.ids != '' and params.ids.size() > 0">
                and tr.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 (
            tr.truck_no like concat('%',#{params.keywords},'%')
            )
        </if>
        ORDER BY tr.create_time desc
    </select>

    <select id="findPage" resultType="com.sckw.fleet.model.vo.KwfTruckVo" parameterType="java.util.Map" >
        SELECT
        tr.id, tr.ent_id entId, tr.truck_no truckNo, tr.type, tr.energy_type energyType, tr.use_type useType,
        tr.spec, tr.actual_weight actualWeight, tr.gross_weight grossWeight, tr.tare_weight tareWeight,
        tr.color, tr.eev, tr.trailer_no trailerNo, tr.total_complete totalComplete, tr.total_take totalTake,
        tr.total_weight totalWeight, tr.business_status businessStatus, tr.remark, tr.status, tr.auth_status authStatus,
        tr.create_by createBy, tr.create_time createTime, tr.update_by updateBy, tr.update_time updateTime,tr.car_axis,tr.vin,tr.brand,
        trl.owner, trl.vin, trl.reg_time regTime, trl.grant_time grantTime, trl.grant_unit grantUnit,
        trl.driving_no drivingNo, tranl.road_tran_no roadTranNo, tranl.firm_name tranFirmName,
        tranl.business_license_no businessLicenseNo, fl.`name` fleetName
        from kwf_truck tr
        left join kwf_truck_ent tre on tre.truck_id = tr.id
        left join kwf_truck_license trl on trl.truck_id = tr.id and trl.del_flag = 0
        left join kwf_transport_license tranl on tranl.truck_id = tr.id and tranl.del_flag = 0
        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=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="energyType != null and energyType != ''">
            and tr.energy_type = #{energyType, jdbcType=VARCHAR}
        </if>
        <if test="useType != null and useType != ''">
            and tr.use_type = #{useType, 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="authStatus != null and authStatus != '' and authStatus != 0">
            and tr.auth_status = #{authStatus, jdbcType=VARCHAR}
        </if>
        <if test="startTime != null and startTime != '' " >
            and DATE( tr.create_time) <![CDATA[ >= ]]> #{startTime,jdbcType=TIMESTAMP}
        </if>
        <if test="endTime != null and endTime != '' " >
            and DATE( tr.create_time ) <![CDATA[ <= ]]> #{endTime,jdbcType=TIMESTAMP}
        </if>
        <choose>
            <when test="ids != null and ids != '' and ids.size() > 0">
                and tr.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},'%')
                <choose>
                    <when test="entIds != null and entIds != '' and entIds.size() > 0">
                        or tre.ent_id in
                        <foreach collection="entIds" item="id" open="(" close=")" separator=",">
                            #{id,jdbcType=BIGINT}
                        </foreach>
                    </when>
                    <when test="createBys != null and createBys != '' and createBys.size() > 0">
                        or tr.create_by in
                        <foreach collection="createBys" item="id" open="(" close=")" separator=",">
                            #{id,jdbcType=BIGINT}
                        </foreach>
                    </when>
                </choose>
            )
        </if>
        ORDER BY tr.create_time desc
    </select>

    <select id="findList" resultType="java.util.Map" parameterType="java.util.Map" >
        SELECT
            tr.id, tre.ent_id entId, truck_no truckNo, type, energy_type energyType, use_type useType,
            spec, actual_weight actualWeight, gross_weight grossWeight, tare_weight tareWeight,
            color, eev, trailer_no trailerNo, total_complete totalComplete, total_take totalTake,
            total_weight totalWeight, business_status businessStatus, tr.remark, tr.status, tr.auth_status authStatus,
            tr.create_by createBy, tr.create_time createTime, tr.update_by updateBy, tr.update_time updateTime,
            tr.type as truckType
        from kwf_truck tr
        left join kwf_truck_ent tre on tre.truck_id = tr.id
        where tr.del_flag = 0 and tre.del_flag = 0
        <if test="entId != null and entId != ''">
            and tre.ent_id = #{entId, jdbcType=VARCHAR}
        </if>
        <if test="truckNo != null and truckNo != ''">
            and tr.truck_no like concat('%',#{truckNo},'%')
        </if>
        <if test="wholeTruckNo != null and wholeTruckNo != ''">
            and tr.truck_no = #{wholeTruckNo, jdbcType=VARCHAR}
        </if>
        <if test="truckType != null and truckType != ''">
            and tr.type = #{truckType, jdbcType=VARCHAR}
        </if>
        <if test="energyType != null and energyType != ''">
            and tr.energy_type = #{energyType, jdbcType=VARCHAR}
        </if>
        <if test="useType != null and useType != ''">
            and tr.use_type = #{useType, jdbcType=VARCHAR}
        </if>
        <if test="status != null and status != ''">
            and tr.status = #{status, 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( tr.create_time) <![CDATA[ >= ]]> #{startTime,jdbcType=TIMESTAMP}
        </if>
        <if test="endTime != null and endTime != '' " >
            and DATE( tr.create_time ) <![CDATA[ <= ]]> #{endTime,jdbcType=TIMESTAMP}
        </if>
        <choose>
            <when test="truckNos != null and truckNos != '' and truckNos.size() > 0">
                and tr.truck_no in
                <foreach collection="truckNos" item="truckNo" open="(" close=")" separator=",">
                    #{truckNo,jdbcType=VARCHAR}
                </foreach>
            </when>
        </choose>
        <if test="keywords != null and keywords != ''">
            and (
            tr.truck_no like concat('%',#{keywords},'%')
            )
        </if>
        ORDER BY tr.create_time desc
    </select>

    <select id="findTruck" resultType="com.sckw.fleet.model.KwfTruck" parameterType="java.util.Map" >
        SELECT
        id, ent_id entId, truck_no truckNo, type, energy_type energyType, use_type useType,
        spec, actual_weight actualWeight, gross_weight grossWeight, tare_weight tareWeight,
        color, eev, trailer_no trailerNo, total_complete totalComplete, total_take totalTake,
        total_weight totalWeight, business_status businessStatus, remark, status, auth_status authStatus
        from kwf_truck tr
        where del_flag = 0
        <if test="entId != null and entId != ''">
            and ent_id = #{entId, jdbcType=VARCHAR}
        </if>
        <if test="truckNo != null and truckNo != ''">
            and truck_no = #{truckNo, jdbcType=VARCHAR}
        </if>
        <if test="type != null and type != ''">
            and type = #{type, jdbcType=VARCHAR}
        </if>
        <if test="energyType != null and energyType != ''">
            and energy_type = #{energyType, jdbcType=VARCHAR}
        </if>
        <if test="useType != null and useType != ''">
            and use_type = #{useType, 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 auth_status = #{authStatus, jdbcType=VARCHAR}
        </if>
        <choose>
            <when test="truckNos != null and truckNos != '' and truckNos.size() > 0">
                and truck_no in
                <foreach collection="truckNos" item="truckNo" open="(" close=")" separator=",">
                    #{truckNo,jdbcType=VARCHAR}
                </foreach>
            </when>
        </choose>
        ORDER BY create_time desc
    </select>

    <select id="findEntTruck" resultType="com.sckw.fleet.model.KwfTruck" parameterType="com.sckw.fleet.model.KwfTruck" >
        SELECT
            tr.id, tre.ent_id entId, truck_no truckNo, type, energy_type energyType, use_type useType,
            spec, actual_weight actualWeight, gross_weight grossWeight, tare_weight tareWeight,
            color, eev, trailer_no trailerNo, total_complete totalComplete, total_take totalTake,
            total_weight totalWeight, business_status businessStatus, tr.remark, tr.status,
            tr.create_by createBy, tr.create_time createTime, tr.update_by updateBy, tr.update_time updateTime
        from kwf_truck tr
        left join kwf_truck_ent tre on tre.truck_id = tr.id
        where tr.del_flag = 0 and tre.del_flag = 0
        and tre.ent_id = #{entId, jdbcType=VARCHAR}
        and tr.id = #{id, jdbcType=VARCHAR}
    </select>

    <select id="capacityStatistics" resultType="java.util.Map" >
        SELECT
        ent_id entId, count(1) capacityTotal, sum(actual_weight) capacityAmount
        from kwf_truck
        where del_flag = 0
        <choose>
            <when test="entIds != null and entIds != '' and entIds.size() > 0">
                and ent_id in
                <foreach collection="entIds" item="item" open="(" close=")" separator=",">
                    #{item,jdbcType=BIGINT}
                </foreach>
            </when>
        </choose>
        GROUP BY ent_id
        HAVING 1=1
        <if test="capacityAmountMin != null and capacityAmountMin != ''">
            and capacityAmount <![CDATA[ >= ]]> #{capacityAmountMin, jdbcType=INTEGER}
        </if>
        <if test="capacityAmountMax != null and capacityAmountMax != ''">
            and capacityAmount <![CDATA[ <= ]]> #{capacityAmountMax, jdbcType=INTEGER}
        </if>
    </select>

    <select id="findTruckByEnt" resultType="com.sckw.fleet.model.vo.KwfTruckMonitorVo" >
        SELECT
            kt.truck_no truckNo, kt.business_status businessStatus, kte.ent_id entId
        from kwf_truck kt
        left join kwf_truck_ent kte on kte.truck_id = kt.id
        where kt.del_flag = 0 and kte.del_flag = 0
        and kte.ent_id = #{entId, jdbcType=BIGINT}
        <if test="truckNo != null and truckNo != ''">
            and kt.truck_no = #{truckNo, jdbcType=VARCHAR}
        </if>
    </select>

    <select id="findTruckByAll" resultType="com.sckw.fleet.model.vo.KwfTruckMonitorVo" >
        SELECT
        kt.truck_no, kt.business_status businessStatus, kt.ent_id entId
        from kwf_truck kt
        where kt.del_flag = 0
        <if test="params.truckNo != null and params.truckNo != ''">
            and kt.truck_no = #{truckNo, jdbcType=VARCHAR}
        </if>
        <if test="authEntIdList != null and authEntIdList.size() != 0">
            and kt.ent_id in
            <foreach collection="authEntIdList" separator="," close=")" open="(" item="item">
                #{item}
            </foreach>
        </if>
    </select>

    <select id="truckStatistics" resultType="java.util.Map" >
        select
        tabA.abnormalTotal, tabB.idleTotal, tabC.inTaskTotal, (tabB.idleTotal + tabC.inTaskTotal) allTotal
        from
        (SELECT
        count(1) abnormalTotal, '' indexNum
        from kwf_truck tr
        left join kwf_truck_ent tre on tre.truck_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_truck tr
        left join kwf_truck_ent tre on tre.truck_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_truck tr
        left join kwf_truck_ent tre on tre.truck_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>
