<?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.hotwj.platform.resourceManagement.vehicleManagement.mapper.HotVehicleMapper">

    <select id="selectVehicleCategoryStat"
            resultType="com.hotwj.platform.resourceManagement.vehicleManagement.domain.vo.HotVehicleCategoryStatVo">
        SELECT vehicle_category, count(1) as count
        FROM hot_vehicle
        <where>
            <if test="companyId != null">
                AND company_id = #{companyId}
            </if>
            AND is_deleted = 0
        </where>
        GROUP BY vehicle_category
    </select>

    <select id="selectAvailableForThreeInspect"
            resultType="com.hotwj.platform.resourceManagement.vehicleManagement.domain.vo.HotVehicleVo">
        SELECT v.*
        FROM hot_vehicle v
        WHERE v.company_id = #{companyId}
          AND v.is_deleted = 0
          AND v.operation_status = 1
          AND (
            NOT EXISTS (SELECT 1 FROM hot_vehicle_three_inspect i WHERE i.vehicle_id = v.id AND i.is_deleted = 0)
                OR
            EXISTS (SELECT 1
                    FROM hot_vehicle_three_inspect i
                    WHERE i.vehicle_id = v.id
                      AND i.is_deleted = 0
                      AND i.inspect_phase = 3
                      AND i.create_time = (SELECT MAX(i2.create_time)
                                           FROM hot_vehicle_three_inspect i2
                                           WHERE i2.vehicle_id = v.id
                                             AND i2.is_deleted = 0))
            )
    </select>

</mapper>
