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

    <resultMap id="RoomResultMap" type="org.example.entity.HospitalRoom">
        <id property="code" column="code"/>
        <result property="outCode" column="out_code"/>
        <result property="name" column="name"/>
        <result property="bedCount" column="bed_count"/>
        <result property="belongDept" column="belong_dept"/>
        <result property="belongWard" column="belong_ward"/>
        <result property="sort" column="sort"/>
        <result property="enabled" column="enabled"/>
        <result property="remark" column="remark"/>
    </resultMap>

    <!-- 查询所有病房 -->
    <select id="findAll" resultMap="RoomResultMap">
        SELECT code, out_code, name, bed_count, belong_dept, belong_ward, sort, enabled, remark
        FROM tb_hospital_room
    </select>

    <!-- 根据条件查询病房 -->
    <select id="findByConditions" resultMap="RoomResultMap">
        SELECT code, out_code, name, bed_count, belong_dept, belong_ward, sort, enabled, remark
        FROM tb_hospital_room
        WHERE 1=1
        <if test="code != null and code != ''">AND code = #{code}</if>
        <if test="department != null and department != ''">AND belong_dept LIKE CONCAT('%', #{department}, '%')</if>
        <if test="ward != null and ward != ''">AND belong_ward LIKE CONCAT('%', #{ward}, '%')</if>
        <if test="enabled != null">AND enabled = #{enabled}</if>
    </select>

    <!-- 根据病房代码查询病房 -->
    <select id="findByCode" parameterType="string" resultMap="RoomResultMap">
        SELECT code, out_code, name, bed_count, belong_dept, belong_ward, sort, enabled, remark
        FROM tb_hospital_room
        WHERE code = #{code} LIMIT 1
    </select>

    <!-- 根据病房代码和所属病区查询病房数量 -->
    <select id="countByCodeAndWard" resultType="int">
        SELECT COUNT(*) FROM tb_hospital_room
        WHERE code = #{code} AND belong_ward = #{belongWard}
    </select>

    <!-- 根据病房代码和所属病区查询病房数量（排除指定病房） -->
    <select id="countByCodeAndWardExcluding" resultType="int">
        SELECT COUNT(*) FROM tb_hospital_room
        WHERE code = #{code} AND belong_ward = #{belongWard} AND code != #{excludeCode}
    </select>

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

    <!-- 插入新病房 -->
    <insert id="insert" parameterType="org.example.entity.HospitalRoom">
        INSERT INTO tb_hospital_room(code, out_code, name, bed_count, belong_dept, belong_ward, sort, enabled, remark)
        VALUES(#{code}, #{outCode}, #{name}, #{bedCount}, #{belongDept}, #{belongWard}, #{sort}, #{enabled}, #{remark})
    </insert>

    <!-- 更新病房信息 -->
    <update id="update" parameterType="map">
        UPDATE tb_hospital_room
        SET code = #{room.code}
        <if test="room.outCode != null">, out_code = #{room.outCode}</if>
        <if test="room.name != null">, name = #{room.name}</if>
        <if test="room.bedCount != null">, bed_count = #{room.bedCount}</if>
        <if test="room.belongDept != null">, belong_dept = #{room.belongDept}</if>
        <if test="room.belongWard != null">, belong_ward = #{room.belongWard}</if>
        <if test="room.sort != null">, sort = #{room.sort}</if>
        <if test="room.enabled != null">, enabled = #{room.enabled}</if>
        <if test="room.remark != null">, remark = #{room.remark}</if>
        WHERE code = #{originCode}
    </update>

</mapper>
