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

    <select id="report" resultType="com.ningdatech.carapi.car.rpt.model.po.RptDailySpecialVehiclesCompanyLaunchRate60PO">
        select
            company_id,company_name,region_id,region_name,vehicle_nums,monitored_vehicle_nums,
            unmonitored_vehicle_nums,onlineVehicleNum,offlineVehicleNum,launch_rate
        from(SELECT
                      c.id company_id,
                      c.name company_name,
                      r.id region_id,
                      r.name region_name,
                      count(b.car_plate) vehicle_nums,
                      count(b.car_plate) monitored_vehicle_nums,
                      (count(b.car_plate) - count(b.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 ${param.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(#{param.date}) and o.month = month(#{param.date}) and o.day = day(#{param.date})
            <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>
              ${param.dataScopeSql}
              group by o.car_plate
         ) o on b.car_plate = o.car_plate
            left join (
            select o.car_plate from ${param.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(#{param.date}) and o.month = month(#{param.date}) and o.day = day(#{param.date})
            and status = 1
            <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>
            ${param.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="param.companyName != null and param.companyName != ''">
            and c.name like concat('%',#{param.companyName},'%')
        </if>
        <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>
        ${param.dataScopeSql}
        group by c.id,c.name,r.id,r.name) o
        where 1 = 1
        <if test="param.launchRate != null">
            and launch_rate &lt;= #{param.launchRate}
        </if>

    </select>
</mapper>
