<?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.ningdatech.carapi.car.rpt.mapper.RptOtherAlarmCleanVehiclesMapper">
    <select id="reportPage" resultType="com.ningdatech.carapi.car.rpt.model.po.RptOtherAlarmCleanVehiclesPO">
        select
            company_id,company_name,region_id,region_name,
            vehicleNums,onlineNums,onlineRate,offlineNums,offlineRate,
            alarmVehicleNums,alarmProcessNums,
            enterpriseType,enterpriseTypeName,
            IFNULL(round(alarmVehicleNums/vehicleNums * 100,2),0) alarmVehicleRate,
            IFNULL(round(alarmProcessNums/vehicleNums * 100,2),0) alarmProcessRate
        from(select
                 c.id company_id,
                 c.name company_name,
                 r.id region_id,
                 r.name region_name,
                 IFNULL(c.vehicle_num,0) vehicleNums,
                 IFNULL(c.online_vehicle_num,0) onlineNums,
                 IFNULL(round(c.online_vehicle_num/c.vehicle_num * 100,2),0) onlineRate,
                 IFNULL(c.offline_vehicle_num,0) offlineNums,
                 IFNULL(round(c.offline_vehicle_num/c.vehicle_num * 100,2),0) offlineRate,
                 IFNULL(count(case when a.processing_status = 1 then 1 end),0) alarmProcessNums,
                 IFNULL(count(a.id),0) alarmVehicleNums,
                 GROUP_CONCAT(distinct t.id) enterpriseType,
                 GROUP_CONCAT(distinct t.name) enterpriseTypeName
             from nd_company_info c
                      left join nd_region r on c.region_id = r.id
                      left join nd_company_type_related tr on c.id = tr.company_id
                      left join nd_company_type t on tr.company_type_id = t.id
                      left join nd_vehicle_base_info b on c.id = b.company_id
                      left join nd_driver_abnormal_behavior_analysis a on b.car_plate = a.car_plate
             where b.deleted = 0 and b.audit_state = 2 and b.vehicle_type in (1,2,3,4)
            <if test="param.regionIds != null and param.regionIds.size > 0">
                and r.id in
                <foreach collection="param.regionIds" separator="," open="(" close=")" item="item">
                    #{item}
                </foreach>
            </if>
            <if test="param.companyName != null and param.companyName != ''">
                and c.name like concat('%',#{param.companyName},'%')
            </if>
            <if test="param.enterpriseType != null">
                and t.id = #{param.enterpriseType}
            </if>
            <if test="param.startTime != null and param.startTime != ''">
                and a.alarm_time &gt;= #{param.startTime}
            </if>
            <if test="param.endTime != null and param.endTime != ''">
                and a.alarm_time &lt;= #{param.endTime}
            </if>
            <if test="param.alarmType != null">
                and a.alarm_type = #{param.alarmType}
            </if>
             ${param.dataScopeSql}
             group by c.id,c.name,r.id,r.name,c.vehicle_num,c.online_vehicle_num,c.offline_vehicle_num
            ) ew
        order by IFNULL(round(alarmVehicleNums/vehicleNums * 100,2),0) desc,
                 IFNULL(round(alarmProcessNums/vehicleNums * 100,2),0) desc,
                 onlineRate desc
    </select>
</mapper>
