<?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.DoctorPatientMapper">

    <resultMap id="doctorPatientMap" type="com.example.entity.DoctorPatient">
        <id property="id" column="id"/>
        <result property="doctorId" column="doctor_id"/>
        <result property="patientId" column="patient_id"/>
        <result property="relationshipStart" column="relationship_start"/>
        <result property="status" column="status"/>
        <result property="createTime" column="create_time"/>
        <association property="doctor" javaType="com.example.entity.Doctor">
            <id property="id" column="d_id"/>
            <result property="username" column="d_username"/>
            <result property="name" column="d_name"/>
            <result property="sex" column="d_sex"/>
            <result property="phone" column="d_phone"/>
            <result property="email" column="d_email"/>
            <result property="certificateNo" column="d_certificate_no"/>
            <result property="hospital" column="d_hospital"/>
            <result property="department" column="d_department"/>
        </association>
        <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="sex" column="p_sex"/>
            <result property="phone" column="p_phone"/>
            <result property="email" column="p_email"/>
        </association>
    </resultMap>

    <select id="selectByDoctorId" resultMap="doctorPatientMap">
        SELECT 
            dp.*,
            p.id AS p_id,
            p.username AS p_username,
            p.name AS p_name,
            p.sex AS p_sex,
            p.phone AS p_phone,
            p.email AS p_email,
            d.id AS d_id,
            d.username AS d_username,
            d.name AS d_name
        FROM 
            doctor_patient dp
            LEFT JOIN patient p ON dp.patient_id = p.id
            LEFT JOIN doctor d ON dp.doctor_id = d.id
        WHERE 
            dp.doctor_id = #{doctorId}
            <if test="name != null and name != ''">
                AND p.name LIKE CONCAT('%', #{name}, '%')
            </if>
        ORDER BY 
            dp.create_time DESC
    </select>

    <select id="selectByPatientId" resultMap="doctorPatientMap">
        SELECT 
            dp.*,
            p.id AS p_id,
            p.username AS p_username,
            p.name AS p_name,
            p.sex AS p_sex,
            p.phone AS p_phone,
            p.email AS p_email,
            d.id AS d_id,
            d.username AS d_username,
            d.name AS d_name,
            d.sex AS d_sex,
            d.phone AS d_phone,
            d.email AS d_email,
            d.certificate_no AS d_certificate_no,
            d.hospital AS d_hospital,
            d.department AS d_department
        FROM 
            doctor_patient dp
            LEFT JOIN patient p ON dp.patient_id = p.id
            LEFT JOIN doctor d ON dp.doctor_id = d.id
        WHERE 
            dp.patient_id = #{patientId}
            <if test="name != null and name != ''">
                AND d.name LIKE CONCAT('%', #{name}, '%')
            </if>
        ORDER BY 
            dp.create_time DESC
    </select>

</mapper> 