<?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.inspect.job.mapper.PatrolDeviceStateMapper">
    <update id="initData">
        INSERT INTO patrol_device_state_record (patrol_device_code, patrol_device_name, time, type, create_time)
        select patrol_device_code as patrol_device_code,
               patrol_device_name as patrol_device_name,
               now() as time,type as type,create_time as create_time
        from basedata_eqpbook
        where type != 12 and type != 20 and del_flag = 0 and patrol_device_code is not null

    </update>

    <select id="queryData" resultType="HashMap">
        select patrol_device_code                                patrol_device_code,
               any_value(patrol_device_name)                     patrol_device_name,
               any_value(create_time)                            commission_time,
               sum(online_time_sum)                              onlineSum,
               sum(offline_count)                                offlineCount,
               count(case when state = 0 then 1 else 0 end)      nomalDays,
               count(case when task_state = 1 then 1 else 0 end) taskDays,
               round(avg(record_time), 3)                        recordTime,
               count(1)                                          totalDays
        from patrol_device_state_record
        group by patrol_device_code
    </select>

    <select id="queryStationCode" resultType="java.lang.String">
        select station_no stationNo
        from basedata_station limit 1

    </select>

    <update id="initDataInfo">
        -- //录像时长
        UPDATE basedata_statis_nvr nvr,
            patrol_device_state_record record
        SET record.record_time = nvr.duration
        WHERE
            nvr.patrol_device_code = record.patrol_device_code
          AND DATE_FORMAT(record.time
            , '%Y-%m-%d' ) = DATE_FORMAT(CURRENT_DATE ()- 1
            , '%Y-%m-%d')
          AND DATE_FORMAT(nvr.create_time
            , '%Y-%m-%d') = DATE_FORMAT(CURRENT_DATE ()- 1
            , '%Y-%m-%d');
        -- //在线时长、离线次数
        UPDATE basedata_statis_robotuav_online_day nvr,
            patrol_device_state_record record
        SET record.online_time_sum = nvr.total_online_time, record.offline_count = nvr.offline_count
        WHERE
            nvr.patrol_device_code = record.patrol_device_code
          AND DATE_FORMAT( record.time
            , '%Y-%m-%d' ) = DATE_FORMAT(
            CURRENT_DATE ()- 1
            , '%Y-%m-%d')
          AND DATE_FORMAT(nvr.create_time
            , '%Y-%m-%d') = DATE_FORMAT(CURRENT_DATE ()- 1
            , '%Y-%m-%d');
        -- //更新设备状态
        UPDATE basedata_mont_patdevstadata nvr,
            patrol_device_state_record record
        SET record.state = nvr.`value`
        WHERE
            nvr.patrol_device_code = record.patrol_device_code
          AND DATE_FORMAT( record.time
            , '%Y-%m-%d' ) = DATE_FORMAT(
            CURRENT_DATE ()- 1
            , '%Y-%m-%d')
          AND DATE_FORMAT(nvr.time
            , '%Y-%m-%d') = DATE_FORMAT(CURRENT_DATE ()- 1
            , '%Y-%m-%d');
        -- //更新任务是否执行
        UPDATE
            patrol_device_state_record record,
            (SELECT
            (CASE COUNT (*) WHEN (COUNT (1) > 0) THEN 0 ELSE 1 END) AS task_state,record.patrol_device_code
            FROM
            patrol_task nvr,
            basedata_mont_taskstadata sta,
            patrol_device_state_record record
            WHERE
            find_in_set(nvr.dev_no,record.patrol_device_code)
            AND nvr.task_code = sta.task_code
            AND DATE_FORMAT( sta.create_time, '%Y-%m-%d' ) = DATE_FORMAT( CURRENT_DATE ()- 1, '%Y-%m-%d')
            AND DATE_FORMAT( record.time, '%Y-%m-%d' ) = DATE_FORMAT(CURRENT_DATE ()- 1, '%Y-%m-%d')
            GROUP BY record.patrol_device_code) aa
        set record.task_state = aa.task_state
        WHERE
            DATE_FORMAT( record.time
            , '%Y-%m-%d' ) = DATE_FORMAT(CURRENT_DATE ()- 1
            , '%Y-%m-%d')
          AND record.patrol_device_code = aa.patrol_device_code;
    </update>

    <resultMap type="com.inspect.job.domain.task.EqpBookChannel" id="BasedataEqpBookChannelResult">
        <result property="channelId" column="channel_id"/>
        <result property="channelCode" column="channel_code"/>
        <result property="channelName" column="channel_name"/>
        <result property="onlineState" column="online_state"/>
    </resultMap>
    <select id="selectEqpBookChannel" resultMap="BasedataEqpBookChannelResult">
        select channel_id,channel_code,channel_name,online_state
        from basedata_eqpbook_channel
    </select>

    <update id="updateEqpBookChannel" parameterType="com.inspect.job.domain.task.EqpBookChannel">
        update basedata_eqpbook_channel
        <trim prefix="SET" suffixOverrides=",">
            <if test="channelName != null and channelName != ''">channel_name = #{channelName},</if>
            <if test="onlineState != null and onlineState != ''">online_state = #{onlineState},</if>
        </trim>
        where channel_id = #{channelId}
    </update>

    <select id="selectPatrolPresetParam" parameterType="Long" resultType="java.util.Map">
        SELECT a.preset_param_code
        FROM patrol_preset_param a, patrol_preset_pos b
        WHERE a.preset_pos_id = b.preset_pos_id AND b.patrol_point_id = #{pointId}
            limit 1
    </select>

</mapper>