feat: 完成 PRD 差距与修复,新增审核流与日志清理功能,支持出库信息与动态字段,优化 C 端用户体验

This commit is contained in:
Nixevol
2026-06-29 23:41:39 +08:00
parent d18b7c592d
commit 2fba42c9fd
39 changed files with 1247 additions and 185 deletions
+80 -5
View File
@@ -1,12 +1,34 @@
import { Processor, WorkerHost } from '@nestjs/bullmq';
import { Logger } from '@nestjs/common';
import { Job } from 'bullmq';
import { createHash } from 'crypto';
import * as ExcelJS from 'exceljs';
import { JobStatus, OUTBOUND_IMPORT_COLUMNS, QUEUE_NAMES } from '@artveris/shared';
import { JobStatus, OUTBOUND_IMPORT_COLUMNS, ProofStatus, QUEUE_NAMES } from '@artveris/shared';
import { PrismaService } from '../prisma/prisma.service';
import { StorageService } from '../storage/storage.service';
import { JobsService } from '../jobs/jobs.service';
// 将 ExcelJS 单元格值规整为字符串(兼容 Date / 富文本 / 公式结果 / 超链接 / 数字)
function cellToString(v: unknown): string {
if (v === null || v === undefined) return '';
if (v instanceof Date) return v.toISOString();
if (typeof v === 'object') {
const o = v as Record<string, unknown>;
if (typeof o.text === 'string') return o.text.trim();
if (o.result !== undefined && o.result !== null) return String(o.result).trim();
if (Array.isArray(o.richText)) return o.richText.map((r: any) => r.text).join('').trim();
if (o.hyperlink) return String(o.hyperlink).trim();
}
return String(v).trim();
}
// 校验并解析日期,非法返回 null(区分 undefined=未填)
function parseDate(v?: string): Date | undefined | null {
if (!v) return undefined;
const d = new Date(v);
return isNaN(d.getTime()) ? null : d;
}
interface ImportJobData {
jobId: string;
fileKey: string;
@@ -55,8 +77,7 @@ export class ImportProcessor extends WorkerHost {
const values: Record<string, string> = {};
let empty = true;
colToKey.forEach((key, col) => {
const v = row.getCell(col).value;
const text = v === null || v === undefined ? '' : String(v).trim();
const text = cellToString(row.getCell(col).value);
if (text) empty = false;
values[key] = text;
});
@@ -74,22 +95,76 @@ export class ImportProcessor extends WorkerHost {
fail++;
continue;
}
const code = await this.prisma.traceCode.findUnique({ where: { shortCode } });
const code = await this.prisma.traceCode.findUnique({
where: { shortCode },
include: { batch: true },
});
if (!code) {
row.reason = '短码不存在';
fail++;
continue;
}
// 批次编号核对:填写则必须与该码实际批次一致
if (row.values.batchNo && row.values.batchNo !== (code.batch?.batchNo ?? '')) {
row.reason = `批次编号与该码实际批次不符(实际:${code.batch?.batchNo ?? '无'})`;
fail++;
continue;
}
// 字段格式校验
const outTime = parseDate(row.values.outTime);
if (outTime === null) {
row.reason = '出库时间格式不正确(应为 YYYY-MM-DD HH:mm)';
fail++;
continue;
}
const onchainAt = parseDate(row.values.onchainAt);
if (onchainAt === null) {
row.reason = '上链时间格式不正确(应为 YYYY-MM-DD HH:mm)';
fail++;
continue;
}
let blockHeight: bigint | undefined;
if (row.values.blockHeight) {
if (!/^\d+$/.test(row.values.blockHeight)) {
row.reason = '区块高度应为整数';
fail++;
continue;
}
blockHeight = BigInt(row.values.blockHeight);
}
try {
await this.prisma.traceCode.update({
where: { id: code.id },
data: {
sku: row.values.sku || undefined,
archiveNo: row.values.archiveNo || undefined,
outTime: row.values.outTime ? new Date(row.values.outTime) : undefined,
outTime: outTime ?? undefined,
outImage: row.values.outImage || undefined,
remark: row.values.remark || undefined,
},
});
// 录入上链哈希则登记一条已上链存证(同码同哈希去重,避免重复导入污染)
if (row.values.txHash) {
const dup = await this.prisma.blockchainProof.findFirst({
where: { traceCodeId: code.id, txHash: row.values.txHash },
});
if (!dup) {
const dataHash =
'0x' + createHash('sha256').update(`${code.code}:${row.values.txHash}`).digest('hex');
await this.prisma.blockchainProof.create({
data: {
traceCodeId: code.id,
dataHash,
txHash: row.values.txHash,
blockHeight: blockHeight ?? null,
chainName: '手工登记',
onchainAt: onchainAt ?? new Date(),
status: ProofStatus.ONCHAIN,
rawResponse: { source: 'import' },
},
});
}
}
row.ok = true;
success++;
} catch (e) {