feat: 增强追溯码单码管理

This commit is contained in:
Nixevol
2026-07-04 05:38:58 +08:00
parent 79bd6270c0
commit a1120848e9
10 changed files with 222 additions and 9 deletions
+3 -2
View File
@@ -112,9 +112,10 @@ export class ArtworkService {
});
if (!a) throw new BusinessException(ErrorCode.ARTWORK_NOT_FOUND, '艺术品不存在', 404);
// 码统计:总数、已激活数、查询总次数(按款聚合)
const [total, active, agg] = await this.prisma.$transaction([
const [total, active, batchTotal, agg] = await this.prisma.$transaction([
this.prisma.traceCode.count({ where: { artworkId: id } }),
this.prisma.traceCode.count({ where: { artworkId: id, status: 'ACTIVE' } }),
this.prisma.batch.count({ where: { artworkId: id } }),
this.prisma.traceCode.aggregate({ where: { artworkId: id }, _sum: { queryCount: true } }),
]);
return {
@@ -126,7 +127,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 },
codeStats: { total, active, batchTotal, queryTotal: agg._sum.queryCount ?? 0 },
};
}
@@ -116,6 +116,25 @@ export class BatchStatusDto {
abnormal?: boolean;
}
export class UpdateTraceCodeDto {
@ApiPropertyOptional({ description: '目标分组 ID,null 表示移出分组' })
@IsOptional()
@Type(() => Number)
@IsInt()
groupId?: number | null;
@ApiPropertyOptional({ description: '目标批次 ID' })
@IsOptional()
@Type(() => Number)
@IsInt()
batchId?: number;
@ApiPropertyOptional({ description: 'true 停用,false 启用' })
@IsOptional()
@IsBoolean()
disabled?: boolean;
}
export class QueryBatchDto extends PaginationDto {
@ApiPropertyOptional({ description: '作品/款式 ID' })
@IsOptional()
@@ -22,6 +22,7 @@ import {
GroupDto,
QueryBatchDto,
QueryTraceCodeDto,
UpdateTraceCodeDto,
} from './dto/trace-code.dto';
import { Roles } from '../common/decorators/roles.decorator';
import { Public } from '../common/decorators/public.decorator';
@@ -87,6 +88,20 @@ export class TraceCodeController {
return this.service.batchSetStatus(dto);
}
@Put('trace-codes/:id')
@Roles(RoleType.OPERATOR)
@ApiOperation({ summary: '更新单个追溯码' })
updateOne(@Param('id') id: string, @Body() dto: UpdateTraceCodeDto) {
return this.service.updateOne(id, dto);
}
@Delete('trace-codes/:id')
@Roles(RoleType.OPERATOR)
@ApiOperation({ summary: '删除单个追溯码' })
removeOne(@Param('id') id: string) {
return this.service.removeOne(id);
}
@Get('trace-codes')
@ApiOperation({ summary: '追溯码列表' })
list(@Query() query: QueryTraceCodeDto) {
@@ -21,6 +21,7 @@ import {
GroupDto,
QueryBatchDto,
QueryTraceCodeDto,
UpdateTraceCodeDto,
} from './dto/trace-code.dto';
@Injectable()
@@ -138,6 +139,76 @@ export class TraceCodeService {
return { success: true, count: ids.length };
}
async updateOne(id: string, dto: UpdateTraceCodeDto) {
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 = {};
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.disabled !== undefined) {
data.status = dto.disabled
? TraceCodeStatus.DISABLED
: code.firstActivatedAt
? TraceCodeStatus.ACTIVE
: TraceCodeStatus.INACTIVE;
}
if (Object.keys(data).length === 0) return code;
if (data.batchId !== undefined && data.batchId !== code.batchId) {
await this.prisma.$transaction([
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 } } }),
]);
} else {
await this.prisma.traceCode.update({ where: { id: traceCodeId }, data });
}
return this.prisma.traceCode.findUnique({
where: { id: traceCodeId },
include: {
artwork: { select: { id: true, title: true, archiveNo: true } },
batch: { select: { id: true, batchNo: true } },
group: { select: { id: true, name: true } },
},
});
}
async removeOne(id: string) {
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);
await this.prisma.$transaction([
this.prisma.queryLog.updateMany({
where: { traceCodeId },
data: { traceCodeId: null },
}),
this.prisma.traceCode.delete({ where: { id: traceCodeId } }),
this.prisma.batch.update({
where: { id: code.batchId },
data: { generatedCount: { decrement: 1 } },
}),
]);
return { success: true };
}
async listBatches(query: QueryBatchDto) {
const where: Prisma.BatchWhereInput = {};
if (query.artworkId) where.artworkId = query.artworkId;