BuildStatisticsMapper.xml 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3. <mapper namespace="cn.iocoder.yudao.module.system.dal.mysql.biz.BuildStatisticsMapper">
  4. <!-- 按楼层聚合:
  5. 1)房间级子查询先算出每个房间的总床位数与三种状态的床位数;
  6. 2)外层再按楼层汇总,整间空房 = 房间内有床位且全部床位空闲。
  7. 说明:楼栋、楼层、房间、床位表均无 deleted 字段,故不做删除过滤。 -->
  8. <select id="selectFloorBedStatistics"
  9. resultType="cn.iocoder.yudao.module.system.dal.mysql.biz.bo.BuildFloorBedStatisticsBO">
  10. SELECT st.name AS tenantName,
  11. t.tenant_id AS tenantId,
  12. t.build_id AS buildId,
  13. t.build_name AS buildName,
  14. t.floor_id AS floorId,
  15. t.floor_name AS floorName,
  16. COUNT(t.room_id) AS totalRoomCount,
  17. COUNT(CASE WHEN t.bed_count &gt; 0 AND t.bed_count = t.empty_bed_count THEN 1 END) AS emptyRoomCount,
  18. COALESCE(SUM(t.bed_count), 0) AS totalBedCount,
  19. COALESCE(SUM(t.occupied_bed_count), 0) AS occupiedBedCount,
  20. COALESCE(SUM(t.empty_bed_count), 0) AS emptyBedCount,
  21. COALESCE(SUM(t.temporary_bed_count), 0) AS temporaryOccupationCount
  22. FROM (
  23. SELECT bu.tenant_id AS tenant_id,
  24. bu.id AS build_id,
  25. bu.build_name AS build_name,
  26. f.id AS floor_id,
  27. f.floor_name AS floor_name,
  28. r.id AS room_id,
  29. COUNT(b.id) AS bed_count,
  30. COUNT(CASE WHEN b.status = 1 THEN 1 END) AS occupied_bed_count,
  31. COUNT(CASE WHEN b.status = 0 THEN 1 END) AS empty_bed_count,
  32. COUNT(CASE WHEN b.status = 2 THEN 1 END) AS temporary_bed_count
  33. FROM org_build bu
  34. INNER JOIN org_build_floor f ON f.build_id = bu.id
  35. LEFT JOIN org_build_room r ON r.floor_id = f.id
  36. LEFT JOIN org_build_bed b ON b.room_id = r.id
  37. WHERE bu.org_type = #{orgType}
  38. AND bu.tenant_id IN
  39. <foreach collection="tenantIds" item="tenantId" open="(" separator="," close=")">
  40. #{tenantId}
  41. </foreach>
  42. <if test="buildIds != null and buildIds.size() > 0">
  43. AND bu.id IN
  44. <foreach collection="buildIds" item="buildId" open="(" separator="," close=")">
  45. #{buildId}
  46. </foreach>
  47. </if>
  48. GROUP BY bu.tenant_id, bu.id, bu.build_name, f.id, f.floor_name, r.id
  49. ) t
  50. LEFT JOIN system_tenant st ON st.id = t.tenant_id
  51. GROUP BY t.tenant_id, t.build_id, t.build_name, t.floor_id, t.floor_name, st.name
  52. ORDER BY t.tenant_id, t.build_id, t.floor_id
  53. </select>
  54. </mapper>