feat: 实现模板、艺术品建档、溯源节点与证书水印

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Nixevol
2026-06-28 16:05:13 +08:00
co-authored by Cursor
parent 6e5c767a53
commit b4108d7434
20 changed files with 1087 additions and 1 deletions
@@ -0,0 +1,47 @@
import { Body, Controller, Delete, Get, Param, ParseIntPipe, Post, Put, Query } from '@nestjs/common';
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
import { RoleType } from '@artveris/shared';
import { ArtworkService } from './artwork.service';
import { CreateArtworkDto, QueryArtworkDto, UpdateArtworkDto } from './dto/artwork.dto';
import { Roles } from '../common/decorators/roles.decorator';
import { CurrentUser, AuthUser } from '../common/decorators/current-user.decorator';
@ApiTags('艺术品建档')
@ApiBearerAuth()
@Controller('artworks')
export class ArtworkController {
constructor(private readonly artworkService: ArtworkService) {}
@Get()
@ApiOperation({ summary: '艺术品列表' })
list(@Query() query: QueryArtworkDto) {
return this.artworkService.list(query);
}
@Get(':id')
@ApiOperation({ summary: '艺术品详情' })
get(@Param('id', ParseIntPipe) id: number) {
return this.artworkService.get(id);
}
@Post()
@Roles(RoleType.OPERATOR)
@ApiOperation({ summary: '创建艺术品档案' })
create(@Body() dto: CreateArtworkDto, @CurrentUser() user: AuthUser) {
return this.artworkService.create(dto, user.id);
}
@Put(':id')
@Roles(RoleType.OPERATOR)
@ApiOperation({ summary: '更新艺术品档案' })
update(@Param('id', ParseIntPipe) id: number, @Body() dto: UpdateArtworkDto) {
return this.artworkService.update(id, dto);
}
@Delete(':id')
@Roles(RoleType.OPERATOR)
@ApiOperation({ summary: '停用/删除艺术品(软删除)' })
remove(@Param('id', ParseIntPipe) id: number) {
return this.artworkService.remove(id);
}
}
+10
View File
@@ -0,0 +1,10 @@
import { Module } from '@nestjs/common';
import { ArtworkService } from './artwork.service';
import { ArtworkController } from './artwork.controller';
@Module({
controllers: [ArtworkController],
providers: [ArtworkService],
exports: [ArtworkService],
})
export class ArtworkModule {}
+130
View File
@@ -0,0 +1,130 @@
import { Injectable } from '@nestjs/common';
import { Prisma } from '@prisma/client';
import { ArtworkStatus, ErrorCode } from '@artveris/shared';
import { PrismaService } from '../prisma/prisma.service';
import { StorageService } from '../storage/storage.service';
import { BusinessException } from '../common/exceptions/business.exception';
import { buildPage } from '../common/utils/pagination.util';
import { CreateArtworkDto, QueryArtworkDto, UpdateArtworkDto } from './dto/artwork.dto';
interface MediaItem {
key: string;
name?: string;
type?: string;
}
@Injectable()
export class ArtworkService {
constructor(
private readonly prisma: PrismaService,
private readonly storage: StorageService,
) {}
private mapMedia(items: unknown): Array<MediaItem & { url: string }> {
if (!Array.isArray(items)) return [];
return (items as MediaItem[])
.filter((i) => i && i.key)
.map((i) => ({ ...i, url: this.storage.getUrl(i.key) }));
}
async create(dto: CreateArtworkDto, userId: number) {
const tpl = await this.prisma.template.findFirst({
where: { id: dto.templateId, deletedAt: null },
});
if (!tpl) throw new BusinessException(ErrorCode.TEMPLATE_NOT_FOUND, '模板不存在', 404);
const artwork = await this.prisma.artwork.create({
data: {
templateId: dto.templateId,
title: dto.title,
artist: dto.artist,
dynamicFields: (dto.dynamicFields ?? {}) as Prisma.InputJsonValue,
images: (dto.images ?? []) as unknown as Prisma.InputJsonValue,
createdBy: userId,
},
});
return this.get(artwork.id);
}
async list(query: QueryArtworkDto) {
const where: Prisma.ArtworkWhereInput = { deletedAt: null };
if (query.keyword) {
where.OR = [
{ title: { contains: query.keyword } },
{ artist: { contains: query.keyword } },
];
}
if (query.status) where.status = query.status;
if (query.templateId) where.templateId = query.templateId;
const [rows, total] = await this.prisma.$transaction([
this.prisma.artwork.findMany({
where,
include: {
template: { select: { id: true, name: true } },
traceCode: { select: { id: true, code: true, shortCode: true, status: true } },
},
orderBy: { id: 'desc' },
skip: query.skip,
take: query.take,
}),
this.prisma.artwork.count({ where }),
]);
const list = rows.map((a) => ({
...a,
images: this.mapMedia(a.images),
}));
return buildPage(list, total, query.page, query.pageSize);
}
async get(id: number) {
const a = await this.prisma.artwork.findFirst({
where: { id, deletedAt: null },
include: {
template: { include: { fields: { orderBy: { sortOrder: 'asc' } } } },
traceNodes: { orderBy: { sortOrder: 'asc' } },
certificates: { orderBy: { id: 'asc' } },
traceCode: { include: { proofs: { orderBy: { id: 'desc' } } } },
},
});
if (!a) throw new BusinessException(ErrorCode.ARTWORK_NOT_FOUND, '艺术品不存在', 404);
return {
...a,
images: this.mapMedia(a.images),
traceNodes: a.traceNodes.map((n) => ({ ...n, mediaFiles: this.mapMedia(n.mediaFiles) })),
certificates: a.certificates.map((c) => ({
...c,
originUrl: c.originFileUrl ? this.storage.getUrl(c.originFileUrl) : null,
watermarkUrl: c.watermarkFileUrl ? this.storage.getUrl(c.watermarkFileUrl) : null,
})),
};
}
async update(id: number, dto: UpdateArtworkDto) {
await this.ensureExists(id);
const data: Prisma.ArtworkUpdateInput = {
title: dto.title,
artist: dto.artist,
status: dto.status,
};
if (dto.dynamicFields !== undefined) {
data.dynamicFields = dto.dynamicFields as Prisma.InputJsonValue;
}
if (dto.images !== undefined) {
data.images = dto.images as unknown as Prisma.InputJsonValue;
}
await this.prisma.artwork.update({ where: { id }, data });
return this.get(id);
}
async remove(id: number) {
await this.ensureExists(id);
await this.prisma.artwork.update({ where: { id }, data: { deletedAt: new Date() } });
return { success: true };
}
private async ensureExists(id: number) {
const a = await this.prisma.artwork.findFirst({ where: { id, deletedAt: null } });
if (!a) throw new BusinessException(ErrorCode.ARTWORK_NOT_FOUND, '艺术品不存在', 404);
return a;
}
}
+112
View File
@@ -0,0 +1,112 @@
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { Type } from 'class-transformer';
import {
IsArray,
IsEnum,
IsInt,
IsNotEmpty,
IsObject,
IsOptional,
IsString,
MaxLength,
ValidateNested,
} from 'class-validator';
import { ArtworkStatus } from '@artveris/shared';
import { PaginationDto } from '../../common/dto/pagination.dto';
export class MediaItemDto {
@ApiProperty({ description: '存储 key(上传接口返回)' })
@IsString()
@IsNotEmpty()
key!: string;
@ApiPropertyOptional({ description: '原始文件名' })
@IsOptional()
@IsString()
name?: string;
@ApiPropertyOptional({ description: '类型:image | video' })
@IsOptional()
@IsString()
type?: string;
}
export class CreateArtworkDto {
@ApiProperty({ description: '模板 ID' })
@Type(() => Number)
@IsInt()
templateId!: number;
@ApiProperty({ description: '作品名称' })
@IsString()
@IsNotEmpty()
@MaxLength(120)
title!: string;
@ApiPropertyOptional({ description: '艺术家 / 创作者' })
@IsOptional()
@IsString()
@MaxLength(120)
artist?: string;
@ApiPropertyOptional({ description: '动态字段数据(按模板字段键存储)' })
@IsOptional()
@IsObject()
dynamicFields?: Record<string, unknown>;
@ApiPropertyOptional({ description: '作品图片', type: [MediaItemDto] })
@IsOptional()
@IsArray()
@ValidateNested({ each: true })
@Type(() => MediaItemDto)
images?: MediaItemDto[];
}
export class UpdateArtworkDto {
@ApiPropertyOptional({ description: '作品名称' })
@IsOptional()
@IsString()
@MaxLength(120)
title?: string;
@ApiPropertyOptional({ description: '艺术家 / 创作者' })
@IsOptional()
@IsString()
@MaxLength(120)
artist?: string;
@ApiPropertyOptional({ description: '动态字段数据' })
@IsOptional()
@IsObject()
dynamicFields?: Record<string, unknown>;
@ApiPropertyOptional({ description: '作品图片', type: [MediaItemDto] })
@IsOptional()
@IsArray()
@ValidateNested({ each: true })
@Type(() => MediaItemDto)
images?: MediaItemDto[];
@ApiPropertyOptional({ description: '状态', enum: ArtworkStatus })
@IsOptional()
@IsEnum(ArtworkStatus)
status?: ArtworkStatus;
}
export class QueryArtworkDto extends PaginationDto {
@ApiPropertyOptional({ description: '关键词(名称/艺术家)' })
@IsOptional()
@IsString()
keyword?: string;
@ApiPropertyOptional({ description: '状态', enum: ArtworkStatus })
@IsOptional()
@IsEnum(ArtworkStatus)
status?: ArtworkStatus;
@ApiPropertyOptional({ description: '模板 ID' })
@IsOptional()
@Type(() => Number)
@IsInt()
templateId?: number;
}