<?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.RegionMapper">
    
    <resultMap type="com.ruoyi.web.v1.model.Region" id="RegionResult">
        <result property="id"    column="id"    />
        <result property="regionName"    column="region_name"    />
        <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"    />
    </resultMap>

    <resultMap type="com.ruoyi.web.v1.DTO.RegionTreeDTO" id="RegionTreeResult">
        <result property="id"    column="id"    />
        <result property="regionName"    column="region_name"    />
        <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"    />
        <collection  property="monitors"   javaType="java.util.List"  resultMap="MonitorResult" />
    </resultMap>

    <resultMap type="com.ruoyi.web.v1.model.Monitor" id="MonitorResult">
        <result property="id"    column="monitor_id"    />
        <result property="monitorName"    column="monitor_name"    />
        <result property="regionId"    column="region_id"    />
        <result property="deviceId"    column="device_id"    />
        <result property="channelId"    column="channel_id"    />
        <result property="monitorBrand"    column="monitor_brand"    />
        <result property="ipAddr"    column="ip_addr"    />
    </resultMap>

    <sql id="selectRegionVo">
        select id, region_name, create_time, create_by, update_time, update_by, del_flag from region
    </sql>

    <select id="selectRegionTreeList" parameterType="com.ruoyi.web.v1.model.Region" resultMap="RegionTreeResult">
        select a.*,b.id as monitor_id,b.monitor_name,b.monitor_brand,b.ip_addr,b.device_id,b.channel_id
        from region as a
        left join monitor as b on b.region_id = a.id and b.del_flag = '0'
        <where>
            a.del_flag = '0'
            <if test="regionName != null  and regionName != ''"> and a.region_name like concat('%', #{regionName}, '%')</if>
        </where>
        order by a.create_time desc
    </select>

    <select id="selectRegionList" parameterType="com.ruoyi.web.v1.model.Region" resultMap="RegionResult">
        <include refid="selectRegionVo"/>
        <where>
            del_flag = '0'
            <if test="regionName != null  and regionName != ''"> and region_name like concat('%', #{regionName}, '%')</if>
        </where>
        order by create_time desc
    </select>
    
    <select id="selectRegionById" parameterType="Long" resultMap="RegionResult">
        <include refid="selectRegionVo"/>
        where id = #{id} and del_flag = '0'
    </select>


    <select id="checkRegionNameUnique" parameterType="String" resultMap="RegionResult">
        select id, region_name from region where region_name = #{regionName} and del_flag = '0' limit 1
    </select>

    <insert id="insertRegion" parameterType="com.ruoyi.web.v1.model.Region" useGeneratedKeys="true" keyProperty="id">
        insert into region
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="regionName != null and regionName != ''">region_name,</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="regionName != null and regionName != ''">#{regionName},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createBy != null">#{createBy},</if>
            '0'
         </trim>
    </insert>

    <update id="updateRegion" parameterType="com.ruoyi.web.v1.model.Region">
        update region
        <trim prefix="SET" suffixOverrides=",">
            <if test="regionName != null and regionName != ''">region_name = #{regionName},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
        </trim>
        where id = #{id}
    </update>

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

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