<?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.ruoyi.web.work.mapper.FollowRecordMapper">
    
    <select id="selectList" resultType="com.ruoyi.web.work.domain.FollowRecord">
        SELECT
        k.id,
        k.template_name,
        k.patient_name,
        k.state,
        k.create_time,
        k.create_by,
        d.dept_name AS deptName
        FROM
        tb_follow_record k
        LEFT JOIN sys_dept d ON d.dept_id = k.dept_id
        WHERE 1=1
            <if test="type != null "> and k.type = #{type}</if>
            <if test="patientName != null  and patientName != ''"> AND k.patient_name like concat('%', #{patientName}, '%')</if>
            <if test="patientId != null "> AND k.patient_id = #{patientId}</if>
            <if test="userId != null "> and k.user_id = #{userId}</if>
            <if test="state != null "> and k.state = #{state}</if>
            <if test="deptId != null "> and k.dept_id = #{deptId}</if>
            <if test="deptId != null and deptId != 0">AND (k.dept_id = #{deptId} OR k.dept_id IN ( SELECT t.dept_id FROM sys_dept t WHERE find_in_set(#{deptId}, ancestors) ))</if>
            <!-- 数据范围过滤 -->
            ${params.dataScope}
    </select>

    <select id="selectAppList" resultType="com.ruoyi.web.work.domain.vo.FollowRecordVoList">
        SELECT
        r.id,
        r.template_name,
        r.op,
        r.state,
        r.create_time,
        d.dept_name,
        u.nick_name AS doctorName
        FROM
        tb_follow_record r
        LEFT JOIN sys_dept d ON d.dept_id = r.dept_id
        LEFT JOIN sys_user u ON u.user_id = r.user_id
        WHERE r.patient_id = #{patientId}
            <if test="type != null "> AND r.type = #{type}</if>
            <if test="state != null "> AND r.state = #{state}</if>
        ORDER BY r.id DESC
    </select>

    <select id="selectVisitList" resultType="com.ruoyi.web.work.domain.vo.VisitVoList">
        SELECT
            v.pat_id id,
            v.hospital_status,
            v.admission_time,
            v.discharge_time,
            p.name,
            p.phone
        FROM
            tb_visit v
                LEFT JOIN tb_patient p ON p.id = v.pat_id
        WHERE
            v.doctor_code =#{doctorCode}
        <if test="name != null  and name != ''"> AND p.name like concat('%', #{name}, '%')</if>
        ORDER BY v.id DESC
    </select>

</mapper>