package com.org.web.hospital.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.org.web.hospital.domain.BcHospitalRecord;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface BcHospitalRecordMapper extends BaseMapper<BcHospitalRecord> {

    @Select("<script>" +

            "select * " +
            "from bc_hospital_record " +
            "where is_delete = 0 " +

            "   <if test = 'personId != null'>" +
            "       and person_id = #{personId} " +
            "   </if>" +

            " order by create_time desc " +
            "</script>")
    List<BcHospitalRecord> queryList(BcHospitalRecord record);

    @Select("<script>" +

            "select a.*,b.name as person_name,c.name as room_name,d.name as bed_name " +
            "from bc_hospital_record a " +
            "left join bc_person_support b on a.person_id = b.id " +
            "left join bc_room c on a.room_id = c.id " +
            "left join bc_room_bed d on a.bed_id = d.id " +
            "where a.is_delete = 0 " +

            "  <if test = 'type != null'> " +
            "       <choose> " +
                        /*查询入院记录*/
            "           <when test=\"type == 1\"> " +
            "               and a.check_out_time is null " +
            "           </when> " +
            "           <when test=\"type == 2\"> " +
            "                and a.check_out_time is not null " +

            "               <if test = 'returnType != null and returnType == 2'>" +
            "                   and year(a.check_out_time) = year(curdate()) " +
            "               </if>" +

            "           </when> " +
            "       </choose> " +
            "  </if> " +

            "   <if test = 'personId != null '>" +
            "       and a.person_id = #{personId} " +
            "   </if>" +
            "   <if test = 'personName != null and personName != \"\" '>" +
            "       and b.name like concat('%', #{personName}, '%')  " +
            "   </if>" +

            "   <if test = 'roomId != null '>" +
            "       and a.room_id = #{roomId} " +
            "   </if>" +

            "   <if test = 'bedId != null '>" +
            "       and a.bed_id = #{bedId} " +
            "   </if>" +

            " order by a.create_time desc " +
            "</script>")
    List<BcHospitalRecord> checkList(BcHospitalRecord record);

    @Select("<script>" +
            "select * " +
            "from bc_hospital_record " +
            "where is_delete = 0 " +
            "   <if test = 'personId != null '>" +
            "       and person_id = #{personId} " +
            "   </if>" +
            "   <if test = 'bedId != null '>" +
            "       and bed_id = #{bedId} " +
            "   </if>" +
            "and check_out_time is null "+
            "</script>" )
    BcHospitalRecord getLastRecordByPersonId(@Param("personId") Long personId, @Param("bedId") Long bedId);

    @Update("update bc_hospital_record set is_delete = 1, delete_by = #{deleteBy}, delete_time = now() " +
            "where is_delete = 0 " +
            "and person_id = #{personId} ")
    void deleteByPersonId(@Param("personId") Long personId, @Param("deleteBy") Long deleteBy);
}
