| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 |
- 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: '导出失败' })
- }
- }
- }
|