package thyyxxk.webserver.dao.his.zhuyuanyisheng.emr;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.mapstruct.Mapper;
import thyyxxk.webserver.entity.zhuyuanyisheng.emr.EmrAuditDetail;
import thyyxxk.webserver.entity.zhuyuanyisheng.emr.EmrProgressNote;
import thyyxxk.webserver.entity.zhuyuanyisheng.emr.EmrRuleData;
import thyyxxk.webserver.entity.zhuyuanyisheng.emrcontrolrule.QualityControlAnalysisOfMedicalRecords;

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

@Mapper
public interface EmrControlDaoV2 {

    @Select("select rtrim(inpatient_no)                                                       as inpatient_no, " +
            "       admiss_times, " +
            "       sex, " +
            "       (select [dbo].[F_GetAge]( " +
            "                       (select birth_date " +
            "                        from a_patient_mi pm " +
            "                        where pm.inpatient_no = zy.inpatient_no), " +
            "                       admiss_date))                                             as 'age', " +
            "       clinic_diag_str /*门急诊诊断*/, " +
            "       (select [dbo].F_GetDeptName(zk_ward))                                     as deptName, " +
            "       (select dbo.f_GetEmpName(refer_physician))                                as tubeBedDoctorName, " +
            "       admiss_date, " +
            "       rtrim(zy.name)                                                            as name, " +
            "       dis_date, " +
            "       (select name " +
            "        from t_case_frontsheet_disdiag " +
            "        where bah = inpatient_no " +
            "          and times = admiss_times " +
            "          and no = 1)                                                            as dis_diag, " +
            "       (select [dbo].F_GetDeptName(admiss_ward))                                 as admiss_dept_name, " +
            "       (select dbo.f_GetEmpName(consult_physician))                              as consult_physician_name, " +
            "       (select dbo.f_GetEmpName(dept_director))                                  as dept_director_name, " +
            "       (select [dbo].[f_get_zy_date](inpatient_no, admiss_times))                as admissDay, " +
            "       total_charge, " +
            "       (select dbo.F_GetDeptName(dis_dept))                                      as disDeptName, " +
            "       (select count(1) " +
            "        from view_yz_all_order v " +
            "        where v.inpatient_no = zy.inpatient_no " +
            "          and v.admiss_times = zy.admiss_times " +
            "          and v.status_flag = '5' " +
            "          and order_code in " +
            "              (select order_code " +
            "               from yz_zd_order_item_confirm " +
            "               where yz_zd_order_item_confirm.item_name = N'死亡')) " +
            "                                                                                 as death, " +
            "       (select name from zd_statutory_epidemic where code = ba.fdcrb)            as fdcrb, " +
            "       pathology_diag_comment                                                    as pathologicDiagStr, " +
            "       surgery.code                                                              as ssbm, " +
            "       surgery.name                                                              as ssmc, " +
            "       operator_name                                                             as operator_name, " +
            "       cut,heal,level, " +
            "       (select name from zd_anaesthesia where anaesthesia = zd_anaesthesia.code) as anaesthesia_name, " +
            "       ba.qjcs                                                                   as qjcs, " +
            "       ba.gmwy                                                                   as allergicMedicine, " +
            "       ba.kss                                                                    as kss /*1-是 2-否*/, " +
            "       ba.xjpy                                                                   as xjpy " +
            "from ${tableName} zy " +
            "         left join t_case_frontsheet_surgery surgery " +
            "                   on (zy.inpatient_no = surgery.bah and zy.admiss_times = surgery.times and no = 1) " +
            "         left join batj_ba2 ba on (ba.zyh = zy.inpatient_no and ba.zycs = zy.admiss_times)" +
            " ${ew.customSqlSegment} ")
    List<QualityControlAnalysisOfMedicalRecords> queryEmrRulePatient(@Param(Constants.WRAPPER) QueryWrapper<?> queryWrapper,
                                                                     @Param("tableName") String tableName);


