<?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="org.example.mapper.TerminalMapper">

    <resultMap id="TerminalResultMap" type="org.example.entity.Terminal">
        <id property="id" column="id"/>
        <result property="terminalType" column="terminal_type"/>
        <result property="terminalNumber" column="terminal_number"/>
        <result property="terminalDesc" column="terminal_desc"/>
        <result property="deptCode" column="dept_code"/>
        <result property="wardCode" column="ward_code"/>
        <result property="ipAddress" column="ip_address"/>
        <result property="subnetMask" column="subnet_mask"/>
        <result property="gatewayAddress" column="gateway_address"/>
        <result property="macAddress" column="mac_address"/>
        <result property="isOnline" column="is_online"/>
        <result property="createTime" column="create_time"/>
    </resultMap>

    <!-- 查询所有设备 -->
    <select id="findAll" resultMap="TerminalResultMap">
        SELECT id, terminal_type, terminal_number, terminal_desc, dept_code, ward_code, ip_address, subnet_mask, gateway_address, mac_address, is_online
        FROM tb_terminal
    </select>

    <!-- 根据设备编号查询设备 -->
    <select id="findByTerminalNumber" parameterType="string" resultMap="TerminalResultMap">
        SELECT id, terminal_type, terminal_number, terminal_desc, dept_code, ward_code, ip_address, subnet_mask, gateway_address, mac_address, is_online
        FROM tb_terminal
        WHERE terminal_number = #{terminalNumber} LIMIT 1
    </select>

    <!-- 根据设备类型模糊查询设备列表 -->
    <select id="findByTerminalTypeContaining" parameterType="string" resultMap="TerminalResultMap">
        SELECT id, terminal_type, terminal_number, terminal_desc, dept_code, ward_code, ip_address, subnet_mask, gateway_address, mac_address, is_online
        FROM tb_terminal
        WHERE terminal_type LIKE CONCAT('%', #{terminalType}, '%')
    </select>

    <!-- 根据所属科室模糊查询设备列表 -->
    <select id="findByDeptCodeContaining" parameterType="string" resultMap="TerminalResultMap">
        SELECT id, terminal_type, terminal_number, terminal_desc, dept_code, ward_code, ip_address, subnet_mask, gateway_address, mac_address, is_online
        FROM tb_terminal
        WHERE dept_code LIKE CONCAT('%', #{deptCode}, '%')
    </select>

    <!-- 根据所属病区模糊查询设备列表 -->
    <select id="findByWardCodeContaining" parameterType="string" resultMap="TerminalResultMap">
        SELECT id, terminal_type, terminal_number, terminal_desc, dept_code, ward_code, ip_address, subnet_mask, gateway_address, mac_address, is_online
        FROM tb_terminal
        WHERE ward_code LIKE CONCAT('%', #{wardCode}, '%')
    </select>

    <!-- 根据在线状态查询设备列表 -->
    <select id="findByIsOnline" parameterType="boolean" resultMap="TerminalResultMap">
        SELECT id, terminal_type, terminal_number, terminal_desc, dept_code, ward_code, ip_address, subnet_mask, gateway_address, mac_address, is_online
        FROM tb_terminal
        WHERE is_online = #{isOnline}
    </select>

    <!-- 根据设备名称模糊查询设备列表 -->
    <select id="findByTerminalDescContaining" parameterType="string" resultMap="TerminalResultMap">
        SELECT id, terminal_type, terminal_number, terminal_desc, dept_code, ward_code, ip_address, subnet_mask, gateway_address, mac_address, is_online
        FROM tb_terminal
        WHERE terminal_desc LIKE CONCAT('%', #{terminalDesc}, '%')
    </select>

    <!-- 根据IP地址查询设备 -->
    <select id="findByIpAddress" parameterType="string" resultMap="TerminalResultMap">
        SELECT id, terminal_type, terminal_number, terminal_desc, dept_code, ward_code, ip_address, subnet_mask, gateway_address, mac_address, is_online
        FROM tb_terminal
        WHERE ip_address = #{ipAddress} LIMIT 1
    </select>

    <!-- 插入新设备 -->
    <insert id="insert" parameterType="org.example.entity.Terminal">
        INSERT INTO tb_terminal(terminal_type, terminal_number, terminal_desc, dept_code, ward_code, ip_address, subnet_mask, gateway_address, mac_address, is_online, create_time, create_user)
        VALUES(#{terminalType}, #{terminalNumber}, #{terminalDesc}, #{deptCode}, #{wardCode}, #{ipAddress}, #{subnetMask}, #{gatewayAddress}, #{macAddress}, #{isOnline}, NOW(), 0)
    </insert>

    <!-- 更新设备信息 -->
    <update id="update" parameterType="map">
        UPDATE tb_terminal
        SET terminal_type = #{terminal.terminalType},
            terminal_number = #{terminal.terminalNumber},
            terminal_desc = #{terminal.terminalDesc},
            dept_code = #{terminal.deptCode},
            ward_code = #{terminal.wardCode},
            ip_address = #{terminal.ipAddress},
            subnet_mask = #{terminal.subnetMask},
            gateway_address = #{terminal.gatewayAddress},
            mac_address = #{terminal.macAddress},
            is_online = #{terminal.isOnline}
        WHERE terminal_number = #{terminalNumber}
    </update>

    <!-- 删除设备 -->
    <delete id="deleteByTerminalNumber" parameterType="string">
        DELETE FROM tb_terminal WHERE terminal_number = #{terminalNumber}
    </delete>

</mapper>