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:
@@ -85,6 +85,42 @@ export class ClassesController {
|
||||
return result;
|
||||
}
|
||||
|
||||
/** 从钉钉同步部门创建班级 */
|
||||
@Post('from-department')
|
||||
@RequirePermission('class:create')
|
||||
async createFromDepartment(
|
||||
@Body() dto: { departmentId: number; name?: string; classType?: string },
|
||||
@Request() req: any,
|
||||
) {
|
||||
const result = await this.service.createFromDepartment(dto);
|
||||
await this.logService.log({
|
||||
userId: req.user?.id,
|
||||
username: req.user?.username,
|
||||
module: '班级管理',
|
||||
action: '从部门创建班级',
|
||||
targetId: result.id,
|
||||
targetType: 'class',
|
||||
detail: `班级${result.code} ${result.name}`,
|
||||
ipAddress: extractRequestInfo(req).ipAddress,
|
||||
userAgent: extractRequestInfo(req).userAgent,
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
/** 归档班级 */
|
||||
@Put(':id/archive')
|
||||
@RequirePermission('class:edit')
|
||||
async archive(@Param('id') id: string) {
|
||||
return this.service.archive(+id);
|
||||
}
|
||||
|
||||
/** 取消归档 */
|
||||
@Put(':id/restore')
|
||||
@RequirePermission('class:edit')
|
||||
async restore(@Param('id') id: string) {
|
||||
return this.service.restore(+id);
|
||||
}
|
||||
|
||||
@Put(':id')
|
||||
@RequirePermission('class:edit')
|
||||
async update(
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { CommonModule } from '../common/common.module';
|
||||
import { Class, ClassStudent, ClassTeacher, ClassSchedule, AttendanceRecord } from '../entities';
|
||||
import { Class, ClassStudent, ClassTeacher, ClassSchedule, AttendanceRecord, Department } from '../entities';
|
||||
import { ClassesService } from './classes.service';
|
||||
import { ClassesController } from './classes.controller';
|
||||
import { OperationLogsModule } from '../operation-logs/operation-logs.module';
|
||||
import { NotificationsModule } from '../notifications/notifications.module';
|
||||
|
||||
@Module({
|
||||
imports: [TypeOrmModule.forFeature([Class, ClassStudent, ClassTeacher, ClassSchedule, AttendanceRecord]), OperationLogsModule, NotificationsModule, CommonModule],
|
||||
imports: [TypeOrmModule.forFeature([Class, ClassStudent, ClassTeacher, ClassSchedule, AttendanceRecord, Department]), OperationLogsModule, NotificationsModule, CommonModule],
|
||||
controllers: [ClassesController],
|
||||
providers: [ClassesService],
|
||||
exports: [ClassesService],
|
||||
|
||||
@@ -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 },
|
||||
|
||||
@@ -98,6 +98,10 @@ export class QueryClassDto {
|
||||
|
||||
@IsOptional() @IsString()
|
||||
keyword?: string;
|
||||
|
||||
@IsOptional()
|
||||
@Type(() => Boolean)
|
||||
isArchived?: boolean;
|
||||
}
|
||||
|
||||
export class AddStudentsDto {
|
||||
|
||||
@@ -35,6 +35,13 @@ export class DepartmentsController {
|
||||
return this.departmentsService.findTree();
|
||||
}
|
||||
|
||||
/** 获取钉钉同步的部门树,供"从部门创建班级"使用 */
|
||||
@Get('synced')
|
||||
@RequirePermission('department:view')
|
||||
findSyncedTree() {
|
||||
return this.departmentsService.findSyncedTree();
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
@RequirePermission('department:view')
|
||||
findOne(@Param('id', ParseIntPipe) id: number) {
|
||||
|
||||
@@ -52,6 +52,35 @@ export class DepartmentsService {
|
||||
return roots;
|
||||
}
|
||||
|
||||
/** 获取钉钉同步的部门树(只返回 source='dingtalk' 的部门) */
|
||||
async findSyncedTree(): Promise<Department[]> {
|
||||
const all = await this.deptRepo.find({
|
||||
where: { status: 'active', source: 'dingtalk' },
|
||||
order: { sortOrder: 'ASC', name: 'ASC' },
|
||||
});
|
||||
|
||||
const byParent = new Map<number | null, Department[]>();
|
||||
for (const dept of all) {
|
||||
const key = dept.parentId ?? null;
|
||||
const list = byParent.get(key);
|
||||
if (list) {
|
||||
list.push(dept);
|
||||
} else {
|
||||
byParent.set(key, [dept]);
|
||||
}
|
||||
}
|
||||
|
||||
const attachChildren = (dept: Department): void => {
|
||||
const children = byParent.get(dept.id) ?? [];
|
||||
dept.children = children;
|
||||
for (const child of children) attachChildren(child);
|
||||
};
|
||||
|
||||
const roots = byParent.get(null) ?? [];
|
||||
for (const root of roots) attachChildren(root);
|
||||
return roots;
|
||||
}
|
||||
|
||||
async findOne(id: number): Promise<Department> {
|
||||
const dept = await this.deptRepo.findOne({ where: { id } });
|
||||
if (!dept) throw new NotFoundException('部门不存在');
|
||||
|
||||
@@ -64,6 +64,9 @@ export class Class {
|
||||
@Column({ name: 'notes', type: 'text', nullable: true })
|
||||
notes: string;
|
||||
|
||||
|
||||
@Column({ name: 'is_archived', default: false })
|
||||
isArchived: boolean;
|
||||
@CreateDateColumn({ name: 'created_at' })
|
||||
createdAt: Date;
|
||||
|
||||
|
||||
@@ -35,6 +35,9 @@ export class User {
|
||||
@Column({ name: 'last_login_at', type: 'datetime', nullable: true })
|
||||
lastLoginAt: Date;
|
||||
|
||||
@Column({ name: 'is_archived', default: false })
|
||||
isArchived: boolean;
|
||||
|
||||
@CreateDateColumn({ name: 'created_at' })
|
||||
createdAt: Date;
|
||||
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { Department, User, Student, UserDingMapping } from '../entities';
|
||||
import { Department, User, Student, UserDingMapping, Class } from '../entities';
|
||||
import { DingTalkService } from './dingtalk.service';
|
||||
import { WeComService } from './wecom.service';
|
||||
|
||||
@Module({
|
||||
imports: [TypeOrmModule.forFeature([Department, User, Student, UserDingMapping])],
|
||||
imports: [TypeOrmModule.forFeature([Department, User, Student, UserDingMapping, Class])],
|
||||
providers: [DingTalkService, WeComService],
|
||||
exports: [DingTalkService, WeComService],
|
||||
})
|
||||
|
||||
@@ -343,6 +343,9 @@ export class OccupanciesService {
|
||||
if (remaining === 0) {
|
||||
await runner.manager.update(Room, occ.roomId, { gender: null as any });
|
||||
}
|
||||
// 释放床位/柜子
|
||||
if (occ.bedId) await runner.manager.update(Bed, occ.bedId, { status: 'available' });
|
||||
if (occ.lockerId) await runner.manager.update(Locker, occ.lockerId, { status: 'available' });
|
||||
success++;
|
||||
}
|
||||
await runner.commitTransaction();
|
||||
|
||||
@@ -93,6 +93,7 @@ export class SyncController {
|
||||
return { success: true, data: status };
|
||||
}
|
||||
|
||||
|
||||
private parseRootDeptId(rootDeptId: string): number {
|
||||
const parsed = parseInt(rootDeptId, 10);
|
||||
if (isNaN(parsed)) {
|
||||
|
||||
Reference in New Issue
Block a user