package thyyxxk.webserver.dao.his.outpatient.triage;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
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.dictionary.CodeName;
import thyyxxk.webserver.entity.outpatient.triage.MessageForPush;
import thyyxxk.webserver.entity.outpatient.triage.MzYshTzxx;
import thyyxxk.webserver.entity.outpatient.triage.MzfzPatientOrder;
import thyyxxk.webserver.entity.outpatient.triage.MzfzZdDeptRoom;
import thyyxxk.webserver.entity.outpatient.triage.TriageNotifyRelation;

import java.util.List;
import java.util.Map;

@Mapper
public interface TriageDao {
    @Select("select describe from t_triage_user_depts where code='system'")
    String getBigScreenFooter();

    @Select("select chosen_dept from t_triage_user_depts where code=#{code}")
    String selectChosenDepts(@Param("code") String code);

    @Select("select rtrim(patient_id) from mz_patient_mi where social_no=#{idNo}")
    List<String> selectPatientIdByIdNo(@Param("idNo") String idNo);

    // 根据code获取科室信息
    @Select("<script>" +
            "select rtrim(code) as code, rtrim(name) as name, rtrim(py_code) as pyCode " +
            "from zd_unit_code where code in " +
            "<foreach collection='codes' item='code' open='(' separator=',' close=')'> " +
            "#{code}" +
            "</foreach>" +
            "</script>")
    List<CodeName> getDept(@Param("codes") String[] codes);

    // 获取未分诊的病人
    @Select("<script>" +
            "select visit_date,rtrim(patient_id) as patientId,times,rtrim(name) as name, " +
            "fz_no,slow_flag,status_flag,status_time,call_time,req_order,ampm,dept_code, " +
            "rtrim(room_code) as roomCode,doctor_code,serial_no, gh_no, req_type, " +
            "deptName=(select d.name from zd_unit_code d where d.code=a.dept_code), " +
            "roomName=(select d.room_name from mzfz_zd_dept_room d where d.room_code=a.room_code), " +
            "roomStatus=(select d.status_flag from mzfz_zd_dept_room d where d.room_code=a.room_code), " +
            "doctorName=(select rtrim(d.name) from a_employee_mi d where d.code=a.doctor_code), " +
            "reqName=(select rtrim(d.name) from mzy_zd_charge_type d where d.code=a.req_type), " +
            "reqFee=(select d.req_fee from mzy_reqrec d where d.patient_id=a.patient_id and d.times=a.times), " +
            "clinicFee=(select d.clinic_fee from mzy_reqrec d where d.patient_id=a.patient_id and d.times=a.times), " +
            "socialNo=(select rtrim(d.social_no) from mz_patient_mi d where d.patient_id=a.patient_id) " +
            "from mzfz_patient_order a with(nolock) where datediff(day, visit_date, getdate())=0 " +
            "and isnull(status_flag,'') in ('','0') and dept_code in " +
            "<foreach collection='deptCodes' item='deptCode' open='(' separator=',' close=')'>" +
            "#{deptCode}" +
            "</foreach>" +
            "</script>")
    List<MzfzPatientOrder> getUnTriagedPatients(@Param("deptCodes") String[] deptCodes);

    // 获取已分诊病人
    @Select("<script>" +
            "select visit_date,patient_id, times, name, " +
            "dept_code, dept_name=(select name from zd_unit_code where code=(select dept_code " +
            "from mzfz_zd_dept_room where room_code=a.room_code)), " +
            "room_code,room_name=(select room_name from mzfz_zd_dept_room where room_code=a.room_code), " +
            "room_status=(select status_flag from mzfz_zd_dept_room where room_code=a.room_code), " +
            "doctor_code,doctor_name=(select name from a_employee_mi where code=doctor_code), " +
            "serial_no, gh_no, req_type,reqName=(select rtrim(name) from mzy_zd_charge_type where code=a.req_type), " +
            "fz_no, slow_flag, status_flag, status_time,call_time, req_order, ampm, " +
            "socialNo=(select rtrim(d.social_no) from mz_patient_mi d where d.patient_id=a.patient_id) " +
            "from mzfz_patient_order a with(nolock) " +
            "where datediff(day, visit_date, getdate())=0 and status_flag in ('1','2','3') " +
            "and dept_code in " +
            "<foreach collection='deptCodes' item='deptCode' open='(' separator=',' close=')'>" +
            "#{deptCode}" +
            "</foreach> order by fz_no" +
            "</script>")
    List<MzfzPatientOrder> getTriagedPatients(@Param("deptCodes") String[] deptCodes);

