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

    <resultMap id="WardResultMap" type="org.example.entity.HospitalWard">
        <id property="code" column="code"/>
        <result property="outCode" column="out_code"/>
        <result property="name" column="name"/>
        <result property="address" column="address"/>
        <result property="glkeshi" column="glkeshi"/>
        <result property="telephone" column="telephone"/>
        <result property="director" column="director"/>
        <result property="headNurse" column="head_nurse"/>
        <result property="bedCount" column="bed_count"/>
        <result property="bedOpenCount" column="bed_open_count"/>
        <result property="sort" column="sort"/>
        <result property="enabled" column="enabled"/>
        <result property="remark" column="remark"/>
    </resultMap>

    <!-- 查询所有病区 -->
    <select id="findAll" resultMap="WardResultMap">
        SELECT code, out_code, name, address, glkeshi, telephone, director, head_nurse, bed_count, bed_open_count, sort, enabled, remark
        FROM tb_hospital_ward
    </select>

    <!-- 根据病区代码查询病区 -->
    <select id="findByCode" parameterType="string" resultMap="WardResultMap">
        SELECT code, out_code, name, address, glkeshi, telephone, director, head_nurse, bed_count, bed_open_count, sort, enabled, remark
        FROM tb_hospital_ward
        WHERE code = #{code} LIMIT 1
    </select>

    <!-- 根据病区名称模糊查询病区列表 -->
    <select id="findByNameContaining" parameterType="string" resultMap="WardResultMap">
        SELECT code, out_code, name, address, glkeshi, telephone, director, head_nurse, bed_count, bed_open_count, sort, enabled, remark
        FROM tb_hospital_ward
        WHERE name LIKE CONCAT('%', #{name}, '%')
    </select>

    <!-- 根据关联科室模糊查询病区列表 -->
    <select id="findByGlkeshiContaining" parameterType="string" resultMap="WardResultMap">
        SELECT code, out_code, name, address, glkeshi, telephone, director, head_nurse, bed_count, bed_open_count, sort, enabled, remark
        FROM tb_hospital_ward
        WHERE glkeshi LIKE CONCAT('%', #{glkeshi}, '%')
    </select>

    <!-- 根据启用状态查询病区列表 -->
    <select id="findByEnabled" parameterType="boolean" resultMap="WardResultMap">
        SELECT code, out_code, name, address, glkeshi, telephone, director, head_nurse, bed_count, bed_open_count, sort, enabled, remark
        FROM tb_hospital_ward
        WHERE enabled = #{enabled}
    </select>

    <!-- 检查是否存在指定代码的病区（用于唯一性检查） -->
    <select id="findByCodeExcluding" resultMap="WardResultMap">
        SELECT code, out_code, name, address, glkeshi, telephone, director, head_nurse, bed_count, bed_open_count, sort, enabled, remark
        FROM tb_hospital_ward
        WHERE code = #{code} AND code != #{excludeCode}
    </select>

    <!-- 插入新病区 -->
    <insert id="insert" parameterType="org.example.entity.HospitalWard">
        INSERT INTO tb_hospital_ward(code, out_code, name, address, glkeshi, telephone, director, head_nurse, bed_count, bed_open_count, sort, enabled, remark)
        VALUES(#{code}, #{outCode}, #{name}, #{address}, #{glkeshi}, #{telephone}, #{director}, #{headNurse}, #{bedCount}, #{bedOpenCount}, #{sort}, #{enabled}, #{remark})
    </insert>

    <!-- 更新病区信息 -->
    <update id="update" parameterType="map">
        UPDATE tb_hospital_ward
        SET code = #{ward.code}
        <if test="ward.outCode != null">, out_code = #{ward.outCode}</if>
        <if test="ward.name != null">, name = #{ward.name}</if>
        <if test="ward.address != null">, address = #{ward.address}</if>
        <if test="ward.glkeshi != null">, glkeshi = #{ward.glkeshi}</if>
        <if test="ward.telephone != null">, telephone = #{ward.telephone}</if>
        <if test="ward.director != null">, director = #{ward.director}</if>
        <if test="ward.headNurse != null">, head_nurse = #{ward.headNurse}</if>
        <if test="ward.bedCount != null">, bed_count = #{ward.bedCount}</if>
        <if test="ward.bedOpenCount != null">, bed_open_count = #{ward.bedOpenCount}</if>
        <if test="ward.sort != null">, sort = #{ward.sort}</if>
        <if test="ward.enabled != null">, enabled = #{ward.enabled}</if>
        <if test="ward.remark != null">, remark = #{ward.remark}</if>
        WHERE code = #{originCode}
    </update>

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

</mapper>
