<?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.emoon.chat.mapper.PatientRecordMapper">

    <!-- 批量插入（只插入非空字段） -->
    <insert id="insertBatchSomeColumn" parameterType="java.util.List">
        INSERT INTO patient_record (
        id, patient_id, visit_id, admission_no, name, age, gender, birth_date,
        nurse, attending_doctor, admission_date, discharge_date, admission_dept, discharge_dept,
        admission_note, first_course_record, course_records, stage_summary, ward_rounds,
        consultation_records, difficult_case_discussion, long_stay_discussion, death_case_discussion,
        rescue_records, surgery_records, lab_reports, exam_reports, consent_forms,
        orders, discharge_summary, status, create_date
        )
        VALUES
        <foreach collection="records" item="item" separator=",">
            (
            #{item.id}, #{item.patientId}, #{item.visitId}, #{item.admissionNo}, #{item.name},
            #{item.age}, #{item.gender}, #{item.birthDate}, #{item.nurse}, #{item.attendingDoctor},
            #{item.admissionDate}, #{item.dischargeDate}, #{item.admissionDept}, #{item.dischargeDept},
            #{item.admissionNote}, #{item.firstCourseRecord}, #{item.courseRecords}, #{item.stageSummary},
            #{item.wardRounds}, #{item.consultationRecords}, #{item.difficultCaseDiscussion}, #{item.longStayDiscussion},
            #{item.deathCaseDiscussion}, #{item.rescueRecords}, #{item.surgeryRecords}, #{item.labReports},
            #{item.examReports}, #{item.consentForms}, #{item.orders}, #{item.dischargeSummary},
            #{item.status}, #{item.createDate}
            )
        </foreach>
    </insert>

    <update id="updateStatusById">
        UPDATE patient_record
        SET status = #{status}
        WHERE id = #{id}
    </update>

    <!-- 注意：这里不写 resultMap，MyBatis 会自动返回 Map -->
    <select id="selectDetailMapById" resultType="java.util.HashMap">
        SELECT
            types.qcType AS type,
            types.qcTypeName AS typeName,

            CASE types.qcType
                WHEN 0 THEN pr.admission_note
                WHEN 1 THEN pr.first_course_record
                WHEN 2 THEN pr.course_records
                WHEN 3 THEN pr.stage_summary
                WHEN 4 THEN pr.ward_rounds
                WHEN 5 THEN pr.consultation_records
                WHEN 6 THEN pr.difficult_case_discussion
                WHEN 7 THEN pr.long_stay_discussion
                WHEN 8 THEN pr.death_case_discussion
                WHEN 9 THEN pr.rescue_records
                WHEN 10 THEN pr.surgery_records
                WHEN 11 THEN pr.lab_reports
                WHEN 12 THEN pr.exam_reports
                WHEN 13 THEN pr.orders
                WHEN 14 THEN pr.discharge_summary
                WHEN 15 THEN pr.consent_forms
                ELSE NULL
                END AS content,

            mrr.qc_result AS qcResult,
            mrr.qc_score AS qcScore,
            mrr.qc_comments AS qcComments,
            mrr.qc_review_comments AS qcReviewComments,
            mrr.qc_name AS qcName,
            mrr.qc_start_time AS qcStartTime,
            mrr.qc_end_time AS qcEndTime

        FROM (
                 SELECT 0 AS qcType, '住院病历' AS qcTypeName
                 UNION ALL SELECT 1, '首次病程记录'
                 UNION ALL SELECT 2, '病程记录'
                 UNION ALL SELECT 3, '阶段小结'
                 UNION ALL SELECT 4, '查房记录'
                 UNION ALL SELECT 5, '会诊记录'
                 UNION ALL SELECT 6, '疑难病例讨论记录'
                 UNION ALL SELECT 7, '超长住院讨论记录'
                 UNION ALL SELECT 8, '死亡病例讨论记录'
                 UNION ALL SELECT 9, '抢救记录'
                 UNION ALL SELECT 10, '手术记录'
                 UNION ALL SELECT 11, '检验报告单'
                 UNION ALL SELECT 12, '检查报告单'
                 UNION ALL SELECT 13, '医嘱记录'
                 UNION ALL SELECT 14, '出院小结'
--                  UNION ALL SELECT 15, '知情同意书'
             ) types
                 LEFT JOIN patient_record pr ON pr.id = #{id}
                 LEFT JOIN medical_record_result mrr
                           ON mrr.patient_record_id = pr.id
                               AND mrr.qc_type = types.qcType
        ORDER BY types.qcType
    </select>



    <!-- 根据状态查询病历ID列表 -->
    <select id="findIdsByStatus" resultType="java.lang.String">
        SELECT id FROM patient_record
        WHERE status = #{status}
        <if test="limit > 0">
            LIMIT #{limit}
        </if>
    </select>






</mapper>
