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


import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.*;
import org.apache.poi.ss.formula.functions.Count;
import thyyxxk.webserver.constants.sidicts.ListType;
import thyyxxk.webserver.entity.fluorescenceTest.FluorescenceSpecimenResult;
import thyyxxk.webserver.entity.zhuyuanyisheng.emr.EmrLimitUnlock;
import thyyxxk.webserver.entity.zhuyuanyisheng.emr.EmrProgressNote;
import thyyxxk.webserver.entity.zhuyuanyisheng.emrcontrolrule.*;

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

public interface EmrControlDao {

    @Select("select count(1) " +
            "from emr_control_temp_code a, " +
            "     emr_control_rule b " +
            "where a.rule_id = b.id " +
            "  and code = #{code} " +
            "  and b.type = #{type} ")
    int getEmrRuleCount(String code, int type);

    @Insert("insert into emr_control_temp_code (code, rule_id) values (#{code},#{id})")
    void insertControlRule(@Param("id") String id, @Param("code") String code);


    @Insert("insert into emr_control_rule (id, type, off_or_on_rule,prompt_statement,name,sql,prescription,superior_doctor_time) " +
            "values (#{id},#{param.type},#{param.offOrOnRule},#{param.promptStatement},#{param.name}," +
            "#{param.sql},#{param.prescription},#{param.superiorDoctorTime})")
    void insertRule(@Param("id") String id, @Param("param") RuleAdd param);

    @Select("select name, id, group_name,sql_sentence " +
            "from emr_data_maintenance " +
            "where data_type = 3")
    List<Map<String, Object>> getAvailableObjects();

    @Select("select sql_sentence " +
            " from emr_data_maintenance " +
            " where id = 7")
    String selectedSpecifyTheTimeOfAdmissionSql();

    @Select("select rtrim(inpatient_no)                                                  as inpatient_no, " +
            "       admiss_times, " +
            "       (select rtrim(name) from zd_unit_code where code = zk_ward)          as deptName, " +
            "       (select rtrim(name) from a_employee_mi where code = refer_physician) as tubeBedDoctorName, " +
            "       admiss_date," +
            "       rtrim(name) as name " +
            "from zy_actpatient " +
            "where ${condition} ")
    List<QualityControlAnalysisOfMedicalRecords> selectedPatientsAdmittedAtTheSpecifiedTime(String condition);


    @Select("select id, " +
            "       type, " +
            "       off_or_on_rule, " +
            "       prompt_statement, " +
            "       name, " +
            "       sql, " +
            "       prescription," +
            "       superior_doctor_time " +
            "from emr_control_rule ")
    List<EmrRule> selectRules();

    @Select("select id, " +
            "       type, " +
            "       off_or_on_rule, " +
            "       prompt_statement, " +
            "       name, " +
            "       sql, " +
            "       prescription " +
            "from emr_control_rule " +
            "where off_or_on_rule = 1")
    List<EmrRule> selectRulesOpen();

    @Select("select code " +
            "from emr_control_temp_code " +
            "where rule_id = #{id} ")
    List<String> selectedEmrCode(String id);

    @Select("${sql}")
    Map<String, Object> executeSql(String sql);

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

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

    /**
     * 医嘱下达后是否创建了指定的片段
     *
     * @return
     */
    @Select("select count(1) from emr_progress_note where document_id = #{id} and creation_time > #{start} and creation_time <= #{end} ")
    Integer theDoctorOrdersWhetherToCreateFragments(Integer id, Date start, Date end);

    @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("<script>" +
            "select count(1) " +
            "from emr_patient_data " +
            "where pat_no = #{patNo} " +
            "  and times = #{times} " +
            "  and create_date &gt;= #{date} " +
            "  and emr_category_code in " +
            "<foreach collection='codeList' item='item'  open='(' close=')' separator=','>" +
            "#{item}" +
            "</foreach>" +
            "</script>")
    int determineWhetherTheDataWasCreated(String patNo,
                                          Integer times,
                                          Date date,
                                          List<String> codeList);

    @Select("select rtrim(code_rs) " +
            "from dj_user_role a, " +
            "     a_employee_mi b " +
            "where role_id = 38 " +
            "  and a.user_code = b.code " +
            "  and isnull(b.del_flag, 0) = 0")
    List<String> peopleWhoGetMedicalDepartment();

    @Update("update emr_control_rule set off_or_on_rule = #{offOn} where id = #{id} ")
    int switchQualityControl(String id, Integer offOn);

    @Delete("delete emr_control_rule where id = #{id};" +
            "delete emr_control_temp_code where rule_id = #{id} ")
    void deleteRuleById(String id);

    @Select("select code from emr_control_temp_code where rule_id = #{id}")
    List<String> selectEmrCodeByRuleId(String id);

    @Update("update emr_data_maintenance " +
            "set sql_sentence = #{date} " +
            "where id = 7")
    void setTheTime(String date);


    @Select("select sql_sentence " +
            "from emr_data_maintenance where id = 7 ")
    String getAdmissDate();


    @Select("select id, " +
            "       type, " +
            "       off_or_on_rule, " +
            "       prompt_statement, " +
            "       name, " +
            "       sql, " +
            "       prescription, " +
            "       superior_doctor_time, " +
            "       code, " +
            "       rule_id " +
            "from emr_control_rule a, " +
            "     emr_control_temp_code b " +
            "where a.id = b.rule_id " +
            "  and b.code = #{code}" +
            "  and off_or_on_rule = 1 ")
    List<EmrRule> selectRuleByEmrCode(String code);

    @Insert("insert into emr_rule_unlock (pat_no, times, emr_id, code, " +
            "date,input_id,input_remark,emr_name) " +
            " values " +
            " (#{patNo},#{times},#{id},#{code},#{date},#{inputId},#{remarks},#{name})")
    void insertRemoveRestrictions(RemoveRestrictions data);

