package thyyxxk.webserver.dao.his.medicaladvice.patientinfo;

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 thyyxxk.webserver.entity.inpatient.ZyActpatient;
import thyyxxk.webserver.entity.medicaladvice.medicamanage.MzZyReq;
import thyyxxk.webserver.entity.medicaladvice.medicamanage.patientinfo.ZyBedMi;

import java.util.List;

/**
 * @Description:
 * @Author:lihong
 * @Date: 2023/5/31
 */
@Mapper
public interface AdjustBedDao {
    @Select("  select code from a_employee_mi where  dept_code = #{ward} and  code in ( " +
            "    select user_code from dj_user_role where role_id in (4,12,18)" +
            "    ) ")
    List<String> getAdmins(String ward);
    @Update(" update mz_zy_req set  bed_no = #{bedNo}, req_status =#{reqStatus},pre_bed_confirm_id=#{preBedConfirmId},pre_bed_confirm_date=#{preBedConfirmDate},pre_bed_confirm_remark=#{preBedConfirmRemark} where patient_id=#{patientId} and CONVERT(VARCHAR(30),visit_date,20) =#{visitDateStr} ")
    void updateBedNo(MzZyReq mzZyReq);
    @Select("  select top 1 1 from zy_bed_mi where bed_no = #{bedNo} and ward_code = #{wardCode} and bed_status ='1' ")
    Integer isIdleBedNo(@Param("bedNo") String bedNo,@Param("wardCode") String wardCode);
    @Select(" select  bed_no from zy_bed_mi where bed_status ='1' and ward_code= #{ward} order by cast(bed_no as int) ")
    List<String> selectIdleBedNoList(String ward);
    @Select(" SELECT zy_bed_mi.room_no,\n" +
            "       zy_bed_mi.bed_no,\n" +
            "       inpatient_no=isnull(zy_actpatient.inpatient_no,zy_bed_mi.inpatient_no),\n" +
            "       zy_actpatient.name,\n" +
            "       sex  =case when isnull(zy_actpatient.inpatient_no,'') <> '' then isnull(zy_actpatient.sex,'1') else '0' end,\n" +
            "       admiss_date=isnull(zy_actpatient.admiss_hour,zy_actpatient.admiss_date),\n" +
            "       zy_bed_mi.ward_code,\n" +
            "       zy_bed_mi.dept_code,\n" +
            "       zy_bed_mi.bed_status,\n" +
            "       admiss_times=isnull(zy_actpatient.admiss_times,zy_bed_mi.admiss_times),\n" +
            "       zy_bed_mi.remark  ,\n" +
            "       zy_actpatient.refer_physician,\n" +
            "       refer_physician_name =(select name from a_employee_mi where zy_actpatient.refer_physician= code),\n" +
            "       zy_actpatient.responce_type,\n" +
            "       responce_type_name = (select name from  zy_zd_responce_type where zy_actpatient.responce_type=code),\n" +
            "       zy_actpatient.admiss_diag_str,\n" +
            "       (select name from a_employee_mi where code = zy_actpatient.refer_physician) as zyDoctorName,\n" +
            "       (select name from zd_unit_code where zy_actpatient.small_dept = code) as smallDeptName,\n" +
            "       (select birth_date from a_patient_mi where inpatient_no = zy_actpatient.inpatient_no) as birthDate,\n" +
            "       (select SYSDATETIME()) AS currentDate,\n" +
            "       zy_actpatient.balance\n" +
            " from zy_bed_mi left join  zy_actpatient  on  zy_bed_mi.inpatient_no = zy_actpatient.inpatient_no\n" +
            "   where   zy_bed_mi.ward_code = #{wardCode} order by cast(zy_bed_mi.bed_no as int) ")
    List<ZyBedMi> selectZybedMi(ZyBedMi param);
    @Select(" select top 1 * from   mz_zy_req   where patient_id=#{patientId} and CONVERT(VARCHAR(30),visit_date,20) =#{visitDateStr} ")
    MzZyReq selectMzZyReq(MzZyReq mzZyReq);
    @Select(" select top 1 1 from zy_actpatient where bed_no =#{bedNo} and dept =#{wardCode}")
    Integer isExistPatient(ZyBedMi param);
    @Update(" update zy_bed_mi set inpatient_no =#{inpatientNo} , admiss_times =#{admissTimes} , bed_status =#{bedStatus} where bed_no =#{bedNo} and dept_code =#{wardCode}")
    void updatePatientByBed(ZyBedMi param);
    @Select("  select top 1 req_status from mz_zy_req where patient_id=#{patientId} and CONVERT(VARCHAR(30),visit_date,20) =#{visitDateStr} ")
    String selectReqStatus(MzZyReq req);
    @Select(" select top 1 inpatient_no , admiss_times from zy_actpatient where bed_no =#{bedNo} and dept =#{wardCode} ")
    ZyBedMi selectActInpatientByBed(ZyBedMi param);
    @Update(" update zy_actpatient SET bed_no =#{bedNo} , bed_status =#{bedStatus} , ward =#{wardCode} WHERE ( inpatient_no =#{inpatientNo} or inpatient_no like ( #{inpatientNo} + '$%' ) ) AND admiss_times =#{admissTimes};")
    void updateActpatientBed(ZyBedMi zyBedMi);
    @Update(" update yz_yp_zy_order SET bed_no =#{bedNo} WHERE ( inpatient_no =#{inpatientNo} or inpatient_no =( #{inpatientNo} + '$1' ) or inpatient_no =( #{inpatientNo} + '$2' ) or inpatient_no =( #{inpatientNo} + '$3' ) or inpatient_no =( #{inpatientNo} + '$4' ) or inpatient_no =( #{inpatientNo} + '$5' ) or inpatient_no =( #{inpatientNo} + '$6' ) ) AND admiss_times =#{admissTimes}")
    void updateYpZyOrder(ZyBedMi zyBedMi);
    @Update(" update yz_act_occ SET bed_no =#{bedNo} WHERE ( inpatient_no =#{inpatientNo} or inpatient_no =( #{inpatientNo} + '$1' ) or inpatient_no =( #{inpatientNo} + '$2' ) or inpatient_no =( #{inpatientNo} + '$3' ) or inpatient_no =( #{inpatientNo} + '$4' ) or inpatient_no =( #{inpatientNo} + '$5' ) or inpatient_no =( #{inpatientNo} + '$6' ) ) AND admiss_times =#{admissTimes}")
    void updateYzActOcc(ZyBedMi zyBedMi);
    @Select(" select top 1 1 from mz_zy_req where req_ward=#{wardCode} and visit_date >=#{startVisitDate} and visit_date<=#{endVisitDate} and bed_no=#{bedNo}")
    Integer queryIsPreBed(ZyBedMi param);
    @Select(" select top 1 rtrim(name) name  from mz_patient_mi where patient_id =#{patientId}  ")
    String selectPatientMi(@Param("patientId") String patientId);

    @Select("select rtrim(inpatient_no) as inpatientNo, " +
            "admiss_times,rtrim(name) as name,admiss_date,rtrim(bed_no) as bedNo, " +
            "gender=case when sex='1' then N'男' when sex='2' then N'女' else N'未知' end, " +
            "wardName=(select rtrim(d.name) from zd_unit_code d where d.code=ward), " +
            "smallDeptName=(select rtrim(d.name) from zd_unit_code d where d.code=small_dept) " +
            "from zy_actpatient where ward=#{ward} and bed_status='0' ")
    List<ZyActpatient> getPreAdmPatients(String ward);

    @Update("update zy_actpatient set bed_status='1',receive_time=getdate(),phry=#{phry} " +
            "where inpatient_no=#{patNo}")
    void updateBedStatus(String patNo, String phry);
}
