<?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.tongran.rocketmq.mapper.RmMonitorPolicyMapper">
    
    <resultMap type="RmMonitorPolicy" id="RmMonitorPolicyResult">
        <result property="id"    column="id"    />
        <result property="templateId"    column="template_id"    />
        <result property="resourceGroupId"    column="resource_group_id"    />
        <result property="policyName"    column="policy_name"    />
        <result property="description"    column="description"    />
        <result property="status"    column="status"    />
        <result property="deployTime"    column="deploy_time"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="updateBy"    column="update_by"    />
        <result property="switchType"    column="switch_type"    />
        <result property="deployDevice"    column="deploy_device"    />
        <result property="resourceType"    column="resource_type"    />
        <result property="priority"    column="priority"    />
    </resultMap>

    <sql id="selectRmMonitorPolicyVo">
        select id, template_id, resource_group_id, policy_name, description, status, deploy_time, create_time, update_time, create_by, update_by, switch_type, deploy_device, resource_type, priority from rm_monitor_policy
    </sql>

    <select id="selectRmMonitorPolicyList" parameterType="RmMonitorPolicy" resultMap="RmMonitorPolicyResult">
        <include refid="selectRmMonitorPolicyVo"/>
        <where>
            <if test="templateId != null "> and template_id = #{templateId}</if>
            <if test="resourceType != null "> and resource_type = #{resourceType}</if>
            <if test="resourceGroupId != null "> and resource_group_id = #{resourceGroupId}</if>
            <if test="policyName != null  and policyName != ''"> and policy_name like concat('%', #{policyName}, '%')</if>
            <if test="description != null  and description != ''"> and description = #{description}</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
            <if test="deployTime != null "> and deploy_time = #{deployTime}</if>
            <if test="priority != null "> and priority = #{priority}</if>
            <if test="queryName != null and queryName != ''">
                and (policy_name like concat('%', #{queryName}, '%')
                or
                <foreach item="item" index="index" collection="queryName.split(',')" open="(" close=")" separator="or">
                    FIND_IN_SET(#{item}, REPLACE(deploy_device, '\n', ',')) > 0
                </foreach>
                )
            </if>
        </where>
        order by create_time desc
    </select>
    
    <select id="selectRmMonitorPolicyById" parameterType="Long" resultMap="RmMonitorPolicyResult">
        <include refid="selectRmMonitorPolicyVo"/>
        where id = #{id}
    </select>

    <insert id="insertRmMonitorPolicy" parameterType="RmMonitorPolicy" useGeneratedKeys="true" keyProperty="id">
        insert into rm_monitor_policy
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="templateId != null">template_id,</if>
            <if test="resourceGroupId != null">resource_group_id,</if>
            <if test="policyName != null and policyName != ''">policy_name,</if>
            <if test="description != null">description,</if>
            <if test="status != null and status != ''">status,</if>
            <if test="deployTime != null">deploy_time,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="switchType != null">switch_type,</if>
            <if test="deployDevice != null">deploy_device,</if>
            <if test="resourceType != null">resource_type,</if>
            <if test="priority != null">priority,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="templateId != null">#{templateId},</if>
            <if test="resourceGroupId != null">#{resourceGroupId},</if>
            <if test="policyName != null and policyName != ''">#{policyName},</if>
            <if test="description != null">#{description},</if>
            <if test="status != null and status != ''">#{status},</if>
            <if test="deployTime != null">#{deployTime},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="switchType != null">#{switchType},</if>
            <if test="deployDevice != null">#{deployDevice},</if>
            <if test="resourceType != null">#{resourceType},</if>
            <if test="priority != null">#{priority},</if>
        </trim>
    </insert>

    <update id="updateRmMonitorPolicy" parameterType="RmMonitorPolicy">
        update rm_monitor_policy
        <trim prefix="SET" suffixOverrides=",">
            <if test="templateId != null">template_id = #{templateId},</if>
            <if test="resourceGroupId != null">resource_group_id = #{resourceGroupId},</if>
            <if test="policyName != null and policyName != ''">policy_name = #{policyName},</if>
            <if test="description != null">description = #{description},</if>
            <if test="status != null and status != ''">status = #{status},</if>
            <if test="deployTime != null">deploy_time = #{deployTime},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="switchType != null">switch_type = #{switchType},</if>
            <if test="deployDevice != null">deploy_device = #{deployDevice},</if>
            <if test="resourceType != null">resource_type = #{resourceType},</if>
            <if test="priority != null">priority = #{priority},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteRmMonitorPolicyById" parameterType="Long">
        delete from rm_monitor_policy where id = #{id}
    </delete>

    <delete id="deleteRmMonitorPolicyByIds" parameterType="String">
        delete from rm_monitor_policy where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    <select id="selectRmMonitorPolicyByClientId" parameterType="RmMonitorPolicy" resultMap="RmMonitorPolicyResult">
        <include refid="selectRmMonitorPolicyVo"/>
        <where>
            <if test="resourceType != null "> and resource_type = #{resourceType}</if>
            <if test="priority != null "> and priority = #{priority}</if>
            <if test="status != null "> and status = #{status}</if>
            <if test="deployDevice != null ">
                and FIND_IN_SET(#{deployDevice}, REPLACE(deploy_device, '\n', ',')) > 0
            </if>
        </where>
        limit 1
    </select>
</mapper>