    /**
     * 查询指定质控患者数据
     *
     * @param start        开始时间
     * @param end          结束时间
     * @param finalControl 0 - 环节质控 1-终末质控
     * @param sql          额外的sql
     * @return 返回患者数据
     */
    @Select("select rtrim(inpatient_no)                                                       as inpatient_no, " +
            "       admiss_times, " +
            "       rtrim(inpatient_no)                                                       as pat_no, " +
            "       admiss_times as times," +
            "       sex, " +
            "       (select [dbo].[F_GetAge]( " +
            "                       (select birth_date " +
            "                        from a_patient_mi pm " +
            "                        where pm.inpatient_no = zy.inpatient_no), " +
            "                       admiss_date))                                             as 'age', " +
            "       clinic_diag_str /*门急诊诊断*/, " +
            "       (select [dbo].F_GetDeptName(zk_ward))                                     as deptName, " +
            "       (select dbo.f_GetEmpName(refer_physician))                                as tubeBedDoctorName, " +
            "       admiss_date, " +
            "       rtrim(zy.name)                                                            as name, " +
            "       dis_date, " +
            "       (select name " +
            "        from t_case_frontsheet_disdiag " +
            "        where bah = inpatient_no " +
            "          and times = admiss_times " +
            "          and no = 1)                                                            as dis_diag, " +
            "       (select [dbo].F_GetDeptName(admiss_ward))                                 as admiss_dept_name, " +
            "       (select dbo.f_GetEmpName(consult_physician))                              as consult_physician_name, " +
            "       (select dbo.f_GetEmpName(dept_director))                                  as dept_director_name, " +
            "       (select [dbo].[f_get_zy_date](inpatient_no, admiss_times))                as admissDay, " +
            "       total_charge, " +
            "       (select dbo.F_GetDeptName(dis_dept))                                      as disDeptName, " +
            "       (select count(1) " +
            "        from view_yz_all_order v " +
            "        where v.inpatient_no = zy.inpatient_no " +
            "          and v.admiss_times = zy.admiss_times " +
            "          and v.status_flag = '5' " +
            "          and order_code in " +
            "              (select order_code " +
            "               from yz_zd_order_item_confirm " +
            "               where yz_zd_order_item_confirm.item_name = N'死亡')) " +
            "                                                                                 as death, " +
            "       (select name from zd_statutory_epidemic where code = ba.fdcrb)            as fdcrb, " +
            "       pathology_diag_comment                                                    as pathologicDiagStr, " +
            "       surgery.code                                                              as ssbm, " +
            "       surgery.name                                                              as ssmc, " +
            "       operator_name                                                             as operator_name, " +
            "       cut, " +
            "       heal, " +
            "       level, " +
            "       (select name from zd_anaesthesia where anaesthesia = zd_anaesthesia.code) as anaesthesia_name, " +
            "       ba.qjcs                                                                   as qjcs, " +
            "       ba.gmwy                                                                   as allergicMedicine, " +
            "       ba.kss                                                                    as kss /*1-是 2-否*/, " +
            "       ba.xjpy                                                                   as xjpy " +
            "from (select * " +
            "      from (select pat_no, times " +
            "            from emr_audit_detail " +
            "            where review_time >= #{start} " +
            "              and review_time <= #{end}" +
            "              and final_control = #{finalControl} " +
            "              and history = 0  " +
            "            group by pat_no, times) temp, " +
            "           view_zy_patient_all b " +
            "      where temp.pat_no = b.inpatient_no " +
            "        and temp.times = b.admiss_times) zy " +
            "         left join t_case_frontsheet_surgery surgery " +
            "                   on (zy.inpatient_no = surgery.bah and zy.admiss_times = surgery.times and no = 1) " +
            "         left join batj_ba2 ba on (ba.zyh = zy.inpatient_no and ba.zycs = zy.admiss_times) ${sql} ")
    List<QualityControlAnalysisOfMedicalRecords> selectSuggestionByTimeAndFinalControl(String start, String end, Integer finalControl, String sql);

    @Select("select a.name, remark, number_of_defects, b.emr_name,scoring_criteria, " +
            "       a.pat_no, " +
            "       a.times " +
            "from emr_audit_detail a " +
            "         left join emr_patient_data b on (a.emr_id = b.id) " +
            "where doctor = #{userCode} " +
            "  and a.history = 0 " +
            "  and remediation_status is null")
    List<EmrAuditDetail> selectedAudit(String userCode);

    @Select("select id, rule_data, type from emr_rule ")
    List<EmrRuleData> selectEmrRule();

    @Select("${sql}")
    Date executeSqlDate(String sql);

    @Select("${sql}")
    List<Date> executeSqlDateList(String sql);

    @Select("select id, " +
            "       document_id, " +
            "       name, " +
            "       create_id, " +
            "       creation_time, " +
            "       refer_physician, " +
            "       consult_physician, " +
            "       dept_director " +
            "from emr_progress_note " +
            "where document_id = (select top 1 id " +
            "                     from emr_patient_data " +
            "                     where pat_no = #{patNo} " +
            "                       and times = #{times} " +
            "                       and del_flag = 0 " +
            "                       and emr_category_code = 'shoucibingchengjilu')" +
            " order by creation_time ")
    List<EmrProgressNote> fragmentInterval(String patNo, Integer times);


