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
+103
View File
@@ -0,0 +1,103 @@
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { Type } from 'class-transformer';
import {
ArrayMinSize,
IsArray,
IsBoolean,
IsEnum,
IsInt,
IsNotEmpty,
IsOptional,
IsString,
MaxLength,
ValidateNested,
} from 'class-validator';
import { FieldType, TemplateStatus } from '@artveris/shared';
import { PaginationDto } from '../../common/dto/pagination.dto';
export class TemplateFieldDto {
@ApiProperty({ description: '字段键(英文唯一标识)' })
@IsString()
@IsNotEmpty()
@MaxLength(50)
fieldKey!: string;
@ApiProperty({ description: '字段名称' })
@IsString()
@IsNotEmpty()
@MaxLength(50)
label!: string;
@ApiProperty({ description: '字段类型', enum: FieldType })
@IsEnum(FieldType)
type!: FieldType;
@ApiPropertyOptional({ description: '是否必填' })
@IsOptional()
@IsBoolean()
required?: boolean;
@ApiPropertyOptional({ description: '排序' })
@IsOptional()
@IsInt()
sortOrder?: number;
}
export class CreateTemplateDto {
@ApiProperty({ description: '模板名称' })
@IsString()
@IsNotEmpty()
@MaxLength(80)
name!: string;
@ApiPropertyOptional({ description: '备注' })
@IsOptional()
@IsString()
@MaxLength(255)
remark?: string;
@ApiProperty({ description: '字段列表', type: [TemplateFieldDto] })
@IsArray()
@ArrayMinSize(1)
@ValidateNested({ each: true })
@Type(() => TemplateFieldDto)
fields!: TemplateFieldDto[];
}
export class UpdateTemplateDto {
@ApiPropertyOptional({ description: '模板名称' })
@IsOptional()
@IsString()
@MaxLength(80)
name?: string;
@ApiPropertyOptional({ description: '备注' })
@IsOptional()
@IsString()
@MaxLength(255)
remark?: string;
@ApiPropertyOptional({ description: '状态', enum: TemplateStatus })
@IsOptional()
@IsEnum(TemplateStatus)
status?: TemplateStatus;
@ApiPropertyOptional({ description: '字段列表(提供则整体替换)', type: [TemplateFieldDto] })
@IsOptional()
@IsArray()
@ValidateNested({ each: true })
@Type(() => TemplateFieldDto)
fields?: TemplateFieldDto[];
}
export class QueryTemplateDto extends PaginationDto {
@ApiPropertyOptional({ description: '关键词' })
@IsOptional()
@IsString()
keyword?: string;
@ApiPropertyOptional({ description: '状态', enum: TemplateStatus })
@IsOptional()
@IsEnum(TemplateStatus)
status?: TemplateStatus;
}
@@ -0,0 +1,52 @@
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 { TemplateService } from './template.service';
import { CreateTemplateDto, QueryTemplateDto, UpdateTemplateDto } from './dto/template.dto';
import { Roles } from '../common/decorators/roles.decorator';
@ApiTags('溯源模板')
@ApiBearerAuth()
@Controller('templates')
export class TemplateController {
constructor(private readonly templateService: TemplateService) {}
@Get()
@ApiOperation({ summary: '模板列表' })
list(@Query() query: QueryTemplateDto) {
return this.templateService.list(query);
}
@Get('options')
@ApiOperation({ summary: '启用模板(供建档选择)' })
options() {
return this.templateService.options();
}
@Get(':id')
@ApiOperation({ summary: '模板详情' })
get(@Param('id', ParseIntPipe) id: number) {
return this.templateService.get(id);
}
@Post()
@Roles(RoleType.OPERATOR)
@ApiOperation({ summary: '创建模板' })
create(@Body() dto: CreateTemplateDto) {
return this.templateService.create(dto);
}
@Put(':id')
@Roles(RoleType.OPERATOR)
@ApiOperation({ summary: '更新模板' })
update(@Param('id', ParseIntPipe) id: number, @Body() dto: UpdateTemplateDto) {
return this.templateService.update(id, dto);
}
@Delete(':id')
@Roles(RoleType.OPERATOR)
@ApiOperation({ summary: '删除模板' })
remove(@Param('id', ParseIntPipe) id: number) {
return this.templateService.remove(id);
}
}
+10
View File
@@ -0,0 +1,10 @@
import { Module } from '@nestjs/common';
import { TemplateService } from './template.service';
import { TemplateController } from './template.controller';
@Module({
controllers: [TemplateController],
providers: [TemplateService],
exports: [TemplateService],
})
export class TemplateModule {}
+95
View File
@@ -0,0 +1,95 @@
import { Injectable } from '@nestjs/common';
import { Prisma } from '@prisma/client';
import { ErrorCode, TemplateStatus } from '@artveris/shared';
import { PrismaService } from '../prisma/prisma.service';
import { BusinessException } from '../common/exceptions/business.exception';
import { buildPage } from '../common/utils/pagination.util';
import { CreateTemplateDto, QueryTemplateDto, UpdateTemplateDto, TemplateFieldDto } from './dto/template.dto';
@Injectable()
export class TemplateService {
constructor(private readonly prisma: PrismaService) {}
private fieldsCreate(fields: TemplateFieldDto[]) {
return fields.map((f, i) => ({
fieldKey: f.fieldKey,
label: f.label,
type: f.type,
required: f.required ?? false,
sortOrder: f.sortOrder ?? i,
}));
}
async create(dto: CreateTemplateDto) {
return this.prisma.template.create({
data: {
name: dto.name,
remark: dto.remark,
fields: { create: this.fieldsCreate(dto.fields) },
},
include: { fields: { orderBy: { sortOrder: 'asc' } } },
});
}
async list(query: QueryTemplateDto) {
const where: Prisma.TemplateWhereInput = { deletedAt: null };
if (query.keyword) where.name = { contains: query.keyword };
if (query.status) where.status = query.status;
const [list, total] = await this.prisma.$transaction([
this.prisma.template.findMany({
where,
include: { fields: { orderBy: { sortOrder: 'asc' } }, _count: { select: { artworks: true } } },
orderBy: { id: 'desc' },
skip: query.skip,
take: query.take,
}),
this.prisma.template.count({ where }),
]);
return buildPage(list, total, query.page, query.pageSize);
}
// 供建档表单选择的启用模板(含字段)
async options() {
return this.prisma.template.findMany({
where: { deletedAt: null, status: TemplateStatus.ENABLED },
include: { fields: { orderBy: { sortOrder: 'asc' } } },
orderBy: { id: 'desc' },
});
}
async get(id: number) {
const tpl = await this.prisma.template.findFirst({
where: { id, deletedAt: null },
include: { fields: { orderBy: { sortOrder: 'asc' } } },
});
if (!tpl) throw new BusinessException(ErrorCode.TEMPLATE_NOT_FOUND, '模板不存在', 404);
return tpl;
}
async update(id: number, dto: UpdateTemplateDto) {
await this.get(id);
return this.prisma.$transaction(async (tx) => {
if (dto.fields) {
await tx.templateField.deleteMany({ where: { templateId: id } });
await tx.templateField.createMany({
data: this.fieldsCreate(dto.fields).map((f) => ({ ...f, templateId: id })),
});
}
return tx.template.update({
where: { id },
data: { name: dto.name, remark: dto.remark, status: dto.status },
include: { fields: { orderBy: { sortOrder: 'asc' } } },
});
});
}
async remove(id: number) {
await this.get(id);
const used = await this.prisma.artwork.count({ where: { templateId: id, deletedAt: null } });
if (used > 0) {
throw new BusinessException(ErrorCode.TEMPLATE_IN_USE, '模板已被作品使用,无法删除(可改为停用)');
}
await this.prisma.template.update({ where: { id }, data: { deletedAt: new Date() } });
return { success: true };
}
}