<?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.yfd.platform.modules.robotapi.mapper.RobotOfflineLogMapper">

    <select id="getOnlineDurationList" resultType="java.util.Map">
        SELECT robot_code patroldevice_code,patroldevice_name,production_date,ROUND(total_seconds/3600) value,'h' unit
        FROM (
        SELECT
        rrl.robot_code,
        sp.production_date,
        sp.patroldevice_name,
        UNIX_TIMESTAMP(
        NOW())- UNIX_TIMESTAMP(
        min( report_time )) - COALESCE (( SELECT SUM( UNIX_TIMESTAMP( offline_end_time )- UNIX_TIMESTAMP(
        offline_start_time )) FROM iis_robot_offline_log WHERE
        offline_start_time &gt;= NOW()
        <if test="robotCode != null and robotCode != ''">
            AND robot_code =#{robotCode}
        </if>
        ), 0 ) total_seconds
        FROM
        iis_robot_run_log rrl LEFT JOIN iis_substation_patroldevice sp ON rrl.robot_code= sp.internationalId
        WHERE
        rrl.report_time > sp.production_date
        <if test="robotCode != null and robotCode != ''">
            AND rrl.robot_code =#{robotCode}
        </if>
        GROUP BY
        rrl.robot_code,sp.patroldevice_name,sp.production_date
        ) calculated_duration
    </select>
    <select id="getOfflineTimes" resultType="java.util.Map">
        SELECT
        rol.robot_code patroldevice_code,
        sp.patroldevice_name,
        sp.production_date,
        rol.offline_sum
        VALUE
        ,
        '次' unit
        FROM
        iis_robot_offline_log rol
        INNER JOIN iis_substation_patroldevice sp ON rol.robot_code = sp.internationalId
        WHERE 1=1
        <if test="robotCode != null and robotCode != ''">
            AND rol.robot_code =#{robotCode}
        </if>
        ORDER BY
        rol.offline_end_time DESC
    </select>
    <select id="getOnlineDurationPage" resultType="java.util.Map">
        SELECT robot_code patroldevice_code,`online`,recentStartTime,recentEndTime,onlineNumber,normalDay,checkNormal,type,patroldevice_name,production_date,ROUND(total_seconds/3600) onlineDuration,'h' unit
        FROM (
        SELECT
        rrl.robot_code,
        sp.production_date,
        sp.patroldevice_name,
        sp.type,
        DATEDIFF(MAX(rrl.report_time), MIN(rrl.report_time)) + 1 AS normalDay,
        DATEDIFF(MAX(rrl.report_time), MIN(rrl.report_time)) + 1 AS checkNormal,
        sp.online,
        ( SELECT report_time FROM iis_robot_run_log rrl1 WHERE rrl1.robot_code = rrl.robot_code ORDER BY report_time DESC LIMIT 1 ) recentStartTime,
        (SELECT offline_end_time FROM iis_robot_offline_log rol WHERE rol.robot_code=rrl.robot_code ORDER BY offline_end_time DESC LIMIT 1) recentEndTime,
        (SELECT offline_sum FROM iis_robot_offline_log rol WHERE rol.robot_code=rrl.robot_code ORDER BY offline_end_time DESC LIMIT 1) onlineNumber,
        UNIX_TIMESTAMP(
        MAX(rrl.report_time))- UNIX_TIMESTAMP(
        min( report_time )) - COALESCE (( SELECT SUM( UNIX_TIMESTAMP( offline_end_time )- UNIX_TIMESTAMP(
        offline_start_time )) FROM iis_robot_offline_log WHERE
        offline_start_time &gt;= NOW()
        <if test="patrolDeviceCode != null and patrolDeviceCode != ''">
            AND robot_code =#{patrolDeviceCode}
        </if>
        ), 0 ) total_seconds
        FROM
        iis_robot_run_log rrl RIGHT JOIN iis_substation_patroldevice sp ON rrl.robot_code= sp.internationalId
        WHERE
        rrl.report_time > sp.production_date
        <if test="patrolDeviceCode != null and patrolDeviceCode != ''">
            AND rrl.robot_code =#{patrolDeviceCode}
        </if>
        <if test="patrolDeviceName != null and patrolDeviceName != ''">
            AND sp.patroldevice_name =#{patrolDeviceName}
        </if>
        <if test="stationId != null and stationId != ''">
            AND sp.station_id =#{stationId}
        </if>
        <if test="type != null and type != ''">
            AND sp.type =#{type}
        </if>
        GROUP BY
        rrl.robot_code,sp.type,sp.online,sp.patroldevice_name,sp.production_date
        ) calculated_duration
    </select>
    <select id="normalInspectionDays" resultType="java.util.Map">
        SELECT
        tr.patroldevice_code,
        tr.patroldevice_name,
        COUNT(DISTINCT DATE( tr.time ))	VALUE,
        sp.production_date,
        '天' unit
        FROM
        iis_task_result tr LEFT JOIN iis_substation_patroldevice sp ON tr.patroldevice_code=sp.internationalId
        WHERE
        1 =1
        <if test="robotCode != null and robotCode != ''">
            AND rol.robot_code =#{robotCode}
        </if>
        GROUP BY
        tr.patroldevice_code,
        tr.patroldevice_name,
        sp.production_date
    </select>
    <select id="normalRunDays" resultType="java.util.Map">
        SELECT
        rrl.robot_code patroldevice_code,
        sp.patroldevice_name,
        sp.production_date,
        rrl.days AS
        VALUE
        ,
        '天' unit
        FROM
        ( SELECT COUNT( DISTINCT DATE( report_time )) days, robot_code FROM iis_robot_run_log GROUP BY robot_code ) rrl
        INNER JOIN iis_substation_patroldevice sp ON rrl.robot_code = sp.internationalId
        WHERE
        1 =1
        <if test="robotCode != null and robotCode != ''">
            AND rol.robot_code =#{robotCode}
        </if>

    </select>

</mapper>
