package thyyxxk.webserver.dao.his.zygl;

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.medicaladvice.medicamanage.MzZyReq;

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

/**
 * @Description: 住院证
 * @Author:lihong
 * @Date: 2025/5/8
 */
@Mapper
public interface MzZyReqDao {
    /**
     * 保存住院证
     * @param mzZyReq
     * @return
     */
    @Insert("INSERT INTO mz_zy_req(patient_id,name,visit_date,doctor_code,dept_code,req_dept,req_ward,bed_no,icd_code,icd_text,confirm_id,confirm_date," +
            "zy_serial_no,responce_type,admiss_status,req_status,small_dept,icd_code_new,icd_text_new,condition_code,route_of_admission) VALUES (#{patientId}, #{name}, #{visitDate}, #{doctorCode}, #{deptCode}," +
            " #{reqDept}, #{reqWard}, #{bedNo}, #{icdCode}, #{icdText}, #{confirmId}, #{confirmDate}, #{zySerialNo}, #{responceType}, #{admissStatus}, #{reqStatus}, #{smallDept}," +
            " #{icdCodeNew}, #{icdTextNew},#{conditionCode}, #{routeOfAdmission})")
    int insert(MzZyReq mzZyReq);

    /**
     * 查询病人的入院证（有效）
     * @param patientId
     * @param reqStatus
     * @return
     */
    @Select({"<script>" ,"select * from mz_zy_req WITH(NOLOCK) where patient_id=#{patientId} " ,
            "<when test='reqStatus!=null'>",
            " and req_status = #{reqStatus}  ",
            "</when>",
            " order by visit_date desc ","</script>"})
    List<MzZyReq> selectMzZyReqByPatientId(@Param("patientId") String patientId, @Param("reqStatus") String reqStatus);

    /**
     * 设置门诊祝愿证为已经入院
     * @param mzZyReq
     * @return
     */
    @Update("update mz_zy_req set confirm_id=#{confirmId},confirm_date=#{confirmDate},zy_serial_no=#{zySerialNo},req_status=#{reqStatus} where patient_id =#{patientId} and req_status='0' ")
    int updateMzZyReq(MzZyReq mzZyReq);
    @Update("update mz_zy_req set confirm_id=#{confirmId},confirm_date=#{confirmDate},req_status=#{reqStatus} where patient_id =#{patientId} and req_status='3' ")
    int updateMzZyReqStatus(MzZyReq mzZyReq);

    /**
     * 根据患者id和医生编码查询未处理的住院申请
     * @param patientId
     * @param doctorCode
     * @return
     */
    @Select("select * from mz_zy_req where patient_id=#{patientId} and req_status = 0 and doctor_code=#{doctorCode}")
    List<MzZyReq> selectMzZyReqByDoctorCode(@Param("patientId") String patientId,@Param("doctorCode") String doctorCode);

    /**
     * 删除该医生未确认的住院证 只删除3天内的
     * @param patientId
     * @param doctorCode
     * @return
     */
    @Delete("delete from mz_zy_req where patient_id=#{patientId} and req_status = 0 and doctor_code=#{doctorCode} and DATEDIFF(day,visit_date,getdate()) <=3")
    int deleteMzZyReqByDoctorCode(@Param("patientId") String patientId,@Param("doctorCode") String doctorCode);

    /**
     * 删除未确认的住院证
     * @param patientId
     * @return
     */
    @Delete("delete from mz_zy_req where patient_id=#{patientId} and req_status = 0 ")
    int deleteMzZyReq(@Param("patientId") String patientId);



    /**
     * 门诊医生 （科室）住院证开立情况  zy_req_num 入院申请  zy_req_ry 入院人数  zy_req_ty 退院人数
     * @param beginDate
     * @param endDate
     * @param doctorCode
     * @param deptCode
     * @param chargeDate true 需要按照日期统计 null 不用按日期统计，只是汇总统计
     * @return
     */
    @Select({"<script>" ,
            " SELECT " ,
            "rtrim(doctor_code) doctor_code," ,
            "zy_req_num = COUNT ( * )," ,
            "<when test='chargeDate!=null'>",
            " charge_date=convert(varchar(12),visit_date,111),",
            "</when>",
            "zy_req_ry = SUM ( CASE WHEN req_status = '1' THEN 1 ELSE 0 END )," ,
            "zy_req_ty = SUM ( CASE WHEN req_status = '2' THEN 1 ELSE 0 END ) " ,
            " FROM " ,
            " mz_zy_req WITH(NOLOCK) " ,
            " WHERE 1=1 " ,
            "<when test='beginDate!=null'>",
            " and visit_date &gt;=#{beginDate} ",
            "</when>",
            "<when test='endDate!=null'>",
            " and visit_date &lt;=#{endDate} ",
            "</when>",
            "<when test='doctorCode!=null'>",
            " and doctor_code =#{doctorCode} ",
            "</when>",
            "<when test='deptCode!=null'>",
            " and dept_code =#{deptCode} ",
            "</when>",
            " GROUP BY " ,
            " doctor_code" ,
            "<when test='chargeDate!=null'>",
            " ,CONVERT ( VARCHAR ( 12 ), visit_date, 111 )",
            "   order by doctor_code,convert(VARCHAR(12),visit_date,111) asc ",
            "</when>",
            "</script>"})
    List<Map<String,Object>> selectWorkloadStatistics(@Param("beginDate") String beginDate, @Param("endDate") String endDate, @Param("doctorCode") String doctorCode, @Param("deptCode") String deptCode, @Param("chargeDate") Boolean chargeDate);

    /**
     * 查询住院申请明细
     * @param beginDate
     * @param endDate
     * @param doctorCode
     * @param deptCode
     * @return
     */
    @Select({"<script>" +
            " SELECT " +
            " patient_id,name, visit_date,doctor_code,dept_code ,req_dept,req_ward,icd_text,admiss_status,req_status,confirm_date,icd_code_new,icd_text_new,condition_code,route_of_admission " +
            " FROM " +
            " mz_zy_req WITH(NOLOCK) " +
            " WHERE 1=1 " +
            "<when test='beginDate!=null'>",
            " and visit_date &gt;=#{beginDate} ",
            "</when>",
            "<when test='endDate!=null'>",
            " and visit_date &lt;=#{endDate} ",
            "</when>",
            "<when test='doctorCode!=null'>",
            " and doctor_code =#{doctorCode} ",
            "</when>",
            "<when test='deptCode!=null'>",
            " and dept_code =#{deptCode} ",
            "</when>",
            " order by visit_date desc </script>"})
    List<MzZyReq> selectMzZyReqs(@Param("beginDate") String beginDate, @Param("endDate") String endDate, @Param("doctorCode") String doctorCode, @Param("deptCode") String deptCode);
    @Select(" select top 1 *  from mz_zy_req where patient_id =#{patientId} and req_status !='4'  order by visit_date desc ")
    MzZyReq selectLastMzZyReq(String patientId);
}
