<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.eitc.patient.mapper.MedicalRecordMapper">
    <select id="medicalRecordList" resultType="com.eitc.patient.dto.MedicalRecordListDto">
        select emr.id,
            ep.patient_name           as            patientName,
            emr.create_date           as            createDate,
            emr.visit_type            as            visitType,
            emr.department            as            department,
            emr.attending_doctor_name as            attendingDoctorName,
            (SELECT GROUP_CONCAT(DISTINCT emrd.tooth_position ORDER BY emrd.tooth_position SEPARATOR ',') AS toothPosition
            FROM erm_medical_record_details emrd where emrd.is_del = 0 and emrd.medical_record_id = emr.id) toothPosition
        from erm_medical_record emr
        left join erm_patient ep on emr.patient_id = ep.id
        where emr.is_del = 0
            <if test="searchParam != null and searchParam != ''">
                and (
                    emr.chief_complaint_content like concat('%', #{searchParam}, '%')
                    or emr.dispose_content like concat('%', #{searchParam}, '%')
                    or emr.inspect_content like concat('%', #{searchParam}, '%')
                    or emr.present_illness_content like concat('%', #{searchParam}, '%')
                    or emr.treatment_planning_content like concat('%', #{searchParam}, '%')
                    or emr.note_content like concat('%', #{searchParam}, '%')
                    or ep.patient_name like concat('%', #{searchParam}, '%')
                    or emr.attending_doctor_name like concat('%', #{searchParam}, '%')
                )
            </if>
        order by emr.create_date desc
    </select>

    <resultMap type="com.eitc.patient.domain.MedicalRecord" id="medicalRecordResult">
        <id property="id" column="id"/>
        <result property="patientId" column="patient_id"/>
        <result property="status" column="status"/>
        <result property="visitType" column="visit_type"/>
        <result property="attendingDoctorId" column="attending_doctor_id"/>
        <result property="attendingDoctorName" column="attending_doctor_name"/>
        <result property="outpatientService" column="outpatient_service"/>
        <result property="department" column="department"/>
        <result property="cost" column="cost"/>
        <result property="createDate" column="create_date"/>
        <result property="createUser" column="create_user"/>
        <result property="updateDate" column="update_date"/>
        <result property="updateUser" column="update_user"/>
    </resultMap>

    <select id="countByDiagnosisTypeName" resultType="java.util.Map">
        select * from (
            select (select diagnosis_name from erm_diagnosis_manage where id = diagnosis_manage_id) diagnosisTypeName, count(*) count
            from erm_medical_record_details
            where is_del = 0
            group by diagnosis_manage_id order by count desc
        ) a where a.diagnosisTypeName is not null
        limit 10
    </select>

    <select id="findListByToothPosition" resultMap="medicalRecordResult">
        SELECT *
        FROM erm_medical_record emr
        where emr.is_del = 0
        and emr.id in (select distinct emrd.medical_record_id from erm_medical_record_details emrd
        where emrd.is_del = 0
        <if test="toothPositionList != null">
            and tooth_position in
            <foreach collection="toothPositionList" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        )
        and emr.patient_id = #{patientId}
        order by emr.create_date desc
    </select>

</mapper>
