90 lines
2.3 KiB
TypeScript
Executable File
90 lines
2.3 KiB
TypeScript
Executable File
import { Injectable } from '@nestjs/common';
|
|
import {
|
|
db,
|
|
Prisma,
|
|
UserProfile,
|
|
VisitType,
|
|
Post,
|
|
PostType,
|
|
RolePerms,
|
|
ResPerm,
|
|
ObjectType,
|
|
CourseMethodSchema,
|
|
} from '@nice/common';
|
|
import { MessageService } from '../message/message.service';
|
|
import { BaseService } from '../base/base.service';
|
|
import { DepartmentService } from '../department/department.service';
|
|
import EventBus, { CrudOperation } from '@server/utils/event-bus';
|
|
import { BaseTreeService } from '../base/base.tree.service';
|
|
import { z } from 'zod';
|
|
@Injectable()
|
|
export class CourseService extends BaseTreeService<Prisma.CourseDelegate> {
|
|
constructor(
|
|
private readonly messageService: MessageService,
|
|
private readonly departmentService: DepartmentService,
|
|
) {
|
|
super(db, ObjectType.COURSE, 'courseAncestry', true);
|
|
}
|
|
async create(
|
|
args: Prisma.CourseCreateArgs,
|
|
params?: { staff?: UserProfile; tx?: Prisma.TransactionClient },
|
|
) {
|
|
const result = await super.create(args);
|
|
EventBus.emit('dataChanged', {
|
|
type: ObjectType.COURSE,
|
|
operation: CrudOperation.CREATED,
|
|
data: result,
|
|
});
|
|
return result;
|
|
}
|
|
async update(args: Prisma.CourseUpdateArgs, staff?: UserProfile) {
|
|
const result = await super.update(args);
|
|
EventBus.emit('dataChanged', {
|
|
type: ObjectType.COURSE,
|
|
operation: CrudOperation.UPDATED,
|
|
data: result,
|
|
});
|
|
return result;
|
|
}
|
|
/**
|
|
* 查找学生并包含成绩信息
|
|
*/
|
|
async findWithScores(courseId: number) {
|
|
return await db.course.findUnique({
|
|
where: { id: courseId },
|
|
include: {
|
|
scores: {
|
|
include: {
|
|
course: true,
|
|
student: true,
|
|
}
|
|
}
|
|
}
|
|
});
|
|
}
|
|
/**
|
|
* 搜索学生
|
|
*/
|
|
async search(params: { name?: string, courseId?: number, limit?: number }) {
|
|
const { name, courseId, limit = 30 } = params;
|
|
|
|
return await db.course.findMany({
|
|
where: {
|
|
...(name && { name: { contains: name } }),
|
|
...(courseId && { courseId })
|
|
},
|
|
include: {
|
|
scores: true
|
|
},
|
|
take: limit
|
|
});
|
|
}
|
|
private emitDataChangedEvent(data: any, operation: CrudOperation) {
|
|
EventBus.emit("dataChanged", {
|
|
type: this.objectType,
|
|
operation,
|
|
data,
|
|
});
|
|
}
|
|
}
|