package thyyxxk.webserver.dao.his.inpatient.adverseevent;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.ibatis.annotations.*;
import thyyxxk.webserver.entity.adverseevent.AdverseDept;
import thyyxxk.webserver.entity.adverseevent.HandlerInfo;
import thyyxxk.webserver.entity.adverseevent.ReportDetail;
import thyyxxk.webserver.entity.adverseevent.ReportIndex;
import thyyxxk.webserver.entity.dictionary.CodeName;

import java.util.Date;
import java.util.List;

@Mapper
public interface AdverseEventDao {
    @Select("select rtrim(code_rs) from a_employee_mi with(nolock) where code=#{code}")
    String selectCodeRsByCode(String code);

    @Insert("insert into adverse_event (t_id, category, occur_datetime, submit_datetime, " +
            "department, dept_code, user_name, user_level, work_year, course, result, nurse_reason, " +
            "nurse_reason_else, patient_reason, patient_reason_else, environ_reason, " +
            "environ_reason_else, manage_reason, manage_reason_else, pro_nurse, " +
            "pro_nurse_else, pro_patient, pro_patient_else, pro_environ, pro_environ_else, " +
            "pro_manage, pro_manage_else, user_id, dept_dealer_id, dept_dealer_name," +
            "event_result) VALUES " +
            "(#{tid}, #{category}, #{occurDatetime}, #{submitDatetime}, #{department}, #{deptCode}," +
            "#{userName}, #{userLevel}, #{workYear}, #{course}, #{result}, #{nurseReason}, " +
            "#{nurseReasonElse}, #{patientReason}, #{patientReasonElse}, #{environReason}, " +
            "#{environReasonElse}, #{manageReason}, #{manageReasonElse}, #{proNurse}, " +
            "#{proNurseElse}, #{proPatient}, #{proPatientElse}, #{proEnviron}, " +
            "#{proEnvironElse}, #{proManage}, #{proManageElse}, #{userId}," +
            "#{deptDealerId}, #{deptDealerName}, #{eventResult})")
    void submitNewReport(ReportDetail reportDetail);

    @Update("update adverse_event set category=#{category}, occur_datetime=#{occurDatetime}, " +
            "department=#{department}, dept_code=#{deptCode}, user_level=#{userLevel}, work_year=#{workYear}, " +
            "course=#{course}, result=#{result}, nurse_reason=#{nurseReason}, " +
            "nurse_reason_else=#{nurseReasonElse}, patient_reason=#{patientReason}, " +
            "patient_reason_else=#{patientReasonElse}, environ_reason=#{environReason}, " +
            "environ_reason_else=#{environReasonElse}, manage_reason=#{manageReason}, " +
            "manage_reason_else=#{manageReasonElse}, pro_nurse=#{proNurse}, " +
            "pro_nurse_else=#{proNurseElse}, pro_patient=#{proPatient}, " +
            "pro_patient_else=#{proPatientElse}, pro_environ=#{proEnviron}, " +
            "pro_environ_else=#{proEnvironElse}, pro_manage=#{proManage}, " +
            "pro_manage_else=#{proManageElse}, event_result=#{eventResult} where p_id=#{pid}")
    void modifyReport(ReportDetail reportDetail);

    @Select("select p_id,category,occur_datetime,submit_datetime,handled " +
            "from adverse_event with(nolock) where user_id=#{userId} and deleted=0 " +
            "order by occur_datetime desc")
    List<ReportIndex> getHistories(String userId);

    @Select("select * from adverse_event with(nolock) where p_id=#{pid}")
    ReportDetail getReportDetail(Integer pid);

    @Select("select dept_id as deptId from adverse_dept with(nolock) where leader_code=#{code} " +
            "union select rtrim(dept_code) as deptId from a_employee_mi where code=#{code}")
    List<String> getLeaderDepts(String code);

    @Select("<script>" +
            "select p_id,category,occur_datetime,submit_datetime,user_name,handled " +
            "from adverse_event with(nolock) where deleted=0 and dept_code in " +
            "<foreach collection='deptCodes' item='deptCode' separator=',' open='(' close=')'>" +
            "#{deptCode}" +
            "</foreach> " +
            "order by occur_datetime desc" +
            "</script>")
    List<ReportIndex> getReportsForDeptLeader(List<String> deptCodes);

    @Select("select p_id,category,occur_datetime,submit_datetime,handled," +
            "user_name,dept_code, department, dept_dealer_name, printed " +
            "from adverse_event with(nolock) where " +
            "deleted=0 and occur_datetime>=#{start} and occur_datetime<=#{end} " +
            "and dept_code like #{ward} ${statement} order by occur_datetime desc")
    IPage<ReportIndex> getAllReports(IPage<ReportIndex> iPage, Date start, Date end, 
                                     String ward, String statement);

    @Select("select rtrim(code_rs) as code, rtrim(name) as  name " +
            "from a_employee_mi with(nolock) where code=#{code}")
    CodeName selectUserCodeRsAndName(String code);

    @Update("update adverse_event set handled=1, dept_dealing=#{dealing}, " +
            "dept_dealer_id=#{userId}, dept_dealer_name=#{userName}, " +
            "dept_deal_time=#{datetime},event_level=#{eventLevel} where p_id=#{pid}")
    void updateDeptHandled(HandlerInfo handlerInfo);

    @Update("update adverse_event set final_dealing=#{dealing},final_dealer_id=#{userId}, " +
            "final_dealer_name=#{userName},final_deal_time=#{datetime}, " +
            "event_level=#{eventLevel} where p_id=#{pid}")
    void updateFinalHandled(HandlerInfo handlerInfo);

    @Select("select dept_id code, dept_name name from adverse_dept " +
            "with(nolock) group by dept_id, dept_name")
    List<CodeName> getAllDepts();

    @Update("update adverse_event set deleted=1,delete_operate_id=#{operId}, " +
            "delete_time=getdate() where p_id=#{pid}")
    void updateDeleted(Integer pid, String operId);

    @Update("update adverse_event set printed=1 where p_id=#{pid}")
    void updatePrinted(Integer pid);

    @Update("update adverse_event set feedback=#{feedback}," +
            "feedback_staff_id=#{userId},feedback_staff_name=#{userName}, " +
            "feedback_time=getdate() where p_id=#{pid}")
    void updateFeedback(HandlerInfo handlerInfo);

    @Select("select * from adverse_dept with(nolock)")
    List<AdverseDept> getAllAdverseDept();

    @Insert("insert into adverse_dept (dept_id, dept_name, leader_id, leader_name, leader_code) " +
            "values (#{deptId},#{deptName},#{leaderId},#{leaderName},#{leaderCode})")
    void insertAdverseDept(AdverseDept adverseDept);

    @Update("update adverse_dept set dept_name=#{deptName},leader_id=#{leaderId}, " +
            "leader_name=#{leaderName},leader_code=#{leaderCode} where dept_id=#{deptId}")
    void updateAdverseDept(AdverseDept adverseDept);

    @Delete("delete from adverse_dept where dept_id=#{deptId} and leader_code=#{leaderCode}")
    void deleteAdverseDept(AdverseDept adverseDept);
}
