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

    <select id="selectPageWithElderInfo" resultType="cn.iocoder.yudao.module.system.controller.admin.biz.vo.elderlyitemsround.ElderlyItemsRoundPageRespVO">
        SELECT
        eir.elder_id,ei.elder_name AS elderName,eir.recorder,eir.role,eir.tenant_id
        FROM elderly_items_round eir
        LEFT JOIN elderly_info ei ON ei.id = eir.elder_id
        WHERE 1 = 1
        <if test="reqVO.tenantIds != null">
            AND eir.tenant_id IN
            <foreach item="id" collection="reqVO.tenantIds" open="(" separator="," close=")">
                #{id}
            </foreach>
        </if>
        <if test="reqVO.role != null and reqVO.role != ''">
            AND eir.role = #{reqVO.role}
        </if>
        <if test="reqVO.elderName != null and reqVO.elderName != ''">
            AND ei.elder_name LIKE CONCAT('%', #{reqVO.elderName}, '%')
        </if>
        <if test="reqVO.roundTime != null and reqVO.roundTime.length &gt;= 1">
            AND eir.round_time &gt;= #{reqVO.roundTime[0]}
        </if>
        <if test="reqVO.roundTime != null and reqVO.roundTime.length == 2">
            AND eir.round_time &lt; #{reqVO.roundTime[1]} + INTERVAL 1 DAY
        </if>
        GROUP BY eir.elder_id,ei.elder_name,eir.recorder,eir.role,eir.tenant_id
        ORDER BY eir.round_time DESC, eir.id DESC
    </select>

    <select id="selectByIdWithElderInfo" resultType="cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderlyItemsRoundDO">
        SELECT
            eir.*,
            ei.elder_name AS elderName,
            CONCAT(build.build_name,'-',floor.floor_name,'-',room.room_name) AS roomName
        FROM elderly_items_round eir
        LEFT JOIN elderly_info ei ON ei.id = eir.elder_id
        LEFT JOIN org_build build ON build.id = ei.build_id
        LEFT JOIN org_build_floor floor ON floor.id = ei.floor_id
        LEFT JOIN org_build_room room ON room.id = ei.room_id
        WHERE eir.id = #{id}
    </select>

    <select id="selectMonthDayMinStatus" resultType="java.util.HashMap">
        SELECT
            eir.elder_id AS elderId,
            DAY(eir.round_time) AS day,
            MIN(eir.status) AS minStatus
        FROM elderly_items_round eir
        INNER JOIN elderly_info ei ON ei.id = eir.elder_id AND ei.tenant_id = eir.tenant_id
        WHERE eir.tenant_id = #{tenantId}
          AND eir.round_time &gt;= #{startTime}
          AND eir.round_time &lt; #{endTime}
          AND ei.deleted = 0
          AND ei.in_status = 1
          AND ei.bed_id IS NOT NULL
        GROUP BY eir.elder_id, DAY(eir.round_time)
    </select>

</mapper>
