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

    <select id="selectPageWithElderInfo"
            resultType="cn.iocoder.yudao.module.system.controller.admin.nursing.vo.ElderlySpecialNursingLogSimpleRespVO">
        SELECT
            ei.id as elderId,
            ei.elder_name AS elderName,
            ei.bed_id AS bedId,
            ei.nurse_level_name AS nurseLevelName,
            MAX(log.finish_time) AS finishTime
        FROM elderly_special_nursing_log log
        LEFT JOIN elderly_info ei
            ON ei.id = log.elder_id
            AND ei.tenant_id = log.tenant_id
        WHERE 1 = 1
            <if test="pageVO.tenantIds != null and pageVO.tenantIds.length > 0">
                AND log.tenant_id IN
                <foreach collection="pageVO.tenantIds" item="tenantId" open="(" separator="," close=")">
                    #{tenantId}
                </foreach>
            </if>
            <if test="pageVO.elderId != null">
                AND log.elder_id = #{pageVO.elderId}
            </if>
            <if test="pageVO.elderName != null and pageVO.elderName != ''">
                AND ei.elder_name LIKE CONCAT('%', #{pageVO.elderName}, '%')
            </if>
        GROUP BY log.tenant_id, log.elder_id, ei.elder_name, ei.bed_id, ei.nurse_level_name
        ORDER BY MAX(log.finish_time) DESC
    </select>

    <select id="selectListByElderIdAndFinishTimeWithItemName"
            resultType="cn.iocoder.yudao.module.system.controller.admin.nursing.vo.ElderlySpecialNursingLogRespVO">
        SELECT
            log.id,
            log.special_plan_item_id AS specialPlanItemId,
            item.nurse_item_name AS specialPlanItemName,
            log.elder_id AS elderId,
            log.finish_time AS finishTime,
            log.remark,
            log.image_url AS imageUrl,
            log.status,
            log.tenant_id AS tenantId
        FROM elderly_special_nursing_log log
        LEFT JOIN elderly_special_nursing_plan_item item
            ON item.id = log.special_plan_item_id
            AND item.tenant_id = log.tenant_id
        WHERE log.elder_id = #{elderId}
            <if test="tenantId != null">
                AND log.tenant_id = #{tenantId}
            </if>
            <if test="begin != null">
                AND log.finish_time &gt;= #{begin}
            </if>
            <if test="endExclusive != null">
                AND log.finish_time &lt; #{endExclusive}
            </if>
        ORDER BY log.finish_time DESC, log.id DESC
    </select>

</mapper>
