import { Body, Controller, Delete, Get, Param, ParseIntPipe, Post, Put, Query, Res, } from '@nestjs/common'; import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger'; import { Response } from 'express'; import { RoleType } from '@artveris/shared'; import { TraceCodeService } from './trace-code.service'; import { BatchGroupDto, BatchStatusDto, BindArtworkDto, GenerateBatchDto, GroupDto, QueryBatchDto, QueryTraceCodeDto, } from './dto/trace-code.dto'; import { Roles } from '../common/decorators/roles.decorator'; import { Public } from '../common/decorators/public.decorator'; import { CurrentUser, AuthUser } from '../common/decorators/current-user.decorator'; @ApiTags('批量生码') @ApiBearerAuth() @Controller() export class TraceCodeController { constructor(private readonly service: TraceCodeService) {} @Post('trace-codes/batch') @Roles(RoleType.OPERATOR) @ApiOperation({ summary: '按批次异步生成追溯码' }) generate(@Body() dto: GenerateBatchDto, @CurrentUser() user: AuthUser) { return this.service.generateBatch(dto, user.id); } @Get('batches') @ApiOperation({ summary: '批次列表' }) batches(@Query() query: QueryBatchDto) { return this.service.listBatches(query); } @Get('trace-code-groups') @ApiOperation({ summary: '分组列表' }) groups() { return this.service.listGroups(); } @Post('trace-code-groups') @Roles(RoleType.OPERATOR) @ApiOperation({ summary: '创建分组' }) createGroup(@Body() dto: GroupDto) { return this.service.createGroup(dto); } @Put('trace-code-groups/:id') @Roles(RoleType.OPERATOR) @ApiOperation({ summary: '更新分组' }) updateGroup(@Param('id', ParseIntPipe) id: number, @Body() dto: GroupDto) { return this.service.updateGroup(id, dto); } @Delete('trace-code-groups/:id') @Roles(RoleType.OPERATOR) @ApiOperation({ summary: '删除分组' }) removeGroup(@Param('id', ParseIntPipe) id: number) { return this.service.removeGroup(id); } @Put('trace-codes/batch/group') @Roles(RoleType.OPERATOR) @ApiOperation({ summary: '批量设置分组' }) batchGroup(@Body() dto: BatchGroupDto) { return this.service.batchSetGroup(dto); } @Put('trace-codes/batch/status') @Roles(RoleType.OPERATOR) @ApiOperation({ summary: '批量启用/停用' }) batchStatus(@Body() dto: BatchStatusDto) { return this.service.batchSetStatus(dto); } @Get('trace-codes') @ApiOperation({ summary: '追溯码列表' }) list(@Query() query: QueryTraceCodeDto) { return this.service.list(query); } @Get('trace-codes/export') @Roles(RoleType.OPERATOR) @ApiOperation({ summary: '导出追溯码 Excel' }) export(@Query('batchId') batchId: string | undefined, @Res() res: Response) { return this.service.exportExcel(batchId ? Number(batchId) : undefined, res); } @Public() @Get('trace-codes/:id/qrcode') @ApiOperation({ summary: '获取追溯码二维码 PNG' }) async qrcode(@Param('id') id: string, @Res() res: Response) { const buf = await this.service.getQrPng(BigInt(id)); res.setHeader('Content-Type', 'image/png'); res.end(buf); } @Put('trace-codes/:id/bind') @Roles(RoleType.OPERATOR) @ApiOperation({ summary: '绑定艺术品' }) bind(@Param('id') id: string, @Body() dto: BindArtworkDto) { return this.service.bind(BigInt(id), dto.artworkId); } @Put('trace-codes/:id/unbind') @Roles(RoleType.OPERATOR) @ApiOperation({ summary: '解绑艺术品' }) unbind(@Param('id') id: string) { return this.service.unbind(BigInt(id)); } }