<?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.fuyuanshen.equipment.mapper.DeviceMapper">
    <resultMap id="BaseResultMap" type="com.fuyuanshen.equipment.domain.Device">
        <id column="id" property="id"/>
        <result column="device_type" property="deviceType"/>
        <result column="customer_id" property="customerId"/>
        <result column="device_no" property="deviceNo"/>
        <result column="device_name" property="deviceName"/>
        <result column="device_pic" property="devicePic"/>
        <result column="device_mac" property="deviceMac"/>
        <result column="device_sn" property="deviceSn"/>
        <result column="device_status" property="deviceStatus"/>
        <result column="remark" property="remark"/>
        <result column="create_by" property="createBy"/>
        <result column="update_by" property="updateBy"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
        <result column="binding_status" property="bindingStatus"/>
    </resultMap>

    <sql id="Base_Column_List">
        id
        , device_type, customer_id, device_name, device_pic, device_mac, device_sn,
        remark, create_by, update_by, create_time, update_time
    </sql>

    <sql id="device_Column_List">
        d
        .
        id
        ,d.device_type,
    d.customer_id, d.device_name, d.device_pic, d.device_mac,
    d.device_sn, d.remark, d.create_by, d.update_by, d.create_time, d.update_time,
    d.binding_status, d.device_status, d.current_owner_id, d.original_owner_id, d.customer_name,
    d.device_imei, d.longitude, d.latitude, d.tenant_id
    </sql>

    <!-- 分页查询设备 -->
    <select id="findAll" resultType="com.fuyuanshen.equipment.domain.Device">
        SELECT *
        FROM (
        SELECT
        da.id AS id, d.id AS deviceId, d.device_name, d.bluetooth_name, d.group_id,
        d.pub_topic, d.sub_topic, d.device_pic,d.online_status,
        d.device_mac, d.device_sn, d.update_by,d.sid,d.tid,d.trid,
        d.device_imei, d.update_time, dg.id AS device_type,
        d.remark, d.binding_status, t.type_name AS typeName,
        da.assignee_id AS customerId, da.assignee_name AS customerName,
        da.active AS deviceStatus, da.create_time AS create_time,
        da.assigner_name AS createByName, da.id AS assignId,
        ROW_NUMBER() OVER (PARTITION BY d.id ORDER BY da.create_time DESC) AS rn
        FROM device d
        LEFT JOIN device_type t ON d.device_type = t.id
        LEFT JOIN device_type_grants dg ON dg.device_type_id = t.id
        LEFT JOIN device_assignments da ON da.device_id = d.id
        <where>
            <!-- 时间范围等其他条件保持原样 -->
            <if test="criteria.deviceName != null and criteria.deviceName.trim() != ''">
                and d.device_name like concat('%', TRIM(#{criteria.deviceName}), '%')
            </if>
            <if test="criteria.deviceMac != null and criteria.deviceMac.trim() != ''">
                and d.device_mac like concat('%', TRIM(#{criteria.deviceMac}), '%')
            </if>
            <if test="criteria.deviceImei != null and criteria.deviceImei.trim() != ''">
                and d.device_imei like concat('%', TRIM(#{criteria.deviceImei}), '%')
            </if>
            <if test="criteria.deviceType != null">
                and d.device_type = #{criteria.deviceType}
            </if>
            <if test="criteria.deviceStatus != null">
                and da.active = #{criteria.deviceStatus}
            </if>
            <if test="criteria.onlineStatus != null">
                and d.online_status = #{criteria.onlineStatus}
            </if>
            <if test="criteria.bindingStatus != null">
                and d.binding_status = #{criteria.bindingStatus}
            </if>
            <if test="criteria.groupId != null">
                and d.group_id = #{criteria.groupId}
            </if>
            <if test="criteria.communicationMode != null">
                and t.communication_mode = #{criteria.communicationMode}
            </if>
            <if test="criteria.params.beginTime != null and criteria.params.endTime != null">
                and da.create_time between #{criteria.params.beginTime} and #{criteria.params.endTime}
            </if>
            <if test="criteria.sid != null">
                and d.sid = #{criteria.sid}
            </if>
            <if test="criteria.isTid == true">
                and d.tid is not null
            </if>
            <if test="criteria.isTid == false">
                and d.tid is null
            </if>
            <!-- 管理员可以查看所有设备，普通用户只能查看自己的设备 -->
            <if test="criteria.isAdmin != true">
                AND da.assignee_id = #{criteria.currentOwnerId}
                AND dg.customer_id = #{criteria.currentOwnerId}
            </if>
        </where>
        ) AS ranked
        WHERE rn = 1
        ORDER BY create_time DESC
    </select>

    <!-- 分页查询设备终端 -->
    <select id="findAllTerminal" resultType="com.fuyuanshen.equipment.domain.Device">
        SELECT *
        FROM (
        SELECT
        da.id AS id, d.id AS deviceId, d.device_name, d.bluetooth_name, d.group_id,
        d.pub_topic, d.sub_topic, d.device_pic,d.online_status,
        d.device_mac, d.device_sn, d.update_by,d.sid,d.tid,d.trid,
        d.device_imei, d.update_time, dg.id AS device_type,
        d.remark, d.binding_status, t.type_name AS typeName, da.create_time AS create_time,
        ROW_NUMBER() OVER (PARTITION BY d.id ORDER BY da.create_time DESC) AS rn
        FROM device d
        LEFT JOIN device_type t ON d.device_type = t.id
        LEFT JOIN device_type_grants dg ON dg.device_type_id = t.id
        LEFT JOIN device_assignments da ON da.device_id = d.id
        LEFT JOIN device_fence_terminal dft ON dft.device_id = d.id
        where d.sid = #{criteria.sid}
        <!-- 时间范围等其他条件保持原样 -->
        <if test="criteria.deviceName != null and criteria.deviceName.trim() != ''">
            and d.device_name like concat('%', TRIM(#{criteria.deviceName}), '%')
        </if>
        <if test="criteria.deviceImei != null and criteria.deviceImei.trim() != ''">
            and d.device_imei like concat('%', TRIM(#{criteria.deviceImei}), '%')
        </if>
        <if test="criteria.isTid == true">
            and dft.fence_id = #{criteria.fenceId}
        </if>
        <if test="criteria.isTid == false">
            and (dft.fence_id is null or dft.fence_id != #{criteria.fenceId})
        </if>
        <!-- 管理员可以查看所有设备，普通用户只能查看自己的设备 -->
        <if test="criteria.isAdmin != true">
            AND da.assignee_id = #{criteria.currentOwnerId}
            AND dg.customer_id = #{criteria.currentOwnerId}
        </if>
        ) AS ranked
        WHERE rn = 1
        ORDER BY create_time DESC
    </select>


    <select id="findAllDevices" resultType="com.fuyuanshen.equipment.domain.Device">
        select
        d.id, d.customer_id, d.device_name,d.bluetooth_name,
        d.device_pic, d.device_mac, d.device_sn, d.create_by, d.update_by,
        d.create_time, d.update_time, d.longitude, d.latitude, d.remark
        from device d
        <where>
            <if test="criteria.deviceName != null and criteria.deviceName.trim() != ''">
                and d.device_name like concat('%', TRIM(#{criteria.deviceName}), '%')
            </if>
            <if test="criteria.deviceMac != null">
                and d.device_mac = #{criteria.deviceMac}
            </if>
            <if test="criteria.deviceTypeId != null">
                and d.device_type = #{criteria.deviceTypeId}
            </if>
            <if test="criteria.createTime != null and criteria.createTime.size() != 0">
                and d.create_time between #{criteria.createTime[0]} and #{criteria.createTime[1]}
            </if>
        </where>
        order by d.id desc
    </select>

    <!-- 根据条件查询 -->
    <select id="findDevices" resultType="com.fuyuanshen.equipment.domain.Device"
            parameterType="com.fuyuanshen.equipment.domain.query.DeviceQueryCriteria">
        SELECT d.*
        FROM device d
        <where>
            <!-- 时间范围等其他条件保持原样 -->
            <if test="criteria.deviceName != null and criteria.deviceName.trim() != ''">
                and d.device_name like concat('%', TRIM(#{criteria.deviceName}), '%')
            </if>
            <if test="criteria.deviceMac != null and criteria.deviceMac.trim() != ''">
                and d.device_mac = #{criteria.deviceMac}
            </if>
            <if test="criteria.deviceImei != null and criteria.deviceImei.trim() != ''">
                and d.device_imei = #{criteria.deviceImei}
            </if>
            <if test="criteria.deviceType != null">
                and d.device_type = #{criteria.deviceType}
            </if>
            <if test="criteria.deviceStatus != null">
                and d.device_status = #{criteria.deviceStatus}
            </if>
            <if test="criteria.groupId != null">
                and d.group_id = #{criteria.groupId}
            </if>
            <if test="criteria.currentOwnerId != null">
                and d.current_owner_id = #{criteria.currentOwnerId}
            </if>
            <if test="criteria.params.beginTime != null and criteria.params.endTime != null">
                and d.create_time between #{criteria.params.beginTime} and #{criteria.params.endTime}
            </if>
        </where>
    </select>

    <!-- 查询APP绑定设备列表 -->
    <select id="queryAppBindDeviceList" resultType="com.fuyuanshen.equipment.domain.vo.AppDeviceVo">
        select d.id, d.device_name, d.device_name,
        d.device_name,
        d.device_mac,
        d.device_sn,
        d.device_imei,
        d.device_pic,
        dt.type_name,
        dt.communication_mode,
        d.bluetooth_name,
        dt.app_model_dictionary detailPageUrl,
        c.binding_time
        from device d
        inner join device_type dt on d.device_type = dt.id
        inner join app_device_bind_record c on d.id = c.device_id
        where c.binding_user_id = #{criteria.bindingUserId}
        <if test="criteria.deviceType != null">
            and d.device_type = #{criteria.deviceType}
        </if>
        <if test="criteria.deviceName != null">
            and d.device_name = #{criteria.deviceName}
        </if>
        <if test="criteria.deviceMac != null">
            and d.device_mac = #{criteria.deviceMac}
        </if>
        <if test="criteria.deviceImei != null">
            and d.device_imei = #{criteria.deviceImei}
        </if>
    </select>


    <select id="queryAppDeviceList" resultType="com.fuyuanshen.equipment.domain.vo.AppDeviceVo">
        select d.id,
        d.device_name,
        d.device_mac,
        d.device_sn,
        d.device_imei,
        d.device_pic,
        dt.type_name,
        dt.communication_mode,
        dt.app_model_dictionary detailPageUrl,
        d.bluetooth_name
        from device d
        inner join device_type dt on d.device_type = dt.id
        where d.binding_user_id = #{criteria.bindingUserId}
        <if test="criteria.deviceType != null">
            and d.device_type = #{criteria.deviceType}
        </if>
        <if test="criteria.deviceName != null">
            and d.device_name = #{criteria.deviceName}
        </if>
        <if test="criteria.deviceMac != null">
            and d.device_mac = #{criteria.deviceMac}
        </if>
        <if test="criteria.deviceImei != null">
            and d.device_imei = #{criteria.deviceImei}
        </if>
    </select>

    <select id="GetDeviceByName" resultType="map" >
        select a.id,
        a.device_type,
        a.device_name,
        a.device_mac,
        a.type_name,
        a.bluetooth_name,
        a.device_imei
        from device a
        <where>
            a.tenant_id = #{tenant_id} and
            a.device_type = #{deviceType}
            AND (
            a.device_name=#{deviceName} or
            a.bluetooth_name=#{deviceName}
            )
        </where>
    </select>

    <select id="getEquipCountByType" resultType="int">
        select count(1) cnt
        from device a
        <where>
            a.tenant_id = #{tenant_id} and
            a.device_type = #{deviceType}
        </where>
    </select>

    <select id="getEquipAllByType" resultType="map">
        select device_mac,bluetooth_name,device_name
        from device a
        <where>
            a.tenant_id = #{tenant_id} and
            a.device_type = #{deviceType}
        </where>

    </select>

    <!-- 获取分配设备的客户 -->
    <select id="getAssignCustomer" resultType="com.fuyuanshen.equipment.domain.Device">
        SELECT *
        FROM device
        WHERE original_device_id = #{customerId}
          AND device_status = 1
    </select>

    <!-- 获取设备链 -->
    <select id="findByOriginalDeviceId" resultType="com.fuyuanshen.equipment.domain.Device"
            parameterType="java.lang.Long">
        SELECT id, original_device_id
        FROM device
        WHERE original_device_id = #{originalDeviceId}
    </select>

    <select id="getDeviceInfo" resultType="com.fuyuanshen.equipment.domain.vo.AppDeviceVo">
        select d.id,
               d.device_name,
               d.device_name,
               d.device_name,
               d.device_mac,
               d.device_sn,
               d.device_imei,
               d.device_pic,
               dt.type_name,
               dt.communication_mode,
               d.bluetooth_name,
               dt.app_model_dictionary detailPageUrl
        from device d
                 inner join device_type dt on d.device_type = dt.id
        where d.device_mac = #{deviceMac}
    </select>

    <select id="queryWebDeviceList" resultType="com.fuyuanshen.equipment.domain.vo.WebDeviceVo">
        select * from (select  d.id, d.device_name,
        d.device_mac,
        d.device_type,
        d.device_sn,
        d.device_imei,
        d.device_pic,
        d.group_id,
        dt.type_name,
        dt.communication_mode,
        d.bluetooth_name,
        dt.pc_model_dictionary detailPageUrl,
        ap.name personnelBy,
        d.device_status,
        d.binding_status,
        d.online_status,
        c.binding_time,
        d.create_time,d.sid,d.tid,d.trid,
        ROW_NUMBER() OVER (PARTITION BY d.id ORDER BY d.id) AS row_num
        from device d
        inner join device_type dt on d.device_type = dt.id
        left join app_device_bind_record c on d.id = c.device_id
        left join app_personnel_info ap on ap.device_id = d.id
        where dt.communication_mode in (0, 2) ORDER BY (case when d.online_status = 1 then 2 else 1 end)  DESC) a where a.row_num = 1
        <if test="criteria.deviceType != null">
            and a.device_type = #{criteria.deviceType}
        </if>
        <if test="criteria.deviceName != null and criteria.deviceName != ''">
            and a.device_name like concat('%', #{criteria.deviceName}, '%')
        </if>
        <if test="criteria.deviceImei != null and criteria.deviceImei != ''">
            and a.device_imei like concat('%',#{criteria.deviceImei}, '%')
        </if>
        <if test="criteria.deviceMac != null and criteria.deviceMac != ''">
            and a.device_mac like concat('%',#{criteria.deviceMac}, '%')
        </if>
        <if test="criteria.content != null and criteria.content != ''">
            AND (a.device_imei like concat('%',#{criteria.content}, '%') or a.device_mac like concat('%',#{criteria.content}, '%') )
        </if>
        <if test="criteria.deviceStatus != null">
            and a.device_status = #{criteria.deviceStatus}
        </if>
        <if test="criteria.personnelBy != null and criteria.personnelBy != ''">
            and a.personnelBy like concat('%', #{criteria.personnelBy}, '%')
        </if>
        <if test="criteria.communicationMode != null">
            and a.communication_mode = #{criteria.communicationMode}
        </if>
        <if test="criteria.groupId != null">
            and a.group_id = #{criteria.groupId}
        </if>
        <if test="criteria.onlineStatus != null">
            and a.online_status = #{criteria.onlineStatus}
        </if>

    </select>
    <select id="getLocationHistory" resultType="com.fuyuanshen.equipment.domain.vo.LocationHistoryVo">
        select a.id,a.device_name,a.device_type,b.type_name deviceTypeName,a.device_imei,a.device_mac from device a
        inner join device_type b on a.device_type = b.id
        where b.communication_mode in (0, 2)
        <if test="bo.deviceType != null">
            AND b.id = #{bo.deviceType}
        </if>
        <if test="bo.deviceName != null and bo.deviceName != ''">
            AND a.device_name like concat('%',#{bo.deviceName},'%')
        </if>
        <if test="bo.deviceMac != null and bo.deviceMac != ''">
            AND a.device_mac like concat('%',#{bo.deviceMac},'%')
        </if>
        <if test="bo.deviceImei != null and bo.deviceImei != ''">
            and a.device_imei like concat('%',#{bo.deviceImei}, '%')
        </if>
        <if test="bo.content != null and bo.content != ''">
            AND (a.device_imei like concat('%',#{bo.content}, '%') or a.device_mac like concat('%',#{bo.content}, '%') )
        </if>
        <if test="bo.startTime != null and bo.startTime != ''">
            AND a.create_time <![CDATA[>=]]> #{bo.startTime}
        </if>
        <if test="bo.endTime != null and bo.endTime != ''">
            AND a.create_time <![CDATA[<=]]> #{bo.endTime}
        </if>
        <if test="bo.groupId != null">
            and a.group_id = #{bo.groupId}
        </if>
        ORDER BY
        a.create_time DESC
    </select>

    <!-- 获取设备总览信息 -->
    <select id="getDeviceOverview" resultType="com.fuyuanshen.equipment.domain.vo.DeviceOverviewVo">
        SELECT (SELECT COUNT(1) FROM device) AS totalDevices,
               (SELECT COUNT(1) FROM device WHERE online_status = 1) AS onlineDevices,
               (SELECT COUNT(DISTINCT device_type) FROM device) AS deviceTypes
    </select>

    <!-- 获取设备通讯方式统计数据 -->
    <select id="getDeviceCommunicationModeStatistics" resultType="com.fuyuanshen.equipment.domain.vo.DeviceCommunicationModeStatisticsVo">
        SELECT 
            dt.communication_mode AS communicationModeValue,
            CASE 
                WHEN dt.communication_mode = 0 THEN '4G'
                WHEN dt.communication_mode = 1 THEN '蓝牙'
                WHEN dt.communication_mode = 2 THEN '4G&amp;蓝牙'
                ELSE '未知'
            END AS communicationModeName,
            COUNT(d.id) AS totalDevices,
            COUNT(CASE WHEN d.online_status = 2 THEN 1 END) AS abnormalDevices
        FROM device_type dt
        LEFT JOIN device d ON dt.id = d.device_type
        WHERE dt.communication_mode IN (0, 1, 2)
        GROUP BY dt.communication_mode
        ORDER BY dt.communication_mode
    </select>

    <!-- 获取数据总览 -->
    <select id="getDataOverview" resultType="com.fuyuanshen.equipment.domain.vo.DataOverviewVo">
        SELECT (SELECT COUNT(1) FROM device)                         AS devicesNumber,
               (SELECT COUNT(1) FROM device WHERE online_status = 1) AS equipmentOnline,
               (SELECT COUNT(1) FROM device WHERE DATE (create_time) = CURDATE() AND binding_status = 1 ) AS bindingNew, (
        SELECT COUNT (1)
        FROM device
        WHERE binding_status = 1 ) AS binding, (
        SELECT COUNT (1)
        FROM device
        WHERE online_status = 2) AS equipmentAbnormal
    </select>

    <!-- 获取设备分类 -->
    <select id="getEquipmentClassification"
            resultType="com.fuyuanshen.equipment.domain.vo.EquipmentClassificationVo">
        SELECT (SELECT COUNT(1)
                FROM device d
                         INNER JOIN device_type dt ON d.device_type = dt.id
                WHERE dt.communication_mode = 0) AS equipment4G,
               (SELECT COUNT(1)
                FROM device d
                         INNER JOIN device_type dt ON d.device_type = dt.id
                WHERE dt.communication_mode = 1) AS deviceBluetooth,
               (SELECT COUNT(1)
                FROM device d
                         INNER JOIN device_type dt ON d.device_type = dt.id
                WHERE dt.communication_mode = 2) AS devices4GAndBluetooth
    </select>

    <!-- 获取告警信息 -->
    <select id="getAlarmInformation" resultType="com.fuyuanshen.equipment.domain.vo.AlarmInformationVo">
        SELECT (SELECT COUNT(1) FROM device_alarm) AS alarmsTotal
             , (SELECT COUNT(1)
                FROM device_alarm
                WHERE treatment_state = 0)         AS processingAlarm
             , (SELECT COUNT(1)
                FROM device_alarm
                WHERE
            DATE (create_time) = CURDATE()) AS alarmsTotalToday
             , (
        SELECT COUNT (1)
        FROM device_alarm
        WHERE treatment_state = 0
          AND DATE (create_time) = CURDATE()) AS processingAlarmToday
            , (
        SELECT COUNT (1)
        FROM device_alarm
        WHERE device_action = 0
            ) AS alarmForced
            , (
        SELECT COUNT (1)
        FROM device_alarm
        WHERE device_action = 1
            ) AS intrusionImpact
            , (
        SELECT COUNT (1)
        FROM device_alarm
        WHERE device_action = 2
            ) AS alarmAuto
            , (
        SELECT COUNT (1)
        FROM device_alarm
        WHERE device_action = 3) AS fenceElectronic

    </select>

    <!-- 获取设备使用数据 -->
    <select id="getEquipmentUsageData" resultType="map">
        SELECT COUNT(CASE WHEN MONTH (dl.create_time) = 1 THEN 1 END) AS m1,
        COUNT(CASE WHEN MONTH (dl.create_time) = 2 THEN 1 END) AS m2,
        COUNT(CASE WHEN MONTH (dl.create_time) = 3 THEN 1 END) AS m3,
        COUNT(CASE WHEN MONTH (dl.create_time) = 4 THEN 1 END) AS m4,
        COUNT(CASE WHEN MONTH (dl.create_time) = 5 THEN 1 END) AS m5,
        COUNT(CASE WHEN MONTH (dl.create_time) = 6 THEN 1 END) AS m6,
        COUNT(CASE WHEN MONTH (dl.create_time) = 7 THEN 1 END) AS m7,
        COUNT(CASE WHEN MONTH (dl.create_time) = 8 THEN 1 END) AS m8,
        COUNT(CASE WHEN MONTH (dl.create_time) = 9 THEN 1 END) AS m9,
        COUNT(CASE WHEN MONTH (dl.create_time) = 10 THEN 1 END) AS m10,
        COUNT(CASE WHEN MONTH (dl.create_time) = 11 THEN 1 END) AS m11,
        COUNT(CASE WHEN MONTH (dl.create_time) = 12 THEN 1 END) AS m12
        FROM device_log dl
        LEFT JOIN device d ON dl.device_id = d.id
        LEFT JOIN device_type dt ON d.device_type = dt.id
        <where>
            <if test="deviceTypeId != null">
                dt.id = #{deviceTypeId}
            </if>
            AND (
            (#{range} = 1 AND dl.create_time >= DATE_SUB(NOW(), INTERVAL 6 MONTH)) OR
            (#{range} = 2 AND dl.create_time >= DATE_SUB(NOW(), INTERVAL 12 MONTH))
            )
        </where>
    </select>

    <select id="getUsageDataForMonth" resultType="java.lang.Integer">
        SELECT COUNT(*)
        FROM device_log dl
        WHERE dl.device_id = #{deviceId}
            AND YEAR (
            dl.create_time) = #{year}
          AND MONTH (dl.create_time) = #{month}
    </select>

    <!-- 获取设备使用频次统计 -->
    <select id="getDeviceUsageFrequency1" resultType="com.fuyuanshen.equipment.domain.vo.DeviceUsageFrequencyVo">
        SELECT 
            device_name AS deviceName,
            COUNT(*) AS frequency
        FROM device_log 
        WHERE create_time >= DATE_SUB(NOW(), INTERVAL #{days} DAY)
        GROUP BY device_name
        ORDER BY frequency DESC
    </select>
    <!-- 获取设备使用频次统计 （设备类型）-->
    <select id="getDeviceUsageFrequency" resultType="com.fuyuanshen.equipment.domain.vo.DeviceUsageFrequencyVo">
        SELECT
            d.type_name AS deviceName,
            COUNT(*) AS frequency
        FROM device_log dl
        INNER JOIN device d ON dl.device_id = d.id
        WHERE dl.create_time >= DATE_SUB(NOW(), INTERVAL #{days} DAY)
        GROUP BY d.device_type, d.type_name
        ORDER BY frequency DESC
    </select>

    <!-- 根据设备IMEI查询设备 -->
    <select id="selectDeviceByImei" resultType="com.fuyuanshen.equipment.domain.Device">
        SELECT *
        FROM device
        WHERE device_imei = #{deviceImei}
    </select>
    <select id="queryOnlineStatusList" resultType="com.fuyuanshen.equipment.domain.vo.OnlineStatusVo">
        SELECT a.id, a.online_status AS onlineStatus, a.device_imei AS deviceImei
        FROM device a left join device_type b on a.device_type = b.id where b.communication_mode in (0, 2) and a.online_status in (1,2)
    </select>

    <!-- 根据设备类型ID查询设备数量 -->
    <select id="countByDeviceTypeId" resultType="int">
        SELECT COUNT(*)
        FROM device
        WHERE device_type = #{deviceTypeId}
    </select>

</mapper>