import { FastifyRequest, FastifyReply, FastifyInstance } from 'fastify' import { SysConfigService } from '../services/sys-config.service' import { CreateSysConfigBody, UpdateSysConfigBody, ListSysConfigQuery, CreateTeamConfigBody, UpdateTeamConfigBody, ListTeamConfigQuery, GetTeamConfigParams } from '../dto/sys-config.dto' import { UserRole } from '../entities/user.entity' export class SysConfigController { private sysConfigService: SysConfigService constructor(app: FastifyInstance) { this.sysConfigService = new SysConfigService(app) } async create(request: FastifyRequest<{ Body: CreateSysConfigBody }>, reply: FastifyReply) { try { const { name } = request.body const config = await this.sysConfigService.create(request.body) return reply.code(201).send(config) } catch (error) { return reply.code(500).send(error) } } async update(request: FastifyRequest<{ Params: { name: string }, Body: UpdateSysConfigBody }>, reply: FastifyReply) { try { const config = await this.sysConfigService.update(request.params.name, request.body, request.body.teamId) return reply.send(config) } catch (error) { return reply.code(500).send(error) } } async delete(request: FastifyRequest<{ Params: { name: string } }>, reply: FastifyReply) { try { await this.sysConfigService.delete(request.params.name) return reply.send({ success: true }) } catch (error) { return reply.code(500).send(error) } } async getByName(request: FastifyRequest<{ Params: { name: string } }>, reply: FastifyReply) { try { const config = await this.sysConfigService.getSysConfig(request.params.name) return reply.send(config) } catch (error) { return reply.code(500).send(error) } } async list(request: FastifyRequest<{ Querystring: ListSysConfigQuery }>, reply: FastifyReply) { try { const { page = 0, size = 20, name, type, teamId } = request.query const configs = await this.sysConfigService.list(Number(page), Number(size), name, type, teamId) return reply.send(configs) } catch (error) { return reply.code(500).send(error) } } async getConfigTypes(request: FastifyRequest, reply: FastifyReply) { try { const types = await this.sysConfigService.getConfigTypes() return reply.send(types) } catch (error) { return reply.code(500).send(error) } } // 团队配置相关接口 async createTeamConfig(request: FastifyRequest<{ Body: CreateTeamConfigBody & { teamId?: number } }>, reply: FastifyReply) { try { const { teamId, ...body } = request.body const userId = request.user.id const userRole = request.user.role const config = await this.sysConfigService.createTeamConfig(body, userId, userRole, teamId) return reply.code(201).send(config) } catch (error) { return reply.code(500).send(error) } } async updateTeamConfig(request: FastifyRequest<{ Params: { name: string }, Body: UpdateTeamConfigBody & { teamId?: number } }>, reply: FastifyReply) { try { const { name } = request.params const { teamId, ...body } = request.body const userId = request.user.id const userRole = request.user.role const config = await this.sysConfigService.updateTeamConfig(name, body, userId, userRole, teamId) return reply.send(config) } catch (error) { return reply.code(500).send(error) } } async deleteTeamConfig(request: FastifyRequest<{ Params: { name: string }, Body: { teamId?: number } }>, reply: FastifyReply) { try { const { name } = request.params const { teamId } = request.body const userId = request.user.id const userRole = request.user.role await this.sysConfigService.deleteTeamConfig(name, userId, userRole, teamId) return reply.send({ success: true }) } catch (error) { return reply.code(500).send(error) } } async getTeamConfig(request: FastifyRequest<{ Params: { name: string }, Querystring: { teamId?: string } }>, reply: FastifyReply) { try { const { name } = request.params const { teamId } = request.query const userId = request.user.id const userRole = request.user.role // 管理员查询:teamId为可选,不传则查询所有团队 const config = await this.sysConfigService.getTeamConfig(name, userId, userRole, teamId ? Number(teamId) : undefined) return reply.send(config) } catch (error) { return reply.code(500).send(error) } } async listTeamConfigs(request: FastifyRequest<{ Querystring: ListTeamConfigQuery & { teamId?: string } }>, reply: FastifyReply) { try { const { teamId, ...query } = request.query const userId = request.user.id const userRole = request.user.role // 管理员查询:teamId为可选,不传则查询所有团队 const configs = await this.sysConfigService.listTeamConfigs(query, userId, userRole, teamId ? Number(teamId) : undefined) return reply.send(configs) } catch (error) { return reply.code(500).send(error) } } }