2026-02-04 21:42:25 +08:00
|
|
|
import { FastifyInstance } from 'fastify';
|
|
|
|
|
import prisma from '../../config/database';
|
|
|
|
|
import { successResponse, paginatedResponse } from '../../utils/response';
|
|
|
|
|
import { NotFoundError, BadRequestError } from '../../utils/errors';
|
|
|
|
|
|
|
|
|
|
export async function topicRoutes(app: FastifyInstance): Promise<void> {
|
|
|
|
|
// 获取主题列表
|
|
|
|
|
app.get('/', async (request, reply) => {
|
|
|
|
|
const { page = 1, limit = 20 } = request.query as any;
|
|
|
|
|
|
|
|
|
|
const [items, total] = await Promise.all([
|
|
|
|
|
prisma.topic.findMany({
|
|
|
|
|
include: {
|
|
|
|
|
llmConfig: true,
|
|
|
|
|
topicSources: { include: { source: true } },
|
|
|
|
|
topicChannels: { include: { channel: true } },
|
|
|
|
|
},
|
|
|
|
|
orderBy: { createdAt: 'desc' },
|
|
|
|
|
skip: (Number(page) - 1) * Number(limit),
|
|
|
|
|
take: Number(limit),
|
|
|
|
|
}),
|
|
|
|
|
prisma.topic.count(),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
paginatedResponse(reply, items, total, Number(page), Number(limit));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 获取单个主题
|
|
|
|
|
app.get('/:id', async (request, reply) => {
|
|
|
|
|
const { id } = request.params as any;
|
|
|
|
|
|
|
|
|
|
const topic = await prisma.topic.findUnique({
|
|
|
|
|
where: { id: parseInt(id) },
|
|
|
|
|
include: {
|
|
|
|
|
llmConfig: true,
|
|
|
|
|
topicSources: { include: { source: true } },
|
|
|
|
|
topicChannels: { include: { channel: true } },
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!topic) {
|
|
|
|
|
throw new NotFoundError('主题不存在');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
successResponse(reply, topic);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 创建主题
|
|
|
|
|
app.post('/', async (request, reply) => {
|
|
|
|
|
const {
|
|
|
|
|
name,
|
|
|
|
|
description,
|
|
|
|
|
processMode,
|
|
|
|
|
llmConfigId,
|
|
|
|
|
scheduleConfig,
|
|
|
|
|
sourceIds,
|
|
|
|
|
channelIds,
|
|
|
|
|
enabled,
|
|
|
|
|
} = request.body as any;
|
|
|
|
|
|
|
|
|
|
if (!name || !processMode || !scheduleConfig) {
|
|
|
|
|
throw new BadRequestError('缺少必填字段');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const topic = await prisma.topic.create({
|
|
|
|
|
data: {
|
|
|
|
|
name,
|
|
|
|
|
description,
|
|
|
|
|
processMode,
|
|
|
|
|
llmConfigId,
|
|
|
|
|
scheduleConfig,
|
|
|
|
|
enabled: enabled ?? true,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 关联数据源
|
|
|
|
|
if (sourceIds?.length > 0) {
|
|
|
|
|
await prisma.topicSource.createMany({
|
|
|
|
|
data: sourceIds.map((sourceId: number) => ({
|
|
|
|
|
topicId: topic.id,
|
|
|
|
|
sourceId,
|
|
|
|
|
})),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 关联推送渠道
|
|
|
|
|
if (channelIds?.length > 0) {
|
|
|
|
|
await prisma.topicChannel.createMany({
|
|
|
|
|
data: channelIds.map((channelId: number) => ({
|
|
|
|
|
topicId: topic.id,
|
|
|
|
|
channelId,
|
|
|
|
|
})),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
successResponse(reply, topic, '主题创建成功', 201);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 更新主题
|
|
|
|
|
app.put('/:id', async (request, reply) => {
|
|
|
|
|
const { id } = request.params as any;
|
|
|
|
|
const {
|
|
|
|
|
name,
|
|
|
|
|
description,
|
|
|
|
|
processMode,
|
|
|
|
|
llmConfigId,
|
|
|
|
|
scheduleConfig,
|
|
|
|
|
sourceIds,
|
|
|
|
|
channelIds,
|
|
|
|
|
enabled,
|
|
|
|
|
} = request.body as any;
|
|
|
|
|
|
|
|
|
|
const existing = await prisma.topic.findUnique({
|
|
|
|
|
where: { id: parseInt(id) },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!existing) {
|
|
|
|
|
throw new NotFoundError('主题不存在');
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-05 15:23:37 +08:00
|
|
|
// 构建更新数据对象,只包含提供的字段
|
|
|
|
|
const updateData: any = {};
|
|
|
|
|
if (name !== undefined) updateData.name = name;
|
|
|
|
|
if (description !== undefined) updateData.description = description;
|
|
|
|
|
if (processMode !== undefined) updateData.processMode = processMode;
|
|
|
|
|
if (llmConfigId !== undefined) updateData.llmConfigId = llmConfigId;
|
|
|
|
|
if (scheduleConfig !== undefined) updateData.scheduleConfig = scheduleConfig;
|
|
|
|
|
if (enabled !== undefined) updateData.enabled = enabled;
|
|
|
|
|
|
2026-02-04 21:42:25 +08:00
|
|
|
const topic = await prisma.topic.update({
|
|
|
|
|
where: { id: parseInt(id) },
|
2026-02-05 15:23:37 +08:00
|
|
|
data: updateData,
|
2026-02-04 21:42:25 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 更新数据源关联
|
|
|
|
|
if (sourceIds !== undefined) {
|
|
|
|
|
await prisma.topicSource.deleteMany({ where: { topicId: topic.id } });
|
|
|
|
|
if (sourceIds.length > 0) {
|
|
|
|
|
await prisma.topicSource.createMany({
|
|
|
|
|
data: sourceIds.map((sourceId: number) => ({
|
|
|
|
|
topicId: topic.id,
|
|
|
|
|
sourceId,
|
|
|
|
|
})),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 更新推送渠道关联
|
|
|
|
|
if (channelIds !== undefined) {
|
|
|
|
|
await prisma.topicChannel.deleteMany({ where: { topicId: topic.id } });
|
|
|
|
|
if (channelIds.length > 0) {
|
|
|
|
|
await prisma.topicChannel.createMany({
|
|
|
|
|
data: channelIds.map((channelId: number) => ({
|
|
|
|
|
topicId: topic.id,
|
|
|
|
|
channelId,
|
|
|
|
|
})),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
successResponse(reply, topic, '主题更新成功');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 删除主题
|
|
|
|
|
app.delete('/:id', async (request, reply) => {
|
|
|
|
|
const { id } = request.params as any;
|
|
|
|
|
|
|
|
|
|
const existing = await prisma.topic.findUnique({
|
|
|
|
|
where: { id: parseInt(id) },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!existing) {
|
|
|
|
|
throw new NotFoundError('主题不存在');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await prisma.topic.delete({ where: { id: parseInt(id) } });
|
|
|
|
|
|
|
|
|
|
successResponse(reply, null, '主题删除成功');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 手动生成报告
|
|
|
|
|
app.post('/:id/generate-report', async (request, reply) => {
|
|
|
|
|
const { id } = request.params as any;
|
|
|
|
|
const { ReportService } = await import('../../services/report.service');
|
|
|
|
|
const { DistributorService } = await import('../../services/distributor.service');
|
|
|
|
|
|
|
|
|
|
const topic = await prisma.topic.findUnique({
|
|
|
|
|
where: { id: parseInt(id) },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!topic) {
|
|
|
|
|
throw new NotFoundError('主题不存在');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const reportService = new ReportService();
|
|
|
|
|
const distributorService = new DistributorService();
|
|
|
|
|
|
|
|
|
|
reportService.generateReportForTopic(topic.id).then(async (reportId) => {
|
|
|
|
|
await distributorService.distributeReportToAllChannels(reportId);
|
|
|
|
|
}).catch((error) => {
|
|
|
|
|
console.error('报告生成失败:', error);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
successResponse(reply, null, '报告生成任务已触发');
|
|
|
|
|
});
|
|
|
|
|
}
|