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

    <!--
        一般情况下，尽可能使用 Mapper 进行 CRUD 增删改查即可。
        无法满足的场景，例如说多表关联查询，才使用 XML 编写 SQL。
        代码生成器暂时只生成 Mapper XML 文件本身，更多推荐 MybatisX 快速开发插件来生成查询。
        文档可见：https://www.iocoder.cn/MyBatis/x-plugins/
     -->
    <select id="selectCurrentPresetCount" resultType="java.lang.Integer">
        SELECT
        COUNT(*)
        FROM
        org_build_bed obb
        INNER JOIN elderly_reservation_bed erb ON obb.id = erb.bed_id AND obb.tenant_id = erb.tenant_id
        WHERE
        obb.STATUS = 2
        AND erb.`status` IN (2,5)
        <if test="reqVO.currentDate != null">
            AND DATE_FORMAT(erb.reservation_check_in_time, '%Y-%m') = DATE_FORMAT(#{reqVO}, '%Y-%m')
        </if>
        <if test="reqVO.tenantIds != null">
            AND obb.tenant_id IN
            <foreach item="id" collection="reqVO.tenantIds" open="(" separator="," close=")">
                #{id}
            </foreach>
        </if>
    </select>

    <select id="selectBedIdByFullName" resultType="java.lang.Long">
        SELECT bed.id AS bed_id
        FROM org_build AS build
                 INNER JOIN org_build_floor AS floor ON build.id = floor.build_id AND build.tenant_id = floor.tenant_id
                 INNER JOIN org_build_room AS room ON floor.id = room.floor_id AND floor.tenant_id = room.tenant_id
                 INNER JOIN org_build_bed AS bed ON room.id = bed.room_id AND room.tenant_id = bed.tenant_id
        WHERE build.tenant_id = #{tenantId}
          AND build.build_name = #{buildName}
          AND floor.floor_name = #{floorName}
          AND room.room_name = #{roomName}
          AND bed.bed_name = #{bedName}
          AND build.org_type = 1
        LIMIT 1
    </select>

    <select id="selectWaitCheckInBedList" resultType="cn.iocoder.yudao.module.system.controller.admin.biz.vo.CheckInWaitBedSimpleRespVO">
        SELECT bed.id AS bedId,
               CONCAT(build.build_name, '-', floor.floor_name, '-', room.room_name, '-', bed.bed_name) AS bedName
        FROM org_build_bed AS bed
                 INNER JOIN org_build_room AS room ON bed.room_id = room.id AND bed.tenant_id = room.tenant_id
                 INNER JOIN org_build_floor AS floor ON room.floor_id = floor.id AND room.tenant_id = floor.tenant_id
                 INNER JOIN org_build AS build ON floor.build_id = build.id AND floor.tenant_id = build.tenant_id
        WHERE bed.tenant_id = #{tenantId}
          AND bed.status = 0
        <if test="keyword != null and keyword != ''">
            AND CONCAT(build.build_name, '-', floor.floor_name, '-', room.room_name, '-', bed.bed_name) LIKE CONCAT('%', #{keyword}, '%')
        </if>
        ORDER BY build.sort ASC, floor.sort ASC, room.id ASC, bed.sort ASC
    </select>

    <select id="selectBedVOList" resultType="cn.iocoder.yudao.module.system.controller.admin.biz.vo.BuildBedVO">
        SELECT build.build_name AS buildName,
               floor.floor_name AS buildFloorName,
               room.room_name AS buildRoomName,
               bed.id AS buildBedId,
               bed.bed_name AS buildBedName
        FROM org_build_bed AS bed
                 INNER JOIN org_build_room AS room ON bed.room_id = room.id AND bed.tenant_id = room.tenant_id
                 INNER JOIN org_build_floor AS floor ON room.floor_id = floor.id AND room.tenant_id = floor.tenant_id
                 INNER JOIN org_build AS build ON floor.build_id = build.id AND floor.tenant_id = build.tenant_id
        WHERE 1 = 1
        <if test="bedName != null and bedName != ''">
            AND bed.bed_name LIKE CONCAT('%', #{bedName}, '%')
        </if>
        <if test="status != null">
            AND bed.status = #{status}
        </if>
        <if test="orgType != null">
            AND bed.org_type = #{orgType}
        </if>
        <if test="tenantIds != null and tenantIds.length > 0">
            AND bed.tenant_id IN
            <foreach item="id" collection="tenantIds" open="(" separator="," close=")">
                #{id}
            </foreach>
        </if>
        ORDER BY build.sort ASC, floor.sort ASC, room.id ASC, bed.sort ASC
    </select>

</mapper>