package thyyxxk.webserver.dao.his.zhuyuanyisheng;

import org.apache.ibatis.annotations.*;
import thyyxxk.webserver.entity.criticalValue.CriticalValue;
import thyyxxk.webserver.entity.criticalValue.SimpleEmployee;
import thyyxxk.webserver.entity.dictionary.CodeName;

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

public interface PatientCriticalValuesDao {

    @Insert("insert into t_critical_message (pat_no,msg_id,msg_type,source_code,source_name," +
            "target_dept_code,target_dept_name,content,create_time,patient_type,inspection_type) " +
            "values (#{patNo},#{msgId},#{msgType},#{sourceCode},#{sourceName},#{targetDeptCode}," +
            "#{targetDeptName},#{content},#{createTime},#{patientType},#{inspectionType})")
    @Options(useGeneratedKeys = true, keyColumn = "id", keyProperty = "id")
    void insertCriticalValue(CriticalValue criticalValue);

    @Select("select isnull(zk_ward,small_dept) zkWard,refer_physician,consult_physician,dept_director " +
            "from zy_actpatient WITH(NOLOCK) where inpatient_no=#{inpatientNo}")
    Map<String,String> selectZkWardByInpatientNo(String inpatientNo);

    @Select("select rtrim(code) as code, rtrim(code_rs) as codeRs from a_employee_mi " +
            "where dept_code=#{dept} and isnull(del_flag,'0')!='1'")
    List<SimpleEmployee> selectEmployeesByDept(String dept);

    @Select("<script>" +
            "select rtrim(code) as code,rtrim(code_rs) as codeRs from a_employee_mi " +
            "where code in " +
            "<foreach collection='codeList' item='code' index='index' open='(' close=')' separator=','>" +
            "#{code}" +
            "</foreach>" +
            "</script>")
    List<SimpleEmployee> selectEmployeesByCodeList(List<String> codeList);

    @Select("select rtrim(dept) as dept from zd_dept_all where small_dept=#{dept}")
    List<String> selectWardListBySmallDept(String dept);

    @Update("update t_critical_message set state='BEEN_SENT',send_time=getdate() where id=#{id}")
    void updateSentState(int id);

    @Select("select code,name from zd_unit_code where code=" +
            "(select isnull(ward,admiss_ward) from zy_actpatient where inpatient_no=#{patNo})")
    CodeName selectTargetDept(String patNo);

    @Select("select code,name from zd_unit_code where code=#{code}")
    CodeName selectTargetDeptByCode(String code);

    @Select("select top 1 code,name from zd_unit_code where name=#{name}")
    CodeName selectTargetDeptByName(String name);

    @Select("select *, " +
            "handleStaffName=(select rtrim(d.name) from a_employee_mi d where d.code=handle_staff) " +
            "from t_critical_message where patient_type='INPATIENT' ")
    List<CriticalValue> selectAllInpatientCriticalValues();

    @Select("select *, " +
            "handleStaffName=(select rtrim(d.name) from a_employee_mi d where d.code=handle_staff) " +
            "from t_critical_message where patient_type='INPATIENT' " +
            "and send_time>=#{d1} and send_time<=#{d2}")
    List<CriticalValue> selectExportInpatientCriticalValues(String d1, String d2);

    @Select("select *, " +
            "handleStaffName=(select rtrim(d.name) from a_employee_mi d where d.code=handle_staff) " +
            "from t_critical_message where pat_no=#{patNo}")
    List<CriticalValue> selectCriticalValuesByPatNo(String patNo);

    @Select("select *, " +
            "handleStaffName=(select rtrim(d.name) from a_employee_mi d where d.code=handle_staff) " +
            "from t_critical_message where id=#{id}")
    List<CriticalValue> selectCriticalValuesById(Integer id);

    @Update("update t_critical_message set handle_time=getdate(), " +
            "handle_staff=#{staff} where id=#{id} ")
    int handleZyCriticalValue(Integer id, String staff);
}
