<?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.homepage.mapper.HomepageMapper">
    <select id="searchAccumulativeOnlineNumByTable" resultType="java.lang.Integer">
        select
            count(b.car_plate)
        from(SELECT
                 o.car_plate,max(o.status) status
             FROM ${tableName} o
                      left join nd_vehicle_base_info v on o.car_plate = v.car_plate
                      left join nd_region r on o.region_id = r.id
                      left join nd_company_info c on o.company_id = c.id
             where year = year(now()) and month = month(now()) and day = day(now())
                <if test="param.regionIds != null and param.regionIds.size>0">
                and o.region_id in
                 <foreach collection="param.regionIds" item="item" open="(" close=")" separator=",">
                    #{item}
                 </foreach>
                </if>
                <if test="param.companyIds != null and param.companyIds.size > 0">
                    and c.id in
                    <foreach collection="param.companyIds" item="item" open="(" close=")" separator=",">
                        #{item}
                    </foreach>
                </if>
                <if test="param.companyIds != null and param.companyIds.size == 0">
                    and c.id in (0)
                </if>
                <if test="param.operatorId != null">
                    and v.operator_id = #{param.operatorId}
                </if>
             ${param.dataScopeSql}
             group by car_plate) b where b.status = 1
    </select>

    <select id="searchAccumulativeOnlineNum" resultType="java.lang.Integer">
        select
        count(b.car_plate)
        from(SELECT
        o.car_plate,max(o.status) status
        FROM nd_vehicle_online_status_day o
        left join nd_vehicle_base_info v on o.car_plate = v.car_plate
        left join nd_region r on o.region_id = r.id
        left join nd_company_info c on o.company_id = c.id
        where year = year(now()) and month = month(now()) and day = day(now())
        <if test="param.regionIds != null and param.regionIds.size>0">
            and o.region_id in
            <foreach collection="param.regionIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="param.companyIds != null and param.companyIds.size > 0">
            and c.id in
            <foreach collection="param.companyIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="param.companyIds != null and param.companyIds.size == 0">
            and c.id in (0)
        </if>
        <if test="param.operatorId != null">
            and v.operator_id = #{param.operatorId}
        </if>
        ${param.dataScopeSql}
        group by car_plate) b where b.status = 1
    </select>

    <select id="selectDriverAbnormalBehaviorNum"
            resultType="com.ningdatech.carapi.homepage.entity.po.DriverAbnormalBehaviorNumPO">
        select
            sum(case when ndaba.alarm_time &gt;= #{param.queryStartTime} and ndaba.alarm_time &lt;= #{param.queryEndTime} then 1 else 0 end) as total,
            sum(case when ndaba.oversee_status = 1 and ndaba.oversee_time &gt;= #{param.queryStartTime}  and ndaba.oversee_time &lt;= #{param.queryEndTime}  then 1 else 0 end ) as overSeedCount,
            sum(case when ndaba.processing_status = 1 and ndaba.processing_time &gt;= #{param.queryStartTime}  and ndaba.processing_time &lt;= #{param.queryEndTime} then 1 else 0 end ) as processedCount
        from nd_driver_abnormal_behavior_analysis as ndaba
        left join nd_region r on ndaba.region_id = r.id
        left join nd_company_info c on ndaba.company_id = c.id
        where 1 = 1
        <if test="param.regionIds != null and param.regionIds.size>0">
            and ndaba.region_id in
            <foreach collection="param.regionIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        ${param.dataScopeSql}
    </select>
</mapper>