package thyyxxk.webserver.dao.his.medicaladvice.nursing;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
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.inpatient.patient.Patient;
import thyyxxk.webserver.entity.medicaladvice.medicamanage.GraphicsParam;
import thyyxxk.webserver.entity.medicaladvice.medicamanage.PsInfoVO;
import thyyxxk.webserver.entity.medicaladvice.medicamanage.YzTemperatureGm;
import thyyxxk.webserver.entity.medicaladvice.medicamanage.YzTemperatureSum;
import thyyxxk.webserver.entity.medicaladvice.medicamanage.YzTemperatureVO;

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

/**
 * @Description:
 * @Author:lihong
 * @Date: 2023/4/21
 */
@Mapper
public interface NursingManagementDao {
    /**
     * @description:获取时间下拉框
     * @author: lihong
     * @date: 2023/4/21 15:01
     * @return: java.util.List<java.util.Map<java.lang.String,java.lang.String>>
     **/
    @Select("select rec_time ='全部'  union all select   rec_time = cast(DATEPART(hh,rec_time)as varchar)  from view_yz_temprature_batch")
   List<String> getrecTimeSelect();
    /**
     * @description:查询三测单 图表数据
     * @author: lihong
     * @date: 2023/4/21 15:24
     * @param: query
     * @return: java.util.List<thyyxxk.webserver.entity.medicaladvice.medicamanage.YzTemperatureVO>
     **/
    @Select("SELECT rtrim(a.inpatient_no) inpatient_no,  rtrim(a.inpatient_no) + '_' + cast(a.admiss_times as varchar) as visit_id ," +
            "       b.admiss_times," +
            "       b.rec_date," +
            "       #{recDateStr} as rec_date_str," +
            "       b.rec_time," +
            "       b.detail_no," +
            "       rtrim(b.patient_state) patient_state," +
            "       rtrim(b.temperature) temperature," +
            "       rtrim(b.temperature_type) temperature_type," +
            "       rtrim(b.pulse) pulse," +
            "       rtrim(b.breathe) breathe," +
            "       rtrim(b.scd_flag) scd_flag," +
            "       rtrim(b.sum_flag) sum_flag," +
            "       rtrim(b.other_info) other_info," +
            "       rtrim(b.body_weight) body_weight," +
            "       rtrim(b.userid) userid," +
            "       b.input_time," +
            "       rtrim(b.modify_userid) modify_userid," +
            "       b.modify_time," +
            "      rtrim(a.name)  name," +
            "       case when a.sex = 1 then '男' when a.sex = 2 then '女' else '未知' end sex," +
            "       rtrim(a.bed_no) bed_no," +
            "       a.admiss_date," +
            "       a.dis_date," +
            "       CONVERT(varchar(100), rec_time, 108) as toStringRecTime,"+
            "       rtrim(b.pressure_am) pressure_am," +
            "       rtrim(b.pressure_pm) pressure_pm," +
            "       rec_time_flag = 0," +
            "     physical_method_flag= (case when isnull(b.physical_method_flag,'') = '' then '0' else b.physical_method_flag end  )," +
            "       rtrim(b.temperature_cool) temperature_cool," +
            "       heart=rtrim(b.heart)," +
            "       scd_specialtext=rtrim(b.scd_specialtext)," +
            "       specialtext_time =b.specialtext_time," +
            "       specialtext_other =rtrim(b.specialtext_other)," +
            "       user_name=(select rtrim(name) from a_employee_mi where code=b.userid)," +
            "       fm_time=b.fm_time," +
            "       cs_time=b.cs_time," +
            "       rtrim(b.pain_lvl) pain_lvl," +
            "       rtrim(b.breathe_machine) breathe_machine" +
            " FROM yz_temperature b join  view_zy_patient_all a on a.inpatient_no = b.inpatient_no and  a.admiss_times = b.admiss_times" +
            " ${ew.customSqlSegment} order by cast(a.bed_no as numeric)")
   List<YzTemperatureVO> listYzTemperature(@Param(Constants.WRAPPER) QueryWrapper<?> query,@Param("recDateStr") String recDateStr);
    /**
     * @description:查询三测单  出入总量列表
     * @author: lihong 
     * @date: 2023/4/21 15:41 
     * @param: query 
     * @return: java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     **/
  @Select("SELECT inpatient_no     = rtrim(a.inpatient_no)," +
          "               admiss_times     = a.admiss_times," +
          "               name=              rtrim(a.name)," +
          "                                  case when a.sex = 1 then '男' when a.sex = 2 then '女' else '未知' end sex," +
          "               bed_no=rtrim(a.bed_no)," +
          "               admiss_date      =a.admiss_date," +
          "               dis_date         =a.dis_date," +
          "               rec_date         =b.rec_date," +
          "               #{recDateStr}            as                                                    rec_date_str," +
          "               pressure_am      =b.pressure_am," +
          "               pressure_pm      =b.pressure_pm," +
          "               intake           =b.intake," +
          "               shit             =b.shit," +
          "               shit_other       =b.shit_other," +
          "               urine_quan_info  =b.urine_quan_info," +
          "               output_other     =b.output_other," +
          "               body_weight      =b.body_weight," +
          "               body_weight_info = b.body_weight_info," +
          "               userid           =rtrim(b.userid)," +
          "               input_time       =b.input_time," +
          "               modify_userid    =rtrim(b.modify_userid)," +
          "               modify_time      =b.modify_time," +
          "               body_length=b.body_length," +
          "               urine_info=rtrim(b.urine_info)," +
          "               intake_info=rtrim(b.intake_info)," +
          "               output_info=rtrim(b.output_info)" +
          "        FROM yz_temperature_sum b" +
          "                  join view_zy_patient_all a on a.inpatient_no = b.inpatient_no and a.admiss_times = b.admiss_times ${ew.customSqlSegment}")
  List<YzTemperatureSum> listYzTemperatureSum(@Param(Constants.WRAPPER) QueryWrapper<?> query,@Param("recDateStr") String recDateStr);
  /**
   * @description:查询过敏药物
   * @author: lihong
   * @date: 2023/4/21 16:09
   **/
  @Select("SELECT rtrim(inpatient_no) inpatient_no,\n" +
          "       admiss_times,\n" +
          "       detail_no,\n" +
          "       gmxm = rtrim(gmxm),\n" +
          "       order_code  ,\n" +
          "       order_time = isnull(result_time,ps_time),\n" +
          "       act_order_no=act_order_no,\n" +
          "       ps_time=ps_time,\n" +
          "       result_time=result_time\n" +
          " FROM yz_temperature_gm\n" +
          " where inpatient_no = #{inpatientNo} and admiss_times = #{admissTimes} and\n" +
          "        act_order_no not in\n" +
          "        (select act_order_no from view_yz_all_order\n" +
          "         where inpatient_no = #{inpatientNo} and\n" +
          "                 admiss_times = #{admissTimes}  and\n" +
          "                 isnull(ps_status,'0')='0')")
  List<YzTemperatureGm> listYzTemperatureGm(YzTemperatureVO yzTemperatureVO);
  /**
   * @description:插入过敏信息
   * @author: lihong
   * @date: 2023/4/23 9:50
   * @param: list
   * @return: java.lang.Integer
   **/
  @Insert(" <script> insert into yz_temperature_gm ( inpatient_no,admiss_times,detail_no,gmxm,order_code,order_time,act_order_no,ps_time,result_time) values " +
          "   <foreach collection='list' item='item'  separator=',' > " +
          " ( #{item.inpatientNo},#{item.admissTimes},#{item.detailNo},#{item.gmxm},#{item.orderCode},#{item.orderTime},#{item.actOrderNo},#{item.psTime},#{item.resultTime} ) " +
          "   </foreach> " +
          " </script>  ")
 Integer batchSaveYzTemperatureGm(List<YzTemperatureGm> list);

@Insert("<script> " +
        " insert into yz_temperature_sum ( inpatient_no,admiss_times,rec_date,pressure_am,pressure_pm,intake,shit,shit_other,shit_unit,shit_shape,urine_quan_am,body_weight,body_weight_info,output_sum,output_other,userid,input_time,modify_userid,modify_time,urine_quan_info,intake_speed,intake_breast_milk,intake_water,intake_milk,urine_quan_color,output_other_info,body_length,urine_info,intake_info,output_info) values " +
        "   <foreach collection='list' item='item'  separator=',' > " +
        "  ( #{item.inpatientNo},#{item.admissTimes},#{item.recDate},#{item.pressureAm},#{item.pressurePm},#{item.intake},#{item.shit},#{item.shitOther},#{item.shitUnit},#{item.shitShape},#{item.urineQuanAm},#{item.bodyWeight},#{item.bodyWeightInfo},#{item.outputSum},#{item.outputOther},#{item.userid},#{item.inputTime},#{item.modifyUserid},#{item.modifyTime},#{item.urineQuanInfo},#{item.intakeSpeed},#{item.intakeBreastMilk},#{item.intakeWater}," +
        "  #{item.intakeMilk},#{item.urineQuanColor},#{item.outputOtherInfo},#{item.bodyLength},#{item.urineInfo},#{item.intakeInfo},#{item.outputInfo} )  " +
        "   </foreach> " +
        "  </script>  ")
 Integer batchSaveYzTemperatureSum(List<YzTemperatureSum> list);

