feat: 完成 PRD 差距与修复,新增审核流与日志清理功能,支持出库信息与动态字段,优化 C 端用户体验
This commit is contained in:
@@ -2,14 +2,27 @@ import { Injectable } from '@nestjs/common';
|
||||
import { InjectQueue } from '@nestjs/bullmq';
|
||||
import { Queue } from 'bullmq';
|
||||
import { Response } from 'express';
|
||||
import { createHash } from 'crypto';
|
||||
import * as ExcelJS from 'exceljs';
|
||||
import { ErrorCode, JobType, OUTBOUND_IMPORT_COLUMNS, QUEUE_NAMES } from '@artveris/shared';
|
||||
import {
|
||||
ErrorCode,
|
||||
JobType,
|
||||
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';
|
||||
import { BusinessException } from '../common/exceptions/business.exception';
|
||||
import { OutboundDto } from './dto/outbound.dto';
|
||||
|
||||
const XLSX_MIME_TYPES = [
|
||||
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
||||
'application/vnd.ms-excel',
|
||||
'application/octet-stream', // 某些浏览器对 .xlsx 的兜底类型
|
||||
];
|
||||
|
||||
@Injectable()
|
||||
export class OutboundService {
|
||||
constructor(
|
||||
@@ -22,7 +35,7 @@ export class OutboundService {
|
||||
async updateSingle(traceCodeId: bigint, dto: OutboundDto) {
|
||||
const code = await this.prisma.traceCode.findUnique({ where: { id: traceCodeId } });
|
||||
if (!code) throw new BusinessException(ErrorCode.TRACE_CODE_NOT_FOUND, '追溯码不存在', 404);
|
||||
return this.prisma.traceCode.update({
|
||||
const updated = await this.prisma.traceCode.update({
|
||||
where: { id: traceCodeId },
|
||||
data: {
|
||||
sku: dto.sku,
|
||||
@@ -32,6 +45,42 @@ export class OutboundService {
|
||||
remark: dto.remark,
|
||||
},
|
||||
});
|
||||
// 录入了上链哈希则记录一条已上链存证(手工登记,不触发 Mock 链)
|
||||
if (dto.txHash) {
|
||||
await this.recordProof(traceCodeId, code.code, {
|
||||
txHash: dto.txHash,
|
||||
onchainAt: dto.onchainAt ? new Date(dto.onchainAt) : new Date(),
|
||||
blockHeight: dto.blockHeight,
|
||||
});
|
||||
}
|
||||
return updated;
|
||||
}
|
||||
|
||||
// 手工登记上链结果:写入一条 ONCHAIN 存证(数据哈希由 code+txHash 派生)
|
||||
// 同码同哈希去重:重复保存不会产生多条存证
|
||||
private async recordProof(
|
||||
traceCodeId: bigint,
|
||||
code: string,
|
||||
p: { txHash: string; onchainAt: Date; blockHeight?: number },
|
||||
) {
|
||||
const dup = await this.prisma.blockchainProof.findFirst({
|
||||
where: { traceCodeId, txHash: p.txHash },
|
||||
});
|
||||
if (dup) return;
|
||||
const dataHash =
|
||||
'0x' + createHash('sha256').update(`${code}:${p.txHash}`).digest('hex');
|
||||
await this.prisma.blockchainProof.create({
|
||||
data: {
|
||||
traceCodeId,
|
||||
dataHash,
|
||||
txHash: p.txHash,
|
||||
blockHeight: p.blockHeight !== undefined ? BigInt(p.blockHeight) : null,
|
||||
chainName: '手工登记',
|
||||
onchainAt: p.onchainAt,
|
||||
status: ProofStatus.ONCHAIN,
|
||||
rawResponse: { source: 'manual' },
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async generateTemplate(res: Response): Promise<void> {
|
||||
@@ -65,6 +114,11 @@ export class OutboundService {
|
||||
|
||||
async startImport(file: Express.Multer.File, userId: number) {
|
||||
if (!file) throw new BusinessException(ErrorCode.BAD_REQUEST, '请上传导入文件');
|
||||
const name = (file.originalname || '').toLowerCase();
|
||||
const okType = XLSX_MIME_TYPES.includes(file.mimetype) || name.endsWith('.xlsx') || name.endsWith('.xls');
|
||||
if (!okType) {
|
||||
throw new BusinessException(ErrorCode.FILE_TYPE_NOT_ALLOWED, '仅支持 Excel(.xlsx/.xls)文件导入');
|
||||
}
|
||||
const saved = await this.storage.save(file.buffer, file.originalname || 'import.xlsx', file.mimetype);
|
||||
const job = await this.jobs.create(JobType.IMPORT_OUTBOUND, 0, userId, { fileKey: saved.key });
|
||||
await this.queue.add('import', { jobId: job.id, fileKey: saved.key });
|
||||
|
||||
Reference in New Issue
Block a user