package cn.hnthyy.thmz.mapper.his.mz;

import cn.hnthyy.thmz.entity.his.mz.MzBlRecord;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

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

/**
 * 病历数据库操作接口
 */
public interface MzBlRecordMapper {
    @Insert("insert into mz_bl_record(patient_id,times,emr_no,emr_type,visit_date,doctor_code,dept_code,first_or_not,emr_chief_complaint,emr_hpi,emr_ps,emr_pe,emr_fzjc," +
            "emr_process,emr_xyy,emr_yypg,emr_gnpg,emr_jkjy,personal_history,family_history,obsterical_history,weight,temperature,sphygmus,breathe,pressure_high," +
            "pressure_floor,pressure_high_left,pressure_floor_left,tentative_diagnosis) VALUES (#{patientId,jdbcType=CHAR}," +
            "#{times,jdbcType=INTEGER},#{emrNo,jdbcType=VARCHAR},#{emrType,jdbcType=CHAR},#{visitDate,jdbcType=TIMESTAMP},#{doctorCode,jdbcType=CHAR}," +
            "#{deptCode,jdbcType=VARCHAR},#{firstOrNot,jdbcType=CHAR},#{emrChiefComplaint,jdbcType=VARCHAR},#{emrHpi,jdbcType=VARCHAR},#{emrPs,jdbcType=VARCHAR}," +
            "#{emrPe,jdbcType=VARCHAR},#{emrFzjc,jdbcType=VARCHAR},#{emrProcess,jdbcType=VARCHAR},#{emrXyy,jdbcType=VARCHAR},#{emrYypg,jdbcType=VARCHAR}," +
            "#{emrGnpg,jdbcType=VARCHAR},#{emrJkjy,jdbcType=VARCHAR},#{personalHistory},#{familyHistory},#{obstericalHistory},#{weight},#{temperature},#{sphygmus}" +
            ",#{breathe},#{pressureHigh},#{pressureFloor},#{pressureHighLeft},#{pressureFloorLeft},#{tentativeDiagnosis})")
    int insertMzBlRecord(MzBlRecord mzBlRecord);


    /**
     * 按照病人编号查询最后一次病历信息
     *
     * @param patientId 病人编号
     * @return
     */
    @Select("select top 1rtrim(patient_id) patient_id,times from mz_bl_record where patient_id= #{patientId,jdbcType=VARCHAR} order by times desc")
    MzBlRecord selectLastMzBlRecordByPatientId(@Param("patientId") String patientId);


    /**
     * 根据病人id 和缴费次数查询病人的门诊病历
     * @param patientId
     * @param times
     * @return
     */
    @Select("select patient_id,times,emr_no,emr_type,visit_date,doctor_code,dept_code,first_or_not,emr_chief_complaint," +
            "emr_hpi,emr_ps,emr_pe,emr_fzjc,emr_process,emr_xyy,emr_yypg,emr_gnpg,emr_jkjy,personal_history,family_history," +
            "obsterical_history,weight,temperature,sphygmus,breathe,pressure_high,pressure_floor,pressure_high_left,pressure_floor_left,tentative_diagnosis " +
            " from mz_bl_record where patient_id= #{patientId,jdbcType=VARCHAR} and times =#{times}")
    MzBlRecord selectMzBlRecordByPatientIdAndTimes(@Param("patientId") String patientId,@Param("times") Integer times);

    /**
     * 查询主诉的分组统计数据
     * @return
     */
    @Select("select cast(emr_chief_complaint as varchar(8000)) emr_chief_complaint, count(1) num,rtrim(doctor_code) doctor_code from mz_bl_record  GROUP BY  cast(emr_chief_complaint as varchar(8000) ),doctor_code ")
    List<Map<String,Object>> selectEmrChiefComplaintCount();

    /**
     * 查询现病史的分组统计数据
     * @return
     */
    @Select("select cast(emr_hpi as varchar(8000)) emr_hpi, count(1) num,rtrim(doctor_code) doctor_code from mz_bl_record  GROUP BY  cast(emr_hpi as varchar(8000) ),doctor_code ")
    List<Map<String,Object>> selectEmrHpiCount();

    /**
     * 查询既往史的分组统计数据
     * @return
     */
    @Select("select cast(emr_ps as varchar(8000)) emr_ps, count(1) num,rtrim(doctor_code) doctor_code from mz_bl_record  GROUP BY  cast(emr_ps as varchar(8000) ),doctor_code ")
    List<Map<String,Object>> selectEmrPsCount();

    /**
     * 删除未缴费的病历信息
     * @param patientId
     * @param times
     * @return
     */
    @Delete("delete  from  mz_bl_record where patient_id =#{patientId,jdbcType=CHAR} and times=#{times} ")
    int deleteNotPayBlRecordByPatientId(@Param("patientId") String patientId,@Param("times") Integer times);
}

