<?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.zksy.base.manhole.mapper.ManholeDataMapper">

    <resultMap id="BaseResultMap" type="com.zksy.base.manhole.domain.ManholeData">
        <id property="id" column="id" jdbcType="VARCHAR"/>
        <result property="versionNumber" column="version_number" jdbcType="VARCHAR"/>
        <result property="batteryLevel" column="battery_level" jdbcType="VARCHAR"/>
        <result property="uploadTime" column="upload_time" jdbcType="VARCHAR"/>
        <result property="temperatureValue" column="temperature_value" jdbcType="VARCHAR"/>
        <result property="signalStrength" column="signal_strength" jdbcType="VARCHAR"/>
        <result property="alarmStatus" column="alarm_status" jdbcType="VARCHAR"/>
        <result property="angleAlarmThreshold" column="angle_alarm_threshold" jdbcType="VARCHAR"/>
        <result property="tiltAngle" column="tilt_angle" jdbcType="VARCHAR"/>
        <result property="activationStatus" column="activation_status" jdbcType="VARCHAR"/>
        <result property="waterInfiltrationAlarmStatus" column="water_infiltration_alarm_status" jdbcType="VARCHAR"/>
        <result property="waterLevelAlarmStatus" column="water_level_alarm_status" jdbcType="VARCHAR"/>
        <result property="simCardNumber" column="sim_card_number" jdbcType="VARCHAR"/>
        <result property="imeiCardNumber" column="imei_card_number" jdbcType="VARCHAR"/>
        <result property="originalData" column="original_data" jdbcType="VARCHAR"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
    </resultMap>

    <sql id="Base_Column_List">
        id, version_number, battery_level, upload_time, temperature_value, 
        signal_strength, alarm_status, angle_alarm_threshold, tilt_angle, 
        activation_status, water_infiltration_alarm_status, water_level_alarm_status, 
        sim_card_number, imei_card_number, original_data, create_time
    </sql>

    <select id="statManholeByDevice" resultType="java.util.Map">
        SELECT 
            imei_card_number as imeiCardNumber,
            COUNT(*) as dataCount,
            SUM(CASE WHEN alarm_status = '1' THEN 1 ELSE 0 END) as alarmCount,
            SUM(CASE WHEN water_infiltration_alarm_status = '1' THEN 1 ELSE 0 END) as waterInfiltrationCount,
            SUM(CASE WHEN water_level_alarm_status = '1' THEN 1 ELSE 0 END) as waterLevelCount,
            AVG(CAST(temperature_value AS DECIMAL(10,2))) as avgTemperature,
            AVG(CAST(tilt_angle AS DECIMAL(10,2))) as avgTiltAngle
        FROM jg_device_data
        WHERE create_time BETWEEN #{startTime} AND #{endTime}
        GROUP BY imei_card_number
        ORDER BY dataCount DESC
    </select>

    <select id="statAlarmTrendByHour" resultType="java.util.Map">
        SELECT 
            TO_CHAR(create_time, 'YYYY-MM-DD HH24:00:00') as hourTime,
            COUNT(*) as dataCount,
            SUM(CASE WHEN alarm_status = '1' THEN 1 ELSE 0 END) as alarmCount,
            SUM(CASE WHEN water_infiltration_alarm_status = '1' THEN 1 ELSE 0 END) as waterInfiltrationCount,
            SUM(CASE WHEN water_level_alarm_status = '1' THEN 1 ELSE 0 END) as waterLevelCount
        FROM jg_device_data
        WHERE imei_card_number = #{imeiCardNumber}
          AND create_time BETWEEN #{startTime} AND #{endTime}
        GROUP BY TO_CHAR(create_time, 'YYYY-MM-DD HH24')
        ORDER BY hourTime
    </select>

    <select id="statTemperatureTrendByHour" resultType="java.util.Map">
        SELECT 
            TO_CHAR(create_time, 'YYYY-MM-DD HH24:00:00') as hourTime,
            AVG(CAST(temperature_value AS DECIMAL(10,2))) as avgTemperature,
            AVG(CAST(tilt_angle AS DECIMAL(10,2))) as avgTiltAngle,
            AVG(CAST(battery_level AS DECIMAL(10,2))) as avgBatteryLevel
        FROM jg_device_data
        WHERE imei_card_number = #{imeiCardNumber}
          AND create_time BETWEEN #{startTime} AND #{endTime}
        GROUP BY TO_CHAR(create_time, 'YYYY-MM-DD HH24')
        ORDER BY hourTime
    </select>

    <select id="getAllImeiCardNumbers" resultType="java.lang.String">
        SELECT DISTINCT imei_card_number 
        FROM jg_device_data 
        WHERE imei_card_number IS NOT NULL 
          AND imei_card_number != ''
        ORDER BY imei_card_number
    </select>

    <select id="getLatestDataByImei" resultType="java.util.Map">
        SELECT 
            id,
            version_number as versionNumber,
            battery_level as batteryLevel,
            upload_time as uploadTime,
            temperature_value as temperatureValue,
            signal_strength as signalStrength,
            alarm_status as alarmStatus,
            angle_alarm_threshold as angleAlarmThreshold,
            tilt_angle as tiltAngle,
            activation_status as activationStatus,
            water_infiltration_alarm_status as waterInfiltrationAlarmStatus,
            water_level_alarm_status as waterLevelAlarmStatus,
            sim_card_number as simCardNumber,
            imei_card_number as imeiCardNumber,
            create_time as createTime
        FROM jg_device_data
        WHERE imei_card_number = #{imeiCardNumber}
        ORDER BY create_time DESC
        LIMIT 1
    </select>

    <select id="getOverallStatistics" resultType="java.util.Map">
        SELECT 
            COUNT(*) as totalDataCount,
            COUNT(DISTINCT imei_card_number) as deviceCount,
            SUM(CASE WHEN alarm_status = '1' THEN 1 ELSE 0 END) as totalAlarmCount,
            SUM(CASE WHEN water_infiltration_alarm_status = '1' THEN 1 ELSE 0 END) as waterInfiltrationCount,
            SUM(CASE WHEN water_level_alarm_status = '1' THEN 1 ELSE 0 END) as waterLevelCount,
            AVG(CAST(temperature_value AS DECIMAL(10,2))) as avgTemperature,
            AVG(CAST(tilt_angle AS DECIMAL(10,2))) as avgTiltAngle,
            AVG(CAST(battery_level AS DECIMAL(10,2))) as avgBatteryLevel
        FROM jg_device_data
        WHERE create_time BETWEEN #{startTime} AND #{endTime}
    </select>

</mapper>