    @Select("<script>" +
            "select visit_date=convert(varchar(19), visit_date, 21),patient_id, times, name, " +
            "dept_code, dept_name=(select name from zd_unit_code where code=(select dept_code " +
            "from mzfz_zd_dept_room where room_code=a.room_code)), " +
            "room_code,room_name=(select room_name from mzfz_zd_dept_room where room_code=a.room_code), " +
            "room_status=(select status_flag from mzfz_zd_dept_room where room_code=a.room_code), " +
            "doctor_code,doctor_name=(select name from a_employee_mi where code=doctor_code), " +
            "serial_no, gh_no, req_type,reqName=(select rtrim(name) from mzy_zd_charge_type where code=a.req_type), " +
            "fz_no, slow_flag, status_flag, status_time,call_time, req_order, ampm, " +
            "socialNo=(select rtrim(d.social_no) from mz_patient_mi d where d.patient_id=a.patient_id) " +
            "from mzfz_patient_order a with(nolock) where datediff(day, visit_date, getdate())=0 and status_flag='9' " +
            "and dept_code in " +
            "<foreach collection='deptCodes' item='deptCode' open='(' separator=',' close=')'>" +
            "#{deptCode}" +
            "</foreach> order by fz_no" +
            "</script>")
    List<MzfzPatientOrder> getTreatedPatients(@Param("deptCodes") String[] deptCodes);

    // 获取大屏幕显示数据
    @Select("<script>" +
            "select visit_date,patient_id, times, name, " +
            "dept_code, dept_name=(select name from zd_unit_code where code=(select dept_code " +
            "from mzfz_zd_dept_room where room_code=a.room_code)), " +
            "rtrim(room_code) as roomCode,room_name=(select room_name from mzfz_zd_dept_room where room_code=a.room_code), " +
            "room_no=(select room_no from mzfz_zd_dept_room where room_code=a.room_code), " +
            "room_status=(select status_flag from mzfz_zd_dept_room where room_code=a.room_code), " +
            "doctor_code,doctor_name=(select rtrim(name) from a_employee_mi where code=doctor_code), " +
            "doctor_title=(select rtrim(name) from zd_emp_title where code=(select emp_tit_code from a_employee_mi where " +
            "code=doctor_code))," +
            "phone_no=(select rtrim(isnull(phone_no,'-')) from mz_patient_mi where patient_id=a.patient_id)," +
            "serial_no, gh_no, req_type,reqName=(select rtrim(name) from mzy_zd_charge_type where code=a.req_type), " +
            "fz_no, slow_flag, status_flag, status_time,call_time, req_order, ampm " +
            "from mzfz_patient_order a with(nolock) where datediff(day, visit_date, getdate())=0 " +
            "and status_flag in ('1','2','3') " +
            "and dept_code in " +
            "<foreach collection='deptCodes' item='deptCode' open='(' separator=',' close=')'>" +
            "#{deptCode}" +
            "</foreach> order by room_code, fz_no" +
            "</script>")
    List<MzfzPatientOrder> getBigScreenData(@Param("deptCodes") String[] deptCodes);

    // 根据科室code获取诊室信息
    @Select("select a.*, b.name as doctor_name,rtrim(c.name) as reqName from mzfz_zd_dept_room a, " +
            "a_employee_mi b, mzy_zd_charge_type c where a.dept_code=#{deptCode} and " +
            "b.code = a.doctor_code and c.code=a.req_type and a.status_flag=0 ")
    List<MzfzZdDeptRoom> getRooms(@Param("deptCode") String deptCode);

