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

    <select id="selectPageWithElderInfo" resultType="cn.iocoder.yudao.module.system.controller.admin.biz.vo.elderlybloodsugarrecord.ElderlyBloodSugarRecordRespVO">
        select ebsr.*, ei.elder_name as elder_name, ei.elder_sex as elder_sex,
               br.room_name as room_name, bb.bed_name as bed_name
        from elderly_blood_sugar_record ebsr
        left join elderly_info ei on ei.id = ebsr.elder_id
        left join org_build_bed bb on bb.id = ei.bed_id
        left join org_build_room br on br.id = bb.room_id
        where 1 = 1
        <if test="params.tenantId != null">
            and ebsr.tenant_id = #{params.tenantId}
        </if>
        <if test="params.elderId != null">
            and ebsr.elder_id = #{params.elderId}
        </if>
        <if test="params.elderName != null and params.elderName != ''">
            and ei.elder_name like concat('%', #{params.elderName}, '%')
        </if>
        <if test="params.recordDate != null">
            and ebsr.record_date between #{params.recordDate[0]} and #{params.recordDate[1]}
        </if>
        <if test="params.tenantIds != null">
            and ebsr.tenant_id in
            <foreach collection="params.tenantIds" item="tenantId" open="(" separator="," close=")">
                #{tenantId}
            </foreach>
        </if>
        order by ebsr.record_date desc, ebsr.id desc
    </select>

    <select id="selectListWithElderInfo" resultType="cn.iocoder.yudao.module.system.controller.admin.biz.vo.elderlybloodsugarrecord.ElderlyBloodSugarRecordRespVO">
        select ebsr.id, ebsr.elder_id, ebsr.record_date, ebsr.blood_sugar, ebsr.remark, ebsr.tenant_id,
               ei.elder_name as elderName, ei.elder_sex as elderSex,
               br.room_name as roomName, bb.bed_name as bedName
        from elderly_blood_sugar_record ebsr
        left join elderly_info ei on ei.id = ebsr.elder_id
        left join org_build_bed bb on bb.id = ei.bed_id
        left join org_build_room br on br.id = bb.room_id
        where 1 = 1
        <if test="params.tenantId != null">
            and ebsr.tenant_id = #{params.tenantId}
        </if>
        <if test="params.elderId != null">
            and ebsr.elder_id = #{params.elderId}
        </if>
        <if test="params.elderName != null and params.elderName != ''">
            and ei.elder_name like concat('%', #{params.elderName}, '%')
        </if>
        <if test="params.recordDate != null">
            and ebsr.record_date between #{params.recordDate[0]} and #{params.recordDate[1]}
        </if>
        <if test="params.tenantIds != null">
            and ebsr.tenant_id in
            <foreach collection="params.tenantIds" item="tenantId" open="(" separator="," close=")">
                #{tenantId}
            </foreach>
        </if>
        order by ebsr.record_date desc, ebsr.id desc
    </select>
</mapper>

