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

    <select id="selectList" resultType="com.ruoyi.web.work.domain.Visit">
        select * from tb_visit v
        <where>
            v.hospital_status = '3'
            <if test="id != null "> and id = #{id}</if>
            <if test="patId != null "> and pat_id = #{patId}</if>
            <if test="cardId != null "> and card_id = #{cardId}</if>
            <if test="patClass != null  and patClass != ''"> and pat_class = #{patClass}</if>
            <if test="bed != null  and bed != ''"> and bed = #{bed}</if>
            <if test="department != null  and department != ''"> and department = #{department}</if>
            <if test="physician != null  and physician != ''"> and physician = #{physician}</if>
            <if test="nurseCode != null  and nurseCode != ''"> and nurse_code = #{nurseCode}</if>
            <if test="doctorCode != null  and doctorCode != ''"> and doctor_code = #{doctorCode}</if>
            <if test="hospitalizedNumber != null  and hospitalizedNumber != ''"> and hospitalized_number = #{hospitalizedNumber}</if>
            <if test="feeCategory != null  and feeCategory != ''"> and fee_category = #{feeCategory}</if>
            <if test="dischargeMethod != null  and dischargeMethod != ''"> and discharge_method = #{dischargeMethod}</if>
            <if test="medicalCode != null  and medicalCode != ''"> and medical_code = #{medicalCode}</if>
            <if test="hospitalStatus != null  and hospitalStatus != ''"> and hospital_status = #{hospitalStatus}</if>
            <if test="registrationStatus != null  and registrationStatus != ''"> and registration_status = #{registrationStatus}</if>
            <if test="admissionTime != null  and admissionTime != ''"> and admission_time = #{admissionTime}</if>
            <if test="dischargeTime != null  and dischargeTime != ''"> and discharge_time = #{dischargeTime}</if>
            <if test="level != null  and level != ''"> and level = #{level}</if>
            <if test="reasonArea != null  and reasonArea != ''"> and reasonArea = #{reasonArea}</if>
            <if test="wardCode != null  and wardCode != ''"> and ward_code = #{wardCode}</if>
            <if test="wardName != null  and wardName != ''"> and ward_name = #{wardName}</if>
            <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
                and admission_time BETWEEN #{startTime} AND #{endTime}
            </if>
            <if test="deptId != null and deptId != 0">AND (v.dept_id = #{deptId} OR v.dept_id IN ( SELECT t.dept_id FROM sys_dept t WHERE find_in_set(#{deptId}, parent_id) ))</if>
        </where>
        <!-- 数据范围过滤 -->
        ${params.dataScope}
    </select>


    <select id="getPatientCardByNameOrDepartmentOrPhysician" resultType="com.ruoyi.web.work.domain.dto.PatientCardDto">
        SELECT
            v.department,
            v.physician,
            tb_patient.`name`,
            v.discharge_method,
            v.discharge_time,
            v.id as visit_id,
            v.pat_id,
            v.card_id
        FROM
            tb_visit v
        LEFT JOIN tb_patient ON v.pat_id = tb_patient.id
        LEFT JOIN sys_dept d ON d.dept_id = v.dept_id
        <where>
            v.hospital_status = '3'
            <if test="patientName!='' and patientName!=null">
               AND tb_patient.`name` like concat('%', #{patientName}, '%')
            </if>
            <if test="department!='' and department!= null">
                AND v.department =#{department}
            </if>
            <if test="physician!='' and physician!= null">
                AND v.physician like concat('%', #{physician}, '%')
            </if>
            <if test="deptId !=null and deptId != 0">AND (v.dept_id = #{deptId} OR v.dept_id IN ( SELECT t.dept_id FROM sys_dept t WHERE find_in_set(#{deptId}, parent_id) ))</if>
        </where>
        <!-- 数据范围过滤 -->
        ${params.dataScope}
    </select>


    <select id="selectVisitsByCardId" resultType="com.ruoyi.web.work.domain.Visit">
        select * from tb_visit  where card_id=#{cardId}
                                        and tb_visit.hospital_status = '3'
    </select>

    <select id="selectVisitsByPatId" resultType="com.ruoyi.web.work.domain.Visit">
        select * from tb_visit where pat_id=#{patId}
        <if test="startTime != null and endTime != null">
            AND admission_time BETWEEN #{startTime} AND #{endTime}
        </if>
    </select>


    <resultMap id="visitResultMap" type="visit">
        <id property="id" column="id"/>
        <result property="patId" column="pat_id"/>
        <result property="cardId" column="card_id"/>
        <result property="patClass" column="pat_class"/>
        <result property="deptId" column="dept_id"/>
        <result property="department" column="department"/>
        <result property="bed" column="bed"/>
        <result property="doctorCode" column="doctor_code"/>
        <result property="physician" column="physician"/>
        <result property="hospitalStatus" column="hospital_status"/>
        <result property="admissionTime" column="admission_time"/>
        <collection  property="patients" javaType="java.util.List" resultMap="patientResultMap"/>
    </resultMap>

    <resultMap id="patientResultMap" type="patient">
        <id property="id" column="patient_id"/>
        <result property="wnPatId" column="wn_pat_id"/>
        <result property="name" column="name"/>
    </resultMap>


    <select id="selectVisitsByDeptId" resultMap="visitResultMap">
        SELECT
            p.id patient_id,p.wn_pat_id,p.name,
            v.id,v.pat_id,v.card_id,v.pat_class,v.dept_code,v.department,v.bed,v.doctor_code,v.physician,v.hospital_status,v.admission_time
        FROM
            tb_visit v
            LEFT JOIN sys_dept d ON v.dept_code = d.dept_code
            LEFT JOIN tb_patient p ON v.pat_id = p.id
        WHERE
            d.dept_id = #{deptId}
    </select>


    <select id="selectVisitsByIdAndUserName" resultMap="visitResultMap">
        SELECT
            p.id patient_id,p.wn_pat_id,p.name,
            v.id,v.pat_id,v.card_id,v.pat_class,v.dept_code,v.department,v.bed,v.doctor_code,v.physician,v.hospital_status,v.admission_time
        FROM
            sys_dept d
            LEFT JOIN sys_user u ON d.dept_id = u.dept_id
            LEFT JOIN tb_visit v ON u.user_name = v.doctor_code
            LEFT JOIN tb_patient p ON p.id = v.pat_id
        WHERE
            d.dept_id = #{deptId}
        AND u.user_name = #{userName}
    </select>
</mapper>