    @Select("select rtrim(social_no) from mz_patient_mi where patient_id=#{patientId}")
    String getSocialNo(@Param("patientId") String patientId);

    @Select("select id from t_covid19_assessment where idcard=#{idcard} and datediff(day, date, getdate())<=3")
    Integer getCovidAssessment(@Param("idcard") String idcard);

    // 分诊
    @Update("update mzfz_patient_order set status_flag='1', call_time=getdate(), slow_flag='0', triage_staff=#{triageStaff}, " +
            "dept_code=#{deptCode}, room_code=#{roomCode}, doctor_code=#{doctorCode}, fz_no=#{patientNum} " +
            "where serial_no=#{serialNo}")
    void fenZhen(MzfzZdDeptRoom room);

    // 复诊
    @Update("update mzfz_patient_order set fz_flag=1 where serial_no=#{serialNo}")
    int fuZhen(@Param("serialNo") Integer serialNo);

    // 获取当前patient_num
    @Select("select (patient_num+1) from mzfz_zd_dept_room where dept_code=#{deptCode} and room_code=#{roomCode}")
    int getPatientNum(@Param("deptCode") String deptCode, @Param("roomCode") String roomCode);

    // 更新patient_num
    @Update("update mzfz_zd_dept_room set patient_num=#{num} where " +
            "dept_code=#{deptCode} and room_code=#{roomCode}")
    void updatePatientNum(@Param("deptCode") String deptCode,
                          @Param("roomCode") String roomCode,
                          @Param("num") int num);

    // 重置病人分诊信息为空
    @Update("update mzfz_patient_order set status_flag=0, room_code='', call_time='', " +
            "slow_flag='' where serial_no=#{serialNo}")
    int cancelTriage(@Param("serialNo") Integer serialNo);

    // 获取所有挂号病人
    @Select("<script>" +
            "select visit_date,rtrim(a.patient_id) as patient_id,a.times,rtrim(b.name) as name,dept_code, " +
            "dept_name=(select name from zd_unit_code where code=dept_code), " +
            "room_code,room_name=(select room_name from mzfz_zd_dept_room where room_code=a.room_code), " +
            "phone=rtrim(isnull(b.phone_no,'-')),rtrim(b.social_no) as socialNo, " +
            "address=rtrim(isnull(b.adress,'-')), " +
            "gender=case when b.sex='1' then N'男' when b.sex='2' then N'女' else N'未知' end, " +
            "regCount=(select count(1) from t_emergency_reg_info d where d.patient_id=a.patient_id and d.times=a.times), " +
            "room_status=(select status_flag from mzfz_zd_dept_room where room_code=a.room_code), " +
            "doctor_code,doctor_name=(select name from a_employee_mi where code=doctor_code), " +
            "serial_no,gh_no,req_type,reqName=(select rtrim(name) from mzy_zd_charge_type where code=a.req_type), " +
            "fz_no,slow_flag,status_flag,status_time,call_time,req_order,ampm, " +
            "req_fee=(select req_fee from mzy_reqrec where patient_id=a.patient_id and times=a.times), " +
            "clinic_fee=(select clinic_fee from mzy_reqrec where patient_id=a.patient_id and times=a.times) " +
            "from mzfz_patient_order a with(nolock),mz_patient_mi b with(nolock) where datediff(day,visit_date,getdate())=0 " +
            "and a.patient_id=b.patient_id " +
            "and (a.patient_id like #{content} or a.name like #{content}) " +
            "and dept_code in " +
            "<foreach collection='deptCodes' item='deptCode' open='(' separator=',' close=')'>" +
            "#{deptCode}" +
            "</foreach> " +
            "order by a.visit_date desc" +
            "</script>")
    List<MzfzPatientOrder> getAllPatients(@Param("deptCodes") String[] deptCodes, @Param("content") String content);

    @Select("select * from mz_ysh_tzxx where patient_id=#{patientId} and times=#{times}")
    MzYshTzxx getMzVitalSigns(String patientId, Integer times);

