<?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.RmResourceRegistrationMapper">

    <resultMap type="RmResourceRegistration" id="RmResourceRegistrationResult">
        <result property="id"    column="id"    />
        <result property="hardwareSn"    column="hardware_sn"    />
        <result property="resourceType"    column="resource_type"    />
        <result property="resourceName"    column="resource_name"    />
        <result property="ipAddress"    column="ip_address"    />
        <result property="resourcePort"    column="resource_port"    />
        <result property="otherPortName"    column="other_port_name"    />
        <result property="agentVersion"    column="agent_version"    />
        <result property="protocol"    column="protocol"    />
        <result property="resourceVersion"    column="resource_version"    />
        <result property="rwPermission"    column="rw_permission"    />
        <result property="snmpDetect"    column="snmp_detect"    />
        <result property="teamName"    column="team_name"    />
        <result property="snmpCollectAddr"    column="snmp_collect_addr"    />
        <result property="snmpCollectPort"    column="snmp_collect_port"    />
        <result property="securityLevel"    column="security_level"    />
        <result property="encryption"    column="encryption"    />
        <result property="resourceUserName"    column="resource_user_name"    />
        <result property="resourcePwd"    column="resource_pwd"    />
        <result property="registrationStatus"    column="registration_status"    />
        <result property="onlineStatus"    column="online_status"    />
        <result property="switchOnlineStatus"    column="switch_online_status"    />
        <result property="description"    column="description"    />
        <result property="customerId"    column="customer_id"    />
        <result property="customerName"    column="customer_name"    />
        <result property="serviceNumber"    column="service_number"    />
        <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"    />
        <result property="clientId"    column="client_id"    />
        <result property="operator"    column="operator"    />
        <result property="province"    column="province"    />
        <result property="publicIp"    column="public_ip"    />
        <result property="businessName"    column="business_name"    />
        <result property="logicalNodeId"    column="logical_node_id"    />
        <result property="multiPublicIpStatus"    column="multi_public_ip_status"    />
        <result property="heartbeatCount"    column="heartbeat_count"    />
        <result property="heartbeatInterval"    column="heartbeat_interval"    />
        <result property="onboardTime"    column="onboard_time"    />
    </resultMap>

    <sql id="selectRmResourceRegistrationVo">
        select id, hardware_sn, resource_type, resource_name, ip_address, resource_port, other_port_name,
               agent_version, protocol, resource_version, rw_permission, snmp_detect, team_name, snmp_collect_addr,
               snmp_collect_port, security_level, encryption, resource_user_name, resource_pwd, registration_status,
               online_status, switch_online_status, description, customer_id, customer_name, service_number,
               create_time, update_time, creator_id, creator_name, updater_id, updater_name, client_id, operator,
               province, public_ip, business_name, logical_node_id, multi_public_ip_status, heartbeat_count,
               heartbeat_interval, onboard_time from rm_resource_registration
    </sql>

    <select id="selectRmResourceRegistrationList" parameterType="RmResourceRegistration" resultMap="RmResourceRegistrationResult">
        <include refid="selectRmResourceRegistrationVo"/>
        <where>
            <if test="hardwareSn != null  and hardwareSn != ''"> and hardware_sn = #{hardwareSn}</if>
            <if test="resourceType != null  and resourceType != ''"> and resource_type = #{resourceType}</if>
            <if test="resourceName != null  and resourceName != ''"> and resource_name like concat('%', #{resourceName}, '%')</if>
            <if test="ipAddress != null  and ipAddress != ''"> and ip_address = #{ipAddress}</if>
            <if test="resourcePort != null  and resourcePort != ''"> and resource_port = #{resourcePort}</if>
            <if test="otherPortName != null  and otherPortName != ''"> and other_port_name like concat('%', #{otherPortName}, '%')</if>
            <if test="agentVersion != null  and agentVersion != ''"> and agent_version = #{agentVersion}</if>
            <if test="protocol != null  and protocol != ''"> and protocol = #{protocol}</if>
            <if test="resourceVersion != null  and resourceVersion != ''"> and resource_version = #{resourceVersion}</if>
            <if test="rwPermission != null  and rwPermission != ''"> and rw_permission = #{rwPermission}</if>
            <if test="snmpDetect != null  and snmpDetect != ''"> and snmp_detect = #{snmpDetect}</if>
            <if test="teamName != null  and teamName != ''"> and team_name like concat('%', #{teamName}, '%')</if>
            <if test="snmpCollectAddr != null  and snmpCollectAddr != ''"> and snmp_collect_addr = #{snmpCollectAddr}</if>
            <if test="snmpCollectPort != null "> and snmp_collect_port = #{snmpCollectPort}</if>
            <if test="securityLevel != null  and securityLevel != ''"> and security_level = #{securityLevel}</if>
            <if test="encryption != null  and encryption != ''"> and encryption = #{encryption}</if>
            <if test="resourceUserName != null  and resourceUserName != ''"> and resource_user_name like concat('%', #{resourceUserName}, '%')</if>
            <if test="resourcePwd != null  and resourcePwd != ''"> and resource_pwd = #{resourcePwd}</if>
            <if test="registrationStatus != null  and registrationStatus != ''"> and registration_status = #{registrationStatus}</if>
            <if test="onlineStatus != null  and onlineStatus != ''"> and online_status = #{onlineStatus}</if>
            <if test="switchOnlineStatus != null  and switchOnlineStatus != ''"> and switch_online_status = #{switchOnlineStatus}</if>
            <if test="description != null  and description != ''"> and description = #{description}</if>
            <if test="customerId != null "> and customer_id = #{customerId}</if>
            <if test="customerName != null  and customerName != ''"> and customer_name like concat('%', #{customerName}, '%')</if>
            <if test="serviceNumber != null  and serviceNumber != ''"> and service_number = #{serviceNumber}</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>
            <if test="queryName != null  and queryName != ''"> and (hardware_sn = #{queryName} or resource_name like concat('%', #{queryName}, '%')) </if>
            <if test="clientId != null  and clientId != ''"> and client_id = #{clientId}</if>
            <if test="operator != null  and operator != ''"> and operator = #{operator}</if>
            <if test="province != null  and province != ''"> and province = #{province}</if>
            <if test="publicIp != null  and publicIp != ''"> and public_ip = #{publicIp}</if>
            <if test="businessName != null  and businessName != ''"> and business_name like concat('%', #{businessName}, '%')</if>
            <if test="logicalNodeId != null  and logicalNodeId != ''"> and logical_node_id = #{logicalNodeId}</if>
            <if test="multiPublicIpStatus != null  and multiPublicIpStatus != ''"> and multi_public_ip_status = #{multiPublicIpStatus}</if>
        </where>
        order by create_time desc
    </select>

    <select id="selectRmResourceRegistrationById" parameterType="Long" resultMap="RmResourceRegistrationResult">
        <include refid="selectRmResourceRegistrationVo"/>
        where id = #{id}
    </select>

    <insert id="insertRmResourceRegistration" parameterType="RmResourceRegistration" useGeneratedKeys="true" keyProperty="id">
        insert into rm_resource_registration
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="hardwareSn != null">hardware_sn,</if>
            <if test="resourceType != null">resource_type,</if>
            <if test="resourceName != null">resource_name,</if>
            <if test="ipAddress != null">ip_address,</if>
            <if test="resourcePort != null">resource_port,</if>
            <if test="otherPortName != null">other_port_name,</if>
            <if test="agentVersion != null">agent_version,</if>
            <if test="protocol != null">protocol,</if>
            <if test="resourceVersion != null">resource_version,</if>
            <if test="rwPermission != null">rw_permission,</if>
            <if test="snmpDetect != null">snmp_detect,</if>
            <if test="teamName != null">team_name,</if>
            <if test="snmpCollectAddr != null">snmp_collect_addr,</if>
            <if test="snmpCollectPort != null">snmp_collect_port,</if>
            <if test="securityLevel != null">security_level,</if>
            <if test="encryption != null">encryption,</if>
            <if test="resourceUserName != null">resource_user_name,</if>
            <if test="resourcePwd != null">resource_pwd,</if>
            <if test="registrationStatus != null">registration_status,</if>
            <if test="onlineStatus != null">online_status,</if>
            <if test="switchOnlineStatus != null">switch_online_status,</if>
            <if test="description != null">description,</if>
            <if test="customerId != null">customer_id,</if>
            <if test="customerName != null">customer_name,</if>
            <if test="serviceNumber != null">service_number,</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>
            <if test="clientId != null">client_id,</if>
            <if test="operator != null">operator,</if>
            <if test="province != null">province,</if>
            <if test="publicIp != null">public_ip,</if>
            <if test="businessName != null">business_name,</if>
            <if test="logicalNodeId != null">logical_node_id,</if>
            <if test="multiPublicIpStatus != null">multi_public_ip_status,</if>
            <if test="heartbeatCount != null">heartbeat_count,</if>
            <if test="heartbeatInterval != null">heartbeat_interval,</if>
            <if test="onboardTime != null">onboard_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="hardwareSn != null">#{hardwareSn},</if>
            <if test="resourceType != null">#{resourceType},</if>
            <if test="resourceName != null">#{resourceName},</if>
            <if test="ipAddress != null">#{ipAddress},</if>
            <if test="resourcePort != null">#{resourcePort},</if>
            <if test="otherPortName != null">#{otherPortName},</if>
            <if test="agentVersion != null">#{agentVersion},</if>
            <if test="protocol != null">#{protocol},</if>
            <if test="resourceVersion != null">#{resourceVersion},</if>
            <if test="rwPermission != null">#{rwPermission},</if>
            <if test="snmpDetect != null">#{snmpDetect},</if>
            <if test="teamName != null">#{teamName},</if>
            <if test="snmpCollectAddr != null">#{snmpCollectAddr},</if>
            <if test="snmpCollectPort != null">#{snmpCollectPort},</if>
            <if test="securityLevel != null">#{securityLevel},</if>
            <if test="encryption != null">#{encryption},</if>
            <if test="resourceUserName != null">#{resourceUserName},</if>
            <if test="resourcePwd != null">#{resourcePwd},</if>
            <if test="registrationStatus != null">#{registrationStatus},</if>
            <if test="onlineStatus != null">#{onlineStatus},</if>
            <if test="switchOnlineStatus != null">#{switchOnlineStatus},</if>
            <if test="description != null">#{description},</if>
            <if test="customerId != null">#{customerId},</if>
            <if test="customerName != null">#{customerName},</if>
            <if test="serviceNumber != null">#{serviceNumber},</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>
            <if test="clientId != null">#{clientId},</if>
            <if test="operator != null">#{operator},</if>
            <if test="province != null">#{province},</if>
            <if test="publicIp != null">#{publicIp},</if>
            <if test="businessName != null">#{businessName},</if>
            <if test="logicalNodeId != null">#{logicalNodeId},</if>
            <if test="multiPublicIpStatus != null">#{multiPublicIpStatus},</if>
            <if test="heartbeatCount != null">#{heartbeatCount},</if>
            <if test="heartbeatInterval != null">#{heartbeatInterval},</if>
            <if test="onboardTime != null">#{onboardTime},</if>
        </trim>
    </insert>

    <update id="updateRmResourceRegistration" parameterType="RmResourceRegistration">
        update rm_resource_registration
        <trim prefix="SET" suffixOverrides=",">
            <if test="hardwareSn != null">hardware_sn = #{hardwareSn},</if>
            <if test="resourceType != null">resource_type = #{resourceType},</if>
            <if test="resourceName != null">resource_name = #{resourceName},</if>
            <if test="ipAddress != null">ip_address = #{ipAddress},</if>
            <if test="resourcePort != null">resource_port = #{resourcePort},</if>
            <if test="otherPortName != null">other_port_name = #{otherPortName},</if>
            <if test="agentVersion != null">agent_version = #{agentVersion},</if>
            <if test="protocol != null">protocol = #{protocol},</if>
            <if test="resourceVersion != null">resource_version = #{resourceVersion},</if>
            <if test="rwPermission != null">rw_permission = #{rwPermission},</if>
            <if test="snmpDetect != null">snmp_detect = #{snmpDetect},</if>
            <if test="teamName != null">team_name = #{teamName},</if>
            <if test="snmpCollectAddr != null">snmp_collect_addr = #{snmpCollectAddr},</if>
            <if test="snmpCollectPort != null">snmp_collect_port = #{snmpCollectPort},</if>
            <if test="securityLevel != null">security_level = #{securityLevel},</if>
            <if test="encryption != null">encryption = #{encryption},</if>
            <if test="resourceUserName != null">resource_user_name = #{resourceUserName},</if>
            <if test="resourcePwd != null">resource_pwd = #{resourcePwd},</if>
            <if test="registrationStatus != null">registration_status = #{registrationStatus},</if>
            <if test="onlineStatus != null">online_status = #{onlineStatus},</if>
            <if test="switchOnlineStatus != null">switch_online_status = #{switchOnlineStatus},</if>
            <if test="description != null">description = #{description},</if>
            <if test="customerId != null">customer_id = #{customerId},</if>
            <if test="customerName != null">customer_name = #{customerName},</if>
            <if test="serviceNumber != null">service_number = #{serviceNumber},</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>
            <if test="clientId != null">client_id = #{clientId},</if>
            <if test="operator != null">operator = #{operator},</if>
            <if test="province != null">province = #{province},</if>
            <if test="publicIp != null">public_ip = #{publicIp},</if>
            <if test="businessName != null">business_name = #{businessName},</if>
            <if test="logicalNodeId != null">logical_node_id = #{logicalNodeId},</if>
            <if test="multiPublicIpStatus != null">multi_public_ip_status = #{multiPublicIpStatus},</if>
            <if test="heartbeatCount != null">heartbeat_count = #{heartbeatCount},</if>
            <if test="heartbeatInterval != null">heartbeat_interval = #{heartbeatInterval},</if>
            <if test="onboardTime != null">onboard_time = #{onboardTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteRmResourceRegistrationById" parameterType="Long">
        delete from rm_resource_registration where id = #{id}
    </delete>

    <delete id="deleteRmResourceRegistrationByIds" parameterType="String">
        delete from rm_resource_registration where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    <!-- 查询所有资源名称（包含设备使用）-->
    <select id="selectAllResourceName" resultType="java.util.Map">
        select id id,resource_name resourceName from rm_resource_registration
        <where>
            and registration_status = 1
        </where>
        group by id
    </select>
    <!-- 查询所有资源名称-->
    <select id="selectAllResourceNameByType" parameterType="RmResourceRegistration" resultType="java.util.Map">
        SELECT
        resource_name AS resourceName,
        CASE
        WHEN resource_type = '1' THEN ip_address
        WHEN resource_type = '2' THEN snmp_collect_addr
        END AS ipAddress,
        hardware_sn AS hardwareSn
        FROM rm_resource_registration
        <where>
            and registration_status = 1
            <if test="hardwareSn != null  and hardwareSn != ''"> and hardware_sn = #{hardwareSn}</if>
            <if test="resourceType != null  and resourceType != ''"> and resource_type = #{resourceType}</if>
            <if test="resourceName != null  and resourceName != ''"> and resource_name like concat('%', #{resourceName}, '%')</if>
            <if test="ipAddress != null  and ipAddress != ''"> and ip_address = #{ipAddress}</if>
            <if test="resourcePort != null  and resourcePort != ''"> and resource_port = #{resourcePort}</if>
            <if test="otherPortName != null  and otherPortName != ''"> and other_port_name = #{otherPortName}</if>
            <if test="protocol != null  and protocol != ''"> and protocol = #{protocol}</if>
            <if test="resourceVersion != null  and resourceVersion != ''"> and resource_version = #{resourceVersion}</if>
            <if test="rwPermission != null  and rwPermission != ''"> and rw_permission = #{rwPermission}</if>
            <if test="securityLevel != null  and securityLevel != ''"> and security_level = #{securityLevel}</if>
            <if test="encryption != null  and encryption != ''"> and encryption = #{encryption}</if>
            <if test="resourceUserName != null  and resourceUserName != ''"> and resource_user_name = #{resourceUserName}</if>
            <if test="resourcePwd != null  and resourcePwd != ''"> and resource_pwd = #{resourcePwd}</if>
            <if test="registrationStatus != null  and registrationStatus != ''"> and registration_status = #{registrationStatus}</if>
            <if test="onlineStatus != null  and onlineStatus != ''"> and online_status = #{onlineStatus}</if>
            <if test="description != null  and description != ''"> and description = #{description}</if>
            <if test="customerId != null "> and customer_id = #{customerId}</if>
            <if test="customerName != null  and customerName != ''"> and customer_name like concat('%', #{customerName}, '%')</if>
            <if test="serviceNumber != null  and serviceNumber != ''"> and service_number = #{serviceNumber}</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>
        group by resource_name
    </select>
    <select id="countBySn" parameterType="String">
        select count(1) from rm_resource_registration
        where hardware_sn = #{hardwareSn}
    </select>
    <update id="updateStatusByResource" parameterType="RmResourceRegistration">
        update rm_resource_registration
        <trim prefix="SET" suffixOverrides=",">
            <if test="resourceType != null">resource_type = #{resourceType},</if>
            <if test="resourceName != null">resource_name = #{resourceName},</if>
            <if test="ipAddress != null">ip_address = #{ipAddress},</if>
            <if test="resourcePort != null">resource_port = #{resourcePort},</if>
            <if test="otherPortName != null">other_port_name = #{otherPortName},</if>
            <if test="agentVersion != null">agent_version = #{agentVersion},</if>
            <if test="protocol != null">protocol = #{protocol},</if>
            <if test="resourceVersion != null">resource_version = #{resourceVersion},</if>
            <if test="rwPermission != null">rw_permission = #{rwPermission},</if>
            <if test="snmpDetect != null">snmp_detect = #{snmpDetect},</if>
            <if test="teamName != null">team_name = #{teamName},</if>
            <if test="snmpCollectAddr != null">snmp_collect_addr = #{snmpCollectAddr},</if>
            <if test="snmpCollectPort != null">snmp_collect_port = #{snmpCollectPort},</if>
            <if test="securityLevel != null">security_level = #{securityLevel},</if>
            <if test="encryption != null">encryption = #{encryption},</if>
            <if test="resourceUserName != null">resource_user_name = #{resourceUserName},</if>
            <if test="resourcePwd != null">resource_pwd = #{resourcePwd},</if>
            <if test="registrationStatus != null">registration_status = #{registrationStatus},</if>
            <if test="onlineStatus != null">online_status = #{onlineStatus},</if>
            <if test="switchOnlineStatus != null">switch_online_status = #{switchOnlineStatus},</if>
            <if test="description != null">description = #{description},</if>
            <if test="customerId != null">customer_id = #{customerId},</if>
            <if test="customerName != null">customer_name = #{customerName},</if>
            <if test="serviceNumber != null">service_number = #{serviceNumber},</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>
            <if test="clientId != null">client_id = #{clientId},</if>
            <if test="operator != null">operator = #{operator},</if>
            <if test="province != null">province = #{province},</if>
            <if test="publicIp != null">public_ip = #{publicIp},</if>
            <if test="businessName != null">business_name = #{businessName},</if>
            <if test="logicalNodeId != null">logical_node_id = #{logicalNodeId},</if>
            <if test="multiPublicIpStatus != null">multi_public_ip_status = #{multiPublicIpStatus},</if>
            <if test="heartbeatCount != null">heartbeat_count = #{heartbeatCount},</if>
            <if test="heartbeatInterval != null">heartbeat_interval = #{heartbeatInterval},</if>
            <if test="onboardTime != null">onboard_time = #{onboardTime},</if>
        </trim>
        <where>
            <choose>
                <when test="hardwareSn != null">
                    and hardware_sn = #{hardwareSn}
                </when>
                <when test="clientId != null">
                    and client_id = #{clientId}
                </when>
                <otherwise>
                    and 1=0 <!-- 如果两个条件都不满足，则不更新任何记录 -->
                </otherwise>
            </choose>
        </where>
    </update>
    <select id="getRegistrationByIds" parameterType="String" resultMap="RmResourceRegistrationResult">
        <include refid="selectRmResourceRegistrationVo"/>
        where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>

    <select id="selectRegistMsgByClientId" parameterType="RmResourceRegistration" resultMap="RmResourceRegistrationResult">
        <include refid="selectRmResourceRegistrationVo"/>
        <where>
            <if test="clientId != null  and clientId != ''"> and client_id = #{clientId}</if>
        </where>
        order by create_time desc
    </select>
    <select id="getAllLogicalNodeId" parameterType="RmResourceRegistration" resultType="java.util.Map">
        select logical_node_id logicalNodeId from rm_resource_registration where logical_node_id != ''
        group by logical_node_id
    </select>
</mapper>