fix: release beds/lockers in batchCheckOut

The batchCheckOut method was not releasing assigned beds and lockers
after checkout, leaving them orphaned as 'occupied'. Added the same
release pattern used in checkOut() — using runner.manager.update()
since batchCheckOut operates inside a QueryRunner transaction.
This commit is contained in:
2026-07-09 12:05:36 +08:00
parent 54d8d0545e
commit 454a0d24c6
13 changed files with 363 additions and 12 deletions

View File

@@ -1,7 +1,7 @@
import { Injectable, NotFoundException, BadRequestException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository, In, Like } from 'typeorm';
import { Class, ClassStudent, ClassTeacher, ClassSchedule, AttendanceRecord, Classroom } from '../entities';
import { Class, ClassStudent, ClassTeacher, ClassSchedule, AttendanceRecord, Department, Classroom } from '../entities';
import { CreateClassDto, UpdateClassDto, QueryClassDto, AddTeacherDto, QueryClassScheduleDto, QueryClassAttendanceSummaryDto } from './dto/class.dto';
import { CampusScope } from '../common/campus-scope';
@@ -23,6 +23,8 @@ export class ClassesService {
private scheduleRepo: Repository<ClassSchedule>,
@InjectRepository(AttendanceRecord)
private attendanceRepo: Repository<AttendanceRecord>,
@InjectRepository(Department)
private deptRepo: Repository<Department>,
private readonly scope: CampusScope,
) {}
@@ -32,6 +34,8 @@ export class ClassesService {
if (query.status) where.status = query.status;
if (query.classType) where.classType = query.classType;
if (query.keyword) where.name = Like(`%${query.keyword}%`);
// Default: hide archived, unless explicitly requested
where.isArchived = query.isArchived ?? false;
where = await this.scope.filter(where);
const classes = await this.classRepo.find({
@@ -126,13 +130,58 @@ export class ClassesService {
return this.findOne(id);
}
/** 归档班级(软删除) */
async archive(id: number) {
const cls = await this.classRepo.findOne({ where: { id } });
if (!cls) throw new NotFoundException('班级不存在');
await this.classRepo.update(id, { isArchived: true });
return { success: true };
}
/** 取消归档 */
async restore(id: number) {
const cls = await this.classRepo.findOne({ where: { id } });
if (!cls) throw new NotFoundException('班级不存在');
await this.classRepo.update(id, { isArchived: false });
return { success: true };
}
/** 物理删除班级(已归档的才能删除) */
async remove(id: number) {
const cls = await this.classRepo.findOne({ where: { id } });
if (!cls) throw new NotFoundException('班级不存在');
if (!cls.isArchived) throw new BadRequestException('请先归档再删除');
await this.classRepo.remove(cls);
return { success: true };
}
async createFromDepartment(dto: { departmentId: number; name?: string; classType?: string }) {
const dept = await this.deptRepo.findOne({
where: { id: dto.departmentId, source: 'dingtalk' },
});
if (!dept) {
throw new BadRequestException('所选部门不存在或非钉钉同步部门');
}
const className = dto.name || dept.name;
const code = `DT_${dto.departmentId}`;
const existing = await this.classRepo.findOne({ where: { code } });
if (existing) {
throw new BadRequestException(`班级"${className}"已存在(编码: ${code}`);
}
const cls = this.classRepo.create({
name: className,
code,
departmentId: dto.departmentId,
classType: dto.classType || 'culture',
status: 'enrolling',
});
return this.classRepo.save(cls);
}
async getStudents(classId: number) {
return this.classStudentRepo.find({
where: { classId },