 @Select(" SELECT" +
         "    yz_act_order.act_order_no," +
         "    yz_act_order.inpatient_no," +
         "    yz_act_order.admiss_times," +
         "    zy_actpatient.name," +
         "    zy_actpatient.bed_no," +
         "    yz_act_order.order_code," +
         "    yz_act_order.order_name," +
         "    yz_act_order.order_time," +
         "    yz_act_order.start_time," +
         "    yz_act_order.end_time," +
         "    yz_act_order.drug_specification," +
         "    yz_act_order.drug_quan," +
         "    yz_supply_type.supply_name," +
         "    yz_act_order.infant_flag," +
         "    yz_act_order.discription," +
         "    yz_act_order.enter_oper," +
         "    yz_act_order.ps_status," +
         "    yz_act_order.ps_oper," +
         "    ps_oper_name=(select rtrim(name) from a_employee_mi where code=yz_act_order.ps_oper),"+
         "    yz_act_order.ps_time," +
         "    yz_act_order.instruction," +
         "    yz_act_order.dose," +
         "    dose_unit =(select name from  yp_zd_unit where code =dose_unit)," +
         "    yz_act_order.drug_occ," +
         "    yz_act_order.frequ_code ," +
         "    yz_act_order.modifier ," +
         "    yz_act_order.parent_no," +
         "    yz_act_order.drug_flag," +
         "    yz_act_order.print_order_change," +
         "    yz_act_order.physician," +
         "    yz_act_order.enter_time," +
         "    yz_act_order.self_buy ," +
         "    exec_time_b=exec_time," +
         "    exec_time," +
         "    exec_id_b=exec_id," +
         "    exec_id," +
         "    exec_id2," +
         "    yz_act_order.ps_result_time" +
         " FROM yz_act_order," +
         "     zy_actpatient," +
         "     yz_supply_type ," +
         "     yp_zd_dict" +
         " WHERE ( yz_act_order.supply_code = yz_supply_type.supply_code) and" +
         "    ( yz_act_order.inpatient_no = zy_actpatient.inpatient_no ) and" +
         "    ( yz_act_order.admiss_times = zy_actpatient.admiss_times ) and" +
         "    ( yz_act_order.status_flag > '1' ) AND" +
         "    ( zy_actpatient.ward = #{curWard} ) AND" +
         "        zy_actpatient.bed_no >= cast(#{startBedNo} as float) and zy_actpatient.bed_no <=cast( #{endBedNo} as float) and" +
         "        yz_act_order.frequ_code  <> ''  and" +
         "       ${psOperCondition}  and" +
         "        yz_act_order.order_code = yp_zd_dict.code and" +
         "        yz_act_order.serial = yp_zd_dict.serial and" +
         "    (yz_supply_type.ps_flag='1' or yz_supply_type.ps_flag='0') order by cast(bed_no as float) ")
 List<PsInfoVO> listPsInfo(String startBedNo, String endBedNo, String curWard,String psOperCondition);
    /**
     * @description:更新皮试信息
     * @author: lihong
     * @date: 2023/4/23 11:00
     * @param: psInfoVO
     * @return: java.lang.Integer
     **/
    @Update("<script> " +
            " <foreach collection='list' item='item'  > " +
            "update yz_act_order set ps_status=#{item.psStatus} ,ps_oper=#{item.psOper},ps_result_time =#{item.psResultTime} where act_order_no =#{item.actOrderNo} ;" +
            " </foreach> </script> ")

