import { FastifyRequest, FastifyReply, FastifyInstance } from 'fastify' import { FishFriendsService } from '../services/fish-friends.service' import { ListFishFriendsQuery, CreateFishFriendsBody, BatchCreateFishFriendsBody, UpdateFishFriendsBody, DeleteFishFriendsBody } from '../dto/fish-friends.dto' export class FishFriendsController { private fishFriendsService: FishFriendsService constructor(app: FastifyInstance) { this.fishFriendsService = new FishFriendsService(app) } async create(request: FastifyRequest<{ Body: CreateFishFriendsBody }>, reply: FastifyReply) { try { const fishFriendsData = request.body // 检查是否已存在相同 ID 的记录(包括已删除的记录) try { await this.fishFriendsService.findByIdIncludeDeleted(fishFriendsData.id) return reply.code(400).send({ message: '记录已存在' }) } catch (error) { // 记录不存在,可以继续创建 } const fishFriends = await this.fishFriendsService.create(fishFriendsData) return reply.code(201).send({ message: '创建成功', fishFriends: { id: fishFriends.id, fishId: fishFriends.fishId, ownerId: fishFriends.ownerId, tgName: fishFriends.tgName, tgUsername: fishFriends.tgUsername, tgRemarkName: fishFriends.tgRemarkName, tgPhone: fishFriends.tgPhone, remark: fishFriends.remark, createdAt: fishFriends.createdAt, updatedAt: fishFriends.updatedAt } }) } catch (error) { console.error('创建记录失败:', error) return reply.code(500).send({ message: '创建失败' }) } } async batchUpsert(request: FastifyRequest<{ Body: BatchCreateFishFriendsBody }>, reply: FastifyReply) { try { const { fishFriends } = request.body if (!fishFriends || !Array.isArray(fishFriends) || fishFriends.length === 0) { return reply.code(400).send({ message: 'error' }) } // 使用批量 upsert 操作 await this.fishFriendsService.batchUpsert(fishFriends) return reply.code(201).send({ message: `ok` }) } catch (error) { return reply.code(500).send({ message: 'error' }) } } async getById(request: FastifyRequest<{ Params: { id: string } }>, reply: FastifyReply) { try { const { id } = request.params const fishFriends = await this.fishFriendsService.findById(id) return reply.send({ fishFriends: { id: fishFriends.id, fishId: fishFriends.fishId, ownerId: fishFriends.ownerId, tgName: fishFriends.tgName, tgUsername: fishFriends.tgUsername, tgRemarkName: fishFriends.tgRemarkName, tgPhone: fishFriends.tgPhone, remark: fishFriends.remark, createdAt: fishFriends.createdAt, updatedAt: fishFriends.updatedAt } }) } catch (error) { return reply.code(404).send({ message: '记录不存在' }) } } async list(request: FastifyRequest<{ Querystring: ListFishFriendsQuery }>, reply: FastifyReply) { try { const query = request.query if (request.user.role !== 'admin') { query.ownerId = request.user.id } const result = await this.fishFriendsService.list(query) return reply.send(result) } catch (error) { console.error('查询记录失败:', error) return reply.code(500).send({ message: '查询失败' }) } } async update(request: FastifyRequest<{ Body: UpdateFishFriendsBody }>, reply: FastifyReply) { try { const { id, ...updateData } = request.body // 检查记录是否存在 try { await this.fishFriendsService.findById(id) } catch (error) { return reply.code(404).send({ message: '记录不存在' }) } const updatedFishFriends = await this.fishFriendsService.update(id, updateData) return reply.send({ message: '更新成功', fishFriends: { id: updatedFishFriends.id, fishId: updatedFishFriends.fishId, ownerId: updatedFishFriends.ownerId, tgName: updatedFishFriends.tgName, tgUsername: updatedFishFriends.tgUsername, tgRemarkName: updatedFishFriends.tgRemarkName, tgPhone: updatedFishFriends.tgPhone, remark: updatedFishFriends.remark, createdAt: updatedFishFriends.createdAt, updatedAt: updatedFishFriends.updatedAt } }) } catch (error) { console.error('更新记录失败:', error) return reply.code(500).send({ message: '更新失败' }) } } async delete(request: FastifyRequest<{ Body: DeleteFishFriendsBody }>, reply: FastifyReply) { try { const { id } = request.body // 检查记录是否存在 try { await this.fishFriendsService.findById(id) } catch (error) { return reply.code(404).send({ message: '记录不存在' }) } await this.fishFriendsService.delete(id) return reply.send({ message: '删除成功' }) } catch (error) { console.error('删除记录失败:', error) return reply.code(500).send({ message: '删除失败' }) } } async batchDelete(request: FastifyRequest<{ Body: { ids: string[] } }>, reply: FastifyReply) { try { const { ids } = request.body if (!ids || ids.length === 0) { return reply.code(400).send({ message: '请提供要删除的记录ID' }) } await this.fishFriendsService.batchDelete(ids) return reply.send({ message: `成功删除 ${ids.length} 条记录` }) } catch (error) { console.error('批量删除记录失败:', error) return reply.code(500).send({ message: '批量删除失败' }) } } async getStatistics(request: FastifyRequest, reply: FastifyReply) { try { const statistics = await this.fishFriendsService.getStatistics() return reply.send({ statistics }) } catch (error) { console.error('获取统计信息失败:', error) return reply.code(500).send({ message: '获取统计信息失败' }) } } async findByFishId(request: FastifyRequest<{ Querystring: { fishId: string } }>, reply: FastifyReply) { try { const { fishId } = request.query const fishFriendsList = await this.fishFriendsService.findByFishId(fishId) return reply.send({ fishFriendsList }) } catch (error) { console.error('按Fish ID查询失败:', error) return reply.code(500).send({ message: '查询失败' }) } } async findByOwnerId(request: FastifyRequest<{ Querystring: { ownerId: number } }>, reply: FastifyReply) { try { const { ownerId } = request.query const fishFriendsList = await this.fishFriendsService.findByOwnerId(ownerId) return reply.send({ fishFriendsList }) } catch (error) { console.error('按所有者ID查询失败:', error) return reply.code(500).send({ message: '查询失败' }) } } async findByTgUsername(request: FastifyRequest<{ Querystring: { tgUsername: string } }>, reply: FastifyReply) { try { const { tgUsername } = request.query const fishFriendsList = await this.fishFriendsService.findByTgUsername(tgUsername) return reply.send({ fishFriendsList }) } catch (error) { console.error('按Telegram用户名查询失败:', error) return reply.code(500).send({ message: '查询失败' }) } } async exportToExcel(request: FastifyRequest<{ Querystring: ListFishFriendsQuery }>, reply: FastifyReply) { try { const query = request.query const excelBuffer = await this.fishFriendsService.exportToExcel(query) // 设置响应头 reply.header('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') reply.header('Content-Disposition', 'attachment; filename="fish_friends_data.xlsx"') reply.header('Content-Length', excelBuffer.length.toString()) return reply.send(excelBuffer) } catch (error) { console.error('导出Excel失败:', error) return reply.code(500).send({ message: '导出失败' }) } } }