diff --git a/apps/server/src/rooms/rooms.service.ts b/apps/server/src/rooms/rooms.service.ts index 89cce35..239e805 100644 --- a/apps/server/src/rooms/rooms.service.ts +++ b/apps/server/src/rooms/rooms.service.ts @@ -17,6 +17,7 @@ export class RoomsService { @InjectRepository(Room) private repo: Repository, @InjectRepository(Occupancy) private occRepo: Repository, @InjectRepository(RoomExpense) private roomExpRepo: Repository, + private readonly scope: CampusScope, @InjectRepository(Bed) private bedRepo: Repository, @InjectRepository(Locker) private lockerRepo: Repository, ) {} @@ -377,8 +378,12 @@ export class RoomsService { const room = await this.repo.findOne({ where: { id: roomId } }); if (!room) throw new NotFoundException('宿舍不存在'); if (room.status === 'archived') throw new BadRequestException('已归档宿舍不能添加床位'); - const existing = await this.bedRepo.count({ where: { roomId } }); - const start = existing + 1; + const existing = await this.bedRepo.find({ where: { roomId }, order: { bedNumber: 'ASC' } }); + const numbers = existing.map(b => { + const match = b.bedNumber.match(/^\d+/); + return match ? parseInt(match[0]) : 0; + }); + const start = numbers.length > 0 ? Math.max(...numbers) + 1 : 1; const beds: Bed[] = []; for (let i = 0; i < dto.count; i++) { beds.push(this.bedRepo.create({ roomId, bedNumber: `${start + i}号床` })); @@ -435,8 +440,12 @@ export class RoomsService { const room = await this.repo.findOne({ where: { id: roomId } }); if (!room) throw new NotFoundException('宿舍不存在'); if (room.status === 'archived') throw new BadRequestException('已归档宿舍不能添加柜子'); - const existing = await this.lockerRepo.count({ where: { roomId } }); - const start = existing + 1; + const existing = await this.lockerRepo.find({ where: { roomId }, order: { lockerNumber: 'ASC' } }); + const numbers = existing.map(b => { + const match = b.lockerNumber.match(/^\d+/); + return match ? parseInt(match[0]) : 0; + }); + const start = numbers.length > 0 ? Math.max(...numbers) + 1 : 1; const lockers: Locker[] = []; for (let i = 0; i < dto.count; i++) { lockers.push(this.lockerRepo.create({ roomId, lockerNumber: `${start + i}号柜` }));