forked from wangziqi/gongxue-base
refactor: remove departmentId from Class entity
This commit is contained in:
@@ -86,27 +86,6 @@ 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;
|
||||
}
|
||||
|
||||
/** 批量导入学生到班级(通过钉钉用户ID) */
|
||||
@Post(':id/students/import')
|
||||
|
||||
@@ -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, Department, Classroom, Student, StudentDingMapping } from '../entities';
|
||||
import { Class, ClassStudent, ClassTeacher, ClassSchedule, AttendanceRecord, Classroom, Student, StudentDingMapping } from '../entities';
|
||||
import { CreateClassDto, UpdateClassDto, QueryClassDto, AddTeacherDto, QueryClassScheduleDto, QueryClassAttendanceSummaryDto, BatchImportStudentsDto } from './dto/class.dto';
|
||||
|
||||
interface RawStudentCount {
|
||||
@@ -22,8 +22,6 @@ export class ClassesService {
|
||||
private scheduleRepo: Repository<ClassSchedule>,
|
||||
@InjectRepository(AttendanceRecord)
|
||||
private attendanceRepo: Repository<AttendanceRecord>,
|
||||
@InjectRepository(Department)
|
||||
private deptRepo: Repository<Department>,
|
||||
@InjectRepository(Student)
|
||||
private studentRepo: Repository<Student>,
|
||||
@InjectRepository(StudentDingMapping)
|
||||
@@ -32,7 +30,6 @@ export class ClassesService {
|
||||
|
||||
async findAll(query: QueryClassDto) {
|
||||
let where: Record<string, unknown> = {};
|
||||
if (query.departmentId) where.departmentId = query.departmentId;
|
||||
if (query.status) where.status = query.status;
|
||||
if (query.classType) where.classType = query.classType;
|
||||
if (query.keyword) where.name = Like(`%${query.keyword}%`);
|
||||
@@ -99,21 +96,6 @@ export class ClassesService {
|
||||
async create(dto: CreateClassDto) {
|
||||
const { studentIds, teachers, dingUserIds, ...classData } = dto;
|
||||
|
||||
// Resolve departmentId: frontend may pass DingTalk dept ID, map to local
|
||||
if (classData.departmentId) {
|
||||
const localDept = await this.deptRepo.findOne({
|
||||
where: { source: 'dingtalk', sourceId: String(classData.departmentId) },
|
||||
});
|
||||
if (localDept) {
|
||||
classData.departmentId = localDept.id;
|
||||
} else {
|
||||
// Verify it's a valid local department ID
|
||||
const exists = await this.deptRepo.findOne({ where: { id: classData.departmentId } });
|
||||
if (!exists) {
|
||||
throw new BadRequestException(`部门 ID ${classData.departmentId} 不存在`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const cls = this.classRepo.create(classData);
|
||||
const saved = await this.classRepo.save(cls);
|
||||
@@ -165,7 +147,7 @@ export class ClassesService {
|
||||
this.studentRepo.create({
|
||||
name: `dd_${dingUserId}`,
|
||||
status: 'active',
|
||||
departmentId: classEntity.departmentId ?? undefined,
|
||||
departmentId: undefined,
|
||||
})
|
||||
);
|
||||
const savedStudents = await this.studentRepo.save(newStudents);
|
||||
@@ -240,32 +222,6 @@ export class ClassesService {
|
||||
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({
|
||||
|
||||
@@ -9,8 +9,6 @@ export class CreateClassDto {
|
||||
@IsString() @IsNotEmpty()
|
||||
code: string;
|
||||
|
||||
@IsOptional() @IsInt()
|
||||
departmentId?: number;
|
||||
|
||||
@IsEnum(ClassType) @IsString() @IsNotEmpty()
|
||||
classType: string;
|
||||
@@ -58,8 +56,6 @@ export class UpdateClassDto {
|
||||
@IsOptional() @IsString()
|
||||
code?: string;
|
||||
|
||||
@IsOptional() @IsInt()
|
||||
departmentId?: number;
|
||||
|
||||
@IsEnum(ClassType) @IsOptional() @IsString()
|
||||
classType?: string;
|
||||
@@ -90,10 +86,6 @@ export class UpdateClassDto {
|
||||
}
|
||||
|
||||
export class QueryClassDto {
|
||||
@IsOptional()
|
||||
@Type(() => Number)
|
||||
@IsInt()
|
||||
departmentId?: number;
|
||||
|
||||
@IsOptional() @IsString()
|
||||
status?: string;
|
||||
|
||||
Reference in New Issue
Block a user