<?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="vip.xiaonuo.coldchain.modular.monitortarget.mapper.MonitorTargetMapper">

    <resultMap id="MonitorTargetResultMap" type="vip.xiaonuo.coldchain.modular.monitortarget.entity.MonitorTarget">
        <result column="alarm_users" property="alarmUsers" javaType="java.util.List" typeHandler="vip.xiaonuo.coldchain.core.handler.SensorAlarmUserTypeHandler"/>
        <result column="notification_channel" property="notificationChannel" javaType="java.util.List" typeHandler="vip.xiaonuo.coldchain.core.handler.NotificationChannelListTypeHandler"/>
    </resultMap>

    <select id="getCountByStatus" resultType="vip.xiaonuo.coldchain.modular.monitortarget.entity.StatusCount">
        SELECT t.status, COUNT(DISTINCT t.id) AS count
        FROM monitor_target t
        LEFT JOIN monitor_target_region r ON r.monitor_target_id = t.id
        WHERE t.DELETE_FLAG = 'NOT_DELETE'
<!--        &lt;!&ndash; 动态条件：仅在 r 存在时检查 r 的 DELETE_FLAG &ndash;&gt;-->
<!--        AND (r.DELETE_FLAG = 'NOT_DELETE' OR r.id IS NULL)-->
        <!-- 动态条件：按房间ID查询 -->
        <if test="roomId != null and roomId.trim() != ''">
            AND t.room_id = #{roomId}
        </if>
        <!-- 动态条件：按机构ID查询 -->
        <if test="orgId != null and orgId.trim() != ''">
            AND t.create_org = #{orgId}
        </if>
        <!-- 动态条件：按 sensor_code 或 name 模糊查询 -->
        <if test="name != null and name.trim() != ''">
            AND (
            t.name LIKE CONCAT('%', #{name}, '%')
            OR r.sensor_code LIKE CONCAT('%', #{name}, '%')
            OR r.name LIKE CONCAT('%', #{name}, '%')
            )
        </if>
        GROUP BY t.status
    </select>





    <select id="selectMonitorTargetByPage" resultMap="MonitorTargetResultMap">
        SELECT t.*
        FROM monitor_target t
        LEFT JOIN monitor_target_region r ON r.monitor_target_id = t.id
        WHERE t.DELETE_FLAG = 'NOT_DELETE'
        <!-- 动态条件：按名称或 sensor_code 或 name 模糊查询 -->
        <if test="param.searchKey != null and param.searchKey.trim() != ''">
            AND (
            t.name LIKE CONCAT('%', #{param.searchKey}, '%')
            OR r.sensor_code LIKE CONCAT('%', #{param.searchKey}, '%')
            OR r.name LIKE CONCAT('%', #{param.searchKey}, '%')
            )
        </if>
        <!-- 动态条件：按创建用户查询 -->
        <if test="param.userId != null and param.userId.trim() != ''">
            AND t.create_user = #{param.userId}
        </if>
        <!-- 动态条件：按房间ID查询 -->
        <if test="param.roomId != null and param.roomId.trim() != ''">
            AND t.room_id = #{param.roomId}
        </if>
        <!-- 动态条件：按机构ID查询 -->
        <if test="param.orgId != null and param.orgId.trim() != ''">
            AND t.create_org = #{param.orgId}
        </if>
        <!-- 动态条件：按冰箱状态查询 -->
        <if test="param.status != null and param.status.trim() != ''">
            AND t.status = #{param.status}
        </if>
        GROUP BY t.id
        <!-- 动态排序 -->
        <choose>
            <when test="param.sortField != null and param.sortField.trim() != '' and param.sortOrder != null and param.sortOrder.trim() != ''">
                ORDER BY ${param.sortField} ${param.sortOrder}
            </when>
            <otherwise>
                ORDER BY t.status ASC, t.name ASC
            </otherwise>
        </choose>
    </select>




</mapper>