<?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
        <where>
            <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>
        </where>
    </select>


    <select id="getPatientCardByNameOrDepartmentOrPhysician" resultType="com.ruoyi.web.work.domain.dto.PatientCardDto">
        SELECT
            tb_visit.department,
            tb_visit.physician,
            tb_patient.`name`,
            tb_visit.discharge_method,
            tb_visit.discharge_time,
            tb_visit.id as visit_id,
            tb_visit.pat_id,
            tb_visit.card_id
        FROM
            tb_visit
                JOIN
            tb_patient ON tb_visit.pat_id = tb_patient.id
        <where>
            <if test="patientName!='' and patientName!=null">
               AND tb_patient.`name` like concat('%', #{patientName}, '%')
            </if>
            <if test="departmentName!='' and departmentName!= null">
                AND tb_visit.department =#{departmentName}
            </if>
            <if test="physician!='' and physician!= null">
                AND tb_visit.physician like concat('%', #{physician}, '%')
            </if>
        </where>
    </select>


    <select id="getAllDepartment" resultType="com.ruoyi.web.work.domain.dto.PatientCardDto">
        select department from tb_visit GROUP BY department
    </select>


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

    <select id="selectVisitsByCardId" resultType="com.ruoyi.web.work.domain.Visit">
        select * from tb_visit where card_id=#{cardId}
    </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>

</mapper>
