refactor: 按甲方确认重构为一款多码(档案编号标识款、批次码、按款统计)

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Nixevol
2026-06-30 12:02:44 +08:00
co-authored by Cursor
parent 67c5887f8f
commit a16e6f1165
34 changed files with 445 additions and 353 deletions
+32 -2
View File
@@ -27,6 +27,11 @@ export class ArtworkService {
.map((i) => ({ ...i, url: this.storage.getUrl(i.key) }));
}
// 档案编号:款级唯一标识,按作品 id 生成,如 ARTVERIS-000001
private buildArchiveNo(id: number): string {
return `ARTVERIS-${String(id).padStart(6, '0')}`;
}
async create(dto: CreateArtworkDto, userId: number) {
const tpl = await this.prisma.template.findFirst({
where: { id: dto.templateId, deletedAt: null },
@@ -42,6 +47,11 @@ export class ArtworkService {
createdBy: userId,
},
});
// 创建后回填档案编号(需要自增 id)
await this.prisma.artwork.update({
where: { id: artwork.id },
data: { archiveNo: this.buildArchiveNo(artwork.id) },
});
return this.get(artwork.id);
}
@@ -51,6 +61,7 @@ export class ArtworkService {
where.OR = [
{ title: { contains: query.keyword } },
{ artist: { contains: query.keyword } },
{ archiveNo: { contains: query.keyword } },
];
}
if (query.status) where.status = query.status;
@@ -62,7 +73,7 @@ export class ArtworkService {
where,
include: {
template: { select: { id: true, name: true } },
traceCode: { select: { id: true, code: true, shortCode: true, status: true } },
_count: { select: { traceCodes: true } },
},
orderBy: { id: 'desc' },
skip: query.skip,
@@ -70,9 +81,21 @@ export class ArtworkService {
}),
this.prisma.artwork.count({ where }),
]);
// 每款查询总次数 = 该款所有码 queryCount 之和(PRD 6.5:按款统计)
const ids = rows.map((r) => r.id);
const sums = ids.length
? await this.prisma.traceCode.groupBy({
by: ['artworkId'],
where: { artworkId: { in: ids } },
_sum: { queryCount: true },
})
: [];
const sumMap = new Map(sums.map((s) => [s.artworkId, s._sum.queryCount ?? 0]));
const list = rows.map((a) => ({
...a,
images: this.mapMedia(a.images),
codeCount: a._count.traceCodes,
queryTotal: sumMap.get(a.id) ?? 0,
}));
return buildPage(list, total, query.page, query.pageSize);
}
@@ -84,10 +107,16 @@ export class ArtworkService {
template: { include: { fields: { orderBy: { sortOrder: 'asc' } } } },
traceNodes: { orderBy: { sortOrder: 'asc' } },
certificates: { orderBy: { id: 'asc' } },
traceCode: { include: { proofs: { orderBy: { id: 'desc' } } } },
batches: { orderBy: { id: 'desc' } },
},
});
if (!a) throw new BusinessException(ErrorCode.ARTWORK_NOT_FOUND, '艺术品不存在', 404);
// 码统计:总数、已激活数、查询总次数(按款聚合)
const [total, active, agg] = await this.prisma.$transaction([
this.prisma.traceCode.count({ where: { artworkId: id } }),
this.prisma.traceCode.count({ where: { artworkId: id, status: 'ACTIVE' } }),
this.prisma.traceCode.aggregate({ where: { artworkId: id }, _sum: { queryCount: true } }),
]);
return {
...a,
images: this.mapMedia(a.images),
@@ -97,6 +126,7 @@ export class ArtworkService {
originUrl: c.originFileUrl ? this.storage.getUrl(c.originFileUrl) : null,
watermarkUrl: c.watermarkFileUrl ? this.storage.getUrl(c.watermarkFileUrl) : null,
})),
codeStats: { total, active, queryTotal: agg._sum.queryCount ?? 0 },
};
}