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 { 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; } }