<?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.LightingMapper">

    <resultMap type="pd" id="LightingDeviceResult">
        <result property="id"    column="id"    />
        <result property="deviceCode"    column="device_code"    />
        <result property="deviceName"    column="device_name"    />
        <result property="buildingId"    column="building_id"    />
        <result property="buildingName"    column="building_name"    />
        <result property="floorId"    column="floor_id"    />
        <result property="floorName"    column="floor_name"    />
        <result property="areaName"    column="area_name"    />
        <result property="circuitNo"    column="circuit_no"    />
        <result property="status"    column="status"    />
        <result property="brightness"    column="brightness"    />
        <result property="controlMode"    column="control_mode"    />
        <result property="isOnline"    column="is_online"    />
        <result property="faultStatus"    column="fault_status"    />
        <result property="faultDesc"    column="fault_desc"    />
        <result property="lastUpdateTime"    column="last_update_time"    />
        <result property="createTime"    column="create_time"    />
    </resultMap>

    <sql id="selectLightingDeviceVo">
        select id, device_code, device_name, building_id, building_name, floor_id, floor_name,
               area_name, circuit_no, status, brightness, control_mode, is_online, fault_status,
               fault_desc, last_update_time, create_time from ibms_lighting_device
    </sql>

    <select id="selectLightingDeviceList" parameterType="pd" resultMap="LightingDeviceResult">
        <include refid="selectLightingDeviceVo"/>
        <where>
            <if test="buildingId != null"> and building_id = #{buildingId}</if>
            <if test="floorId != null"> and floor_id = #{floorId}</if>
            <if test="areaName != null and areaName != ''"> and area_name like concat('%', #{areaName}, '%')</if>
            <if test="status != null"> and status = #{status}</if>
            <if test="controlMode != null"> and control_mode = #{controlMode}</if>
        </where>
        order by building_id, floor_id, device_code
    </select>

    <update id="updateLightingDevice" parameterType="pd">
        update ibms_lighting_device
        <set>
            <if test="status != null">status = #{status},</if>
            <if test="brightness != null">brightness = #{brightness},</if>
            <if test="controlMode != null">control_mode = #{controlMode},</if>
            <if test="faultStatus != null">fault_status = #{faultStatus},</if>
            <if test="faultDesc != null">fault_desc = #{faultDesc},</if>
            <if test="lastUpdateTime != null">last_update_time = #{lastUpdateTime},</if>
            update_time = now()
        </set>
        where device_code = #{deviceCode}
    </update>

    <select id="selectLightingAlarmList" parameterType="pd" resultType="pd">
        select id, device_code, device_name, alarm_type, alarm_level, alarm_desc,
        alarm_time, handle_status, handle_time, handle_user, handle_remark, create_time
        from ibms_lighting_alarm
        <where>
            <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 alarm_time &gt;= #{beginTime}
            </if>
            <if test="endTime != null and endTime != ''">
                and alarm_time &lt;= #{endTime}
            </if>
        </where>
        order by alarm_time desc
    </select>

    <update id="updateLightingAlarm" parameterType="pd">
        update ibms_lighting_alarm
        <set>
            <if test="handleStatus != null">handle_status = #{handleStatus},</if>
            <if test="handleTime != null">handle_time = #{handleTime},</if>
            <if test="handleUser != null">handle_user = #{handleUser},</if>
            <if test="handleRemark != null">handle_remark = #{handleRemark},</if>
        </set>
        where id = #{id}
    </update>

    <select id="selectLightingScheduleList" parameterType="pd" resultType="pd">
        select id, schedule_name, device_ids, schedule_type, start_time, end_time,
               week_days, month_days, exclude_holidays, action_type, brightness, is_enabled,
               create_time, update_time
        from ibms_lighting_schedule
        order by create_time desc
    </select>

    <insert id="insertLightingSchedule" parameterType="pd" useGeneratedKeys="true" keyProperty="id">
        insert into ibms_lighting_schedule
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="scheduleName != null">schedule_name,</if>
            <if test="deviceIds != null">device_ids,</if>
            <if test="scheduleType != null">schedule_type,</if>
            <if test="startTime != null">start_time,</if>
            <if test="endTime != null">end_time,</if>
            <if test="weekDays != null">week_days,</if>
            <if test="monthDays != null">month_days,</if>
            <if test="excludeHolidays != null">exclude_holidays,</if>
            <if test="actionType != null">action_type,</if>
            <if test="brightness != null">brightness,</if>
            <if test="isEnabled != null">is_enabled,</if>
            create_time
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="scheduleName != null">#{scheduleName},</if>
            <if test="deviceIds != null">#{deviceIds},</if>
            <if test="scheduleType != null">#{scheduleType},</if>
            <if test="startTime != null">#{startTime},</if>
            <if test="endTime != null">#{endTime},</if>
            <if test="weekDays != null">#{weekDays},</if>
            <if test="monthDays != null">#{monthDays},</if>
            <if test="excludeHolidays != null">#{excludeHolidays},</if>
            <if test="actionType != null">#{actionType},</if>
            <if test="brightness != null">#{brightness},</if>
            <if test="isEnabled != null">#{isEnabled},</if>
            now()
        </trim>
    </insert>

    <update id="updateLightingSchedule" parameterType="pd">
        update ibms_lighting_schedule
        <set>
            <if test="scheduleName != null">schedule_name = #{scheduleName},</if>
            <if test="deviceIds != null">device_ids = #{deviceIds},</if>
            <if test="scheduleType != null">schedule_type = #{scheduleType},</if>
            <if test="startTime != null">start_time = #{startTime},</if>
            <if test="endTime != null">end_time = #{endTime},</if>
            <if test="weekDays != null">week_days = #{weekDays},</if>
            <if test="monthDays != null">month_days = #{monthDays},</if>
            <if test="excludeHolidays != null">exclude_holidays = #{excludeHolidays},</if>
            <if test="actionType != null">action_type = #{actionType},</if>
            <if test="brightness != null">brightness = #{brightness},</if>
            <if test="isEnabled != null">is_enabled = #{isEnabled},</if>
            update_time = now()
        </set>
        where id = #{id}
    </update>

    <delete id="deleteLightingSchedule" parameterType="Long">
        delete from ibms_lighting_schedule where id = #{id}
    </delete>

</mapper>