fix: 优化后台体验与导出日志交互
This commit is contained in:
@@ -122,12 +122,12 @@ export class ImportPreviewProcessor extends WorkerHost {
|
||||
};
|
||||
|
||||
try {
|
||||
if (!values.shortCode) throw new Error('短码为空');
|
||||
if (!values.shortCode) throw new Error('追溯码为空');
|
||||
const code = await this.prisma.traceCode.findUnique({
|
||||
where: { shortCode: values.shortCode },
|
||||
include: { batch: true },
|
||||
});
|
||||
if (!code) throw new Error('短码不存在');
|
||||
if (!code) throw new Error('追溯码不存在');
|
||||
if (values.batchNo && values.batchNo !== (code.batch?.batchNo ?? '')) {
|
||||
throw new Error(`批次编号不匹配,实际为 ${code.batch?.batchNo ?? '-'}`);
|
||||
}
|
||||
|
||||
@@ -42,7 +42,7 @@ export class ImportProcessor extends WorkerHost {
|
||||
for (const row of rows) {
|
||||
try {
|
||||
const code = await this.prisma.traceCode.findUnique({ where: { shortCode: row.shortCode || '' } });
|
||||
if (!code) throw new Error('短码不存在');
|
||||
if (!code) throw new Error('追溯码不存在');
|
||||
const imageItems = jsonArray<{ key: string; url?: string; name?: string; type?: string }>(row.outImageKeys);
|
||||
await this.prisma.traceCode.update({
|
||||
where: { id: code.id },
|
||||
@@ -112,7 +112,7 @@ export class ImportProcessor extends WorkerHost {
|
||||
const ws = wb.addWorksheet('导入结果');
|
||||
ws.columns = [
|
||||
{ header: '行号', key: 'rowNo', width: 10 },
|
||||
{ header: '短码', key: 'shortCode', width: 18 },
|
||||
{ header: '追溯码', key: 'shortCode', width: 18 },
|
||||
{ header: '批次编号', key: 'batchNo', width: 22 },
|
||||
{ header: '出库时间', key: 'outTime', width: 22 },
|
||||
{ header: '图片数量', key: 'imageCount', width: 10 },
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
import { Injectable, Logger } from '@nestjs/common';
|
||||
import { Cron, CronExpression } from '@nestjs/schedule';
|
||||
import { OutboundImportSessionStatus, Prisma } from '@prisma/client';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { StorageService } from '../storage/storage.service';
|
||||
|
||||
function collectImageKeys(value: Prisma.JsonValue | null): string[] {
|
||||
if (!Array.isArray(value)) return [];
|
||||
return value
|
||||
.map((item) => {
|
||||
if (typeof item === 'string') return item;
|
||||
if (item && typeof item === 'object' && 'key' in item) return String((item as { key?: unknown }).key || '');
|
||||
return '';
|
||||
})
|
||||
.filter(Boolean);
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class OutboundImportCleanupService {
|
||||
private readonly logger = new Logger(OutboundImportCleanupService.name);
|
||||
|
||||
constructor(
|
||||
private readonly prisma: PrismaService,
|
||||
private readonly storage: StorageService,
|
||||
) {}
|
||||
|
||||
@Cron(CronExpression.EVERY_HOUR)
|
||||
async cleanupExpiredPreviewSessions(): Promise<void> {
|
||||
const sessions = await this.prisma.outboundImportSession.findMany({
|
||||
where: {
|
||||
expiresAt: { lt: new Date() },
|
||||
status: {
|
||||
in: [
|
||||
OutboundImportSessionStatus.PARSING,
|
||||
OutboundImportSessionStatus.READY,
|
||||
OutboundImportSessionStatus.FAILED,
|
||||
OutboundImportSessionStatus.EXPIRED,
|
||||
],
|
||||
},
|
||||
},
|
||||
include: { rows: { select: { outImageKeys: true } } },
|
||||
take: 50,
|
||||
});
|
||||
if (!sessions.length) return;
|
||||
|
||||
let removed = 0;
|
||||
for (const session of sessions) {
|
||||
const keys = new Set<string>();
|
||||
if (session.fileKey) keys.add(session.fileKey);
|
||||
for (const row of session.rows) {
|
||||
collectImageKeys(row.outImageKeys as Prisma.JsonValue | null).forEach((key) => keys.add(key));
|
||||
}
|
||||
for (const key of keys) {
|
||||
try {
|
||||
await this.storage.remove(key);
|
||||
} catch (e) {
|
||||
this.logger.warn(`清理出库导入临时文件失败:${key} ${(e as Error).message}`);
|
||||
}
|
||||
}
|
||||
await this.prisma.outboundImportSession.delete({ where: { id: session.id } });
|
||||
removed++;
|
||||
}
|
||||
this.logger.log(`已清理过期出库导入预览会话:${removed}`);
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import { OutboundService } from './outbound.service';
|
||||
import { OutboundController } from './outbound.controller';
|
||||
import { ImportProcessor } from './import.processor';
|
||||
import { ImportPreviewProcessor } from './import-preview.processor';
|
||||
import { OutboundImportCleanupService } from './outbound-import-cleanup.service';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
@@ -14,7 +15,7 @@ import { ImportPreviewProcessor } from './import-preview.processor';
|
||||
),
|
||||
],
|
||||
controllers: [OutboundController],
|
||||
providers: [OutboundService, ImportProcessor, ImportPreviewProcessor],
|
||||
providers: [OutboundService, ImportProcessor, ImportPreviewProcessor, OutboundImportCleanupService],
|
||||
exports: [OutboundService],
|
||||
})
|
||||
export class OutboundModule {}
|
||||
|
||||
@@ -19,6 +19,7 @@ import { StorageService } from '../storage/storage.service';
|
||||
import { JobsService } from '../jobs/jobs.service';
|
||||
import { BusinessException } from '../common/exceptions/business.exception';
|
||||
import { buildPage } from '../common/utils/pagination.util';
|
||||
import { buildOrderBy } from '../common/utils/sort.util';
|
||||
import { AuthUser } from '../common/decorators/current-user.decorator';
|
||||
import { OutboundDto } from './dto/outbound.dto';
|
||||
import { QueryImportRowsDto } from './dto/import-session.dto';
|
||||
@@ -95,7 +96,7 @@ export class OutboundService {
|
||||
ws.columns = OUTBOUND_IMPORT_COLUMNS.map((c) => ({ header: c.header, key: c.key, width: c.key === 'outImages' ? 36 : 22 }));
|
||||
ws.getRow(1).font = { bold: true };
|
||||
ws.addRow({
|
||||
shortCode: '请替换为真实短码1',
|
||||
shortCode: '请替换为真实追溯码1',
|
||||
batchNo: 'B20260704001',
|
||||
outTime: '2026-07-04 10:00',
|
||||
outImages: 'images/out-001.jpg',
|
||||
@@ -105,7 +106,7 @@ export class OutboundService {
|
||||
remark: '单图示例',
|
||||
});
|
||||
ws.addRow({
|
||||
shortCode: '请替换为真实短码2',
|
||||
shortCode: '请替换为真实追溯码2',
|
||||
batchNo: 'B20260704001',
|
||||
outTime: '2026-07-04 11:00',
|
||||
outImages: 'images/out-002.png;images/out-003.jpg',
|
||||
@@ -201,10 +202,11 @@ export class OutboundService {
|
||||
await this.getSession(id, user);
|
||||
const where: Prisma.OutboundImportRowWhereInput = { sessionId: id };
|
||||
if (query.status) where.status = query.status as any;
|
||||
const orderBy = buildOrderBy(query.sortField, query.sortOrder, ['outTime', 'createdAt'] as const, { rowNo: 'asc' });
|
||||
const [list, total] = await this.prisma.$transaction([
|
||||
this.prisma.outboundImportRow.findMany({
|
||||
where,
|
||||
orderBy: { rowNo: 'asc' },
|
||||
orderBy,
|
||||
skip: query.skip,
|
||||
take: query.take,
|
||||
}),
|
||||
|
||||
Reference in New Issue
Block a user