<?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.system.mapper.RmResourceGroupMapper">

    <resultMap type="RmResourceGroup" id="RmResourceGroupResult">
        <result property="id"    column="id"    />
        <result property="groupName"    column="group_name"    />
        <result property="description"    column="description"    />
        <result property="includedDevicesId"    column="included_devices_id"    />
        <result property="includedDevicesName"    column="included_devices_name"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="creatorId"    column="creator_id"    />
        <result property="creatorName"    column="creator_name"    />
        <result property="updaterId"    column="updater_id"    />
        <result property="updaterName"    column="updater_name"    />
    </resultMap>

    <sql id="selectRmResourceGroupVo">
        select id, group_name, description, included_devices_id, included_devices_name, create_time, update_time, creator_id, creator_name, updater_id, updater_name from rm_resource_group
    </sql>

    <select id="selectRmResourceGroupList" parameterType="RmResourceGroup" resultMap="RmResourceGroupResult">
        <include refid="selectRmResourceGroupVo"/>
        <where>
            <if test="groupName != null  and groupName != ''"> and group_name like concat('%', #{groupName}, '%')</if>
            <if test="description != null  and description != ''"> and description = #{description}</if>
            <if test="includedDevicesId != null  and includedDevicesId != ''"> and included_devices_id = #{includedDevicesId}</if>
            <if test="includedDevicesName != null  and includedDevicesName != ''"> and included_devices_name like concat('%', #{includedDevicesName}, '%')</if>
            <if test="creatorId != null "> and creator_id = #{creatorId}</if>
            <if test="creatorName != null  and creatorName != ''"> and creator_name like concat('%', #{creatorName}, '%')</if>
            <if test="updaterId != null "> and updater_id = #{updaterId}</if>
            <if test="updaterName != null  and updaterName != ''"> and updater_name like concat('%', #{updaterName}, '%')</if>
        </where>
    </select>

    <select id="selectRmResourceGroupById" parameterType="Long" resultMap="RmResourceGroupResult">
        <include refid="selectRmResourceGroupVo"/>
        where id = #{id}
    </select>

    <insert id="insertRmResourceGroup" parameterType="RmResourceGroup" useGeneratedKeys="true" keyProperty="id">
        insert into rm_resource_group
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="groupName != null">group_name,</if>
            <if test="description != null">description,</if>
            <if test="includedDevicesId != null">included_devices_id,</if>
            <if test="includedDevicesName != null">included_devices_name,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="creatorId != null">creator_id,</if>
            <if test="creatorName != null">creator_name,</if>
            <if test="updaterId != null">updater_id,</if>
            <if test="updaterName != null">updater_name,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="groupName != null">#{groupName},</if>
            <if test="description != null">#{description},</if>
            <if test="includedDevicesId != null">#{includedDevicesId},</if>
            <if test="includedDevicesName != null">#{includedDevicesName},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="creatorId != null">#{creatorId},</if>
            <if test="creatorName != null">#{creatorName},</if>
            <if test="updaterId != null">#{updaterId},</if>
            <if test="updaterName != null">#{updaterName},</if>
        </trim>
    </insert>

    <update id="updateRmResourceGroup" parameterType="RmResourceGroup">
        update rm_resource_group
        <trim prefix="SET" suffixOverrides=",">
            <if test="groupName != null">group_name = #{groupName},</if>
            <if test="description != null">description = #{description},</if>
            <if test="includedDevicesId != null">included_devices_id = #{includedDevicesId},</if>
            <if test="includedDevicesName != null">included_devices_name = #{includedDevicesName},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="creatorId != null">creator_id = #{creatorId},</if>
            <if test="creatorName != null">creator_name = #{creatorName},</if>
            <if test="updaterId != null">updater_id = #{updaterId},</if>
            <if test="updaterName != null">updater_name = #{updaterName},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteRmResourceGroupById" parameterType="Long">
        delete from rm_resource_group where id = #{id}
    </delete>

    <delete id="deleteRmResourceGroupByIds" parameterType="String">
        delete from rm_resource_group where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="selectMonitorMsgAndGroupMsg" parameterType="Long" resultType="RmResourceGroup">
        select b.monitor_items monitorItems,b.discovery_rules discoveryRules from rm_resource_group a left join rm_monitor_template b on a.id=b.resource_group_id
        where FIND_IN_SET(#{id}, a.included_devices_id) > 0
        limit 1
    </select>

    <select id="exitsResourceById" parameterType="RmResourceGroup" resultType="java.lang.Integer">
        select count(1) from rm_resource_group
        where FIND_IN_SET(#{resourceIds}, included_devices_id) > 0
    </select>

    <update id="updateRmPolicyStatus" parameterType="Long">
        update rm_monitor_policy set status = '0'
        where resource_group_id = #{resourceGroupId}
    </update>
    <update id="updateScriptPolicyStatus" parameterType="Long">
        update rm_deployment_policy set policy_status = '0'
        where resource_group_id = #{resourceGroupId}
    </update>
</mapper>