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

    <select id="report"
            resultType="com.ningdatech.carapi.car.rpt.model.po.RptDailySpecialVehiclesLaunchRatePO">
        SELECT
            <choose>
                <when test="po.regionLevel != null and po.regionLevel == 1">
                    (case when r.level = 3 then r2.name else r.name end) region_name,
                </when>
                <when test="po.regionLevel != null and po.regionLevel != 1">
                    r.name region_name,
                </when>
            </choose>
            count(b.car_plate) vehicle_nums,
            count(o2.car_plate) monitored_vehicle_nums,
            (count(b.car_plate) - count(o2.car_plate)) unmonitored_vehicle_nums,
            count(o2.car_plate) onlineVehicleNum,
            (count(b.car_plate) - count(o2.car_plate)) offlineVehicleNum,
            IFNULL(convert(count(o2.car_plate)/count(b.car_plate) * 100,decimal(10,2)),0) launch_rate
        FROM `nd_vehicle_base_info` b
            left join (
            select o.car_plate from ${po.tableName} o
            left join nd_region r on o.region_id = r.id
            left join nd_company_info c on o.company_id = c.id
            where o.year = year(#{po.date}) and o.month = month(#{po.date}) and o.day = day(#{po.date})
            <if test="po.regionIds != null and po.regionIds.size > 0">
                and o.region_id in
                <foreach collection="po.regionIds" separator="," open="(" close=")" item="item">
                    #{item}
                </foreach>
            </if>
            ${po.dataScopeSql}
            group by o.car_plate
            ) o on b.car_plate = o.car_plate
            left join (
            select o.car_plate from ${po.tableName} o
            left join nd_region r on o.region_id = r.id
            left join nd_company_info c on o.company_id = c.id
            where o.year = year(#{po.date}) and o.month = month(#{po.date}) and o.day = day(#{po.date})
            and status = 1
            <if test="po.regionIds != null and po.regionIds.size > 0">
                and o.region_id in
                <foreach collection="po.regionIds" separator="," open="(" close=")" item="item">
                    #{item}
                </foreach>
            </if>
            ${po.dataScopeSql}
            group by o.car_plate
            ) o2 on b.car_plate = o2.car_plate
            left join nd_region r on b.region_id = r.id
            left join nd_region r2 on r.parent_id = r2.id
            left join nd_region r3 on r2.parent_id = r3.id
            left join nd_company_info c on b.company_id = c.id
        where b.deleted = 0 and b.audit_state = 2 and b.vehicle_type in (1,2,3,4)
        <if test="po.regionIds != null and po.regionIds.size > 0">
            and b.region_id in
            <foreach collection="po.regionIds" separator="," open="(" close=")" item="item">
                #{item}
            </foreach>
        </if>
        ${po.dataScopeSql}
        <choose>
            <when test="po.regionLevel != null and po.regionLevel == 1">
                group by (case when r.level = 3 then r2.name else r.name end)
            </when>
            <when test="po.regionLevel != null and po.regionLevel != 1">
                group by r.name
            </when>
        </choose>
    </select>
</mapper>
