forked from wangziqi/gongxue-base
feat: 集成 AI 对话与只读查询工具
This commit is contained in:
@@ -33,6 +33,17 @@ interface RawStudentCount {
|
||||
count: string;
|
||||
}
|
||||
|
||||
interface AgentClassRow {
|
||||
id: string | number;
|
||||
name: string;
|
||||
code: string;
|
||||
classType: string;
|
||||
status: string;
|
||||
startDate: string | null;
|
||||
endDate: string | null;
|
||||
studentCount: string | number;
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class ClassesService {
|
||||
constructor(
|
||||
@@ -67,6 +78,38 @@ export class ClassesService {
|
||||
if (!assignment) throw new ForbiddenException('只能访问自己被分配的班级');
|
||||
}
|
||||
|
||||
async agentSearchClasses(
|
||||
userId: number,
|
||||
canManageAll: boolean,
|
||||
query: { keyword?: string; status?: string; limit?: number },
|
||||
) {
|
||||
const accessibleClassIds = await this.getAccessibleClassIds(userId, canManageAll);
|
||||
if (accessibleClassIds?.length === 0) return [];
|
||||
|
||||
const qb = this.classRepo
|
||||
.createQueryBuilder('class')
|
||||
.leftJoin(
|
||||
ClassStudent,
|
||||
'classStudent',
|
||||
'classStudent.classId = class.id AND classStudent.status = :activeStudent',
|
||||
{ activeStudent: 'active' },
|
||||
)
|
||||
.select('class.id', 'id')
|
||||
.addSelect('class.name', 'name')
|
||||
.addSelect('class.code', 'code')
|
||||
.addSelect('class.classType', 'classType')
|
||||
.addSelect('class.status', 'status')
|
||||
.addSelect('class.startDate', 'startDate')
|
||||
.addSelect('class.endDate', 'endDate')
|
||||
.addSelect('COUNT(classStudent.id)', 'studentCount')
|
||||
.where('class.isArchived = :isArchived', { isArchived: false });
|
||||
if (accessibleClassIds) qb.andWhere('class.id IN (:...accessibleClassIds)', { accessibleClassIds });
|
||||
if (query.keyword) qb.andWhere('(class.name LIKE :keyword OR class.code LIKE :keyword)', { keyword: `%${query.keyword}%` });
|
||||
if (query.status) qb.andWhere('class.status = :status', { status: query.status });
|
||||
const rows = await qb.groupBy('class.id').orderBy('class.name', 'ASC').limit(query.limit ?? 20).getRawMany<AgentClassRow>();
|
||||
return rows.map((row) => ({ ...row, id: Number(row.id), studentCount: Number(row.studentCount || 0) }));
|
||||
}
|
||||
|
||||
async findAll(query: QueryClassDto, accessibleClassIds?: number[]) {
|
||||
const where: Record<string, unknown> = {};
|
||||
if (query.status) where.status = query.status;
|
||||
|
||||
Reference in New Issue
Block a user