| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- <?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.BuildStatisticsMapper">
- <!-- 按楼层聚合:
- 1)房间级子查询先算出每个房间的总床位数与三种状态的床位数;
- 2)外层再按楼层汇总,整间空房 = 房间内有床位且全部床位空闲。
- 说明:楼栋、楼层、房间、床位表均无 deleted 字段,故不做删除过滤。 -->
- <select id="selectFloorBedStatistics"
- resultType="cn.iocoder.yudao.module.system.dal.mysql.biz.bo.BuildFloorBedStatisticsBO">
- SELECT st.name AS tenantName,
- t.tenant_id AS tenantId,
- t.build_id AS buildId,
- t.build_name AS buildName,
- t.floor_id AS floorId,
- t.floor_name AS floorName,
- COUNT(t.room_id) AS totalRoomCount,
- COUNT(CASE WHEN t.bed_count > 0 AND t.bed_count = t.empty_bed_count THEN 1 END) AS emptyRoomCount,
- COALESCE(SUM(t.bed_count), 0) AS totalBedCount,
- COALESCE(SUM(t.occupied_bed_count), 0) AS occupiedBedCount,
- COALESCE(SUM(t.empty_bed_count), 0) AS emptyBedCount,
- COALESCE(SUM(t.temporary_bed_count), 0) AS temporaryOccupationCount
- FROM (
- SELECT bu.tenant_id AS tenant_id,
- bu.id AS build_id,
- bu.build_name AS build_name,
- f.id AS floor_id,
- f.floor_name AS floor_name,
- r.id AS room_id,
- COUNT(b.id) AS bed_count,
- COUNT(CASE WHEN b.status = 1 THEN 1 END) AS occupied_bed_count,
- COUNT(CASE WHEN b.status = 0 THEN 1 END) AS empty_bed_count,
- COUNT(CASE WHEN b.status = 2 THEN 1 END) AS temporary_bed_count
- FROM org_build bu
- INNER JOIN org_build_floor f ON f.build_id = bu.id
- LEFT JOIN org_build_room r ON r.floor_id = f.id
- LEFT JOIN org_build_bed b ON b.room_id = r.id
- WHERE bu.org_type = #{orgType}
- AND bu.tenant_id IN
- <foreach collection="tenantIds" item="tenantId" open="(" separator="," close=")">
- #{tenantId}
- </foreach>
- <if test="buildIds != null and buildIds.size() > 0">
- AND bu.id IN
- <foreach collection="buildIds" item="buildId" open="(" separator="," close=")">
- #{buildId}
- </foreach>
- </if>
- GROUP BY bu.tenant_id, bu.id, bu.build_name, f.id, f.floor_name, r.id
- ) t
- LEFT JOIN system_tenant st ON st.id = t.tenant_id
- GROUP BY t.tenant_id, t.build_id, t.build_name, t.floor_id, t.floor_name, st.name
- ORDER BY t.tenant_id, t.build_id, t.floor_id
- </select>
- </mapper>
|