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

    <resultMap type="pd" id="ElevatorDeviceResult">
        <result property="id"    column="id"    />
        <result property="deviceCode"    column="device_code"    />
        <result property="deviceName"    column="device_name"    />
        <result property="elevatorType"    column="elevator_type"    />
        <result property="buildingId"    column="building_id"    />
        <result property="buildingName"    column="building_name"    />
        <result property="installPosition"    column="install_position"    />
        <result property="brand"    column="brand"    />
        <result property="model"    column="model"    />
        <result property="maxFloor"    column="max_floor"    />
        <result property="minFloor"    column="min_floor"    />
        <result property="ratedLoad"    column="rated_load"    />
        <result property="ratedSpeed"    column="rated_speed"    />
        <result property="currentFloor"    column="current_floor"    />
        <result property="doorStatus"    column="door_status"    />
        <result property="runStatus"    column="run_status"    />
        <result property="direction"    column="direction"    />
        <result property="isOnline"    column="is_online"    />
        <result property="faultStatus"    column="fault_status"    />
        <result property="maintenanceDate"    column="maintenance_date"    />
        <result property="nextMaintenanceDate"    column="next_maintenance_date"    />
        <result property="lastUpdateTime"    column="last_update_time"    />
        <result property="createTime"    column="create_time"    />
    </resultMap>

    <sql id="selectElevatorDeviceVo">
        select id, device_code, device_name, elevator_type, building_id, building_name,
               install_position, brand, model, max_floor, min_floor, rated_load, rated_speed,
               current_floor, door_status, run_status, direction, is_online, fault_status,
               maintenance_date, next_maintenance_date, last_update_time, create_time
        from ibms_elevator_device
    </sql>

    <select id="selectElevatorDeviceList" parameterType="pd" resultMap="ElevatorDeviceResult">
        <include refid="selectElevatorDeviceVo"/>
        <where>
            <if test="buildingId != null"> and building_id = #{buildingId}</if>
            <if test="elevatorType != null and elevatorType != ''"> and elevator_type = #{elevatorType}</if>
            <if test="runStatus != null"> and run_status = #{runStatus}</if>
            <if test="deviceName != null and deviceName != ''">
                and device_name like concat('%', #{deviceName}, '%')
            </if>
        </where>
        order by building_id, device_code
    </select>

    <select id="selectElevatorRecordList" parameterType="pd" resultType="pd">
        select id, device_code, device_name, start_floor, end_floor, start_time,
        end_time, passenger_count, load_weight, run_duration, create_time
        from ibms_elevator_record
        <where>
            <if test="deviceName != null and deviceName != ''">
                and device_name like concat('%', #{deviceName}, '%')
            </if>
            <if test="beginTime != null and beginTime != ''">
                and start_time &gt;= #{beginTime}
            </if>
            <if test="endTime != null and endTime != ''">
                and start_time &lt;= #{endTime}
            </if>
        </where>
        order by start_time desc
    </select>

    <select id="selectElevatorAlarmList" 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_elevator_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="updateElevatorAlarm" parameterType="pd">
        update ibms_elevator_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="selectElevatorCount" parameterType="pd" resultType="int">
        select count(1) from ibms_elevator_device
        <where>
            <if test="isOnline != null"> and is_online = #{isOnline}</if>
            <if test="faultStatus != null"> and fault_status = #{faultStatus}</if>
        </where>
    </select>

    <select id="selectElevatorTypeDistribution" resultType="pd">
        select elevator_type as type, count(1) as count
        from ibms_elevator_device
        group by elevator_type
    </select>

    <select id="selectElevatorBuildingDistribution" resultType="pd">
        select building_name as building, count(1) as count
        from ibms_elevator_device
        group by building_name
    </select>

</mapper>