fix: 添加默认床位生成逻辑并优化床位添加验证
This commit is contained in:
@@ -110,7 +110,9 @@ export class RoomsService {
|
||||
roomType: dto.roomType ?? parsed.roomType,
|
||||
capacity: dto.capacity ?? parsed.capacity,
|
||||
});
|
||||
return this.repo.save(entity);
|
||||
const room = await this.repo.save(entity);
|
||||
await this.createDefaultBeds(room.id, room.capacity);
|
||||
return room;
|
||||
}
|
||||
|
||||
async update(id: number, dto: UpdateRoomDto) {
|
||||
@@ -312,7 +314,7 @@ export class RoomsService {
|
||||
}
|
||||
// 智能解析房间号
|
||||
const parsed = RoomsService.parseRoomNumber(row.roomNumber.trim());
|
||||
await this.repo.save(
|
||||
const room = await this.repo.save(
|
||||
this.repo.create({
|
||||
roomNumber: row.roomNumber.trim(),
|
||||
building: row.building?.trim() || parsed.building || undefined,
|
||||
@@ -323,6 +325,7 @@ export class RoomsService {
|
||||
monthlyRate: row.monthlyRate ?? undefined,
|
||||
}),
|
||||
);
|
||||
await this.createDefaultBeds(room.id, room.capacity);
|
||||
imported++;
|
||||
}
|
||||
return {
|
||||
@@ -353,6 +356,7 @@ export class RoomsService {
|
||||
const room = await this.repo.findOne({ where: { id: roomId } });
|
||||
if (!room) throw new NotFoundException('宿舍不存在');
|
||||
if (room.status === 'archived') throw new BadRequestException('已归档宿舍不能添加床位');
|
||||
await this.assertCanAddBeds(room, 1);
|
||||
const existing = await this.bedRepo.findOne({ where: { roomId, bedNumber: dto.bedNumber } });
|
||||
if (existing) throw new BadRequestException('该床位编号已存在');
|
||||
const bed = this.bedRepo.create({ ...dto, roomId });
|
||||
@@ -387,6 +391,7 @@ export class RoomsService {
|
||||
if (!room) throw new NotFoundException('宿舍不存在');
|
||||
if (room.status === 'archived') throw new BadRequestException('已归档宿舍不能添加床位');
|
||||
const existing = await this.bedRepo.find({ where: { roomId }, order: { bedNumber: 'ASC' } });
|
||||
this.assertCanAddBedsFromCount(room, existing.length, dto.count);
|
||||
const numbers = existing.map((b) => {
|
||||
const match = b.bedNumber.match(/^\d+/);
|
||||
return match ? parseInt(match[0]) : 0;
|
||||
@@ -399,6 +404,27 @@ export class RoomsService {
|
||||
return this.bedRepo.save(beds);
|
||||
}
|
||||
|
||||
private async createDefaultBeds(roomId: number, capacity: number): Promise<void> {
|
||||
const count = Math.max(capacity ?? 0, 0);
|
||||
if (count === 0) return;
|
||||
const beds = Array.from({ length: count }, (_, index) =>
|
||||
this.bedRepo.create({ roomId, bedNumber: `${index + 1}号床` }),
|
||||
);
|
||||
await this.bedRepo.save(beds);
|
||||
}
|
||||
|
||||
private async assertCanAddBeds(room: Room, count: number): Promise<void> {
|
||||
const existingCount = await this.bedRepo.count({ where: { roomId: room.id } });
|
||||
this.assertCanAddBedsFromCount(room, existingCount, count);
|
||||
}
|
||||
|
||||
private assertCanAddBedsFromCount(room: Room, existingCount: number, count: number): void {
|
||||
const remaining = Math.max((room.capacity ?? 0) - existingCount, 0);
|
||||
if (count > remaining) {
|
||||
throw new BadRequestException(`床位不能超过额定人数,当前已有 ${existingCount} 张,额定 ${room.capacity} 张,最多还能添加 ${remaining} 张`);
|
||||
}
|
||||
}
|
||||
|
||||
// ── 柜子管理 ──
|
||||
|
||||
async getRoomLockers(roomId: number): Promise<Locker[]> {
|
||||
|
||||
Reference in New Issue
Block a user