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

    <resultMap id="BedResultMap" type="org.example.entity.HospitalBed">
        <id property="code" column="code"/>
        <result property="outBed" column="out_code"/>
        <result property="name" column="name"/>
        <result property="type" column="bed_type"/>
        <result property="belongDept" column="belong_dept"/>
        <result property="belongWard" column="belong_ward"/>
        <result property="belongRoom" column="belong_room"/>
        <result property="doctor" column="doctor"/>
        <result property="nurse" column="nurse"/>
        <result property="used" column="used"/>
        <result property="sort" column="sort"/>
        <result property="enabled" column="enabled"/>
        <result property="remark" column="remark"/>
    </resultMap>

    <!-- 查询所有床位 -->
    <select id="findAll" resultMap="BedResultMap">
        SELECT code, out_code, name, bed_type, belong_dept, belong_ward, belong_room, doctor, nurse, used, sort, enabled, remark
        FROM tb_hospital_bed
    </select>

    <!-- 根据床位代码查询床位 -->
    <select id="findByCode" parameterType="string" resultMap="BedResultMap">
        SELECT code, out_code, name, bed_type, belong_dept, belong_ward, belong_room, doctor, nurse, used, sort, enabled, remark
        FROM tb_hospital_bed
        WHERE code = #{code} LIMIT 1
    </select>

    <!-- 根据床位名称模糊查询床位列表 -->
    <select id="findByNameContaining" parameterType="string" resultMap="BedResultMap">
        SELECT code, out_code, name, bed_type, belong_dept, belong_ward, belong_room, doctor, nurse, used, sort, enabled, remark
        FROM tb_hospital_bed
        WHERE name LIKE CONCAT('%', #{name}, '%')
    </select>

    <!-- 根据所属科室模糊查询床位列表 -->
    <select id="findByBelongDeptContaining" parameterType="string" resultMap="BedResultMap">
        SELECT code, out_code, name, bed_type, belong_dept, belong_ward, belong_room, doctor, nurse, used, sort, enabled, remark
        FROM tb_hospital_bed
        WHERE belong_dept LIKE CONCAT('%', #{belongDept}, '%')
    </select>

    <!-- 根据所属病区模糊查询床位列表 -->
    <select id="findByBelongWardContaining" parameterType="string" resultMap="BedResultMap">
        SELECT code, out_code, name, bed_type, belong_dept, belong_ward, belong_room, doctor, nurse, used, sort, enabled, remark
        FROM tb_hospital_bed
        WHERE belong_ward LIKE CONCAT('%', #{belongWard}, '%')
    </select>

    <!-- 根据启用状态查询床位列表 -->
    <select id="findByEnabled" parameterType="boolean" resultMap="BedResultMap">
        SELECT code, out_code, name, bed_type, belong_dept, belong_ward, belong_room, doctor, nurse, used, sort, enabled, remark
        FROM tb_hospital_bed
        WHERE enabled = #{enabled}
    </select>

    <!-- 检查是否存在指定代码的床位（用于唯一性检查） -->
    <select id="findByCodeExcluding" resultMap="BedResultMap">
        SELECT code, out_code, name, bed_type, belong_dept, belong_ward, belong_room, doctor, nurse, used, sort, enabled, remark
        FROM tb_hospital_bed
        WHERE code = #{code} AND code != #{excludeCode}
    </select>

    <!-- 插入新床位 -->
    <insert id="insert" parameterType="org.example.entity.HospitalBed">
        INSERT INTO tb_hospital_bed(code, out_code, name, bed_type, belong_dept, belong_ward, belong_room, doctor, nurse, used, sort, enabled, remark)
        VALUES(#{code}, #{outBed}, #{name}, #{type}, #{belongDept}, #{belongWard}, #{belongRoom}, #{doctor}, #{nurse}, #{used}, #{sort}, #{enabled}, #{remark})
    </insert>

    <!-- 更新床位信息 -->
    <update id="update" parameterType="map">
        UPDATE tb_hospital_bed
        SET code = #{bed.code}
        <if test="bed.outBed != null">, out_code = #{bed.outBed}</if>
        <if test="bed.name != null">, name = #{bed.name}</if>
        <if test="bed.type != null">, bed_type = #{bed.type}</if>
        <if test="bed.belongDept != null">, belong_dept = #{bed.belongDept}</if>
        <if test="bed.belongWard != null">, belong_ward = #{bed.belongWard}</if>
        <if test="bed.belongRoom != null">, belong_room = #{bed.belongRoom}</if>
        <if test="bed.doctor != null">, doctor = #{bed.doctor}</if>
        <if test="bed.nurse != null">, nurse = #{bed.nurse}</if>
        <if test="bed.used != null">, used = #{bed.used}</if>
        <if test="bed.sort != null">, sort = #{bed.sort}</if>
        <if test="bed.enabled != null">, enabled = #{bed.enabled}</if>
        <if test="bed.remark != null">, remark = #{bed.remark}</if>
        WHERE code = #{originCode}
    </update>

    <!-- 根据床位代码删除床位 -->
    <delete id="deleteByCode" parameterType="string">
        DELETE FROM tb_hospital_bed WHERE code = #{code}
    </delete>

</mapper>