    @Select("select top 1 order_time " +
            "from yz_act_order " +
            "where inpatient_no = #{patNo} " +
            "  and admiss_times = #{times} " +
            "  and status_flag in ('3', '4') " +
            "  and order_code in (select order_code from yz_zd_order_item_confirm where item_name = N'${name}')")
    Date whetherThereIsASeriousIllnessDoctorSOrder(String patNo, Integer times, String name);

    @Select("select top 1 emr_document_id " +
            "from emr_patient_data " +
            "where del_flag = 0 " +
            "  and pat_no = #{patNo} " +
            "  and times = #{times} " +
            "  and emr_category_code = 'shoucibingchengjilu' " +
            "order by create_date desc")
    String selectedCourseId(String patNo,
                            Integer times);

    @Select("select count(1) " +
            "from ysh_yj_req " +
            "where  " +
            "  inpatient_no = #{patNo} " +
            "  and admiss_times = #{times}" +
            "  and req_type = 3 " +
            "  and order_type = '08'  ")
    int checkTheResultsCount(String patNo, Integer times);


    @Select("select count(1) " +
            "from ysh_yj_req " +
            "where  " +
            "  inpatient_no = #{patNo} " +
            "  and admiss_times = #{times}" +
            "  and req_type = 3 " +
            "  and order_type = '26'  ")
    int magneticResonanceCount(String patNo, Integer times);

    @Select("select count(1) " +
            "from ysh_yj_req " +
            "where inpatient_no = #{patNo} " +
            "  and admiss_times = #{times} " +
            "  and req_type = 2 " +
            "  and order_type = '038'")
    int numberOfPathologies(String patNo, Integer times);

    @Select("select count(1) " +
            "from ysh_yj_req " +
            "where inpatient_no = #{patNo} " +
            "  and admiss_times = #{times} " +
            "  and req_type = 2 " +
            "  and order_type = '011'")
    int bacterialCultureCount(String patNo, Integer times);

    @Select("<script>" +
            "select id,  " +
            "       pat_no,  " +
            "       times,  " +
            "       approver,  " +
            "       review_time,  " +
            "       name,  " +
            "       scoring_criteria,  " +
            "       remark,  " +
            "       remediation_status,  " +
            "       number_of_defects,  " +
            "       modify_person,  " +
            "       modification_time,  " +
            "       emr_id,  " +
            "       history,  " +
            "       doctor," +
            "       emr_name = (select name from emr_patient_data where emr_audit_detail.emr_id = emr_patient_data.id)," +
            "       final_control  " +
            "from emr_audit_detail  " +
            "where pat_no = #{patNo}  " +
            "  and times = #{times}" +
            "  and history = 0" +
            "  <if test=\"finalControl != null\"> " +
            "  and final_control = #{finalControl}" +
            "  </if>" +
            "</script> ")
    List<EmrAuditDetail> patientQualityControl(String patNo, Integer times, Integer finalControl);

    @Select("select      f_dept_code as fCode, " +
            "    fDept = (select [dbo].F_GetDeptName(f_dept_code)), " +
            "            dept_code   as code, " +
            "    dept  = (select dbo.F_GetDeptName(dept_code)) " +
            "from zy_zk_list " +
            "where inpatient_no = #{patNo}  " +
            "  and admiss_times = #{times} ")
    List<JSONObject> transfer(String patNo, Integer times);


    @Select("select data_element from emr_data_element where pat_no = #{patNo} and times = #{times}")
    JSONObject objects(String patNo, Integer times);


    @Select("select a.id, " +
            "       a.pat_no, " +
            "       b.del_flag, " +
            "       a.times, " +
            "       approver, " +
            "       approver_name = (select rtrim(name) " +
            "                        from a_employee_mi " +
            "                        where code = approver), " +
            "       a.review_time, " +
            "       a.name, " +
            "       scoring_criteria, " +
            "       remark, " +
            "       remediation_status, " +
            "       number_of_defects, " +
            "       modify_person, " +
            "       modification_time, " +
            "       emr_id, " +
            "       history, " +
            "       doctor, " +
            "       final_control, " +
            "       pat_name      = (select rtrim(name) " +
            "                        from a_patient_mi " +
            "                        where a.pat_no = inpatient_no), " +
            "       emr_name      = (select emr_name " +
            "                        from emr_patient_data " +
            "                        where emr_id = emr_patient_data.id) " +
            "from emr_audit_detail a, " +
            "     emr_patient_data b " +
            "where a.emr_id = b.id " +
            "  and history = 0 " +
            "  and b.del_flag = 0 " +
            "  and doctor = #{doctor} " +
            "  and remediation_status is null and number_of_defects > 0 ;")
    List<EmrAuditDetail> getUnprocessedRecommendationsByDoctor(String doctor);

}
