<?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.mtragent.mapper.RmMtrClientRegistrationMapper">

    <resultMap type="RmMtrClientRegistration" id="RmMtrClientRegistrationResult">
        <result property="id"    column="id"    />
        <result property="mtrClientId"    column="mtr_client_id"    />
        <result property="description"    column="description"    />
        <result property="version"    column="version"    />
        <result property="logicalNode"    column="logical_node"    />
        <result property="registerTime"    column="register_time"    />
        <result property="registerStatus"    column="register_status"    />
        <result property="onlineStatus"    column="online_status"    />
        <result property="cpucores"    column="cpucores"    />
        <result property="heartbeatInterval"    column="heartbeat_interval"    />
        <result property="heartbeatCount"    column="heartbeat_count"    />
        <result property="method"    column="method"    />
        <result property="scheduledUpdateTime"    column="scheduled_update_time"    />
        <result property="filePath"    column="file_path"    />
        <result property="fileMd5"    column="file_md5"    />
        <result property="lastUpdateResult"    column="last_update_result"    />
        <result property="lastUpdateTime"    column="last_update_time"    />
        <result property="networkInfo"    column="network_info"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="updateBy"    column="update_by"    />
    </resultMap>

    <sql id="selectRmMtrClientRegistrationVo">
        select id, mtr_client_id, description, version, logical_node, register_time, register_status, online_status, cpucores, heartbeat_interval, heartbeat_count, method, scheduled_update_time, file_path, file_md5, last_update_result, last_update_time, network_info, create_time, update_time, create_by, update_by from rm_mtr_client_registration
    </sql>

    <select id="selectRmMtrClientRegistrationList" parameterType="RmMtrClientRegistration" resultMap="RmMtrClientRegistrationResult">
        <include refid="selectRmMtrClientRegistrationVo"/>
        <where>  
            <if test="mtrClientId != null  and mtrClientId != ''"> and mtr_client_id = #{mtrClientId}</if>
            <if test="description != null  and description != ''"> and description = #{description}</if>
            <if test="version != null  and version != ''"> and version = #{version}</if>
            <if test="logicalNode != null  and logicalNode != ''"> and logical_node = #{logicalNode}</if>
            <if test="registerTime != null "> and register_time = #{registerTime}</if>
            <if test="registerStatus != null  and registerStatus != ''"> and register_status = #{registerStatus}</if>
            <if test="onlineStatus != null  and onlineStatus != ''"> and online_status = #{onlineStatus}</if>
            <if test="heartbeatInterval != null "> and heartbeat_interval = #{heartbeatInterval}</if>
            <if test="heartbeatCount != null "> and heartbeat_count = #{heartbeatCount}</if>
            <if test="method != null  and method != ''"> and method = #{method}</if>
            <if test="scheduledUpdateTime != null  and scheduledUpdateTime != ''"> and scheduled_update_time = #{scheduledUpdateTime}</if>
            <if test="filePath != null  and filePath != ''"> and file_path = #{filePath}</if>
            <if test="fileMd5 != null  and fileMd5 != ''"> and file_md5 = #{fileMd5}</if>
            <if test="lastUpdateResult != null  and lastUpdateResult != ''"> and last_update_result = #{lastUpdateResult}</if>
            <if test="lastUpdateTime != null "> and last_update_time = #{lastUpdateTime}</if>
            <if test="networkInfo != null  and networkInfo != ''"> and network_info = #{networkInfo}</if>
            <!-- 新增：多条件查询，queryName可能是mtr_client_id或关联表的client_id -->
            <if test="queryName != null and queryName != ''">
                and (
                    mtr_client_id like concat('%',#{queryName},'%')
                    or mtr_client_id in (
                    select distinct mtr_client_id from all_mtr_client
                    where client_id like concat('%',#{queryName},'%')
                    )
                )
            </if>
        </where>
        order by create_time desc
    </select>
    
    <select id="selectRmMtrClientRegistrationById" parameterType="Long" resultMap="RmMtrClientRegistrationResult">
        <include refid="selectRmMtrClientRegistrationVo"/>
        where id = #{id}
    </select>

    <insert id="insertRmMtrClientRegistration" parameterType="RmMtrClientRegistration" useGeneratedKeys="true" keyProperty="id">
        insert into rm_mtr_client_registration
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="mtrClientId != null and mtrClientId != ''">mtr_client_id,</if>
            <if test="description != null">description,</if>
            <if test="version != null and version != ''">version,</if>
            <if test="logicalNode != null">logical_node,</if>
            <if test="registerTime != null">register_time,</if>
            <if test="registerStatus != null and registerStatus != ''">register_status,</if>
            <if test="onlineStatus != null and onlineStatus != ''">online_status,</if>
            <if test="cpucores != null">cpucores,</if>
            <if test="heartbeatInterval != null">heartbeat_interval,</if>
            <if test="heartbeatCount != null">heartbeat_count,</if>
            <if test="method != null">method,</if>
            <if test="scheduledUpdateTime != null">scheduled_update_time,</if>
            <if test="filePath != null">file_path,</if>
            <if test="fileMd5 != null">file_md5,</if>
            <if test="lastUpdateResult != null">last_update_result,</if>
            <if test="lastUpdateTime != null">last_update_time,</if>
            <if test="networkInfo != null">network_info,</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>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="mtrClientId != null and mtrClientId != ''">#{mtrClientId},</if>
            <if test="description != null">#{description},</if>
            <if test="version != null and version != ''">#{version},</if>
            <if test="logicalNode != null">#{logicalNode},</if>
            <if test="registerTime != null">#{registerTime},</if>
            <if test="registerStatus != null and registerStatus != ''">#{registerStatus},</if>
            <if test="onlineStatus != null and onlineStatus != ''">#{onlineStatus},</if>
            <if test="cpucores != null">#{cpucores},</if>
            <if test="heartbeatInterval != null">#{heartbeatInterval},</if>
            <if test="heartbeatCount != null">#{heartbeatCount},</if>
            <if test="method != null">#{method},</if>
            <if test="scheduledUpdateTime != null">#{scheduledUpdateTime},</if>
            <if test="filePath != null">#{filePath},</if>
            <if test="fileMd5 != null">#{fileMd5},</if>
            <if test="lastUpdateResult != null">#{lastUpdateResult},</if>
            <if test="lastUpdateTime != null">#{lastUpdateTime},</if>
            <if test="networkInfo != null">#{networkInfo},</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>
        </trim>
    </insert>

    <update id="updateRmMtrClientRegistration" parameterType="RmMtrClientRegistration">
        update rm_mtr_client_registration
        <trim prefix="SET" suffixOverrides=",">
            <if test="mtrClientId != null and mtrClientId != ''">mtr_client_id = #{mtrClientId},</if>
            <if test="description != null">description = #{description},</if>
            <if test="version != null and version != ''">version = #{version},</if>
            <if test="logicalNode != null">logical_node = #{logicalNode},</if>
            <if test="registerTime != null">register_time = #{registerTime},</if>
            <if test="registerStatus != null and registerStatus != ''">register_status = #{registerStatus},</if>
            <if test="onlineStatus != null and onlineStatus != ''">online_status = #{onlineStatus},</if>
            <if test="cpucores != null">cpucores = #{cpucores},</if>
            <if test="heartbeatInterval != null">heartbeat_interval = #{heartbeatInterval},</if>
            <if test="heartbeatCount != null">heartbeat_count = #{heartbeatCount},</if>
            <if test="method != null">method = #{method},</if>
            <if test="scheduledUpdateTime != null">scheduled_update_time = #{scheduledUpdateTime},</if>
            <if test="filePath != null">file_path = #{filePath},</if>
            <if test="fileMd5 != null">file_md5 = #{fileMd5},</if>
            <if test="lastUpdateResult != null">last_update_result = #{lastUpdateResult},</if>
            <if test="lastUpdateTime != null">last_update_time = #{lastUpdateTime},</if>
            <if test="networkInfo != null">network_info = #{networkInfo},</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>
        </trim>
        <where>
            <choose>
                <when test="id != null">
                    and id=#{id}
                </when>
                <when test="mtrClientId">
                    and mtr_client_id = #{mtrClientId}
                </when>
                <otherwise>
                    and 1=0
                </otherwise>
            </choose>
        </where>
    </update>

    <delete id="deleteRmMtrClientRegistrationById" parameterType="Long">
        delete from rm_mtr_client_registration where id = #{id}
    </delete>

    <delete id="deleteRmMtrClientRegistrationByIds" parameterType="String">
        delete from rm_mtr_client_registration where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    <select id="getAllLogicalNode" parameterType="RmMtrClientRegistration" resultMap="RmMtrClientRegistrationResult">
        <include refid="selectRmMtrClientRegistrationVo"/>
        <where>
            and logical_node != ''
        </where>
        group by logical_node
    </select>
    <select id="getMsgByMtrClientId" parameterType="RmMtrClientRegistration" resultMap="RmMtrClientRegistrationResult">
        <include refid="selectRmMtrClientRegistrationVo"/>
        <where>
            <if test="mtrClientId != null  and mtrClientId != ''"> and mtr_client_id = #{mtrClientId}</if>
            <if test="description != null  and description != ''"> and description = #{description}</if>
            <if test="version != null  and version != ''"> and version = #{version}</if>
            <if test="logicalNode != null  and logicalNode != ''"> and logical_node = #{logicalNode}</if>
            <if test="registerTime != null "> and register_time = #{registerTime}</if>
            <if test="registerStatus != null  and registerStatus != ''"> and register_status = #{registerStatus}</if>
            <if test="onlineStatus != null  and onlineStatus != ''"> and online_status = #{onlineStatus}</if>
        </where>
        order by create_time desc
        limit 1
    </select>
</mapper>