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

    <resultMap type="pd" id="IbmsBroadcastDeviceResult">
        <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="zoneId"    column="zone_id"    />
        <result property="zoneCode"    column="zone_code"    />
        <result property="zoneName"    column="zone_name"    />
        <result property="ipAddress"    column="ip_address"    />
        <result property="macAddress"    column="mac_address"    />
        <result property="brand"    column="brand"    />
        <result property="model"    column="model"    />
        <result property="power"    column="power"    />
        <result property="status"    column="status"    />
        <result property="isOnline"    column="is_online"    />
        <result property="faultCode"    column="fault_code"    />
        <result property="faultDesc"    column="fault_desc"    />
        <result property="lastHeartbeat"    column="last_heartbeat"    />
        <result property="positionX"    column="position_x"    />
        <result property="positionY"    column="position_y"    />
        <result property="positionZ"    column="position_z"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <sql id="selectIbmsBroadcastDeviceVo">
        select id, device_code, device_name, device_type, zone_id, zone_code, zone_name,
        ip_address, mac_address, brand, model, power, status, is_online,
        fault_code, fault_desc, last_heartbeat, position_x, position_y, position_z,
        create_time, update_time
        from ibms_broadcast_device
    </sql>

    <select id="selectIbmsBroadcastDeviceList" parameterType="pd" resultMap="IbmsBroadcastDeviceResult">
        <include refid="selectIbmsBroadcastDeviceVo"/>
        <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="deviceType != null and deviceType != ''">
                and device_type = #{deviceType}
            </if>
            <if test="zoneId != null">
                and zone_id = #{zoneId}
            </if>
            <if test="zoneName != null and zoneName != ''">
                and zone_name like concat('%', #{zoneName}, '%')
            </if>
            <if test="status != null">
                and status = #{status}
            </if>
            <if test="isOnline != null">
                and is_online = #{isOnline}
            </if>
        </where>
        order by zone_code, device_type, device_code
    </select>

    <!-- 查询设备统计信息 -->
    <select id="selectDeviceStatistics" resultType="pd">
        select
        count(1) as total,
        sum(case when is_online = 1 then 1 else 0 end) as online,
        sum(case when status = 0 then 1 else 0 end) as fault,
        sum(case when status = 2 then 1 else 0 end) as maintenance
        from ibms_broadcast_device
    </select>

    <!-- 根据分区ID查询设备 -->
    <select id="selectDevicesByZoneId" parameterType="Long" resultMap="IbmsBroadcastDeviceResult">
        <include refid="selectIbmsBroadcastDeviceVo"/>
        where zone_id = #{zoneId}
        order by device_type, device_code
    </select>

</mapper>