<?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.example.mapper.SymptomRecordMapper">

    <resultMap id="symptomRecordMap" type="com.example.entity.SymptomRecord">
        <id property="id" column="id"/>
        <result property="patientId" column="patient_id"/>
        <result property="symptomDesc" column="symptom_desc"/>
        <result property="severityLevel" column="severity_level"/>
        <result property="emotionScore" column="emotion_score"/>
        <result property="energyDuration" column="energy_duration"/>
        <result property="interestRecovery" column="interest_recovery"/>
        <result property="sleepTime" column="sleep_time"/>
        <result property="wakeCount" column="wake_count"/>
        <result property="morningWake" column="morning_wake"/>
        <result property="mealCompletion" column="meal_completion"/>
        <result property="painLocation" column="pain_location"/>
        <result property="painIntensity" column="pain_intensity"/>
        <result property="memoryTest" column="memory_test"/>
        <result property="decisionTime" column="decision_time"/>
        <result property="attentionSpan" column="attention_span"/>
        <result property="suicidalThought" column="suicidal_thought"/>
        <result property="recordTime" column="record_time"/>
        <result property="createTime" column="create_time"/>
        
        <!-- 关联患者信息 -->
        <association property="patient" javaType="com.example.entity.Patient">
            <id property="id" column="p_id"/>
            <result property="username" column="p_username"/>
            <result property="name" column="p_name"/>
            <result property="avatar" column="p_avatar"/>
            <result property="sex" column="p_sex"/>
            <result property="phone" column="p_phone"/>
            <result property="email" column="p_email"/>
        </association>
    </resultMap>
    
    <!-- 查询记录并带患者信息 -->
    <select id="selectWithPatient" resultMap="symptomRecordMap">
        SELECT 
            sr.*,
            p.id AS p_id,
            p.username AS p_username,
            p.name AS p_name,
            p.avatar AS p_avatar,
            p.sex AS p_sex,
            p.phone AS p_phone,
            p.email AS p_email
        FROM 
            symptom_record sr
        LEFT JOIN 
            patient p ON sr.patient_id = p.id
        <where>
            <if test="patientId != null">
                AND sr.patient_id = #{patientId}
            </if>
        </where>
        ORDER BY 
            sr.record_time DESC
    </select>
</mapper> 