package org.example.mapper;

import org.apache.ibatis.annotations.*;
import org.example.entity.HospitalRoom;

import java.util.List;

@Mapper
public interface HospitalRoomMapper {
    
    /**
     * 查询所有病房
     * @return 病房列表
     */
    @Select("SELECT code, out_code AS outCode, name, bed_count AS bedCount, belong_dept AS belongDept, belong_ward AS belongWard, sort, enabled, remark FROM tb_hospital_room")
    List<HospitalRoom> findAll();
    
    /**
     * 根据条件查询病房
     * @param code 病房代码
     * @param department 所属科室
     * @param ward 所属病区
     * @param enabled 是否启用
     * @return 病房列表
     */
    @Select({
        "<script>",
        "SELECT code, out_code AS outCode, name, bed_count AS bedCount, belong_dept AS belongDept, belong_ward AS belongWard, 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>",
        "</script>"
    })
    List<HospitalRoom> findByConditions(@Param("code") String code, 
                                      @Param("department") String department, 
                                      @Param("ward") String ward, 
                                      @Param("enabled") Boolean enabled);
    
    /**
     * 根据病房代码查询病房
     * @param code 病房代码
     * @return 病房信息
     */
    @Select("SELECT code, out_code AS outCode, name, bed_count AS bedCount, belong_dept AS belongDept, belong_ward AS belongWard, sort, enabled, remark FROM tb_hospital_room WHERE code = #{code} LIMIT 1")
    HospitalRoom findByCode(String code);
    
    /**
     * 根据病房代码和所属病区查询病房数量
     * @param code 病房代码
     * @param belongWard 所属病区
     * @return 符合条件的病房数量
     */
    @Select("SELECT COUNT(*) FROM tb_hospital_room WHERE code = #{code} AND belong_ward = #{belongWard}")
    int countByCodeAndWard(@Param("code") String code, @Param("belongWard") String belongWard);
    
    /**
     * 根据病房代码和所属病区查询病房数量（排除指定病房）
     * @param code 病房代码
     * @param belongWard 所属病区
     * @param excludeCode 要排除的病房代码
     * @return 符合条件的病房数量
     */
    @Select("SELECT COUNT(*) FROM tb_hospital_room WHERE code = #{code} AND belong_ward = #{belongWard} AND code != #{excludeCode}")
    int countByCodeAndWardExcluding(@Param("code") String code, @Param("belongWard") String belongWard, @Param("excludeCode") String excludeCode);
    
    /**
     * 根据病房代码删除病房
     * @param code 病房代码
     */
    @Delete("DELETE FROM tb_hospital_room WHERE code = #{code}")
    void deleteByCode(String code);
    
    /**
     * 插入新病房
     * @param room 病房信息
     */
    @Insert("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})")
    void insert(HospitalRoom room);
    
    /**
     * 更新病房信息
     * @param room 病房信息
     */
    @Update({"<script>",
            "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}",
            "</script>"})
    void update(@Param("room") HospitalRoom room, @Param("originCode") String originCode);
}