<?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="cn.iocoder.yudao.module.system.dal.mysql.biz.ElderlyCheckInWaitMapper">

    <select id="selectCheckInWaitListPage" resultType="cn.iocoder.yudao.module.system.api.bpm.vo.CheckInWaitRecordRespVO">
        SELECT
            eciw.*,
            bbf.status AS bpmStatus,
            bbf.process_instance_id,
            bbf.type,
            st.name AS tenantName
        FROM elderly_check_in_wait eciw
        LEFT JOIN system_tenant st ON st.id = eciw.tenant_id
        LEFT JOIN bpm_business_form bbf ON eciw.id = bbf.business_id AND bbf.type = #{pageVO.type}
        WHERE 1 = 1
        <if test="pageVO.elderName != null and pageVO.elderName != ''">
            AND eciw.elder_name LIKE CONCAT('%', #{pageVO.elderName}, '%')
        </if>
        <if test="pageVO.bpmStatus != null">
            AND bbf.status = #{pageVO.bpmStatus}
        </if>
        <if test="pageVO.status != null">
            AND eciw.status = #{pageVO.status}
        </if>
        <if test="pageVO.tenantIds != null and pageVO.tenantIds.length > 0">
            AND eciw.tenant_id IN
            <foreach item="id" collection="pageVO.tenantIds" open="(" separator="," close=")">
                #{id}
            </foreach>
        </if>
        ORDER BY bbf.status DESC,eciw.create_time DESC
    </select>

</mapper>
