<?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.ruoyi.record.mapper.RecordBusbarAlarmMapper">

    <resultMap type="RecordBusbarAlarm" id="RecordBusbarAlarmResult">
        <result property="objId"    column="objId"    />
        <result property="busbarCode"    column="busbar_code"    />
        <result property="cabinetCode"    column="cabinet_code"    />
        <result property="tempMax"    column="temp_max"    />
        <result property="tempMin"    column="temp_min"    />
        <result property="tempAvg"    column="temp_avg"    />
        <result property="tempDiff"    column="temp_diff"    />
        <result property="filePath"    column="file_path"    />
        <result property="alarmType"    column="alarm_type"    />
        <result property="alarmValue"    column="alarm_value"    />
        <result property="isFlag"    column="is_flag"    />
        <result property="recordTime"    column="record_time"    />
    </resultMap>

    <resultMap type="RecordBusbarAlarmCountResult" id="RecordBusbarAlarmCountResult">
        <result property="recordDate" column="record_date" />
        <result property="alarmCount" column="alarm_count" />
    </resultMap>

    <sql id="selectRecordBusbarAlarmVo">
        select objId, busbar_code, cabinet_code, temp_max, temp_min, temp_avg, temp_diff, file_path, alarm_type, alarm_value, is_flag, record_time from record_busbar_alarm
    </sql>

    <select id="selectRecordBusbarAlarmList" parameterType="RecordBusbarAlarm" resultMap="RecordBusbarAlarmResult">
        <include refid="selectRecordBusbarAlarmVo"/>
        <where>
            <if test="objId != null "> and objId = #{objId}</if>
            <if test="busbarCode != null "> and busbar_code = #{busbarCode}</if>
            <if test="cabinetCode != null "> and cabinet_code = #{cabinetCode}</if>
            <if test="tempMax != null "> and temp_max = #{tempMax}</if>
            <if test="tempMin != null "> and temp_min = #{tempMin}</if>
            <if test="tempAvg != null "> and temp_avg = #{tempAvg}</if>
            <if test="tempDiff != null "> and temp_diff = #{tempDiff}</if>
            <if test="filePath != null  and filePath != ''"> and file_path = #{filePath}</if>
            <if test="alarmType != null "> and alarm_type = #{alarmType}</if>
            <if test="alarmValue != null "> and alarm_value = #{alarmValue}</if>
            <if test="isFlag != null "> and is_flag = #{isFlag}</if>
            <if test="params.beginRecordTime != null and params.beginRecordTime != '' and params.endRecordTime != null and params.endRecordTime != ''"> and record_time between #{params.beginRecordTime} and #{params.endRecordTime}</if>
        </where>
    </select>

    <select id="selectRecordBusbarAlarmByObjId" parameterType="Long" resultMap="RecordBusbarAlarmResult">
        <include refid="selectRecordBusbarAlarmVo"/>
        where objId = #{objId}
    </select>

    <insert id="insertRecordBusbarAlarm" parameterType="RecordBusbarAlarm">
        insert into record_busbar_alarm
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="objId != null">objId,</if>
            <if test="busbarCode != null">busbar_code,</if>
            <if test="cabinetCode != null">cabinet_code,</if>
            <if test="tempMax != null">temp_max,</if>
            <if test="tempMin != null">temp_min,</if>
            <if test="tempAvg != null">temp_avg,</if>
            <if test="tempDiff != null">temp_diff,</if>
            <if test="filePath != null">file_path,</if>
            <if test="alarmType != null">alarm_type,</if>
            <if test="alarmValue != null">alarm_value,</if>
            <if test="isFlag != null">is_flag,</if>
            <if test="recordTime != null">record_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="objId != null">#{objId},</if>
            <if test="busbarCode != null">#{busbarCode},</if>
            <if test="cabinetCode != null">#{cabinetCode},</if>
            <if test="tempMax != null">#{tempMax},</if>
            <if test="tempMin != null">#{tempMin},</if>
            <if test="tempAvg != null">#{tempAvg},</if>
            <if test="tempDiff != null">#{tempDiff},</if>
            <if test="filePath != null">#{filePath},</if>
            <if test="alarmType != null">#{alarmType},</if>
            <if test="alarmValue != null">#{alarmValue},</if>
            <if test="isFlag != null">#{isFlag},</if>
            <if test="recordTime != null">#{recordTime},</if>
         </trim>
    </insert>

    <update id="updateRecordBusbarAlarm" parameterType="RecordBusbarAlarm">
        update record_busbar_alarm
        <trim prefix="SET" suffixOverrides=",">
            <if test="busbarCode != null">busbar_code = #{busbarCode},</if>
            <if test="cabinetCode != null">cabinet_code = #{cabinetCode},</if>
            <if test="tempMax != null">temp_max = #{tempMax},</if>
            <if test="tempMin != null">temp_min = #{tempMin},</if>
            <if test="tempAvg != null">temp_avg = #{tempAvg},</if>
            <if test="tempDiff != null">temp_diff = #{tempDiff},</if>
            <if test="filePath != null">file_path = #{filePath},</if>
            <if test="alarmType != null">alarm_type = #{alarmType},</if>
            <if test="alarmValue != null">alarm_value = #{alarmValue},</if>
            <if test="isFlag != null">is_flag = #{isFlag},</if>
            <if test="recordTime != null">record_time = #{recordTime},</if>
        </trim>
        where objId = #{objId}
    </update>

    <delete id="deleteRecordBusbarAlarmByObjId" parameterType="Long">
        delete from record_busbar_alarm where objId = #{objId}
    </delete>

    <delete id="deleteRecordBusbarAlarmByObjIds" parameterType="String">
        delete from record_busbar_alarm where objId in
        <foreach item="objId" collection="array" open="(" separator="," close=")">
            #{objId}
        </foreach>
    </delete>



    <select id="recordBusbarAlarmCount" parameterType="RecordBusbarAlarm" resultMap="RecordBusbarAlarmCountResult">
        SELECT
            DATE(record_time) AS record_date,
            COUNT(*) AS alarm_count
        FROM
            record_busbar_alarm
        WHERE
            record_time >= date_sub(CURDATE(), INTERVAL 7 DAY)
        GROUP BY
            DATE(record_time)
    </select>

</mapper>
