<?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.pm.subsystem.mapper.EnergyDeviceMapper">

    <resultMap type="pd" id="EnergyDeviceResult">
        <result property="id"    column="id"    />
        <result property="deviceCode"    column="device_code"    />
        <result property="deviceName"    column="device_name"    />
        <result property="deviceType"    column="device_type"    />
        <result property="energyType"    column="energy_type"    />
        <result property="building"    column="building"    />
        <result property="floor"    column="floor"    />
        <result property="roomNo"    column="room_no"    />
        <result property="currentPower"    column="current_power"    />
        <result property="ratedPower"    column="rated_power"    />
        <result property="dailyConsumption"    column="daily_consumption"    />
        <result property="monthlyConsumption"    column="monthly_consumption"    />
        <result property="yearlyConsumption"    column="yearly_consumption"    />
        <result property="loadRate"    column="load_rate"    />
        <result property="deviceStatus"    column="device_status"    />
        <result property="isOnline"    column="is_online"    />
        <result property="temperature"    column="temperature"    />
        <result property="powerSetting"    column="power_setting"    />
        <result property="thresholdValue"    column="threshold_value"    />
        <result property="description"    column="description"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <sql id="selectEnergyDeviceVo">
        select id, device_code, device_name, device_type, energy_type, building, floor, room_no,
               current_power, rated_power, daily_consumption, monthly_consumption, yearly_consumption,
               load_rate, device_status, is_online, temperature, power_setting, threshold_value,
               description, create_time, update_time
        from energy_device
    </sql>

    <select id="selectEnergyDeviceList" parameterType="pd" resultMap="EnergyDeviceResult">
        <include refid="selectEnergyDeviceVo"/>
        <where>
            <if test="deviceCode != null and deviceCode != ''"> and device_code like concat('%', #{deviceCode}, '%')</if>
            <if test="deviceName != null and deviceName != ''"> and device_name like concat('%', #{deviceName}, '%')</if>
            <if test="energyType != null and energyType != ''"> and energy_type = #{energyType}</if>
            <if test="building != null and building != ''"> and building = #{building}</if>
            <if test="floor != null and floor != ''"> and floor = #{floor}</if>
            <if test="deviceStatus != null"> and device_status = #{deviceStatus}</if>
            <if test="isOnline != null"> and is_online = #{isOnline}</if>
            <if test="beginTime != null and beginTime != ''"> and date_format(update_time,'%y%m%d') &gt;= date_format(#{beginTime},'%y%m%d')</if>
            <if test="endTime != null and endTime != ''"> and date_format(update_time,'%y%m%d') &lt;= date_format(#{endTime},'%y%m%d')</if>
        </where>
        order by update_time desc
    </select>

    <select id="selectEnergyDeviceById" parameterType="pd" resultMap="EnergyDeviceResult">
        <include refid="selectEnergyDeviceVo"/>
        where id = #{id}
    </select>

    <insert id="insertEnergyDevice" parameterType="pd" useGeneratedKeys="true" keyProperty="id">
        insert into energy_device
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="deviceCode != null and deviceCode != ''">device_code,</if>
            <if test="deviceName != null and deviceName != ''">device_name,</if>
            <if test="deviceType != null and deviceType != ''">device_type,</if>
            <if test="energyType != null and energyType != ''">energy_type,</if>
            <if test="building != null and building != ''">building,</if>
            <if test="floor != null and floor != ''">floor,</if>
            <if test="roomNo != null and roomNo != ''">room_no,</if>
            <if test="currentPower != null">current_power,</if>
            <if test="ratedPower != null">rated_power,</if>
            <if test="dailyConsumption != null">daily_consumption,</if>
            <if test="monthlyConsumption != null">monthly_consumption,</if>
            <if test="yearlyConsumption != null">yearly_consumption,</if>
            <if test="loadRate != null">load_rate,</if>
            <if test="deviceStatus != null">device_status,</if>
            <if test="isOnline != null">is_online,</if>
            <if test="temperature != null">temperature,</if>
            <if test="powerSetting != null">power_setting,</if>
            <if test="thresholdValue != null">threshold_value,</if>
            <if test="description != null and description != ''">description,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="deviceCode != null and deviceCode != ''">#{deviceCode},</if>
            <if test="deviceName != null and deviceName != ''">#{deviceName},</if>
            <if test="deviceType != null and deviceType != ''">#{deviceType},</if>
            <if test="energyType != null and energyType != ''">#{energyType},</if>
            <if test="building != null and building != ''">#{building},</if>
            <if test="floor != null and floor != ''">#{floor},</if>
            <if test="roomNo != null and roomNo != ''">#{roomNo},</if>
            <if test="currentPower != null">#{currentPower},</if>
            <if test="ratedPower != null">#{ratedPower},</if>
            <if test="dailyConsumption != null">#{dailyConsumption},</if>
            <if test="monthlyConsumption != null">#{monthlyConsumption},</if>
            <if test="yearlyConsumption != null">#{yearlyConsumption},</if>
            <if test="loadRate != null">#{loadRate},</if>
            <if test="deviceStatus != null">#{deviceStatus},</if>
            <if test="isOnline != null">#{isOnline},</if>
            <if test="temperature != null">#{temperature},</if>
            <if test="powerSetting != null">#{powerSetting},</if>
            <if test="thresholdValue != null">#{thresholdValue},</if>
            <if test="description != null and description != ''">#{description},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
        </trim>
    </insert>

    <update id="updateEnergyDevice" parameterType="pd">
        update energy_device
        <trim prefix="SET" suffixOverrides=",">
            <if test="deviceName != null and deviceName != ''">device_name = #{deviceName},</if>
            <if test="deviceType != null and deviceType != ''">device_type = #{deviceType},</if>
            <if test="energyType != null and energyType != ''">energy_type = #{energyType},</if>
            <if test="building != null and building != ''">building = #{building},</if>
            <if test="floor != null and floor != ''">floor = #{floor},</if>
            <if test="roomNo != null and roomNo != ''">room_no = #{roomNo},</if>
            <if test="currentPower != null">current_power = #{currentPower},</if>
            <if test="ratedPower != null">rated_power = #{ratedPower},</if>
            <if test="dailyConsumption != null">daily_consumption = #{dailyConsumption},</if>
            <if test="monthlyConsumption != null">monthly_consumption = #{monthlyConsumption},</if>
            <if test="yearlyConsumption != null">yearly_consumption = #{yearlyConsumption},</if>
            <if test="loadRate != null">load_rate = #{loadRate},</if>
            <if test="deviceStatus != null">device_status = #{deviceStatus},</if>
            <if test="isOnline != null">is_online = #{isOnline},</if>
            <if test="temperature != null">temperature = #{temperature},</if>
            <if test="powerSetting != null">power_setting = #{powerSetting},</if>
            <if test="thresholdValue != null">threshold_value = #{thresholdValue},</if>
            <if test="description != null and description != ''">description = #{description},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteEnergyDeviceById" parameterType="Long">
        delete from energy_device where id = #{id}
    </delete>

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

    <!-- 查询能耗统计数据 -->
    <select id="selectEnergyStatistics" parameterType="pd" resultType="pd">
        select
            coalesce(sum(case when energy_type = 'electricity' and date(update_time) = curdate() then daily_consumption else 0 end), 0) as todayElectricity,
            coalesce(sum(case when energy_type = 'water' and date(update_time) = curdate() then daily_consumption else 0 end), 0) as todayWater,
            coalesce(sum(case when energy_type = 'gas' and date(update_time) = curdate() then daily_consumption else 0 end), 0) as todayGas,
            coalesce(sum(case when energy_type = 'electricity' then monthly_consumption else 0 end), 0) as monthElectricity,
            coalesce(sum(case when energy_type = 'water' then monthly_consumption else 0 end), 0) as monthWater,
            coalesce(sum(case when energy_type = 'gas' then monthly_consumption else 0 end), 0) as monthGas,
            count(case when is_online = 1 then 1 end) as onlineDevices,
            count(*) as totalDevices,
            count(case when device_status = 2 then 1 end) as alarmDevices
        from energy_device
    </select>

    <!-- 查询能耗历史记录 -->
    <select id="selectEnergyHistoryList" parameterType="pd" resultType="pd">
        select id, device_code, device_name, energy_type, consumption_value,
        power_value, record_time, create_time
        from energy_history
        <where>
            <if test="deviceCode != null and deviceCode != ''"> and device_code = #{deviceCode}</if>
            <if test="energyType != null and energyType != ''"> and energy_type = #{energyType}</if>
            <if test="beginTime != null and beginTime != ''"> and date_format(record_time,'%y%m%d') &gt;= date_format(#{beginTime},'%y%m%d')</if>
            <if test="endTime != null and endTime != ''"> and date_format(record_time,'%y%m%d') &lt;= date_format(#{endTime},'%y%m%d')</if>
        </where>
        order by record_time desc
    </select>

    <!-- 新增能耗历史记录 -->
    <insert id="insertEnergyHistory" parameterType="pd">
        insert into energy_history (device_code, device_name, energy_type, consumption_value, power_value, record_time, create_time)
        values (#{deviceCode}, #{deviceName}, #{energyType}, #{consumptionValue}, #{powerValue}, #{recordTime}, #{createTime})
    </insert>

    <!-- 查询实时能耗数据 -->
    <select id="selectRealTimeEnergyData" parameterType="String" resultType="pd">
        select device_code, device_name, energy_type, current_power,
               daily_consumption, temperature, device_status, is_online, update_time
        from energy_device
        where device_code = #{deviceCode}
    </select>

    <!-- 查询能耗告警信息 -->
    <select id="selectEnergyAlarmList" parameterType="pd" resultType="pd">
        select id, device_code, device_name, alarm_type, alarm_level, alarm_message,
        alarm_value, threshold_value, alarm_time, handle_status, handle_user,
        handle_time, create_time
        from energy_alarm
        <where>
            <if test="deviceCode != null and deviceCode != ''"> and device_code like concat('%', #{deviceCode}, '%')</if>
            <if test="deviceName != null and deviceName != ''"> and device_name like concat('%', #{deviceName}, '%')</if>
            <if test="alarmType != null and alarmType != ''"> and alarm_type = #{alarmType}</if>
            <if test="alarmLevel != null"> and alarm_level = #{alarmLevel}</if>
            <if test="handleStatus != null"> and handle_status = #{handleStatus}</if>
            <if test="beginTime != null and beginTime != ''"> and date_format(alarm_time,'%y%m%d') &gt;= date_format(#{beginTime},'%y%m%d')</if>
            <if test="endTime != null and endTime != ''"> and date_format(alarm_time,'%y%m%d') &lt;= date_format(#{endTime},'%y%m%d')</if>
        </where>
        order by alarm_time desc
    </select>

    <!-- 新增能耗告警 -->
    <insert id="insertEnergyAlarm" parameterType="pd" useGeneratedKeys="true" keyProperty="id">
        insert into energy_alarm (device_code, device_name, alarm_type, alarm_level, alarm_message,
                                  alarm_value, threshold_value, alarm_time, handle_status, create_time)
        values (#{deviceCode}, #{deviceName}, #{alarmType}, #{alarmLevel}, #{alarmMessage},
                #{alarmValue}, #{thresholdValue}, #{alarmTime}, 0, #{createTime})
    </insert>

    <!-- 更新能耗告警状态 -->
    <update id="updateEnergyAlarmStatus" parameterType="pd">
        update energy_alarm
        set handle_status = #{handleStatus}, handle_user = #{handleUser},
            handle_time = #{handleTime}, handle_remark = #{handleRemark}
        where id = #{id}
    </update>

    <!-- 查询设备控制记录 -->
    <select id="selectDeviceControlList" parameterType="pd" resultType="pd">
        select id, device_code, device_name, control_type, control_value,
        control_user, control_time, control_result, create_time
        from device_control
        <where>
            <if test="deviceCode != null and deviceCode != ''"> and device_code like concat('%', #{deviceCode}, '%')</if>
            <if test="deviceName != null and deviceName != ''"> and device_name like concat('%', #{deviceName}, '%')</if>
            <if test="controlType != null and controlType != ''"> and control_type = #{controlType}</if>
            <if test="controlUser != null and controlUser != ''"> and control_user = #{controlUser}</if>
            <if test="beginTime != null and beginTime != ''"> and date_format(control_time,'%y%m%d') &gt;= date_format(#{beginTime},'%y%m%d')</if>
            <if test="endTime != null and endTime != ''"> and date_format(control_time,'%y%m%d') &lt;= date_format(#{endTime},'%y%m%d')</if>
        </where>
        order by control_time desc
    </select>

    <!-- 新增设备控制记录 -->
    <insert id="insertDeviceControl" parameterType="pd" useGeneratedKeys="true" keyProperty="id">
        insert into device_control (device_code, device_name, control_type, control_value,
                                    control_user, control_time, control_result, create_time)
        values (#{deviceCode}, #{deviceName}, #{controlType}, #{controlValue},
                #{controlUser}, #{controlTime}, #{controlResult}, #{createTime})
    </insert>

</mapper>