package org.example.mapper;

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

import java.util.List;

@Mapper
public interface HospitalWardMapper {
    
    /**
     * 查询所有病区
     * @return 病区列表
     */
    @Select("SELECT code, out_code AS outCode, name, address, glkeshi, telephone, director, head_nurse AS headNurse, bed_count AS bedCount, bed_open_count AS bedOpenCount, sort, enabled, remark FROM tb_hospital_ward")
    List<HospitalWard> findAll();
    
    /**
     * 根据病区代码查询病区
     * @param code 病区代码
     * @return 病区信息
     */
    @Select("SELECT code, out_code AS outCode, name, address, glkeshi, telephone, director, head_nurse AS headNurse, bed_count AS bedCount, bed_open_count AS bedOpenCount, sort, enabled, remark FROM tb_hospital_ward WHERE code = #{code} LIMIT 1")
    HospitalWard findByCode(String code);
    
    /**
     * 根据病区名称模糊查询病区列表
     * @param name 病区名称
     * @return 病区列表
     */
    @Select("SELECT code, out_code AS outCode, name, address, glkeshi, telephone, director, head_nurse AS headNurse, bed_count AS bedCount, bed_open_count AS bedOpenCount, sort, enabled, remark FROM tb_hospital_ward WHERE name LIKE CONCAT('%', #{name}, '%')")
    List<HospitalWard> findByNameContaining(String name);
    
    /**
     * 根据关联科室模糊查询病区列表
     * @param glkeshi 关联科室
     * @return 病区列表
     */
    @Select("SELECT code, out_code AS outCode, name, address, glkeshi, telephone, director, head_nurse AS headNurse, bed_count AS bedCount, bed_open_count AS bedOpenCount, sort, enabled, remark FROM tb_hospital_ward WHERE glkeshi LIKE CONCAT('%', #{glkeshi}, '%')")
    List<HospitalWard> findByGlkeshiContaining(String glkeshi);
    
    /**
     * 根据启用状态查询病区列表
     * @param enabled 是否启用
     * @return 病区列表
     */
    @Select("SELECT code, out_code AS outCode, name, address, glkeshi, telephone, director, head_nurse AS headNurse, bed_count AS bedCount, bed_open_count AS bedOpenCount, sort, enabled, remark FROM tb_hospital_ward WHERE enabled = #{enabled}")
    List<HospitalWard> findByEnabled(Boolean enabled);
    
    /**
     * 检查是否存在指定代码的病区（用于唯一性检查）
     * @param code 病区代码
     * @param excludeCode 要排除的代码（更新时使用）
     * @return 病区列表
     */
    @Select("SELECT code, out_code AS outCode, name, address, glkeshi, telephone, director, head_nurse AS headNurse, bed_count AS bedCount, bed_open_count AS bedOpenCount, sort, enabled, remark FROM tb_hospital_ward WHERE code = #{code} AND code != #{excludeCode}")
    List<HospitalWard> findByCodeExcluding(String code, String excludeCode);
    
    /**
     * 插入新病区
     * @param ward 病区信息
     */
    @Insert("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})")
    void insert(HospitalWard ward);
    
    /**
     * 更新病区信息
     * @param ward 病区信息
     */
    @Update({"<script>",
            "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}",
            "</script>"})
    void update(@Param("ward") HospitalWard ward, @Param("originCode") String originCode);
    
    /**
     * 根据病区代码删除病区
     * @param code 病区代码
     */
    @Delete("DELETE FROM tb_hospital_ward WHERE code = #{code}")
    void deleteByCode(String code);
}