package org.example.mapper;

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

import java.util.List;

@Mapper
public interface HospitalBedMapper {
    
    /**
     * 查询所有床位
     * @return 床位列表
     */
    @Select("SELECT code, out_code AS outBed, name, bed_type AS type, belong_dept AS belongDept, belong_ward AS belongWard, belong_room AS belongRoom, doctor, nurse, used, sort, enabled, remark FROM tb_hospital_bed")
    List<HospitalBed> findAll();
    
    /**
     * 根据床位代码查询床位
     * @param code 床位代码
     * @return 床位信息
     */
    @Select("SELECT code, out_code AS outBed, name, bed_type AS type, belong_dept AS belongDept, belong_ward AS belongWard, belong_room AS belongRoom, doctor, nurse, used, sort, enabled, remark FROM tb_hospital_bed WHERE code = #{code} LIMIT 1")
    HospitalBed findByCode(String code);
    
    /**
     * 根据床位名称模糊查询床位列表
     * @param name 床位名称
     * @return 床位列表
     */
    @Select("SELECT code, out_code AS outBed, name, bed_type AS type, belong_dept AS belongDept, belong_ward AS belongWard, belong_room AS belongRoom, doctor, nurse, used, sort, enabled, remark FROM tb_hospital_bed WHERE name LIKE CONCAT('%', #{name}, '%')")
    List<HospitalBed> findByNameContaining(String name);
    
    /**
     * 根据所属科室模糊查询床位列表
     * @param belongDept 所属科室
     * @return 床位列表
     */
    @Select("SELECT code, out_code AS outBed, name, bed_type AS type, belong_dept AS belongDept, belong_ward AS belongWard, belong_room AS belongRoom, doctor, nurse, used, sort, enabled, remark FROM tb_hospital_bed WHERE belong_dept LIKE CONCAT('%', #{belongDept}, '%')")
    List<HospitalBed> findByBelongDeptContaining(String belongDept);
    
    /**
     * 根据所属病区模糊查询床位列表
     * @param belongWard 所属病区
     * @return 床位列表
     */
    @Select("SELECT code, out_code AS outBed, name, bed_type AS type, belong_dept AS belongDept, belong_ward AS belongWard, belong_room AS belongRoom, doctor, nurse, used, sort, enabled, remark FROM tb_hospital_bed WHERE belong_ward LIKE CONCAT('%', #{belongWard}, '%')")
    List<HospitalBed> findByBelongWardContaining(String belongWard);
    
    /**
     * 根据启用状态查询床位列表
     * @param enabled 是否启用
     * @return 床位列表
     */
    @Select("SELECT code, out_code AS outBed, name, bed_type AS type, belong_dept AS belongDept, belong_ward AS belongWard, belong_room AS belongRoom, doctor, nurse, used, sort, enabled, remark FROM tb_hospital_bed WHERE enabled = #{enabled}")
    List<HospitalBed> findByEnabled(Boolean enabled);
    
    /**
     * 检查是否存在指定代码的床位（用于唯一性检查）
     * @param code 床位代码
     * @param excludeCode 要排除的代码（更新时使用）
     * @return 床位列表
     */
    @Select("SELECT code, out_code AS outBed, name, bed_type AS type, belong_dept AS belongDept, belong_ward AS belongWard, belong_room AS belongRoom, doctor, nurse, used, sort, enabled, remark FROM tb_hospital_bed WHERE code = #{code} AND code != #{excludeCode}")
    List<HospitalBed> findByCodeExcluding(String code, String excludeCode);
    
    /**
     * 插入新床位
     * @param bed 床位信息
     */
    @Insert("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})")
    void insert(HospitalBed bed);
    
    /**
     * 更新床位信息
     * @param bed 床位信息
     */
    @Update({"<script>",
            "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}",
            "</script>"})
    void update(@Param("bed") HospitalBed bed, @Param("originCode") String originCode);
    
    /**
     * 根据床位代码删除床位
     * @param code 床位代码
     */
    @Delete("DELETE FROM tb_hospital_bed WHERE code = #{code}")
    void deleteByCode(String code);
}