refactor: 按甲方确认重构为一款多码(档案编号标识款、批次码、按款统计)
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -17,6 +17,11 @@ import { TraceCodeStatus } from '@artveris/shared';
|
||||
import { PaginationDto } from '../../common/dto/pagination.dto';
|
||||
|
||||
export class GenerateBatchDto {
|
||||
@ApiProperty({ description: '所属作品(款)ID' })
|
||||
@Type(() => Number)
|
||||
@IsInt()
|
||||
artworkId!: number;
|
||||
|
||||
@ApiProperty({ description: '生成数量', minimum: 1, maximum: 10000 })
|
||||
@Type(() => Number)
|
||||
@IsInt()
|
||||
@@ -38,6 +43,12 @@ export class GenerateBatchDto {
|
||||
}
|
||||
|
||||
export class QueryTraceCodeDto extends PaginationDto {
|
||||
@ApiPropertyOptional({ description: '作品(款)ID' })
|
||||
@IsOptional()
|
||||
@Type(() => Number)
|
||||
@IsInt()
|
||||
artworkId?: number;
|
||||
|
||||
@ApiPropertyOptional({ description: '批次 ID' })
|
||||
@IsOptional()
|
||||
@Type(() => Number)
|
||||
@@ -105,13 +116,6 @@ export class BatchStatusDto {
|
||||
abnormal?: boolean;
|
||||
}
|
||||
|
||||
export class BindArtworkDto {
|
||||
@ApiProperty({ description: '艺术品 ID' })
|
||||
@Type(() => Number)
|
||||
@IsInt()
|
||||
artworkId!: number;
|
||||
}
|
||||
|
||||
export class QueryBatchDto extends PaginationDto {
|
||||
@ApiPropertyOptional({ description: '关键词(批次编号)' })
|
||||
@IsOptional()
|
||||
@@ -125,6 +129,12 @@ export class ExportTraceCodeDto {
|
||||
@IsIn(['xlsx', 'csv'])
|
||||
format?: 'xlsx' | 'csv';
|
||||
|
||||
@ApiPropertyOptional({ description: '作品(款)ID' })
|
||||
@IsOptional()
|
||||
@Type(() => Number)
|
||||
@IsInt()
|
||||
artworkId?: number;
|
||||
|
||||
@ApiPropertyOptional({ description: '分组 ID' })
|
||||
@IsOptional()
|
||||
@Type(() => Number)
|
||||
|
||||
@@ -13,6 +13,7 @@ const codeId = customAlphabet(ALPHABET, 18);
|
||||
|
||||
interface GenJobData {
|
||||
batchId: number;
|
||||
artworkId: number;
|
||||
jobId: string;
|
||||
quantity: number;
|
||||
groupId?: number | null;
|
||||
@@ -31,7 +32,7 @@ export class GenCodeProcessor extends WorkerHost {
|
||||
}
|
||||
|
||||
async process(job: Job<GenJobData>): Promise<void> {
|
||||
const { batchId, jobId, quantity, groupId } = job.data;
|
||||
const { batchId, artworkId, jobId, quantity, groupId } = job.data;
|
||||
await this.jobs.start(jobId);
|
||||
await this.prisma.batch.update({ where: { id: batchId }, data: { status: BatchStatus.GENERATING } });
|
||||
|
||||
@@ -45,6 +46,7 @@ export class GenCodeProcessor extends WorkerHost {
|
||||
return {
|
||||
code: codeId(),
|
||||
shortCode,
|
||||
artworkId,
|
||||
batchId,
|
||||
groupId: groupId ?? null,
|
||||
url: this.traceCode.buildTraceUrl(shortCode),
|
||||
|
||||
@@ -17,7 +17,6 @@ import { TraceCodeService } from './trace-code.service';
|
||||
import {
|
||||
BatchGroupDto,
|
||||
BatchStatusDto,
|
||||
BindArtworkDto,
|
||||
ExportTraceCodeDto,
|
||||
GenerateBatchDto,
|
||||
GroupDto,
|
||||
@@ -110,17 +109,4 @@ export class TraceCodeController {
|
||||
res.end(buf);
|
||||
}
|
||||
|
||||
@Put('trace-codes/:id/bind')
|
||||
@Roles(RoleType.OPERATOR)
|
||||
@ApiOperation({ summary: '绑定艺术品' })
|
||||
bind(@Param('id') id: string, @Body() dto: BindArtworkDto) {
|
||||
return this.service.bind(BigInt(id), dto.artworkId);
|
||||
}
|
||||
|
||||
@Put('trace-codes/:id/unbind')
|
||||
@Roles(RoleType.OPERATOR)
|
||||
@ApiOperation({ summary: '解绑艺术品' })
|
||||
unbind(@Param('id') id: string) {
|
||||
return this.service.unbind(BigInt(id));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ import { Response } from 'express';
|
||||
import * as ExcelJS from 'exceljs';
|
||||
import * as QRCode from 'qrcode';
|
||||
import dayjs from 'dayjs';
|
||||
import { ErrorCode, JobType, QUEUE_NAMES, TraceCodeStatus } from '@artveris/shared';
|
||||
import { ArtworkStatus, ErrorCode, JobType, QUEUE_NAMES, TraceCodeStatus } from '@artveris/shared';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { BusinessException } from '../common/exceptions/business.exception';
|
||||
import { buildPage } from '../common/utils/pagination.util';
|
||||
@@ -38,20 +38,28 @@ export class TraceCodeService {
|
||||
}
|
||||
|
||||
async generateBatch(dto: GenerateBatchDto, userId: number) {
|
||||
// 必须指定作品(款):生码即归属该款(一款多码)
|
||||
const artwork = await this.prisma.artwork.findFirst({
|
||||
where: { id: dto.artworkId, deletedAt: null },
|
||||
});
|
||||
if (!artwork) throw new BusinessException(ErrorCode.ARTWORK_NOT_FOUND, '艺术品不存在', 404);
|
||||
|
||||
const batchNo = dto.batchNo || `B${dayjs().format('YYYYMMDDHHmmss')}`;
|
||||
const exists = await this.prisma.batch.findUnique({ where: { batchNo } });
|
||||
if (exists) {
|
||||
throw new BusinessException(ErrorCode.BAD_REQUEST, '批次编号已存在');
|
||||
}
|
||||
const batch = await this.prisma.batch.create({
|
||||
data: { batchNo, quantity: dto.quantity, createdBy: userId },
|
||||
data: { batchNo, artworkId: dto.artworkId, quantity: dto.quantity, createdBy: userId },
|
||||
});
|
||||
const job = await this.jobs.create(JobType.GEN_CODE, dto.quantity, userId, {
|
||||
batchId: batch.id,
|
||||
artworkId: dto.artworkId,
|
||||
groupId: dto.groupId ?? null,
|
||||
});
|
||||
await this.genQueue.add('generate', {
|
||||
batchId: batch.id,
|
||||
artworkId: dto.artworkId,
|
||||
jobId: job.id,
|
||||
quantity: dto.quantity,
|
||||
groupId: dto.groupId ?? null,
|
||||
@@ -113,20 +121,16 @@ export class TraceCodeService {
|
||||
data: { status: TraceCodeStatus.DISABLED },
|
||||
});
|
||||
} else {
|
||||
// 启用:按是否已激活/已绑定恢复对应状态
|
||||
// 启用:已激活过的恢复为 ACTIVE,否则恢复为 INACTIVE
|
||||
await this.prisma.$transaction([
|
||||
this.prisma.traceCode.updateMany({
|
||||
where: { id: { in: ids }, firstActivatedAt: { not: null } },
|
||||
data: { status: TraceCodeStatus.ACTIVE },
|
||||
}),
|
||||
this.prisma.traceCode.updateMany({
|
||||
where: { id: { in: ids }, firstActivatedAt: null, artworkId: { not: null } },
|
||||
where: { id: { in: ids }, firstActivatedAt: null },
|
||||
data: { status: TraceCodeStatus.INACTIVE },
|
||||
}),
|
||||
this.prisma.traceCode.updateMany({
|
||||
where: { id: { in: ids }, firstActivatedAt: null, artworkId: null },
|
||||
data: { status: TraceCodeStatus.UNBOUND },
|
||||
}),
|
||||
]);
|
||||
}
|
||||
return { success: true, count: ids.length };
|
||||
@@ -149,6 +153,7 @@ export class TraceCodeService {
|
||||
|
||||
async list(query: QueryTraceCodeDto) {
|
||||
const where: Prisma.TraceCodeWhereInput = {};
|
||||
if (query.artworkId) where.artworkId = query.artworkId;
|
||||
if (query.batchId) where.batchId = query.batchId;
|
||||
if (query.groupId) where.groupId = query.groupId;
|
||||
if (query.status) where.status = query.status;
|
||||
@@ -156,15 +161,16 @@ export class TraceCodeService {
|
||||
where.OR = [
|
||||
{ shortCode: { contains: query.keyword } },
|
||||
{ code: { contains: query.keyword } },
|
||||
{ sku: { contains: query.keyword } },
|
||||
{ archiveNo: { contains: query.keyword } },
|
||||
{ artwork: { archiveNo: { contains: query.keyword } } },
|
||||
{ artwork: { title: { contains: query.keyword } } },
|
||||
];
|
||||
}
|
||||
const [list, total] = await this.prisma.$transaction([
|
||||
this.prisma.traceCode.findMany({
|
||||
where,
|
||||
include: {
|
||||
artwork: { select: { id: true, title: true } },
|
||||
artwork: { select: { id: true, title: true, archiveNo: true } },
|
||||
batch: { select: { id: true, batchNo: true } },
|
||||
group: { select: { id: true, name: true } },
|
||||
},
|
||||
orderBy: { id: 'desc' },
|
||||
@@ -176,34 +182,6 @@ export class TraceCodeService {
|
||||
return buildPage(list, total, query.page, query.pageSize);
|
||||
}
|
||||
|
||||
async bind(id: bigint, artworkId: number) {
|
||||
const code = await this.prisma.traceCode.findUnique({ where: { id } });
|
||||
if (!code) throw new BusinessException(ErrorCode.TRACE_CODE_NOT_FOUND, '追溯码不存在', 404);
|
||||
const artwork = await this.prisma.artwork.findFirst({ where: { id: artworkId, deletedAt: null } });
|
||||
if (!artwork) throw new BusinessException(ErrorCode.ARTWORK_NOT_FOUND, '艺术品不存在', 404);
|
||||
|
||||
const bound = await this.prisma.traceCode.findUnique({ where: { artworkId } });
|
||||
if (bound && bound.id !== id) {
|
||||
throw new BusinessException(ErrorCode.TRACE_CODE_ALREADY_BOUND, '该艺术品已绑定其他追溯码');
|
||||
}
|
||||
|
||||
const next =
|
||||
code.status === TraceCodeStatus.UNBOUND ? TraceCodeStatus.INACTIVE : code.status;
|
||||
return this.prisma.traceCode.update({
|
||||
where: { id },
|
||||
data: { artworkId, status: next },
|
||||
});
|
||||
}
|
||||
|
||||
async unbind(id: bigint) {
|
||||
const code = await this.prisma.traceCode.findUnique({ where: { id } });
|
||||
if (!code) throw new BusinessException(ErrorCode.TRACE_CODE_NOT_FOUND, '追溯码不存在', 404);
|
||||
return this.prisma.traceCode.update({
|
||||
where: { id },
|
||||
data: { artworkId: null, status: TraceCodeStatus.UNBOUND },
|
||||
});
|
||||
}
|
||||
|
||||
async getQrPng(id: bigint): Promise<Buffer> {
|
||||
const code = await this.prisma.traceCode.findUnique({ where: { id } });
|
||||
if (!code) throw new BusinessException(ErrorCode.TRACE_CODE_NOT_FOUND, '追溯码不存在', 404);
|
||||
@@ -224,6 +202,7 @@ export class TraceCodeService {
|
||||
.map((s) => BigInt(s));
|
||||
where.id = { in: idList };
|
||||
} else {
|
||||
if (dto.artworkId) where.artworkId = dto.artworkId;
|
||||
if (dto.batchId) where.batchId = dto.batchId;
|
||||
if (dto.groupId) where.groupId = dto.groupId;
|
||||
if (dto.status) where.status = dto.status;
|
||||
@@ -231,21 +210,27 @@ export class TraceCodeService {
|
||||
where.OR = [
|
||||
{ shortCode: { contains: dto.keyword } },
|
||||
{ code: { contains: dto.keyword } },
|
||||
{ sku: { contains: dto.keyword } },
|
||||
{ archiveNo: { contains: dto.keyword } },
|
||||
{ artwork: { archiveNo: { contains: dto.keyword } } },
|
||||
{ artwork: { title: { contains: dto.keyword } } },
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
const codes = await this.prisma.traceCode.findMany({
|
||||
where,
|
||||
include: { batch: { select: { batchNo: true } }, group: { select: { name: true } } },
|
||||
include: {
|
||||
batch: { select: { batchNo: true } },
|
||||
group: { select: { name: true } },
|
||||
artwork: { select: { title: true, archiveNo: true } },
|
||||
},
|
||||
orderBy: { id: 'asc' },
|
||||
});
|
||||
|
||||
const wb = new ExcelJS.Workbook();
|
||||
const ws = wb.addWorksheet('追溯码');
|
||||
ws.columns = [
|
||||
{ header: '档案编号', key: 'archiveNo', width: 18 },
|
||||
{ header: '作品名称', key: 'title', width: 20 },
|
||||
{ header: '批次编号', key: 'batchNo', width: 22 },
|
||||
{ header: '分组', key: 'group', width: 14 },
|
||||
{ header: '追溯码', key: 'code', width: 24 },
|
||||
@@ -260,6 +245,8 @@ export class TraceCodeService {
|
||||
const url = c.url || this.buildTraceUrl(c.shortCode);
|
||||
const qr = await QRCode.toDataURL(url, { margin: 1, width: 200 });
|
||||
ws.addRow({
|
||||
archiveNo: c.artwork?.archiveNo ?? '',
|
||||
title: c.artwork?.title ?? '',
|
||||
batchNo: c.batch?.batchNo ?? '',
|
||||
group: c.group?.name ?? '',
|
||||
code: c.code,
|
||||
|
||||
Reference in New Issue
Block a user