feat: 完善追溯码出库上链管理
This commit is contained in:
@@ -129,12 +129,38 @@ export class UpdateTraceCodeDto {
|
||||
@IsInt()
|
||||
batchId?: number;
|
||||
|
||||
@ApiPropertyOptional({ description: '新批次编号,填写后自动创建批次并移动到该批次' })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
@MaxLength(50)
|
||||
batchNo?: string;
|
||||
|
||||
@ApiPropertyOptional({ description: 'true 停用,false 启用' })
|
||||
@IsOptional()
|
||||
@IsBoolean()
|
||||
disabled?: boolean;
|
||||
}
|
||||
|
||||
export class BatchMoveDto {
|
||||
@ApiProperty({ description: '追溯码 ID 列表', type: [String] })
|
||||
@IsArray()
|
||||
@ArrayNotEmpty()
|
||||
@IsString({ each: true })
|
||||
ids!: string[];
|
||||
|
||||
@ApiPropertyOptional({ description: '目标批次 ID' })
|
||||
@IsOptional()
|
||||
@Type(() => Number)
|
||||
@IsInt()
|
||||
batchId?: number;
|
||||
|
||||
@ApiPropertyOptional({ description: '新批次编号,填写后自动创建批次并移动到该批次' })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
@MaxLength(50)
|
||||
batchNo?: string;
|
||||
}
|
||||
|
||||
export class QueryBatchDto extends PaginationDto {
|
||||
@ApiPropertyOptional({ description: '作品/款式 ID' })
|
||||
@IsOptional()
|
||||
|
||||
@@ -16,6 +16,7 @@ import { RoleType } from '@artveris/shared';
|
||||
import { TraceCodeService } from './trace-code.service';
|
||||
import {
|
||||
BatchGroupDto,
|
||||
BatchMoveDto,
|
||||
BatchStatusDto,
|
||||
ExportTraceCodeDto,
|
||||
GenerateBatchDto,
|
||||
@@ -88,11 +89,18 @@ export class TraceCodeController {
|
||||
return this.service.batchSetStatus(dto);
|
||||
}
|
||||
|
||||
@Put('trace-codes/batch/move')
|
||||
@Roles(RoleType.OPERATOR)
|
||||
@ApiOperation({ summary: '批量修改追溯码批次' })
|
||||
batchMove(@Body() dto: BatchMoveDto, @CurrentUser() user: AuthUser) {
|
||||
return this.service.batchMove(dto, user.id);
|
||||
}
|
||||
|
||||
@Put('trace-codes/:id')
|
||||
@Roles(RoleType.OPERATOR)
|
||||
@ApiOperation({ summary: '更新单个追溯码' })
|
||||
updateOne(@Param('id') id: string, @Body() dto: UpdateTraceCodeDto) {
|
||||
return this.service.updateOne(id, dto);
|
||||
updateOne(@Param('id') id: string, @Body() dto: UpdateTraceCodeDto, @CurrentUser() user: AuthUser) {
|
||||
return this.service.updateOne(id, dto, user.id);
|
||||
}
|
||||
|
||||
@Delete('trace-codes/:id')
|
||||
|
||||
@@ -7,7 +7,7 @@ import { Response } from 'express';
|
||||
import * as ExcelJS from 'exceljs';
|
||||
import * as QRCode from 'qrcode';
|
||||
import dayjs from 'dayjs';
|
||||
import { ArtworkStatus, ErrorCode, JobType, QUEUE_NAMES, TraceCodeStatus } from '@artveris/shared';
|
||||
import { ArtworkStatus, BatchStatus, ErrorCode, JobType, QUEUE_NAMES, TraceCodeStatus } from '@artveris/shared';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { StorageService } from '../storage/storage.service';
|
||||
import { BusinessException } from '../common/exceptions/business.exception';
|
||||
@@ -15,6 +15,7 @@ import { buildPage } from '../common/utils/pagination.util';
|
||||
import { JobsService } from '../jobs/jobs.service';
|
||||
import {
|
||||
BatchGroupDto,
|
||||
BatchMoveDto,
|
||||
BatchStatusDto,
|
||||
ExportTraceCodeDto,
|
||||
GenerateBatchDto,
|
||||
@@ -139,24 +140,131 @@ export class TraceCodeService {
|
||||
return { success: true, count: ids.length };
|
||||
}
|
||||
|
||||
async updateOne(id: string, dto: UpdateTraceCodeDto) {
|
||||
private async resolveTargetBatch(options: {
|
||||
artworkId: number;
|
||||
batchId?: number;
|
||||
batchNo?: string;
|
||||
count: number;
|
||||
userId?: number;
|
||||
}) {
|
||||
const batchNo = options.batchNo?.trim();
|
||||
if (batchNo) {
|
||||
const exists = await this.prisma.batch.findUnique({ where: { batchNo } });
|
||||
if (exists) throw new BusinessException(ErrorCode.BAD_REQUEST, '批次编号已存在');
|
||||
return this.prisma.batch.create({
|
||||
data: {
|
||||
batchNo,
|
||||
artworkId: options.artworkId,
|
||||
quantity: options.count,
|
||||
generatedCount: options.count,
|
||||
status: BatchStatus.DONE,
|
||||
createdBy: options.userId,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
if (!options.batchId) {
|
||||
throw new BusinessException(ErrorCode.BAD_REQUEST, '请选择目标批次或填写新批次编号');
|
||||
}
|
||||
|
||||
const targetBatch = await this.prisma.batch.findUnique({ where: { id: options.batchId } });
|
||||
if (!targetBatch || targetBatch.artworkId !== options.artworkId) {
|
||||
throw new BusinessException(ErrorCode.BAD_REQUEST, '目标批次不存在或不属于当前艺术品档案');
|
||||
}
|
||||
return targetBatch;
|
||||
}
|
||||
|
||||
private batchCountUpdates(batchCounts: Map<number, number>, direction: 'increment' | 'decrement') {
|
||||
return [...batchCounts.entries()].map(([batchId, count]) =>
|
||||
this.prisma.batch.update({
|
||||
where: { id: batchId },
|
||||
data: {
|
||||
quantity: { [direction]: count },
|
||||
generatedCount: { [direction]: count },
|
||||
},
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
async batchMove(dto: BatchMoveDto, userId: number) {
|
||||
const ids = dto.ids.map((i) => BigInt(i));
|
||||
const codes = await this.prisma.traceCode.findMany({
|
||||
where: { id: { in: ids } },
|
||||
select: { id: true, artworkId: true, batchId: true },
|
||||
});
|
||||
if (codes.length !== ids.length) {
|
||||
throw new BusinessException(ErrorCode.TRACE_CODE_NOT_FOUND, '部分追溯码不存在', 404);
|
||||
}
|
||||
const artworkIds = new Set(codes.map((c) => c.artworkId));
|
||||
if (artworkIds.size !== 1) {
|
||||
throw new BusinessException(ErrorCode.BAD_REQUEST, '只能批量移动同一艺术品档案下的追溯码');
|
||||
}
|
||||
|
||||
const artworkId = codes[0].artworkId;
|
||||
const targetBatch = await this.resolveTargetBatch({
|
||||
artworkId,
|
||||
batchId: dto.batchId,
|
||||
batchNo: dto.batchNo,
|
||||
count: codes.length,
|
||||
userId,
|
||||
});
|
||||
|
||||
const movedCodes = codes.filter((c) => c.batchId !== targetBatch.id);
|
||||
if (!movedCodes.length) return { success: true, count: 0, batch: targetBatch };
|
||||
|
||||
const sourceCounts = new Map<number, number>();
|
||||
for (const code of movedCodes) {
|
||||
sourceCounts.set(code.batchId, (sourceCounts.get(code.batchId) ?? 0) + 1);
|
||||
}
|
||||
|
||||
const updates = [
|
||||
this.prisma.traceCode.updateMany({
|
||||
where: { id: { in: movedCodes.map((c) => c.id) } },
|
||||
data: { batchId: targetBatch.id },
|
||||
}),
|
||||
...this.batchCountUpdates(sourceCounts, 'decrement'),
|
||||
];
|
||||
if (!dto.batchNo?.trim()) {
|
||||
updates.push(
|
||||
this.prisma.batch.update({
|
||||
where: { id: targetBatch.id },
|
||||
data: {
|
||||
quantity: { increment: movedCodes.length },
|
||||
generatedCount: { increment: movedCodes.length },
|
||||
},
|
||||
}),
|
||||
);
|
||||
}
|
||||
await this.prisma.$transaction(updates);
|
||||
|
||||
return { success: true, count: movedCodes.length, batch: targetBatch };
|
||||
}
|
||||
|
||||
async updateOne(id: string, dto: UpdateTraceCodeDto, userId?: number) {
|
||||
const traceCodeId = BigInt(id);
|
||||
const code = await this.prisma.traceCode.findUnique({ where: { id: traceCodeId } });
|
||||
if (!code) throw new BusinessException(ErrorCode.TRACE_CODE_NOT_FOUND, '追溯码不存在', 404);
|
||||
|
||||
const data: Prisma.TraceCodeUncheckedUpdateInput = {};
|
||||
let targetBatchId: number | undefined;
|
||||
let createdBatch = false;
|
||||
|
||||
if (Object.prototype.hasOwnProperty.call(dto, 'groupId')) {
|
||||
if (dto.groupId !== null && dto.groupId !== undefined) await this.ensureGroup(dto.groupId);
|
||||
data.groupId = dto.groupId ?? null;
|
||||
}
|
||||
|
||||
if (dto.batchId !== undefined) {
|
||||
const targetBatch = await this.prisma.batch.findUnique({ where: { id: dto.batchId } });
|
||||
if (!targetBatch || targetBatch.artworkId !== code.artworkId) {
|
||||
throw new BusinessException(ErrorCode.BAD_REQUEST, '目标批次不存在或不属于当前艺术品档案');
|
||||
}
|
||||
data.batchId = dto.batchId;
|
||||
if (dto.batchId !== undefined || dto.batchNo?.trim()) {
|
||||
const targetBatch = await this.resolveTargetBatch({
|
||||
artworkId: code.artworkId,
|
||||
batchId: dto.batchId,
|
||||
batchNo: dto.batchNo,
|
||||
count: 1,
|
||||
userId,
|
||||
});
|
||||
targetBatchId = targetBatch.id;
|
||||
createdBatch = Boolean(dto.batchNo?.trim());
|
||||
data.batchId = targetBatchId;
|
||||
}
|
||||
|
||||
if (dto.disabled !== undefined) {
|
||||
@@ -169,12 +277,23 @@ export class TraceCodeService {
|
||||
|
||||
if (Object.keys(data).length === 0) return code;
|
||||
|
||||
if (data.batchId !== undefined && data.batchId !== code.batchId) {
|
||||
await this.prisma.$transaction([
|
||||
if (targetBatchId !== undefined && targetBatchId !== code.batchId) {
|
||||
const updates = [
|
||||
this.prisma.traceCode.update({ where: { id: traceCodeId }, data }),
|
||||
this.prisma.batch.update({ where: { id: code.batchId }, data: { generatedCount: { decrement: 1 } } }),
|
||||
this.prisma.batch.update({ where: { id: data.batchId as number }, data: { generatedCount: { increment: 1 } } }),
|
||||
]);
|
||||
this.prisma.batch.update({
|
||||
where: { id: code.batchId },
|
||||
data: { quantity: { decrement: 1 }, generatedCount: { decrement: 1 } },
|
||||
}),
|
||||
];
|
||||
if (!createdBatch) {
|
||||
updates.push(
|
||||
this.prisma.batch.update({
|
||||
where: { id: targetBatchId },
|
||||
data: { quantity: { increment: 1 }, generatedCount: { increment: 1 } },
|
||||
}),
|
||||
);
|
||||
}
|
||||
await this.prisma.$transaction(updates);
|
||||
} else {
|
||||
await this.prisma.traceCode.update({ where: { id: traceCodeId }, data });
|
||||
}
|
||||
@@ -202,7 +321,7 @@ export class TraceCodeService {
|
||||
this.prisma.traceCode.delete({ where: { id: traceCodeId } }),
|
||||
this.prisma.batch.update({
|
||||
where: { id: code.batchId },
|
||||
data: { generatedCount: { decrement: 1 } },
|
||||
data: { quantity: { decrement: 1 }, generatedCount: { decrement: 1 } },
|
||||
}),
|
||||
]);
|
||||
|
||||
@@ -247,9 +366,8 @@ export class TraceCodeService {
|
||||
batch: { select: { id: true, batchNo: true } },
|
||||
group: { select: { id: true, name: true } },
|
||||
proofs: {
|
||||
where: { status: 'ONCHAIN' },
|
||||
orderBy: [{ onchainAt: 'desc' }, { id: 'desc' }],
|
||||
take: 1,
|
||||
orderBy: [{ createdAt: 'desc' }, { id: 'desc' }],
|
||||
take: 10,
|
||||
},
|
||||
},
|
||||
orderBy: { id: 'desc' },
|
||||
@@ -261,10 +379,12 @@ export class TraceCodeService {
|
||||
return buildPage(
|
||||
list.map((code) => {
|
||||
const { proofs, ...rest } = code;
|
||||
const latestOnchainProof = proofs.find((p) => p.status === 'ONCHAIN') ?? null;
|
||||
return {
|
||||
...rest,
|
||||
outImageUrl: code.outImage ? this.storage.getUrl(code.outImage) : null,
|
||||
latestProof: proofs[0] ?? null,
|
||||
latestProof: latestOnchainProof,
|
||||
latestAnyProof: proofs[0] ?? null,
|
||||
};
|
||||
}),
|
||||
total,
|
||||
|
||||
Reference in New Issue
Block a user