import { FastifyRequest, FastifyReply, FastifyInstance } from 'fastify' import { TaskService } from '../services/task.service' import { UpdateTaskBody, ListTaskQuery, ListTaskItemQuery, SendMessageBody } from '../dto/task.dto' import { Task } from '../entities/task.entity' export class TaskController { private taskService: TaskService constructor(app: FastifyInstance) { this.taskService = new TaskService(app) } async create(request: FastifyRequest, reply: FastifyReply) { try { const userId = request.user.id const data = await request.file() if (!data) { return reply.code(400).send({ message: '请选择要上传的文件' }) } const nameField = data.fields['name'] const messageField = data.fields['message'] const name = nameField && !Array.isArray(nameField) && 'value' in nameField ? (nameField.value as string) : undefined const message = messageField && !Array.isArray(messageField) && 'value' in messageField ? (messageField.value as string) : undefined if (!name || !message) { return reply.code(400).send({ message: '任务名称和消息内容不能为空' }) } const buffer = await data.toBuffer() const task = await this.taskService.create({ name, message, userId, buffer }) return reply.code(201).send({ task: { id: task.id, name: task.name, message: task.message } }) } catch (error) { return reply.code(500).send(error) } } async getById(request: FastifyRequest<{ Params: { id: string } }>, reply: FastifyReply) { try { const id = parseInt(request.params.id) if (isNaN(id)) { return reply.code(400).send({ message: '无效的任务ID' }) } const task = await this.taskService.findById(id) return reply.send(task) } catch (error) { return reply.code(500).send({ error }) } } async list(request: FastifyRequest<{ Querystring: ListTaskQuery }>, reply: FastifyReply) { try { const { page, size, userId } = request.query const result = await this.taskService.findAll(Number(page) || 0, Number(size) || 20, userId || request.user.id) return reply.send(result) } catch (error) { return reply.code(500).send({ error }) } } async update(request: FastifyRequest<{ Body: UpdateTaskBody }>, reply: FastifyReply) { try { const { id, name, message, total, sent, successCount, startedAt } = request.body if (!id) { return reply.code(400).send({ message: '任务ID不能为空' }) } const task = await this.taskService.findById(id) if (!task) { return reply.code(500).send({ message: '任务不存在' }) } const updateData: Partial = {} if (name !== undefined) updateData.name = name if (message !== undefined) updateData.message = message if (total !== undefined) updateData.total = total if (sent !== undefined) updateData.sent = sent if (successCount !== undefined) updateData.successCount = successCount if (startedAt !== undefined) updateData.startedAt = startedAt await this.taskService.update(id, updateData) return reply.send({ message: '任务更新成功' }) } catch (error) { return reply.code(500).send({ error }) } } async delete(request: FastifyRequest<{ Params: { id: string } }>, reply: FastifyReply) { try { const id = parseInt(request.params.id) if (isNaN(id)) { return reply.code(400).send({ message: '无效的任务ID' }) } const task = await this.taskService.findById(id) if (!task) { return reply.code(500).send({ message: '任务不存在' }) } await this.taskService.delete(id) return reply.send({ message: '任务删除成功' }) } catch (error) { return reply.code(500).send(error) } } async listTaskItems(request: FastifyRequest<{ Querystring: ListTaskItemQuery }>, reply: FastifyReply) { try { const { page, size, taskId, status } = request.query const result = await this.taskService.findTaskItems( Number(page) || 0, Number(size) || 20, taskId ? Number(taskId) : undefined, status ) return reply.send(result) } catch (error) { return reply.code(500).send({ message: '查询任务项失败', error: error instanceof Error ? error.message : '未知错误' }) } } async startTask(request: FastifyRequest<{ Params: { id: string } }>, reply: FastifyReply) { try { const id = parseInt(request.params.id) if (isNaN(id)) { return reply.code(400).send({ message: '无效的任务ID' }) } await this.taskService.startTask(id) return reply.send({ message: '任务启动成功' }) } catch (error) { return reply.code(500).send({ message: '启动任务失败', error: error instanceof Error ? error.message : '未知错误' }) } } async testSendMessage(request: FastifyRequest<{ Body: SendMessageBody }>, reply: FastifyReply) { try { const { senderId, taskId, delay, count, session, dcId, authKey, sendToVerifyAccounts, verifyAccounts } = request.body if (!senderId || !taskId) { return reply.code(400).send({ success: false, message: 'senderId 和 taskId 不能为空' }) } const result = await this.taskService.testSendMessage( senderId, taskId, delay, count, session, dcId, authKey, sendToVerifyAccounts, verifyAccounts ) return reply.code(200).send(result) } catch (error) { return reply.code(500).send({ success: false, message: '测试发送消息时发生错误', error: error instanceof Error ? error.message : '未知错误' }) } } }