    @Update("update mz_ysh_tzxx set value_sg=#{valueSg}, value_tz=#{valueTz}, value_tw=#{valueTw}, " +
            "value_mb=#{valueMb}, value_hx=#{valueHx},value_ssy=#{valueSsy},value_szy=#{valueSzy}, " +
            "visit_remark=#{visitRemark} where patient_id=#{patientId} and times=#{times}")
    void updateMzVitalSigns(MzYshTzxx param);

    @Insert("insert into mz_ysh_tzxx (patient_id,visit_date,value_sg,value_tz,value_tw,value_mb, " +
            "value_hx,value_ssy,value_szy,visit_remark,times) values " +
            "(#{patientId},#{visitDate},#{valueSg},#{valueTz},#{valueTw},#{valueMb}, " +
            "#{valueHx},#{valueSsy},#{valueSzy},#{visitRemark},#{times})")
    void insertMzVitalSigns(MzYshTzxx param);

    @Select("select dept_code,rtrim(room_code) as room_code,serial_no,rtrim(name) as name,fz_no, " +
            "deptName=(select rtrim(name) from zd_unit_code where code=dept_code), " +
            "doctorName=(select name from a_employee_mi where code=doctor_code), " +
            "doctorTitle=(select rtrim(name) from zd_emp_title " +
            "where code=(select emp_tit_code from a_employee_mi where code=doctor_code))," +
            "roomNo=(select room_no from mzfz_zd_dept_room where " +
            "mzfz_patient_order.room_code=mzfz_zd_dept_room.room_code) " +
            "from mzfz_patient_order with(nolock) where serial_no=#{serialNo}")
    MessageForPush selectMessageForPush(@Param("serialNo") int serialNo);

    @Update("update t_triage_notify_relation set notify_datetime=getdate() " +
            "where serial_no=#{serialNo}; " +
            "update mzfz_patient_order set status_flag=(case when status_flag<3 then 3 else status_flag end) " +
            "where serial_no=#{serialNo}")
    void updateNotifyDatetime(@Param("serialNo") int serialNo);

    @Delete("delete from t_triage_notify_relation where serial_no=#{serialNo}")
    void deleteNotifyRelation(@Param("serialNo") int serialNo);

    @Insert("insert into t_triage_notify_relation(serial_no, socket_sid, room_code) values (#{serialNo}, #{sid}, #{roomCode})")
    void insertNotifyRelation(@Param("serialNo") int serialNo, @Param("sid") String sid, @Param("roomCode") String roomCode);

    @Select("select * from t_triage_notify_relation where serial_no=#{serialNo}")
    TriageNotifyRelation selectTriageNotifyRelation(@Param("serialNo") int serialNo);

    @Select("select rtrim(a.name) as name,a.visit_date," +
            "deptName=(select rtrim(d.name) from zd_unit_code d where d.code=a.dept_code) " +
            "from mzfz_patient_order a with(nolock) where a.serial_no=#{serialNo}")
    MessageForPush selectVipRegMessage(@Param("serialNo") int serialNo);

    @Select("select code from t_triage_user_depts where isnull(del_flag,0)=0 " +
            "and (chosen_dept like '%\"1060000\"%' or chosen_dept like '%\"1060010\"%')")
    List<String> selectVipTriageUser();

    @Select("select rtrim(code_rs) from a_employee_mi where dept_code='2080000' and isnull(del_flag,'0')!='1'")
    List<String> selectCodeRsList();
    @Update(" update t_wechat_patient_bind set patient_id=#{newPatientId},ic_card_no=#{icCardNo} where patient_id=#{oldPatientId} ")
    int saveNewCard(@Param("oldPatientId") String oldPatientId, @Param("newPatientId")String newPatientId,@Param("icCardNo") String icCardNo);
    @Select(" select rtrim(patient_id) patient_id,rtrim(ic_card_no) ic_card_no,rtrim(name) name from mz_patient_mi where patient_id=#{newPatientId} ")
    Map<String,Object> selectCardInfo(@Param("newPatientId") String newPatientId);
}