    void updatePsInfo(List<PsInfoVO> list);

    @Select(" select top 20  rtrim(code),rtrim(name) from a_employee_mi ${ew.customSqlSegment} ")
    List<Map<String, Object>> selectEmpInfo(@Param(Constants.WRAPPER) QueryWrapper<?> query);
     @Insert(" insert into yz_temperature (inpatient_no, admiss_times, rec_date, rec_time, temperature_type, temperature," +
             "                            physical_method_flag, temperature_cool, pulse, heart, breathe, userid, input_time," +
             "                            scd_specialtext, specialtext_time, specialtext_other, modify_userid, modify_time, scd_flag," +
             "                            pain_lvl, breathe_machine)" +
             "values (" +
             "#{inpatientNo},#{admissTimes},#{recDate},#{recTime},#{temperatureType},#{temperature},#{physicalMethodFlag},#{temperatureCool},#{pulse},#{heart},#{breathe},#{userid},#{inputTime},#{scdSpecialtext},#{specialtextTime},#{specialtextOther},#{modifyUserid},#{modifyTime},#{scdFlag},#{painLvl},#{breatheMachine} "+
             ") ")
     void insertYzTemperatureVO(YzTemperatureVO yzTemperatureVO);
       @Select(" insert into yz_temperature_sum (inpatient_no, admiss_times, rec_date, pressure_am, pressure_pm, intake, shit,\n" +
               "                                shit_other, urine_quan_info, output_other, body_weight, body_weight_info, body_length,\n" +
               "                                userid, input_time, modify_userid, modify_time, urine_info, intake_info, output_info)\n" +
               "values (" +
               "#{inpatientNo},#{admissTimes},#{recDate},#{pressureAm},#{pressurePm},#{intake,jdbcType=FLOAT},#{shit,jdbcType=FLOAT},#{shitOther},#{urineQuanInfo,jdbcType=FLOAT},#{outputOther,jdbcType=FLOAT},#{bodyWeight,jdbcType=FLOAT},#{bodyWeightInfo},#{bodyLength,jdbcType=FLOAT},#{userid},#{inputTime},#{modifyUserid},#{modifyTime},#{urineInfo},#{intakeInfo},#{outputInfo} " +
               ")")
      void insertYzTemperatureSum(YzTemperatureSum yzTemperatureSum);
      @Select("INSERT INTO yz_temperature_gm (inpatient_no, admiss_times, gmxm, order_time, act_order_no, ps_time, result_time)\n" +
              "VALUES ( " +
              "#{inpatientNo},#{admissTimes},#{gmxm},#{orderTime},#{actOrderNo},#{psTime},#{resultTime} )")
      void insertYzTemperatureGm(YzTemperatureGm yzTemperatureGm);
    @Select(" select  rtrim(inpatient_no) + '_' +cast(admiss_times as varchar)+ '_' + cast(DATEPART(hh,rec_time)as varchar)  from yz_temperature where rtrim(inpatient_no) + '_' +cast(admiss_times as varchar)+ '_' + cast(DATEPART(hh,rec_time)as varchar) in (${uuid}) " +
            " and convert(char(11),rec_date,121) = #{recDate} ")
    List<String> existYzTemperature(String uuid, String recDate);
    @Update(" <script> " +
            " <foreach collection='list' item='item'  >  " +
            "update yz_temperature set inpatient_no= #{item.inpatientNo},\n" +
            " admiss_times= #{item.admissTimes},\n" +
            "  rec_date= #{item.recDate},\n" +
            " rec_time= #{item.recTime},\n" +
            " temperature_type= #{item.temperatureType},\n" +
            " temperature= #{item.temperature},\n" +
            " physical_method_flag= #{item.physicalMethodFlag},\n" +
            " temperature_cool= #{item.temperatureCool},\n" +
            " pulse= #{item.pulse},\n" +
            " heart= #{item.heart},\n" +
            " breathe= #{item.breathe},\n" +
            " userid= #{item.userid},\n" +
            " input_time= #{item.inputTime},\n" +
            " scd_specialtext= #{item.scdSpecialtext},\n" +
            " specialtext_time= #{item.specialtextTime},\n" +
            " specialtext_other= #{item.specialtextOther},\n" +
            " modify_userid= #{item.modifyUserid},\n" +
            " modify_time= #{item.modifyTime},\n" +
            " scd_flag= #{item.scdFlag},\n" +
            " pain_lvl= #{item.painLvl},\n" +
            " breathe_machine= #{item.breatheMachine}" +
            " where detail_no =#{item.detailNo} ;" +
            "</foreach></script>")
    void updateYzTemperature(List<YzTemperatureVO> list);
    @Select(" select rtrim(inpatient_no) + '_' + cast(admiss_times as varchar) + '_' + rtrim(convert(char(11), rec_date, 121))  from yz_temperature_sum where rtrim(inpatient_no) + '_' + cast(admiss_times as varchar) + '_' + convert(char(11),rec_date,121) in (${toInStringSql})")
    List<String> existYzTemperatureSum(String toInStringSql);
    @Update("<script><foreach collection='list' item='item'  >" +
            "update yz_temperature_sum set inpatient_no= #{item.inpatientNo},\n" +
            "admiss_times= #{item.admissTimes},\n" +
            "rec_date= #{item.recDate},\n" +
            "pressure_am= #{item.pressureAm},\n" +
            "pressure_pm= #{item.pressurePm},\n" +
            "intake= #{item.intake},\n" +
            "shit= #{item.shit,jdbcType=FLOAT},\n" +
            "shit_other= #{item.shitOther},\n" +
            "urine_quan_info= #{item.urineQuanInfo,jdbcType=FLOAT},\n" +
            "output_other= #{item.outputOther,jdbcType=FLOAT},\n" +
            "body_weight= #{item.bodyWeight,jdbcType=FLOAT},\n" +
            "body_weight_info= #{item.bodyWeightInfo},\n" +
            "body_length= #{item.bodyLength,jdbcType=FLOAT},\n" +
            "userid= #{item.userid},\n" +
            "input_time= #{item.inputTime},\n" +
            "modify_userid= #{item.modifyUserid},\n" +
            "modify_time= #{item.modifyTime},\n" +
            "urine_info= #{item.urineInfo},\n" +
            "intake_info= #{item.intakeInfo},\n" +
            "output_info= #{item.outputInfo}   " +
            "  where rtrim(inpatient_no) + '_' + cast(admiss_times as varchar) + '_' + convert(char(11),rec_date,121) = #{item.inpatientNo} + '_' +cast(#{item.admissTimes} as varchar) + '_' +#{item.recDateStr} ;"+
            "</foreach></script>")
    void updateYzTemperatureSum(List<YzTemperatureSum> list);
    @Delete(" delete from yz_temperature_sum where inpatient_no =#{inpatientNo} and admiss_times =#{admissTimes} and convert(char(11),rec_date,121) =#{recDateStr}  ")
    void deleteYzTemperatureSum(YzTemperatureSum data);
    @Delete(" delete from  yz_temperature  where detail_no =#{detailNo}")
    void deleteYzTemperature(Integer detailNo);
    @Select("SELECT yz_act_order.order_time code ,   " +
            "         yz_act_order.order_name name " +
            "    FROM yz_act_order   " +
            " where inpatient_no = #{inpatientNo} and" +
            " admiss_times = #{admissTimes} and " +
            " order_code in (select order_code from yz_zd_order_item_confirm where item_name in ('转科','出院'))" +
            " union all" +
            "  SELECT yz_inact_order.order_time code,   " +
            "         yz_inact_order.order_name  name " +
            "    FROM yz_inact_order   " +
            " where inpatient_no = #{inpatientNo} and" +
            " admiss_times = #{admissTimes} and " +
            " order_code in (select order_code from yz_zd_order_item_confirm where item_name in ('转科','出院')) ")
    List<Map<String, Object>> listOrderTime(@Param("inpatientNo") String inpatientNo, @Param("admissTimes") Integer admissTimes);
    @Select(" select top 1 pressure_am,pressure_pm from yz_temperature_sum where convert(char(11),rec_date,121) =#{toStringRecDate} and inpatient_no =#{inpatientNo} and admiss_times =#{admissTimes} ")
    YzTemperatureSum queryPressureAmAndPm(@Param("inpatientNo") String inpatientNo, @Param("admissTimes") Integer admissTimes, @Param("toStringRecDate") String toStringRecDate);
    @Select(" select *  from yz_temperature_sum where inpatient_no =#{inpatientNo} and admiss_times=#{admissTimes} and rec_date>=#{startRecTime} and rec_date<=#{endRecTime} order by  rec_date")
    List<YzTemperatureSum> selectYzTemperatureSum(GraphicsParam query);
    @Select(" select top 1 rec_date  from  yz_temperature where inpatient_no =#{inpatientNo} and admiss_times =#{admissTimes} and  scd_specialtext ='手术'")
    Date selectOperaDay(GraphicsParam query);
    @Select(" select a.*,b.birth_date  from view_zy_patient_all a join a_patient_mi b on a.inpatient_no=b.inpatient_no where a.inpatient_no =#{inpatientNo} and a.admiss_times =#{admissTimes}")
    Patient getAllPatintInfo(YzTemperatureVO query);
}