    @Select("select top 1 id, " +
            "       pat_no, " +
            "       times, " +
            "       emr_id, " +
            "       code, " +
            "       date, " +
            "       input_id, " +
            "       input_remark, " +
            "       reviewed_by, " +
            "       review_notes, " +
            "       flag " +
            "from emr_rule_unlock " +
            "where pat_no = #{patNo} " +
            "  and times = #{times} " +
            "  and ${sql} " +
            "  and date >= convert(varchar(10), getdate(), 120)" +
            "  and flag = 1" +
            "  and input_id = #{inputId} " +
            "order by date desc ")
    EmrRuleUnlock selectRemoveRestrictions(String patNo,
                                           Integer times,
                                           String sql,
                                           String inputId);


    @Select("select id, " +
            "       pat_no, " +
            "       times, " +
            "       emr_id, " +
            "       code, " +
            "       date, " +
            "       input_id, " +
            "       input_name    = (select rtrim(name) from a_employee_mi where input_id = code), " +
            "       input_remark, " +
            "       reviewed_by, " +
            "       reviewed_name = (select rtrim(name) from a_employee_mi where reviewed_by = code), " +
            "       review_notes, " +
            "       flag," +
            "       emr_name," +
            "       input_date," +
            "       review_time " +
            "from emr_rule_unlock ${ew.customSqlSegment} ")
    List<EmrRuleUnlock> selectRuleUnlock(@Param(Constants.WRAPPER) QueryWrapper<?> queryWrapper);

    @Select("select count(1) " +
            "from emr_rule_unlock " +
            "where id = #{id} " +
            "  and flag = 0 ")
    int selectRuleUnlockById(Integer id);

    @Update(" update emr_rule_unlock " +
            " set " +
            " flag = #{flag}," +
            " reviewed_by = #{reviewedBy}," +
            " review_notes = #{reviewNotes}," +
            " review_time = getdate() " +
            " where id = #{id}")
    void updateUnlockById(EmrRuleUnlock param);

    @Update("update emr_limit_unlock " +
            "set state = 3 " +
            "where pat_no = #{param.patNo} " +
            "  and times = #{param.times} " +
            "  and applicant = #{param.applicant} " +
            "  and effective_time >= getdate()")
    int updateUnlockOld(@Param("param") EmrLimitUnlock param);

    @Insert("insert into emr_limit_unlock (pat_no, times, unlock_json, applicant, application_time, request_remarks, " +
            "                              effective_time, state) " +
            "values (#{param.patNo}, #{param.times},#{param.unlockJson},#{param.applicant},getdate(), #{param.requestRemarks}," +
            "#{param.effectiveTime},0)")
    int applicationUnlock(@Param("param") EmrLimitUnlock param);

    @Select("select id, " +
            "       pat_no, " +
            "       times, " +
            "       unlock_json, " +
            "       applicant," +
            "applicantName =    (select rtrim(name) from a_employee_mi where code = applicant), " +
            "       application_time, " +
            "       request_remarks, " +
            "       approve," +
            "approveName   =    (select rtrim(name) from a_employee_mi where code = approve), " +
            "       review_time, " +
            "       review_notes, " +
            "       effective_time, " +
            "       state," +
            "       dept_name     = (select name\n" +
            "                        from zd_unit_code\n" +
            "                        where code = (select dept_code\n" +
            "                                      from a_employee_mi\n" +
            "                                      where a_employee_mi.code = applicant)) " +
            "from emr_limit_unlock " +
            "${ew.customSqlSegment}")
    IPage<EmrLimitUnlock> selectUnlock(IPage<EmrLimitUnlock> page,
                                       @Param(Constants.WRAPPER) QueryWrapper<?> queryWrapper);

    @Select("select id, " +
            "       pat_no, " +
            "       times, " +
            "       unlock_json, " +
            "       applicant," +
            "applicantName =    (select rtrim(name) from a_employee_mi where code = applicant), " +
            "       application_time, " +
            "       request_remarks, " +
            "       approve," +
            "approveName   =    (select rtrim(name) from a_employee_mi where code = approve), " +
            "       review_time, " +
            "       review_notes, " +
            "       effective_time, " +
            "       state " +
            "from emr_limit_unlock  where id = #{id} ")
    EmrLimitUnlock selectUnlockById(int id);


    @Update("update emr_limit_unlock " +
            "set state       = #{param.state}, " +
            "    approve     = #{param.approve}, " +
            "    review_time = getdate(), " +
            "    review_notes= #{param.reviewNotes} " +
            "where id = #{param.id} ")
    void updateUnlockByIdNew(@Param("param") EmrLimitUnlock param);

    @Select("select id,  " +
            "       pat_no,  " +
            "       times,  " +
            "       unlock_json,  " +
            "       applicant,  " +
            "       application_time,  " +
            "       request_remarks,  " +
            "       approve,  " +
            "       review_time,  " +
            "       review_notes,  " +
            "       effective_time,  " +
            "       state  " +
            "from emr_limit_unlock  " +
            "where pat_no = #{patNo}  " +
            "  and times = #{times}  " +
            "  and applicant = #{applicant}" +
            "  and state = 1  " +
            "  and effective_time > getdate()")
    EmrLimitUnlock selectMyUnlockReqByPatNo(String patNo,
                                            Integer times,
                                            String applicant);

    /**
     * 获取医务部的人
     *
     * @return 人
     */
    @Select("select code_rs\n" +
            "from dj_user_role a,\n" +
            "     a_employee_mi b\n" +
            "where role_id = 38\n" +
            "  and a.user_code = b.code\n" +
            "  and del_flag <> '1'")
    List<String> getYiWuBuUser();

}
