feat: add bed occupancy stats to RoomVisual cards and API

This commit is contained in:
2026-07-09 12:18:30 +08:00
parent 0d82c96ec6
commit 74e6171e17
2 changed files with 24 additions and 14 deletions

View File

@@ -235,25 +235,30 @@ export class RoomsService {
? rooms.filter((r) => r.status !== 'archived' || (occMap.get(r.id)?.length ?? 0) > 0)
: rooms;
// 批量获取床位统计
const allBeds = await this.bedRepo.find({
where: { roomId: In(visibleRooms.map((r) => r.id)) },
});
const bedMap = new Map<number, { total: number; occupied: number }>();
for (const bed of allBeds) {
if (!bedMap.has(bed.roomId)) bedMap.set(bed.roomId, { total: 0, occupied: 0 });
const entry = bedMap.get(bed.roomId)!;
entry.total++;
if (bed.status === 'occupied') entry.occupied++;
}
return {
buildings,
rooms: visibleRooms.map((room) => {
const occ = occMap.get(room.id) || [];
// 计算机构标注
const orgs = [...new Set(occ.map((o: any) => o.organization).filter(Boolean))];
let orgLabel: string | null = null;
if (orgs.length > 0 && occ.length > 0) {
const allSameOrg = occ.every((o: any) => o.organization && o.organization === orgs[0]);
if (allSameOrg) {
orgLabel = `均为${orgs[0]}人员`;
} else {
orgLabel = `存在${orgs.join('、')}人员`;
}
orgLabel = allSameOrg ? `均为${orgs[0]}人员` : `存在${orgs.join('、')}人员`;
}
// 计算租户颜色:所有住户同一租户则使用该颜色
const tenantColors = [...new Set(occ.map((o: any) => o.tenantColor).filter(Boolean))];
const tenantColor: string | null = tenantColors.length === 1 ? tenantColors[0] : null;
// 房间涉及的租户 id供前端按租赁方筛选
const tenantIds = [...new Set(occ.map((o: any) => o.tenantId).filter(Boolean))];
return {
id: room.id,
@@ -263,6 +268,8 @@ export class RoomsService {
capacity: room.capacity,
status: room.status,
currentCount: occ.length,
totalBeds: bedMap.get(room.id)?.total ?? 0,
occupiedBeds: bedMap.get(room.id)?.occupied ?? 0,
occupants: occ,
orgLabel,
tenantColor,