feat: 实现模板、艺术品建档、溯源节点与证书水印
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { InjectQueue } from '@nestjs/bullmq';
|
||||
import { Queue } from 'bullmq';
|
||||
import { ErrorCode, QUEUE_NAMES } from '@artveris/shared';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { StorageService } from '../storage/storage.service';
|
||||
import { BusinessException } from '../common/exceptions/business.exception';
|
||||
import { CreateCertificateDto, UpdateCertificateDto } from './dto/certificate.dto';
|
||||
|
||||
@Injectable()
|
||||
export class CertificateService {
|
||||
constructor(
|
||||
private readonly prisma: PrismaService,
|
||||
private readonly storage: StorageService,
|
||||
@InjectQueue(QUEUE_NAMES.WATERMARK) private readonly watermarkQueue: Queue,
|
||||
) {}
|
||||
|
||||
private withUrls<T extends { originFileUrl: string; watermarkFileUrl: string | null }>(c: T) {
|
||||
return {
|
||||
...c,
|
||||
originUrl: c.originFileUrl ? this.storage.getUrl(c.originFileUrl) : null,
|
||||
watermarkUrl: c.watermarkFileUrl ? this.storage.getUrl(c.watermarkFileUrl) : null,
|
||||
};
|
||||
}
|
||||
|
||||
private async ensureArtwork(artworkId: number) {
|
||||
const a = await this.prisma.artwork.findFirst({ where: { id: artworkId, deletedAt: null } });
|
||||
if (!a) throw new BusinessException(ErrorCode.ARTWORK_NOT_FOUND, '艺术品不存在', 404);
|
||||
}
|
||||
|
||||
async listByArtwork(artworkId: number) {
|
||||
const list = await this.prisma.certificate.findMany({
|
||||
where: { artworkId },
|
||||
orderBy: { id: 'asc' },
|
||||
});
|
||||
return list.map((c) => this.withUrls(c));
|
||||
}
|
||||
|
||||
async create(artworkId: number, dto: CreateCertificateDto) {
|
||||
await this.ensureArtwork(artworkId);
|
||||
const cert = await this.prisma.certificate.create({
|
||||
data: {
|
||||
artworkId,
|
||||
name: dto.name,
|
||||
issuer: dto.issuer,
|
||||
issuedAt: dto.issuedAt ? new Date(dto.issuedAt) : null,
|
||||
type: dto.type,
|
||||
originFileUrl: dto.originKey,
|
||||
},
|
||||
});
|
||||
await this.enqueueWatermark(cert.id, dto.originKey);
|
||||
return this.withUrls(cert);
|
||||
}
|
||||
|
||||
async update(id: number, dto: UpdateCertificateDto) {
|
||||
const cert = await this.ensureCert(id);
|
||||
const updated = await this.prisma.certificate.update({
|
||||
where: { id },
|
||||
data: {
|
||||
name: dto.name,
|
||||
issuer: dto.issuer,
|
||||
issuedAt: dto.issuedAt ? new Date(dto.issuedAt) : undefined,
|
||||
type: dto.type,
|
||||
originFileUrl: dto.originKey ?? undefined,
|
||||
// 重新上传原件则清空水印,等待重做
|
||||
watermarkFileUrl: dto.originKey ? null : undefined,
|
||||
},
|
||||
});
|
||||
if (dto.originKey) {
|
||||
await this.enqueueWatermark(id, dto.originKey);
|
||||
}
|
||||
return this.withUrls(updated);
|
||||
}
|
||||
|
||||
async remove(id: number) {
|
||||
await this.ensureCert(id);
|
||||
await this.prisma.certificate.delete({ where: { id } });
|
||||
return { success: true };
|
||||
}
|
||||
|
||||
private async enqueueWatermark(certificateId: number, originKey: string) {
|
||||
await this.watermarkQueue.add('generate', { certificateId, originKey });
|
||||
}
|
||||
|
||||
private async ensureCert(id: number) {
|
||||
const c = await this.prisma.certificate.findUnique({ where: { id } });
|
||||
if (!c) throw new BusinessException(ErrorCode.CERTIFICATE_NOT_FOUND, '认证文件不存在', 404);
|
||||
return c;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user