<?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="statistics" resultType="com.sckw.fleet.model.vo.KwfTableTopCount" parameterType="java.util.Map" >
        SELECT
            tr.`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="type != null and type != ''">
            and tr.type = #{type, 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},'%')
            )
        </if>
        GROUP BY tr.`status`
    </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.create_by createBy, tr.create_time createTime, tr.update_by updateBy, tr.update_time updateTime,
        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 = #{type, 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>
        <choose>
            <when test="ids != null">
                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},'%')
            )
        </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.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
        <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="type != null and type != ''">
            and tr.type = #{type, 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="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 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="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>

</mapper>
