<?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.ruoyi.web.v1.mapper.MonitorMapper">
    
    <resultMap type="com.ruoyi.web.v1.model.Monitor" id="MonitorResult">
        <result property="id"    column="id"    />
        <result property="monitorName"    column="monitor_name"    />
        <result property="regionId"    column="region_id"    />
        <result property="monitorBrand"    column="monitor_brand"    />
        <result property="ipAddr"    column="ip_addr"    />
        <result property="deviceId"    column="device_id"    />
        <result property="channelId"    column="channel_id"    />
        <result property="createTime"    column="create_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="delFlag"    column="del_flag"    />
        <association property="region"  javaType="com.ruoyi.web.v1.model.Region"  resultMap="RegionResult" />
    </resultMap>

    <resultMap type="com.ruoyi.web.v1.model.Region" id="RegionResult">
        <result property="id"    column="id"    />
        <result property="regionName"    column="region_name"    />
    </resultMap>

    <sql id="selectMonitorVo">
        select id, monitor_name, region_id, monitor_brand, ip_addr, create_time, create_by, update_time, update_by, del_flag from monitor
    </sql>

    <select id="selectMonitorList" parameterType="com.ruoyi.web.v1.model.Monitor" resultMap="MonitorResult">
        select a.*, b.region_name
            from monitor as a
            left join region as b on b.id = a.region_id
        <where>
            a.del_flag = '0'
            <if test="monitorName != null  and monitorName != ''"> and a.monitor_name like concat('%', #{monitorName}, '%')</if>
            <if test="regionId != null  and regionId != ''"> and a.region_id =#{regionId}</if>
        </where>
        order by a.create_time desc
    </select>

    <select id="checkMonitorNameUnique" parameterType="String" resultMap="MonitorResult">
        select id, monitor_name from monitor where monitor_name = #{monitorName} and del_flag = '0' limit 1
    </select>

    <select id="selectMonitorById" parameterType="Long" resultMap="MonitorResult">
        <include refid="selectMonitorVo"/>
        where id = #{id} and del_flag = '0'
    </select>

    <insert id="insertMonitor" parameterType="com.ruoyi.web.v1.model.Monitor" useGeneratedKeys="true" keyProperty="id">
        insert into monitor
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="monitorName != null">monitor_name,</if>
            <if test="regionId != null">region_id,</if>
            <if test="monitorBrand != null">monitor_brand,</if>
            <if test="ipAddr != null">ip_addr,</if>
            <if test="deviceId != null">device_id,</if>
            <if test="channelId != null">channel_id,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createBy != null">create_by,</if>
            del_flag
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="monitorName != null">#{monitorName},</if>
            <if test="regionId != null">#{regionId},</if>
            <if test="monitorBrand != null">#{monitorBrand},</if>
            <if test="ipAddr != null">#{ipAddr},</if>
            <if test="deviceId != null">#{deviceId},</if>
            <if test="channelId != null">#{channelId},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createBy != null">#{createBy},</if>
            '0'
         </trim>
    </insert>

    <insert id="insertMonitorBatch" parameterType="list" >
        INSERT INTO monitor (monitor_name,monitor_brand,ip_addr,device_id,channel_id, create_time, create_by,del_flag)
        VALUES
        <foreach collection="list" item="item" index="index" separator=",">
            (#{item.monitorName}, #{item.monitorBrand}, #{item.ipAddr}, #{item.deviceId},#{item.channelId},#{item.createTime}, #{item.createBy},'0')
        </foreach>
    </insert>

    <update id="updateMonitor" parameterType="com.ruoyi.web.v1.model.Monitor">
        update monitor
        <trim prefix="SET" suffixOverrides=",">
            <if test="monitorName != null">monitor_name = #{monitorName},</if>
            <if test="regionId != null">region_id = #{regionId},</if>
            <if test="monitorBrand != null">monitor_brand = #{monitorBrand},</if>
            <if test="ipAddr != null">ip_addr = #{ipAddr},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteMonitorById" parameterType="Long">
        update monitor set del_flag = '2' where id = #{id}
    </delete>

    <delete id="deleteMonitorByIds" parameterType="String">
        update monitor set del_flag = '2' where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <delete id="deleteAllMonitor">
        update monitor set del_flag = '2' where del_flag = '0'
    </delete>

    <!--根据区域ID统计设备数量-->
    <select id="countMonitorByRegion" parameterType="Long" resultType="Integer">
        select count(1) from monitor
        where region_id = #{regionId} and del_flag = '0'
    </select>
</mapper>