<?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.xinelu.manage.mapper.consultationInfo.ConsultationInfoMapper">

    <resultMap type="ConsultationInfo" id="ConsultationInfoResult">
        <result property="id" column="id"/>
        <result property="patientId" column="patient_id"/>
        <result property="patientName" column="patient_name"/>
        <result property="cardNo" column="card_no"/>
        <result property="phone" column="phone"/>
        <result property="address" column="address"/>
        <result property="doctorId" column="doctor_id"/>
        <result property="doctorName" column="doctor_name"/>
        <result property="consultationType" column="consultation_type"/>
        <result property="status" column="status"/>
        <result property="appointmentDate" column="appointment_date"/>
        <result property="appointmentStartTime" column="appointment_start_time"/>
        <result property="appointmentEndTime" column="appointment_end_time"/>
        <result property="videoUrl" column="video_url"/>
        <result property="problemDescription" column="problem_description"/>
        <result property="situationDescription" column="situation_description"/>
        <result property="problemStatement" column="problem_statement"/>
        <result property="medicalRecord" column="medical_record"/>
	    <result property="schedulePlanDetailId" column="schedule_plan_detail_id"/>
        <result property="delFlag" column="del_flag"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <sql id="selectConsultationInfoVo">
        select id,
               patient_id,
               patient_name,
               card_no,
               phone,
               address,
               doctor_id,
               doctor_name,
               consultation_type,
               status,
               appointment_date,
               appointment_start_time,
               appointment_end_time,
               video_url,
               problem_description,
               situation_description,
               problem_statement,
               medical_record,
               schedule_plan_detail_id,
               del_flag,
               create_by,
               create_time,
               update_by,
               update_time
        from consultation_info
    </sql>

    <select id="selectConsultationInfoList" parameterType="com.xinelu.manage.dto.consultationInfo.ConsultationInfoDTO" resultType="com.xinelu.manage.vo.consultationInfo.ConsultationInfoVO">
        SELECT
        ci.id,
        ci.patient_id,
        ci.patient_name,
        ci.card_no,
        ci.phone,
        ci.address,
        ci.doctor_id,
        ci.doctor_name,
        ci.problem_description,
        ci.create_time,
	    ci.status,
        (SELECT content FROM  chat_record cr WHERE  cr.consultation_id = ci.id ORDER BY create_time DESC LIMIT 1) as content,
        (SELECT message_type FROM  chat_record cr WHERE  cr.consultation_id = ci.id ORDER BY create_time DESC LIMIT 1) as messageType,
        (SELECT COUNT(cr.id) FROM  chat_record cr WHERE  cr.consultation_id = ci.id AND read_status = '0'
        <if test="patientId != null ">
            and sender_id= ci.doctor_id
        </if>
        <if test="doctorId != null ">
            and sender_id= ci.patient_id
        </if>
         ) AS messageCount
        FROM
        consultation_info ci
        <where>
            <if test="patientId != null ">
                and ci.patient_id = #{patientId}
            </if>
            <if test="patientName != null  and patientName != ''">
                and ci.patient_name like concat('%', #{patientName}, '%')
            </if>
            <if test="doctorId != null  and doctorId != ''">
                and ci.doctor_id = #{doctorId}
            </if>
            <if test="doctorName != null  and doctorName != ''">
                and ci.doctor_name like concat('%', #{doctorName}, '%')
            </if>
            <if test="consultationType != null  and consultationType != ''">
                and ci.consultation_type = #{consultationType}
            </if>
            <if test="status != null ">
                and ci.status = #{status}
            </if>
            <if test="appointmentDate != null ">
                and ci.appointment_date = #{appointmentDate}
            </if>
            <if test="appointmentStartTime != null ">
                and ci.appointment_start_time = #{appointmentStartTime}
            </if>
            <if test="appointmentEndTime != null ">
                and ci.appointment_end_time = #{appointmentEndTime}
            </if>
        </where>
        ORDER BY ci.create_time DESC
    </select>

    <select id="selectConsultationInfoById" parameterType="Long" resultType="com.xinelu.manage.dto.consultationInfo.ConsultationInfoDTO">
        select id,
               patient_id,
               patient_name,
               card_no,
               phone,
               address,
               doctor_id,
               doctor_name,
               consultation_type,
               status,
               appointment_date,
               appointment_start_time,
               appointment_end_time,
               video_url,
               problem_description,
               situation_description,
               problem_statement,
               medical_record,
               schedule_plan_detail_id,
               del_flag,
               create_by,
               create_time,
               update_by,
               update_time
        from consultation_info
        where id = #{id}
    </select>

    <select id="getConsultationFiles" parameterType="Long" resultType="string">
        SELECT file_url  FROM consultation_file WHERE del_flag='0' AND consultation_id=#{id}
    </select>

    <insert id="insertConsultationInfo" parameterType="ConsultationInfo" useGeneratedKeys="true" keyProperty="id">
        insert into consultation_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,
            </if>
            <if test="patientId != null">patient_id,
            </if>
            <if test="patientName != null">patient_name,
            </if>
            <if test="cardNo != null">card_no,
            </if>
            <if test="phone != null">phone,
            </if>
            <if test="address != null">address,
            </if>
            <if test="doctorId != null">doctor_id,
            </if>
            <if test="doctorName != null">doctor_name,
            </if>
            <if test="consultationType != null">consultation_type,
            </if>
            <if test="status != null">status,
            </if>
            <if test="appointmentDate != null">appointment_date,
            </if>
            <if test="appointmentStartTime != null">appointment_start_time,
            </if>
            <if test="appointmentEndTime != null">appointment_end_time,
            </if>
            <if test="videoUrl != null">video_url,
            </if>
            <if test="problemDescription != null">problem_description,
            </if>
            <if test="situationDescription != null">situation_description,
            </if>
            <if test="problemStatement != null">problem_statement,
            </if>
            <if test="medicalRecord != null">medical_record,
            </if>
	        <if test="schedulePlanDetailId != null">schedule_plan_detail_id,
	        </if>
            <if test="delFlag != null">del_flag,
            </if>
            <if test="createBy != null">create_by,
            </if>
            <if test="createTime != null">create_time,
            </if>
            <if test="updateBy != null">update_by,
            </if>
            <if test="updateTime != null">update_time,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},
            </if>
            <if test="patientId != null">#{patientId},
            </if>
            <if test="patientName != null">#{patientName},
            </if>
            <if test="cardNo != null">#{cardNo},
            </if>
            <if test="phone != null">#{phone},
            </if>
            <if test="address != null">#{address},
            </if>
            <if test="doctorId != null">#{doctorId},
            </if>
            <if test="doctorName != null">#{doctorName},
            </if>
            <if test="consultationType != null">#{consultationType},
            </if>
            <if test="status != null">#{status},
            </if>
            <if test="appointmentDate != null">#{appointmentDate},
            </if>
            <if test="appointmentStartTime != null">#{appointmentStartTime},
            </if>
            <if test="appointmentEndTime != null">#{appointmentEndTime},
            </if>
            <if test="videoUrl != null">#{videoUrl},
            </if>
            <if test="problemDescription != null">#{problemDescription},
            </if>
            <if test="situationDescription != null">#{situationDescription},
            </if>
            <if test="problemStatement != null">#{problemStatement},
            </if>
            <if test="medicalRecord != null">#{medicalRecord},
            </if>
	        <if test="schedulePlanDetailId != null">#{schedulePlanDetailId},
	        </if>
            <if test="delFlag != null">#{delFlag},
            </if>
            <if test="createBy != null">#{createBy},
            </if>
            <if test="createTime != null">#{createTime},
            </if>
            <if test="updateBy != null">#{updateBy},
            </if>
            <if test="updateTime != null">#{updateTime},
            </if>
        </trim>
    </insert>

    <insert id="insertConsultationFile">
        insert into consultation_file(consultation_id, file_url) values
        <foreach item="item" index="index" collection="fileUrls" separator=",">
            (#{consultationId},#{item})
        </foreach>
    </insert>

    <update id="updateConsultationInfo" parameterType="ConsultationInfo">
        update consultation_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="patientId != null">patient_id =
                #{patientId},
            </if>
            <if test="patientName != null">patient_name =
                #{patientName},
            </if>
            <if test="cardNo != null">card_no =
                #{cardNo},
            </if>
            <if test="phone != null">phone =
                #{phone},
            </if>
            <if test="address != null">address =
                #{address},
            </if>
            <if test="doctorId != null">doctor_id =
                #{doctorId},
            </if>
            <if test="doctorName != null">doctor_name =
                #{doctorName},
            </if>
            <if test="consultationType != null">consultation_type =
                #{consultationType},
            </if>
            <if test="status != null">status =
                #{status},
            </if>
            <if test="appointmentDate != null">appointment_date =
                #{appointmentDate},
            </if>
            <if test="appointmentStartTime != null">appointment_start_time =
                #{appointmentStartTime},
            </if>
            <if test="appointmentEndTime != null">appointment_end_time =
                #{appointmentEndTime},
            </if>
            <if test="videoUrl != null">video_url =
                #{videoUrl},
            </if>
            <if test="problemDescription != null">problem_description =
                #{problemDescription},
            </if>
            <if test="situationDescription != null">situation_description =
                #{situationDescription},
            </if>
            <if test="problemStatement != null">problem_statement =
                #{problemStatement},
            </if>
            <if test="medicalRecord != null">medical_record =
                #{medicalRecord},
            </if>
	        <if test="schedulePlanDetailId != null">schedule_plan_detail_id =
                #{schedulePlanDetailId},
	        </if>
            <if test="delFlag != null">del_flag =
                #{delFlag},
            </if>
            <if test="createBy != null">create_by =
                #{createBy},
            </if>
            <if test="createTime != null">create_time =
                #{createTime},
            </if>
            <if test="updateBy != null">update_by =
                #{updateBy},
            </if>
            <if test="updateTime != null">update_time =
                #{updateTime},
            </if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteConsultationInfoById" parameterType="Long">
        delete
        from consultation_info
        where id = #{id}
    </delete>

    <delete id="deleteConsultationInfoByIds" parameterType="String">
        delete from consultation_info where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>
